0% found this document useful (0 votes)
19 views11 pages

DBMS

SQL (Structured Query Language) is a standard language for managing databases, allowing users to create, read, update, and delete data. It includes various command types such as DDL, DML, DCL, TCL, and DQL, each serving different functions in database management. Normalization is a process to reduce redundancy and improve data integrity, involving several normal forms that structure data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views11 pages

DBMS

SQL (Structured Query Language) is a standard language for managing databases, allowing users to create, read, update, and delete data. It includes various command types such as DDL, DML, DCL, TCL, and DQL, each serving different functions in database management. Normalization is a process to reduce redundancy and improve data integrity, involving several normal forms that structure data efficiently.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SQL

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.

Key Features of SQL:

 Simple and easy to learn

 High-performance query processing

 Support for complex queries

 Platform-independent

Types of SQL Commands:

SQL commands are categorized into five main types:

1. DDL (Data Definition Language)

2. DML (Data Manipulation Language)

3. DCL (Data Control Language)

4. TCL (Transaction Control Language)

5. DQL (Data Query Language)

1. Data Definition Language (DDL)

DDL commands define and modify database structure.

 CREATE: Creates a new table, view, or database.

CREATE TABLE Employees (ID INT PRIMARY KEY, Name VARCHAR (50),Position VARCHAR
(50),Salary DECIMAL (10, 2));

 ALTER: Modifies an existing database object.

ALTER TABLE Employees ADD Email VARCHAR (100);

 DROP: Deletes a table or database.

DROP TABLE Employees;

 TRUNCATE: Removes all records from a table but keeps the structure.

TRUNCATE TABLE Employees;

2. Data Manipulation Language (DML)

DML commands handle data within tables.

 INSERT: Adds new records.


INSERT INTO Employees (ID, Name, Position, Salary)

VALUES (1, 'John Doe', 'Manager', 70000);

 UPDATE: Modifies existing records.

UPDATE Employees SET Salary = 75000 WHERE ID = 1;

 DELETE: Removes records.

DELETE FROM Employees WHERE ID = 1;

3. Data Query Language (DQL)

DQL commands retrieve data.

 SELECT: Fetches data from tables.

SELECT * FROM Employees;

 Using conditions:

SELECT Name, Position FROM Employees WHERE Salary > 60000;

4. Data Control Language (DCL)

DCL commands manage access.

 GRANT: Gives access rights.

GRANT SELECT, INSERT ON Employees TO 'user1';

 REVOKE: Removes access rights.

REVOKE INSERT ON Employees FROM 'user1';

5. Transaction Control Language (TCL)

TCL commands handle transactions.

 COMMIT: Saves changes. (COMMIT;)

 ROLLBACK: Undoes changes. (ROLLBACK;)

 SAVEPOINT: Sets a point to roll back to.

What is a Normal Form?

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.

The process of applying these rules is called normalization!

Normal Forms in DBMS:


1. First Normal Form (1NF)

Goal: Eliminate duplicate columns and ensure each column contains atomic (indivisible)
values.

Rules:

 Each column must contain atomic values (no repeating groups or arrays).

 Each row must be unique, with a primary key.

Example (Before 1NF):

I Name Phone Numbers


D

1 John 12345, 67890

2 Alice 54321

After 1NF (Atomic Values):

I Name Phone Number


D

1 John 12345

1 John 67890

2 Alice 54321

2. Second Normal Form (2NF)

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.

Example (Before 2NF):

OrderID ProductID ProductName Quantity

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:

OrderID ProductID Quantity

1 101 5

2 102 3

Products Table:

ProductI ProductName
D

101 Apple

102 Banana

3. Third Normal Form (3NF)

Goal: Remove transitive dependencies (where non-key attributes depend on other non-
key attributes).

Rules:

 Must be in 2NF.

 Non-key attributes must not depend on other non-key attributes.

Example (Before 3NF):

StudentI Name Department DeptHead


D

1 John IT Alice

2 Mike HR Bob

Here, DeptHead depends on Department, not on StudentID.

After 3NF (Split Tables):


