Database Management System: Course Code: CSL-402
Database Management System: Course Code: CSL-402
LAB MANUAL OF
Database Management
System
Course Code: CSL-402
Semester: IV (CBCGS)
Roll No: 65
Vision:
Mission:
To develop human resources with sound knowledge in theory and practice of computer science
and engineering
To motivate the students to solve real-world problems to help the society grow
To provide a learning ambience to enhance innovations, team spirit and leadership qualities for
students
Lab Code Lab Name Credits
CSL-402 Database Management System Lab 1
Lab Objectives:
1. To explore design and development of relational model.
2. To present SQL and procedural interface to SQL comprehensively.
3. To introduce the concept of transaction and transaction processing.
Lab Outcomes:
1. Design ER/EER diagram and convert to relational model for the real world application.
2. Apply DDL, DML, DCL and TCL commands.
3. Write simple and complex queries.
4. USePL / SQL constructor.
5. Demonstrate the concept of concurrent transactions execution and frontend-backend
connectivity.
Term Work:
2. Journal must include at least 2 assignments on content of theory and practical of “Database
Management System”
3. The final certification and acceptance of term work ensures that satisfactory performance of
laboratory work and minimum passing marks in term work.
4. Total 25 Marks (Experiments: 15-marks, Attendance Theory& Practical: 05-marks,
Assignments: 05-marks)
Oral & Practical exam
1. Experiments--------------------------------------------------(15)Marks.
2. Attendance Theory& Practical-----------------------------(05)Marks
3. Assignment----------------------------------------------------(05)Marks
Total Marks-----------------------------------------------------(25)Marks
List of Experiments
1 Identify the case study and detail statement of problem. Design an Entity-
Relationship (ER) / Extended Entity-Relationship (EER) Model.
3 Create a database using Data Definition Language (DDL) and apply integrity
constraints for the specified System.
THEROY: -
An entity-relationship (ER) diagram, also called an entity-relationship model, is aptly named: It shows the
relationships between entities. It is most commonly used to organize data within databases or information
systems.
There are two kinds of ER diagrams: Conceptual and Physical. Conceptual diagram models can be used as
the foundation for logical data models or to form commonality relationships between ER models as a basis
for data-model
A conceptual ER diagram uses six standard symbols. They are:
1) Entities are objects or concepts that represent important data. Also known as strong entities or parent
entities, these entities will often have weak entities that depend on them.
2) Attributes are characteristics of an entity, i.e. many-to-many or one-to-one.
3) Relationships are associations between entities.
4) Weak entities depend on another entity.
5) Multivalued attributes are attributes that can have more than one value.
6) Weak relationships are the connections between a weak entity and its parent.
Physical diagram models are more granular, showing the processes necessary to add information to a
database. Rather than using symbols, they are made up of a series of tables.
Entities are connected using a system of notation called crow’s foot notation. The styling of the
endpoint of each line distinguishes the relationship.
Enhanced entity-relationship (EER) diagrams are basically an expanded upon version of ER diagrams.
EER models are helpful tools for designing databases with high-level models. With their enhanced features,
you can plan databases more thoroughly by delving into the properties and constraints with more precision.
An EER diagram provides you with all the elements of an ER diagram while adding:
DIAGRAM / OUTPUT: -
(A)
(B)
THEROY: -
ER Model, when conceptualized into diagrams, gives a good overview of entity-relationship, which is
easier to understand. ER diagrams can be mapped to relational schema, that is, it is possible to create
relational schema using ER diagram. We cannot import all the ER constraints into relational model, but an
approximate schema can be generated.
There are several processes and algorithms available to convert ER Diagrams into Relational Schema.
Some of them are automated and some of them are manual. We may focus here on the mapping diagram
contents to relational basics.
ER diagrams mainly comprise of −
Mapping Process:
Mapping Process:
Mapping Process:
Mapping Process:
Mapping Process:
DIAGRAM / OUTPUT: -
(A)
(B)
(C)
(D)
CONCLUSION: - Thus we have Identify the Mapping ER/EER to Relational schema model.
Experiment No 3: Usage of Data Definition Language (DDL) to create database and apply integrity constrains
for the specified System.
THEROY: -
DDL is a set of guidelines to which all the Structured Query Languages adhere to. As in the computer
programming languages, we have the OOPS guidelines that all the programming languages adhere to,
similarly, we have Data Definition Language standards that all the database languages adhere to – MySQL,
Oracle, SQL Server, etc.
Data Definition Language deals with the structure of the database where the data is to be stored. It does
not deal with the data itself. Thus, in any structured query language, a command that can modify the
structure or the tables or relations of the database, is a DDL command. A command that modifies the data
stored is a DML command. A command that modifies the authorization rules is a DCL command. A
command that queries the database to fetch results is a DQL command.
Examples of DDL commands:
1) CREATE – is used to create the database or its objects (like table, index, function, views,
store procedure and triggers).
2) DROP – is used to delete objects from the database.
3) ALTER-is used to alter the structure of the database.
4) TRUNCATE–is used to remove all records from a table, including all spaces allocated for
S.E/IV 16 Database Management System (CSL402)
Vidya Vikas Education Trust’s
Integrity Constraints: -
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to be performed in such a
way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
OUTPUT: -
REQUIREMENTS: - Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6,
W3Schools
THEROY: -
database.
There are three basic constructs which allow database program and
• INSERT
• UPDATE
• DELETE
(A) INSERT: -
This is a statement is a SQL query. This command is used to insert data into the row of a
table.
Syntax:
Or
(B) UPDATE: -
This command is used to update or modify the value of a column in the table.
Syntax:
(C) DELETE: -
Syntax:
OUTPUT: -
(A)
INSERT INTO students (RollNo, FIrstName, LastName) VALUES ('60', 'Tom', Erichsen');
(B)
UPDATE students
WHERE StudID = 3;
(C)
CONCLUSION: - Thus we have Apply DML Commands for the specified system.
REQUIREMENTS: - Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6, W3Schools
THEROY: -
after that, you’re doing manipulations with a data (insert, update, delete) and at the
end, you need to commit a transaction.
COMMIT: -
As a mentioned above COMMIT command finishes transaction and stores all
changes made inside of a transaction.
Example/Output:
START TRANSACTION;
INSERT INTO student (name, lastname) VALUES ('Dmytro', 'Shvechikov');
COMMIT;
ROLLBACK: -
ROLLBACK statement reverts all changes made in the scope of transaction.
Example/Output:
START TRANSACTION;
THEROY: -
Join in DBMS is a binary operation which allows you to combine join product and selection in
one single statement. The goal of creating a join condition is that it helps you to combine the
data from two or more DBMS tables. The tables in DBMS are associated using the primary key
and foreign keys.
TYPES OF
JOINS: -
● Inner Join
o Theta Join
o EQUI join:
o Natural Join (⋈)
● Outer Join
o Left Outer Join (A B)
o Right Outer Join (A B)
o Full Outer Join (A B)
INNER JOIN: The INNER JOIN keyword selects all rows from both the tables as long as the condition
satisfies. This keyword will create the result-set by combining all rows from both the tables where the
condition satisfies i.e., value of the common field will be same.
LEFT JOIN: This join returns all the rows of the table on the left side of the join and matching rows for the
table on the right side of join. The rows for which there is no matching row on right side, the result-set will
contain null. LEFT JOIN is also known as LEFT OUTER JOIN.
RIGHT JOIN: RIGHT JOIN is similar to LEFT JOIN. This join returns all the rows of the table on the
right side of the join and matching rows for the table on the left side of join. The rows for which there is
no matching row on left side, the result-set will contain null. RIGHT JOIN is also known as RIGHT
OUTER JOIN.
FULL JOIN: FULL JOIN creates the result-set by combining result of both LEFT JOIN and RIGHT
JOIN. The result-set will contain all the rows from both the tables. The rows for which there is no
matching, the result-set will contain NULL values.
DIAGRAM / OUTPUT: -
REQUIREMENTS: - Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6, W3Schools
THEROY: -
SIMPLE QUERIES: -
➢ CREATE TABLE
CREATE TABLE does just what it sounds like: it creates a table in the database. You
can specify the name of the table and the columns that should be in the table.
CREATE TABLE table_name (
column_1 datatype,
column_2 datatype,
column_3 datatype
);
➢ ALTER TABLE
ALTER TABLE changes the structure of a table. Here is how you would add a column
to a database:
➢ CHECK
The CHECK constraint is used to limit the value range that can be placed in a
column.
If you define a CHECK constraint on a single column it allows only certain
values for this column. If you define a CHECK constraint on a table it can limit
the values in certain columns based on values in other columns in the row.
The following SQL creates a CHECK constraint on the “Age” column when the
“Persons” table is created. The CHECK constraint ensures that you cannot have
any person below 18 years.
CREATE TABLE Persons (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CHECK (Age>=18)
);
OUTPUT: -
+-----------+-------------------+------------+------------------------+
| studentID | FirstName | LastName | FullName |
+-----------+-------------------+------------+------------------------+
| 1 | Monique | Davis | Monique Davis |
| 2 | Teri | Gutierrez | Teri Gutierrez |
| 3 | Spencer | Pautier | Spencer Pautier |
| 4 | Louis | Ramsey | Louis Ramsey |
| 5 | Alvin | Greene | Alvin Greene |
| |
+-----------+-------------------+------------+------------------------+
9 rows in set (0.00 sec)
+-----------+------------------------+-----------+---------------------+
| studentID | FullName | sat_score | rcd_updated |
+-----------+------------------------+-----------+---------------------+
| 1 | Monique Davis | 400 | 2017-08-16 15:34:50 |
| 2 | Teri Gutierrez | 800 | 2017-08-16 15:34:50 |
| 3 | Spencer Pautier | 1000 | 2017-08-16 15:34:50 |
| 4 | Louis Ramsey | 1200 | 2017-08-16 15:34:50 |
| 5 | Alvin Greene | 1200 | 2017-08-16 15:34:50 |
| |
+-----------+------------------------+-----------+---------------------+
9 rows in set (0.00 sec)
+-----------+----------------------+-----------+---------------------+
| studentID | FullName | sat_score | rcd_updated |
+-----------+----------------------+-----------+---------------------+
| 1 | Monique Davis | 400 | 2017-08-16 15:34:50 |
| 2 | Teri Gutierrez | 800 | 2017-08-16 15:34:50 |
| 4 | Louis Ramsey | 1200 | 2017-08-16 15:34:50 |
| 5 | Alvin Greene | 1200 | 2017-08-16 15:34:50 |
| |
+-----------+----------------------+-----------+---------------------+
5 rows in set (0.00 sec)
CONCLUSION: - Thus we have Perform Simple queries, string manipulation operations and aggregate
functions.
REQUIREMENTS: -
Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6, W3Schools
THEROY: -
VIEWS: -
A view can contain all rows of a table or select rows from a table. A view can be
created from one or many tables which depends on the written SQL query to create
a view. Views, which are a type of virtual tables allow users to do the following –
1)Structure data in a way that users or classes of users find natural or intuitive.
2)Restrict access to the data in such a way that a user can see and (sometimes)
modify exactly what they need and no more.
3)Summarize data from various tables which can be used to generate reports.
TRIGGERS: -
Triggers are stored programs, which are automatically executed or fired when some
events occur. Triggers are, in fact, written to be executed in response to any of the
following events −
S.E/IV 32 Database Management System (CSL402)
Vidya Vikas Education Trust’s
Triggers can be defined on the table, view, schema, or database with which the event
is associated.
Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
OUTPUT: -
REQUIREMENTS: -
Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6, W3Schools
THEROY: -
Create MySQL Database at the Localhost :-
First, let me tell you what PHPMyAdmin is. It’s a control panel from where you can manage the database that
you’ve created. Open your browser and go to localhost/PHPMyAdmin or click “Admin” in XAMPP UI.
When you first installed XAMPP, it only created the username for it to be accessed, you now have to add a
password to it by yourself. For this, you have to go to User account where the user is the same as the one shown
in this picture:
Now click Edit privileges and go to Change Admin password, type your password there and save it. Remember
this password as it will be used to connect to your Database.
Note: It is not necessary to change the password to access databases on the localhost. It is a good practice and
that is why we have used a password.
Create Database
Now return to the homepage of PHPMyAdmin. Click the New button to create a new database.
In the new window, name your database as per your need, I am naming it “practice”. Now select Collation as
utf8_general_ci, as we are using it for learning purposes and it will handle all of our queries and data that will be
covered in this tutorial series. Now click on Create and your database will be created.
The newly created database will be empty now, as there are no tables in it. I will be covering that in the
upcoming series where we will learn how to create tables and insert data in it. In this tutorial, we are going to
connect this database to localhost using PHP.
Now, locate the folder where you installed XAMPP and open the htdocs folder (usually c:/xampp). Create a new
folder inside c:/xampp/htdocs/ and name it “practice” we will place web files in this folder. Why we have created
a folder in htdocs? XAMPP uses folders in htdocs to execute and run your PHP sites.
Note: If you are using WAMP, then add your practice folder in c:/wamp/www folder.
S.E/IV 36 Database Management System (CSL402)
Vidya Vikas Education Trust’s
Create a new PHP file and name it db_connnection.php and save it. Why am I creating a separate database
connection file? Because if you have created multiple files in which you want to insert data or select data from
the databases, you don’t need to write the code for database connection every time. You just have to include it by
using PHP custom function include (include ‘connection.php’) on the top of your code and call its function and
use it. It also helps when you are moving your project location from one PC to another and you have to change
the values on the single file and all the changes will be applied to all the other files automatically. Write the
following code in your dB connection file.
Experiment No 10: - Implementation and demonstration of Transaction and Concurrency control techniques
using locks.
S.E/IV 37 Database Management System (CSL402)
Vidya Vikas Education Trust’s
REQUIREMENTS: -
Relational Schema, Web Browser- Google Chrome, MySQL Editor v1.6, W3Schools
THEROY: -
Concurrent access is quite easy if all users are just reading data. There is no way they can interfere
with one another. Though for any practical Database, it would have a mix of READ and WRITE
operations and hence the concurrency is a challenge.
DBMS Concurrency Control is used to address such conflicts, which mostly occur with a multi-user
system. Therefore, Concurrency Control is the most important element for proper functioning of a
Database Management System where two or more database transactions are executed
simultaneously, which require access to the same data.
● Lock-Based Protocols
● Two Phase Locking Protocol
● Timestamp-Based Protocols
● Validation-Based Protocols
Lock-based Protocols
Lock Based Protocols in DBMS is a mechanism in which a transaction cannot Read or Write the
data until it acquires an appropriate lock. Lock based protocols help to eliminate the concurrency
problem in DBMS for simultaneous transactions by locking or isolating a particular transaction to a
single user.
A lock is a data variable which is associated with a data item. This lock signifies that operations that
can be performed on the data item. Locks in DBMS help synchronize access to the database items
by concurrent transactions.
All lock requests are made to the concurrency-control manager. Transactions proceed only once the
lock request is granted.
Binary Locks: A Binary lock on a data item can either locked or unlocked states.
Shared/exclusive: This type of locking mechanism separates the locks in DBMS based on their
uses. If a lock is acquired on a data item to perform a write operation, it is called an exclusive lock.
For example, when a transaction needs to update the account balance of a person. You can allows
this transaction by placing X lock on it. Therefore, when the second transaction wants to read or
write, exclusive lock prevent this operation.
3. Simplistic Lock Protocol
This type of lock-based protocols allows transactions to obtain a lock on every object before
beginning operation. Transactions may unlock the data item after finishing the 'write' operation.
4. Pre-claiming Locking
Pre-claiming lock protocol helps to evaluate operations and create a list of required data items which
are needed to initiate an execution process. In the situation when all locks are granted, the
transaction executes. After that, all locks release when all of its operations are over.
Starvation
Starvation is the situation when a transaction needs to wait for an indefinite period to acquire a lock.
Following are the reasons for Starvation:
Deadlock
Deadlock refers to a specific situation where two or more processes are waiting for each other to
release a resource or more than two processes are waiting for the resource in a circular chain.
● In the first phase, when the transaction begins to execute, it requires permission for the locks
it needs.
● The second part is where the transaction obtains all the locks. When a transaction releases its
first lock, the third phase starts.
● In this third phase, the transaction cannot demand any new locks. Instead, it only releases the
acquired locks.
The Two-Phase Locking protocol allows each transaction to make a lock or unlock request in two
steps:
● Growing Phase: In this phase transaction may obtain locks but may not release any locks.
● Shrinking Phase: In this phase, a transaction may release locks but not obtain any new lock
It is true that the 2PL protocol offers serializability. However, it does not ensure that deadlocks do
not happen.
In the above-given diagram, you can see that local and global deadlock detectors are searching for
deadlocks and solve them with resuming transactions to their initial states.
Strict Two-Phase Locking Method
Strict-Two phase locking system is almost similar to 2PL. The only difference is that Strict-2PL
never releases a lock after using it. It holds all the locks until the commit point and releases all the
locks at one go when the process is over.
Centralized 2PL
In Centralized 2 PL, a single site is responsible for lock management process. It has only one lock
manager for the entire DBMS.
Primary copy 2PL
Primary copy 2PL mechanism, many lock managers are distributed to different sites. After that, a
particular lock manager is responsible for managing the lock for a set of data items. When the
primary copy has been updated, the change is propagated to the slaves.
Distributed 2PL
S.E/IV 41 Database Management System (CSL402)
Vidya Vikas Education Trust’s
In this kind of two-phase locking mechanism, Lock managers are distributed to all sites. They are
responsible for managing locks for data at that site. If no data is replicated, it is equivalent to
primary copy 2PL. Communication costs of Distributed 2PL are quite higher than primary copy
2PL.
CONCLUSION: - Thus we have implemented & demonstrated the Transaction and Concurrency control
techniques using locks.