0% found this document useful (0 votes)
12 views

DBMS Diya

database management system

Uploaded by

shdadj246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DBMS Diya

database management system

Uploaded by

shdadj246
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

PRACTICAL FILE

OF
DATABASE MANAGEMENT SYSTEMS LAB

SESSION : 2024-25
Submitted By : Ajay
Roll No. : 2022027504
Semester : V
B. Tech (CSE)

Submitted to : Mrs. Divya (Assistant Professor)


Department of Computer Science and Engineering,
State Institute of Engineering and Technology, Nilokheri (Karnal),
Haryana – 132117

(Affiliated to Kurukshetra University, Kurukshetra)

i
Practical Page
S. No. Title of Practical Signature
Date No.

Write the queries for Data


1. Definition Language (DDL) in
RDBMS.

Write the queries for Data


2. Manipulation Language (DML) in
RDBMS.

Write the queries for Data Control


3.
Language (DCL) in RDBMS.

To perform various integrity


4.
constraints on relational database.

Create a database and perform the


following operations: -
a. Arithmetic and Relational
5. operations
b. Group by & having clauses
c. Like predicate for pattern
matching in database.

Write SQL queries for relational


6.
algebra

Write SQL queries for extracting


7.
data from more than one table

Write SQL queries for sub queries,


8.
nested queries.

Concepts for ROLL BACK,


9.
COMMIT & CHECK POINTS

Using two tables create a view,


10. which shall perform natural join,
equi join, outer joins.

ii
Practical Page
S. No. Title of Practical Signature
Date No.

Write a procedure for computing


income tax of employee on the basic
of following conditions:
a. If gross pay<=40,000 then I.T rate
is 0%.
b. If gross pay>40,000 but <60000
then I.T rate is 10%.
11.
c. If gross pay>60,000 but
<1,00,0000 then I.T rate is 20%.
d. if gross pay>1,00,0000 then I.T
rate is 30%.
For this purpose create a table with
name, ssn, gross salary and income
tax of the employee.

Write trigger for before and after


12. insertion, deletion and updation
process.

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:

Command Description Syntax

CREATE TABLE
Create database or its objects (table,
table_name (column1
CREATE index, function, views, store
data_type, column2
procedure, and triggers)
data_type, ...);

DROP Delete objects from the database DROP TABLE table_name;

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;

DDL Commands and Examples:


1. CREATE: Used to create new database objects such as tables, views, or indexes.

4
2. ALTER: Used to modify the structure of an existing table, such as adding, deleting, or
modifying columns.

3. DROP: Used to delete objects such as tables or databases.

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. DESCRIBE: The DESCRIBE command in MySQL is used to display the structure of a


table, including information about the columns, their data types, nullability, default values, and
any indexes or keys defined on the table. It provides a quick way to get the schema of 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:

Command Description Syntax

INSERT INTO table_name (column1,


INSERT Insert data into a table column2, ...) VALUES (value1, value2,
...);

UPDATE table_name SET column1 =


Update existing data within a
UPDATE value1, column2 = value2 WHERE
table
condition;

Delete records from a DELETE FROM table_name WHERE


DELETE
database table condition;

SELECT column1, column2, ... FROM


SELECT Select data from a database
table_name;

DML Commands and Examples:


1. INSERT: Used to add new rows of data into a table.

7
2. UPDATE: Used to modify existing records in a table.

3. DELETE: Used to remove records from 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:

Command Description Syntax

Assigns new privileges to a user GRANT privilege_type


account, allowing access to specific [(column_list)] ON [object_type]
GRANT
database objects, actions, or object_name TO user [WITH
functions. GRANT OPTION];

REVOKE [GRANT OPTION


Removes previously granted
FOR] privilege_type
privileges from a user account,
REVOKE [(column_list)] ON [object_type]
taking away their access to certain
object_name FROM user
database objects or actions.
[CASCADE];

DCL Commands and Examples:


1. GRANT: Used to give a user access privileges to database objects.

2. REVOKE: Used to take back privileges granted to a user.

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.

Test For Not-Null Constraints-

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.

Test For Primary Key Constraints-

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 .

(-):Subtraction - Subtracts right hand operand from left hand operand .

15
(*):Multiplication - Multiplies values on either side of the operator.

(/):Division - Divides left hand operand by right hand operand.

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’:

2. Match a String Ending with a Pattern:


To retrieve employees whose names end 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:

4. Match a String with a Single Character:


To retrieve employees whose names have exactly 4 characters:

5. Match with Specific Characters in Certain Positions:


Retrieve employees whose department names start with "C" and end with "e":

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

4. Set Difference (-):


Set Difference: Finds tuples in one relation that are not in another.
Equivalent SQL operation: EXCEPT

5. Cartesian Product (×):


Combines all tuples from two relations.
Equivalent SQL operation: CROSS JOIN

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.

1. Subquery in the WHERE Clause


A subquery in the WHERE clause is used to filter the results based on the result of
another query. A single-row subquery returns a single value and is often used in
conditions such as =, <, >, etc.

2. Subquery in the FROM Clause


A subquery in the FROM clause can be used to create a temporary table, which is then
used in the outer query. The subquery in the FROM clause acts as a derived table or
temporary view for the outer query.

3. Subquery with IN Operator


The IN operator is used when the subquery returns multiple rows. It is used to compare
the result of the subquery with a list of values. A multi-row subquery returns multiple
values, which can be used with operators like IN, ANY, or ALL.

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.

5. Subquery in the SELECT Clause


A subquery in the SELECT clause can be used to return calculated values or derived
data for each row returned by the main query. The subquery in the SELECT clause
computes a value for each row in the outer query.

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.

AN EXAMPLE WITH COMMIT, ROLLBACK, and CHECKPOINT

28
29
Practical No. – 10
Aim: Using two tables create a view, which shall perform natural join, equi join, outer
joins.

Theory:

30

You might also like