Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
7 views30 pages

Checked

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 30

PRACTICAL FILE

OF
DATABASE MANAGEMENT SYSTEMS LAB

SESSION : 2024-25
Submitted By : Drishti Sharma

Roll No. : 2022027517


Semester : V
B. Tech (CSE)

Submitted to : Mrs. Divya Mam

Department of Computer Science and Engineering,


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

(Affiliated to Kurukshetra University, Kurukshetra)


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


8.
queries, nested queries.

Concepts for ROLLBACK,


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%.
c. If gross pay>60,000 but
11. <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 - 1
Aim: Write the queries for Data Definition Language (DDL) in RDBMS.

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.

This guide will introduce you to the various SQL sublanguage commands,
including Data Definition Language (DDL), Data Query Language (DQL), Data
Manipulation Language (DML), Data Control Language (DCL), and Transaction Control
Language (TCL)

List of DDL Commands:

Command Description Syntax

Create database or its objects CREATE TABLE


(table, index, function, views, table_name (column1 data_type,
CREATE
store procedure, and triggers) column2 data_type, ...);

Delete objects from the


DROP DROP TABLE table_name;
database

Alter the structure of the ALTER TABLE table_name


ALTER ADD COLUMN column_name
database
data_type;
Remove all records from a
table, including all spaces TRUNCATE TABLE
TRUNCAT
allocated for the records are table_name;
E
removed

Add comments to the data COMMENT 'comment_text'


COMMENT
dictionary ON TABLE table_name;

DESCRIBE Commands to describe the DESCRIBE table_name;


structure of a table.

Rename an object existing in the RENAME TABLE


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.

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. DESCRIBE: Used to describe the structure of a table existing database object, such as a table.

6. RENAME: Used to rename an existing database object, such as a table.


Practical – 2
Aim: Write the queries for Data Manipulation Language (DML) in RDBMS.

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. This guide will introduce you to the various SQL sublanguage
commands, including Data Definition Language (DDL), Data Query
Language (DQL), Data Manipulation Language (DML), Data Control Language (DCL),
and Transaction Control Language (TCL).
DML is an abbreviation for Data Manipulation Language. Represents a collection of
programming languages explicitly used to make changes to the database, such as: CRUD
operations to create, read, update and delete data.Data Manipulation Language, also known as
DML, is a set of sql command that are used to manipulate data within database tables or
query views. Data analysts, scientists, engineers, and anyone using SQL rely on DML in
order to access, transform, and analyze data. If you’ve ever worked with SQL, you’ve likely
used DML commands. If not, don’t worry—in the next section, I’ll explain DML commands
in more detail.
List of DML commands:

mand 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
UPDATE = value1, column2 = value2 WHERE
within a table
condition;

Delete records from a DELETE FROM table_name WHERE


DELETE
database table condition;

SELECT column1, column2, ...FROM tab


Select data from a database
SELECT le_name;
DML Commands and Examples:
1.INSERT: Used to add new rows of data into a table.

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

3. DELETE: Used to remove records from a table.

4. SELECT: Used to select data from a database.


Practical – 3
Aim: Write the queries for Data Control Language (DCL) in RDBMS.

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 [(column_list)] ON [object_type]
GRANT
specific database objects, actions, object_name TO user [WITH
or functions. GRANT OPTION];

Removes previously granted REVOKE [GRANT


privileges from a user account, OPTION
REVOKE taking away their access to certain FOR] privilege_type
database objects or actions. [(column_list)] ON
[object_type] object_name
FROM user [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.


Practical – 4
Aim: To perform various integrity constraints on relational database.

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.

Integrity Constraints are the protocols that a table's data columns must follow. These are used to
restrict the types of information that can be entered into a table. This means that the data in the
database is accurate and reliable.

In Relational Database Model , constraints are guidelines or limitations imposed on database


tables to maintain the integrity, correctness, and consistency of the data. Constraints can be used
to enforce data linkages across tables, verify that data is unique, and stop the insertion of
erroneous data. The various types of integrity constraints are

Types of Integrity Constraints:


 Domain Constraints
 Key Constraints
 Entity integrity Constraints
 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-

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.

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.
Test For Key Constraints-

4.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 Refierential Integrity Constraints-


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 .

(*):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.
Relational operators like =, ≠, ≥, <, >, <= AND, BETWEEN.
Practical No. – 5(b)
Aim: Create a database and perform Group by & having clauses.

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

3. Match a String Containing a Pattern Anywhere:


To retrieve employees whose names contain the letter 'ishti' 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":
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
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
6. Join (⋈):
Combines tuples from two relations based on a related attribute.
Equivalent SQL operation: INNER JOIN
Practical No. – 6
Aim: Write SQL queries for relational algebra.
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.
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.
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.
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.

You might also like