DBMS
DBMS
SQL (Structured Query Language) is a standard language used to interact with databases. It
allows users to create, read, update, and delete data, as well as manage database structures.
Platform-independent
CREATE TABLE Employees (ID INT PRIMARY KEY, Name VARCHAR (50),Position VARCHAR
(50),Salary DECIMAL (10, 2));
TRUNCATE: Removes all records from a table but keeps the structure.
Using conditions:
A normal form is a set of rules or guidelines used in database design to structure tables in a
way that reduces redundancy and avoids data anomalies. By organizing data into normal
forms, you ensure that your database is more efficient, consistent, and easier to maintain.
Goal: Eliminate duplicate columns and ensure each column contains atomic (indivisible)
values.
Rules:
Each column must contain atomic values (no repeating groups or arrays).
2 Alice 54321
1 John 12345
1 John 67890
2 Alice 54321
Goal: Remove partial dependencies (where non-key columns depend only on part of the
primary key).
Rules:
Must be in 1NF.
All non-key attributes must be fully dependent on the entire primary key.
1 101 Apple 5
2 102 Banana 3
Here, ProductName depends only on ProductID, not on the full key (OrderID, ProductID).
After 2NF (Split Tables):
Orders Table:
1 101 5
2 102 3
Products Table:
ProductI ProductName
D
101 Apple
102 Banana
Goal: Remove transitive dependencies (where non-key attributes depend on other non-
key attributes).
Rules:
Must be in 2NF.
1 John IT Alice
2 Mike HR Bob
1 John IT
2 Mike HR
Departments Table:
Departmen DeptHead
t
IT Alice
HR Bob
Goal: Handle anomalies not covered by 3NF (when a candidate key depends on another
candidate key).
Rules:
Must be in 3NF.
1 Math Prof. A
2 Math Prof. A
3 Physics Prof. B
StudentI Course
D
1 Math
2 Math
3 Physics
Course Instructor
Math Prof. A
Physics Prof. B
5. Fourth Normal Form (4NF)
Rules:
Must be in BCNF.
A record should not have two or more independent multi-valued facts about an
entity.
1 Math Chess
1 Math Painting
1 Physics Chess
StudentI Course
D
1 Math
1 Physics
StudentI Hobby
D
1 Chess
1 Painting
Uses of Normalization:
1. Schema:
Example:
SQL
CopyEdit
2. Instance of a Database:
A database instance is the actual data stored in the database at a specific point in
time.
The schema is the design, while the instance is the real data at any moment.
3. Aggregation Functions:
Functions that perform calculations on a set of values and return a single result.
Example:
SQL
CopyEdit
4. GROUP BY Clause:
Groups rows with the same values in specified columns and allows you to use
aggregate functions.
Example:
sql
CopyEdit
FROM Employees
GROUP BY Department;
5. HAVING Clause:
Example:
sql
CopyEdit
FROM Employees
GROUP BY Department
6. ORDER BY Clause:
Example:
SQL
CopyEdit
FROM Employees
ORDER BY Salary DESC;
SQL Joins
In SQL, joins are used to combine rows from two or more tables based on a related column
between them. They help retrieve meaningful data from multiple tables as if they were a
single table. Understanding the different types of joins is crucial for database management
and query optimization.
INNER JOIN
Returns only the records that have matching values in both tables. It filters out rows
without corresponding matches in the other table.
Returns all the records from the left table and the matching records from the right table. If
there is no match, NULL values are returned for columns from the right table.
Returns all the records from the right table and the matching records from the left table. If
there is no match, NULL values are returned for columns from the left table.
Returns all the records when there is a match in either the left or right table. If there is no
match, NULL values are returned for unmatched rows in both tables.
CROSS JOIN
Returns the Cartesian product of two tables, combining each row from the first table with
every row from the second table. It does not require a condition to join the tables.
SELF JOIN
Joins a table with itself. It is useful for hierarchical data or situations where rows in a table
need to be compared to other rows in the same table.
KEYS:
Primary Key
Definition: Uniquely identifies each record in a table. It must contain unique values
and cannot contain NULL.
Candidate Key
Definition: A set of attributes that can uniquely identify records. A table can have
multiple candidate keys, but only one primary key.
Example: Email and PhoneNumber could both be candidate keys in a Users table.
Alternate Key
Definition: Candidate keys that are not chosen as the primary key.
Composite Key
Unique Key
Definition: Ensures all values in a column or set of columns are unique, but unlike a
primary key, it can accept a single NULL value.
Super Key
Definition: A set of attributes that can uniquely identify a row. It includes primary
keys, candidate keys, and more.