DBMS Diya
DBMS Diya
OF
DATABASE MANAGEMENT SYSTEMS LAB
SESSION : 2024-25
Submitted By : Ajay
Roll No. : 2022027504
Semester : V
B. Tech (CSE)
i
Practical Page
S. No. Title of Practical Signature
Date No.
ii
Practical Page
S. No. Title of Practical Signature
Date No.
iii
Practical No. - 1
Aim: Write the queries for Data Definition Language (DDL) in RDBMS.
Theory:
Data Definition Language (DDL): Data Definition Language (DDL) is a subset of SQL
(Structured Query Language) used to define and manage database structures, such as tables,
indexes, and schema. The primary DDL commands are CREATE, ALTER, DROP,
TRUNCATE, and RENAME. These commands allow users to define, modify, and remove the
structure of database objects but not data. These commands are normally not used by a general
user, who should be accessing the database via an application.
List of DDL Commands:
CREATE TABLE
Create database or its objects (table,
table_name (column1
CREATE index, function, views, store
data_type, column2
procedure, and triggers)
data_type, ...);
ALTER TABLE
ALTER Alter the structure of the database table_name ADD COLUMN
column_name data_type;
Remove all records from a table,
TRUNCATE TABLE
TRUNCATE including all spaces allocated for the
table_name;
records are removed
COMMENT 'comment_text'
COMMENT Add comments to the data dictionary
ON TABLE table_name;
RENAME TABLE
Rename an object existing in the
RENAME old_table_name TO
database
new_table_name;
4
2. ALTER: Used to modify the structure of an existing table, such as adding, deleting, or
modifying columns.
4. TRUNCATE: Used to remove all records from a table, but not the table itself.
5
5. RENAME: Used to rename an existing database object, such as a table.
6
Practical No. – 2
Aim: Write the queries for Data Manipulation Language (DML) in RDBMS.
Theory:
The SQL commands that deal with the manipulation of data present in the database belong to
DML or Data Manipulation Language and this includes most of the SQL statements. It is the
component of the SQL statement that controls access to data and to the database. Basically,
DCL statements are grouped with DML statements.
Data Manipulation Language (DML) is a subset of SQL used to retrieve, insert, update, and
delete data in a relational database. DML commands are used to manage the data stored within
database objects like tables. The primary DML commands are INSERT, UPDATE, and
DELETE.
List of DML commands:
7
2. UPDATE: Used to modify existing records in a table.
4. SELECT: The SELECT command in MySQL is used to retrieve data from one or more
tables. It allows you to query the database and display the results in a readable format.
8
Practical No. – 3
Aim: Write the queries for Data Control Language (DCL) in RDBMS.
Theory:
Data Control Language (DCL) is a subset of SQL that deals with the rights, permissions, and
control of access to data in an RDBMS. The primary DCL commands are GRANT and
REVOKE. These commands are used to give or remove access privileges to users on database
objects like tables, views, and procedures.
List of DCL commands:
9
Practical No. – 4
Aim: To perform various integrity constraints on relational database.
Theory:
Integrity constraints are the set of predefined rules that are used to maintain the quality of
information. Integrity constraints ensure that the data insertion, data updating, data deleting
and other processes have to be performed in such a way that the data integrity is not affected.
They act as guidelines ensuring that data in the database remain accurate and consistent. So,
integrity constraints are used to protect databases. The various types of integrity constraints are
Types of Integrity Constraints:
Domain Constraints
Entity integrity Constraints
Key Constraints
Primary Key Constrains
Referential integrity constraints
1. Domain Constraints: Domain constraints define the allowable values for a column. They
restrict the type of data that can be stored in each field to maintain data integrity.
Test For Domain Constraints-
10
2. Not-Null Constraints: The Not-Null constraint ensures that a column cannot have a NULL
value, meaning data must be provided for this column.
3. Entity Integrity Constraints: Entity integrity ensures that each record in a table can be
uniquely identified. This is typically achieved through a Primary Key constraint. Entity
integrity constraints state that primary key can never contain null value because primary key is
used to determine individual rows in a relation uniquely, if primary key contains null value
then we cannot identify those rows. A table can contain null value in it except primary key
field.
11
Test For Entity Integrity Constraints-
4. Key Constraints: Key constraints define the uniqueness of data in certain columns.
Common key constraints are Primary Key and Unique Key. The Primary Key ensures that no
two records have the same value for the key column, while the Unique Key ensures uniqueness
but allows one NULL value.
12
Test For Key Constraints-
5. Primary Key Constraints: The Primary Key constraint enforces both the entity integrity
and uniqueness of a column or set of columns. It ensures that no two rows have the same
primary key value and that the primary key cannot be null.
13
6. Referential Integrity Constraints: Referential integrity ensures that relationships between
tables remain consistent. A Foreign Key constraint is used to enforce referential integrity by
ensuring that a value in one table corresponds to a value in another table.
Test For Referential Integrity Constraints-
14
Practical No. – 5(A)
Aim: Create a database and perform the Arithmetic and Relational operations
Theory:
Arithmetic Operations:
Arithmetic operations in SQL are used to perform basic mathematical operations on data within
tables. They include addition (+), subtraction (-), multiplication (*), division (/), and modulus
(%). These operators can be used in queries to compute and manipulate values stored in
database tables.
(+) : Addition - Adds values on either side of the operator .
15
(*):Multiplication - Multiplies values on either side of the operator.
Relational Operations:
Use relational operators to filter data based on specific conditions.
16
Relational operators like =, ≠, ≥, <, >, <=, AND, BETWEEN.
17
Practical No. – 5(b)
Aim: Create a database and perform Group by & having clauses.
Theory:
The GROUP BY clause is often used with aggregate functions (MAX, SUM, AVG) to group
the results by one or more columns or In simple words we can say that The GROUP BY clause
is used in collaboration with the SELECT statement to arrange required data into groups.
Having Clause is basically like the aggregate function with the GROUP BY clause. The
HAVING clause is used instead of WHERE with aggregate functions.
Syntax:
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1, column2, ...;
Perform GROUP BY Clause
18
Practical No. – 5(c)
Aim: Create a database and perform Like predicate for pattern matching in database.
Theory:
The LIKE predicate in SQL is used for pattern matching in strings. It allows us to search for a
specified pattern within a column's value. The LIKE operator is often used with the SELECT
statement to filter rows based on patterns.
Syntax of the LIKE Predicate:
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
Wildcards in the LIKE Predicate:
1. Percent Sign (%): Represents zero or more characters.
2. Underscore (_): Represents a single character.
Common Usage Patterns:
%pattern%: Matches any string that contains pattern anywhere.
pattern%: Matches any string that starts with pattern.
%pattern: Matches any string that ends with pattern.
_pattern_: Matches any string with exactly one character before and after pattern.
1. Match a String starting with a pattern:
To retrieve employees whose names start with the letter ‘A’:
19
3. Match a String Containing a Pattern Anywhere:
To retrieve employees whose names contain the letter 'kit' anywhere in their name:
20
Practical No. – 6
Aim: Write SQL queries for relational algebra.
Theory:
Relational Algebra is a procedural query language that operates on relations (tables)
and uses operators to produce a new relation. It serves as the theoretical foundation for
SQL (Structured Query Language). SQL implements relational algebra operations to
query and manipulate databases effectively.
1. Selection (σ): Filters rows based on a condition.
2. Projection (π): Selects specific columns.
3. Union (∪): Combines results of two tables with distinct rows.
4. Set Difference (-): Finds rows in one table not present in another.
5. Cartesian Product (×): Combines every row of one table with every row of
another.
6. Join (⋈): Combines rows from two tables based on a condition.
1. Selection (σ):
Selection: A relational algebra operation to retrieve rows that satisfy a given condition.
Equivalent SQL operation: SELECT ... WHERE
2. Projection (π):
A relational algebra operation to retrieve specific columns (attributes) from a relation.
Equivalent SQL operation: SELECT column_names
21
3. Union (∪):
Combines tuples from two relations, removing duplicates.
Equivalent SQL operation: UNION
22
6. Join (⋈):
Combines tuples from two relations based on a related attribute.
Equivalent SQL operation: INNER JOIN
23
Practical No. – 7
Aim: Write SQL queries for extracting data from more than one table.
Theory:
In database management, extracting data from more than one table involves performing
operations like Joins, Subqueries, and Set Operations to retrieve meaningful data by combining
or filtering rows from multiple relations.
1. Inner Join
Combines rows from two tables where there is a match in the specified column(s).
2. Left Join
Retrieves all rows from the left table and matching rows from the right table. Rows
with no match in the right table contain NULL.
3. Right Join
Retrieves all rows from the right table and matching rows from the left table. Rows
with no match in the left table contain NULL.
24
4. Full Join
A Full Join (or Full Outer Join) returns all rows from both tables, with matching rows
where available. If there is no match, the result will contain NULL in the columns from
the table that lacks the matching row.
However, MySQL does not support FULL OUTER JOIN natively. You can simulate it
using a combination of LEFT JOIN and RIGHT JOIN with a UNION.
25
Practical No. – 8
Aim: Write SQL queries for sub queries, nested queries.
Theory:
A subquery is a query nested inside another query, used to retrieve data that will be
used by the outer query. Subqueries can be used in SELECT, INSERT, UPDATE, and
DELETE statements.
There are two types of subqueries:
1. Single-row subquery: Returns a single value.
2. Multi-row subquery: Returns multiple rows.
26
4. Subquery with EXISTS Operator
The EXISTS operator is used when you want to check if a subquery returns any result.
It returns TRUE if the subquery returns any rows, otherwise FALSE. The EXISTS
subquery is often used to check for the existence of rows.
6. Correlated Subquery
A correlated subquery is a subquery that references columns from the outer query. The
subquery is executed once for each row of the outer query. A correlated subquery
references columns from the outer query, meaning it cannot be executed independently.
27
Practical No. – 9
Aim: Concepts for ROLL BACK, COMMIT & CHECK POINTS
Theory:
In relational databases, transaction control commands are used to manage transactions
and ensure data consistency. These commands include ROLLBACK, COMMIT, and
CHECKPOINT.
1. COMMIT
The COMMIT command is used to save all changes made during the current
transaction to the database permanently. Once a COMMIT is issued, the changes
cannot be undone, and they become visible to other users and processes.
2. ROLLBACK
The ROLLBACK command is used to undo any changes made during the current
transaction. This will revert the database back to the state it was in before the transaction
began. It is useful when an error occurs, and you want to discard the changes.
3. CHECKPOINT
A CHECKPOINT is a mechanism used in database systems to ensure that all changes
made to the database are written to disk and stored in the transaction log. It marks a
point where the database is consistent and can be recovered to this point in case of a
failure. In MySQL, it is handled automatically.
28
29
Practical No. – 10
Aim: Using two tables create a view, which shall perform natural join, equi join, outer
joins.
Theory:
30