DBMS Unit 5
DBMS Unit 5
databases. SQL commands are broadly categorized into several types, out of which the three most
important are:
Each of these types of commands serves a different purpose in the database environment.
DDL commands are used to define and manage database schema and objects such as tables, indexes,
and views. These commands deal with the structure of the database rather than the data it holds.
• TRUNCATE: Removes all records from a table but not the table structure.
Examples:
a. CREATE:
sql
CopyEdit
Name VARCHAR(50),
Age INT
);
b. ALTER:
sql
CopyEdit
sql
CopyEdit
d. TRUNCATE:
sql
CopyEdit
Removes all rows from the table, but the table structure remains intact.
DML commands are used for performing operations on the data stored in the database. These
commands allow users to insert, modify, retrieve, and delete data from tables.
• SELECT: Retrieves data from one or more tables (sometimes also considered under DQL –
Data Query Language).
Examples:
a. INSERT:
sql
CopyEdit
INSERT INTO Students (ID, Name, Age) VALUES (1, 'Alice', 20);
b. UPDATE:
sql
CopyEdit
sql
CopyEdit
d. SELECT:
sql
CopyEdit
DCL commands are used to control access to data in a database. They help manage permissions and
access rights of users over database objects.
Examples:
a. GRANT:
sql
CopyEdit
Allows user1 to select and insert data into the Students table.
b. REVOKE:
sql
CopyEdit
SQL (Structured Query Language) is the standard language used to interact with relational
databases. It enables users to perform operations such as creating databases and tables, inserting,
updating, deleting, and retrieving data efficiently.
SQL is both declarative (what to do, not how) and non-procedural, making it accessible and easy to
learn. It is used with most RDBMSs such as MySQL, PostgreSQL, Oracle, MS SQL Server, etc.
SQL provides various data types to define the kind of data a column can hold. These can be broadly
classified into:
a) Numeric Types:
sql
CopyEdit
b) String/Text Types:
sql
CopyEdit
Name VARCHAR(50);
sql
CopyEdit
DOB DATE;
d) Boolean:
Proper selection of data type ensures efficient storage, data integrity, and optimized performance.
a) Simple Query:
Example:
sql
CopyEdit
A query embedded within another query. The inner query provides input to the outer query.
Types:
Example:
sql
CopyEdit
SQL functions are built-in operations used to perform calculations or manipulate data. They are
divided into two main categories:
Example:
sql
CopyEdit
Example:
sql
CopyEdit
Joins in SQL are used to combine rows from two or more tables based on a related column between
them (usually a foreign key). Joins are essential for retrieving meaningful data that is spread across
multiple tables.
a) INNER JOIN:
sql
CopyEdit
FROM Employees
ON Employees.DeptID = Departments.ID;
Returns all records from the left table and matched records from the right table. Non-matching rows
from the right will return NULLs.
sql
CopyEdit
FROM Employees
ON Employees.DeptID = Departments.ID;
Opposite of LEFT JOIN. All records from the right table and matching from the left.
Returns all records when there is a match in either left or right table.
e) CROSS JOIN:
Returns the Cartesian product (every row of table A joined with every row of table B).
sql
CopyEdit
FROM Customers A
Constraints ensure the accuracy and reliability of data in the database. These are rules enforced on
data columns to maintain integrity.
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
sql
CopyEdit
3. Views in SQL:
A View is a virtual table based on the result set of an SQL query. It does not store data physically but
provides a way to simplify complex queries, enhance security, and improve readability.
Syntax:
sql
CopyEdit
FROM Employees
Features of Views:
• Can be used in SELECT, JOIN, and even UPDATE (in certain conditions).
sql
CopyEdit
sql
CopyEdit
UPDATE ActiveEmployees
SET DeptID = 5
Note: The update will reflect in the underlying table if the view is updatable.