โ
Useful SQL Concepts You Should Know ๐๐
1๏ธโฃ Constraints in SQL:
- PRIMARY KEY โ Uniquely identifies each row
- FOREIGN KEY โ Links to another table
- UNIQUE โ Ensures all values are different
- NOT NULL โ Column must have a value
- CHECK โ Validates data before insert/update
2๏ธโฃ SQL Views:
Virtual tables based on result of a query
CREATE VIEW top_students AS
SELECT name, marks FROM students WHERE marks > 90;
3๏ธโฃ Indexing:
Improves query performance
CREATE INDEX idx_name ON employees(name);
4๏ธโฃ SQL Transactions:
Ensure data integrity
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
5๏ธโฃ Triggers:
Automatic actions when events occur
CREATE TRIGGER log_update
AFTER UPDATE ON employees
FOR EACH ROW
INSERT INTO logs(action) VALUES ('Employee updated');
6๏ธโฃ Stored Procedures:
Reusable blocks of SQL logic
CREATE PROCEDURE getTopStudents()
BEGIN
SELECT FROM students WHERE marks > 90;
END;
7๏ธโฃ Common Table Expressions (CTEs):
Temporary named result sets
WITH dept_count AS (
SELECT department, COUNT() AS total FROM employees GROUP BY department
)
SELECT FROM dept_count;
๐ฌ Double Tap โค๏ธ For More!
Post #2433
2.74K
- โค 7