DBMS & SQL Notes (Detailed with
Examples)
Objective of SQL
SQL is used to define, manipulate, retrieve, and control data in relational databases.
Example:
CREATE TABLE Student (
id INT PRIMARY KEY,
name VARCHAR(50),
age INT
);
Joins
Joins combine data from multiple tables.
Example:
SELECT [Link], c.course_name
FROM Student s
INNER JOIN Course c
ON s.course_id = c.course_id;
Functions
Functions perform calculations on data.
Example:
SELECT AVG(marks) FROM Student;
Referential Integrity
Ensures foreign key values match primary key values.
Example:
FOREIGN KEY (course_id) REFERENCES Course(course_id);
Transaction & ACID
A transaction is a logical unit of work.
Example:
BEGIN;
UPDATE Account SET balance = balance - 1000 WHERE id = 1;
UPDATE Account SET balance = balance + 1000 WHERE id = 2;
COMMIT;
Deadlock
Deadlock occurs when transactions wait forever for each other.
Example:
T1 holds A, waits for B
T2 holds B, waits for A