Students Table:

StudentI Name Department


D

1 John IT

2 Mike HR

Departments Table:
Departmen DeptHead
t

IT Alice

HR Bob

4. Boyce-Codd Normal Form (BCNF)

Goal: Handle anomalies not covered by 3NF (when a candidate key depends on another
candidate key).

Rules:

 Must be in 3NF.

 Every determinant must be a candidate key.

Example (Before BCNF):

StudentI Course Instructor


D

1 Math Prof. A

2 Math Prof. A

3 Physics Prof. B

Here, Course → Instructor, but StudentID, Course is the primary key.

After BCNF (Split Tables):


Student Courses Table:

StudentI Course
D

1 Math

2 Math

3 Physics

Course Instructors Table:

Course Instructor

Math Prof. A

Physics Prof. B
5. Fourth Normal Form (4NF)

Goal: Eliminate multi-valued dependencies.

Rules:

 Must be in BCNF.

 A record should not have two or more independent multi-valued facts about an
entity.

Example (Before 4NF):

StudentID Course Hobby

1 Math Chess

1 Math Painting

1 Physics Chess

Here, Course and Hobby are independent.

After 4NF (Split Tables):


Student Courses Table:

StudentI Course
D

1 Math

1 Physics

Student Hobbies Table:

StudentI Hobby
D

1 Chess

1 Painting

6. Fifth Normal Form (5NF or Project-Join Normal Form)

Goal: Decompose tables to eliminate join dependencies while preserving data.

Example: Consider a Course-Teacher-Student relationship. If a student can take multiple


courses, taught by multiple teachers, it might cause redundancy.
Splitting into smaller tables ensures minimal redundancy and preserves all connections
through joins.

Uses of Normalization:

 Reduces data redundancy.

 Improves data integrity and consistency.

 Makes updates and maintenance easier.

 Optimizes database performance in certain scenarios.

Database Concepts and SQL Definitions

1. Schema:

 A schema is the logical structure of a database. It defines how data is organized,


including tables, fields, relationships, and constraints.

 Think of it as a blueprint for your database!

Example:

SQL

CopyEdit

CREATE SCHEMA Company;

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.

 Common aggregate functions:

o SUM(): Total of values

o AVG(): Average value

o COUNT(): Number of rows

o MAX(): Highest value

o MIN(): Lowest value

Example:
SQL

CopyEdit

SELECT AVG(Salary) FROM Employees;

4. GROUP BY Clause:

 Groups rows with the same values in specified columns and allows you to use
aggregate functions.

Example:

sql

CopyEdit

SELECT Department, COUNT (*)

FROM Employees

GROUP BY Department;

5. HAVING Clause:

 Filters grouped results (like WHERE but for groups).

Example:

sql

CopyEdit

SELECT Department, AVG(Salary)

FROM Employees

GROUP BY Department

HAVING AVG(Salary) > 50000;

6. ORDER BY Clause:

 Sorts the results in ascending (ASC) or descending (DESC) order.

Example:

SQL

CopyEdit

SELECT Name, Salary

FROM Employees
ORDER BY Salary DESC;

7. NOT NULL Constraint:

 Ensures a column cannot have NULL values.

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.

LEFT JOIN (LEFT OUTER JOIN)

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.

RIGHT JOIN (RIGHT OUTER JOIN)

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.

FULL JOIN (FULL OUTER JOIN)

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.

 Example: ID column in a Users table.


Foreign Key

 Definition: Establishes a relationship between two tables by referring to the


primary key in another table.

 Example: CustomerID in an Orders table, referencing the ID in a Customers table.

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.

 Example: If ID is the primary key, Email becomes an alternate key.

Composite Key

 Definition: A key made up of two or more columns to uniquely identify a record.

 Example: OrderID and ProductID together can form a composite key in an


OrderDetails table.

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.

 Example: Username in a Users table.

Super Key

 Definition: A set of attributes that can uniquely identify a row. It includes primary
keys, candidate keys, and more.

 Example: {ID, Email} is a super key if ID alone is a primary key.

You might also like