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

Lab Work-2080

Database management system lab manual for beginners.

Uploaded by

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

Lab Work-2080

Database management system lab manual for beginners.

Uploaded by

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

Table of Contents:

Lab Sheet-1: How to Create a Database and various Tables using CMD in MySQL XAMPP. Write
the syntax and explain. .............................................................................................................. 2
Lab Sheet-2: Use of SQL Data Definition Language (DDL) to create the COMPANY Database
and create following tables in MySQL XAMPP. .......................................................................... 2
Lab Sheet-3: Use Draw.io CASE Tool to draw the E-R database of COMPANY database nd
import in word document. ......................................................................................................... 2
Lab Sheet-4: Prepare the requirements (miniworld) of Patan Multiple Campus Library and
draw E-R diagram and their relational database Tables. ............................................................ 2
Lab Sheet-5: Prepare the requirements (miniworld) of Students database of Patan Multiple
Campus BIT Department . Draw E-R diagram and their relational database Tables. .................. 2
Lab Sheet-6: Use of SQL Constraints – The following constraints are commonly used in SQL ... 2
Lab Sheet-7: What is Referential Integrity? .............................................................................. 2
Lab Sheet-8: What is domain constraints? Explain the difference between PRIMARY and
UNIQUE constraunts. Use following constraints to create EMPLOYEE table. ............................. 5
Lab Sheet-9: Create all the COMPANY Database Tables with all various constrains (Primary,
Domain, Foreign and Referential integrity) and insert 5 records in each tables and then use the
following SQL commands. .......................................................................................................... 6
Lab Sheet-9_1: Use of SELECT and FROM clauses. .................................................................. 6
Lab Sheet-9_2 Use of SELECT, FROM and WHERE clauses....................................................... 6
Lab Sheet-9_3: Use of GROUP BY clause ................................................................................. 6
Lab Sheet-9_4: Use of HAVING clause .................................................................................... 6
Lab Sheet-9_5: Use of ORDER BY clause ................................................................................. 6
Lab Sheet-1: How to Create a Database and various Tables using CMD in MySQL XAMPP. Write
the syntax and explain.

Lab Sheet-2: Use of SQL Data Definition Language (DDL) to create the COMPANY Database and
create following tables in MySQL XAMPP.
EMPLOYEE(Ssn,Fname,Mnit,Lname,Bdate,Address,Sex,Salary,Super_ssn,Dno)

DEPARTMENT(Dname,Dnumber,Mgr_ssn,Mgr_start_date)

PROJECT(Pname,Pnumber,Plocation,Dnum)

DEPENDENT(Essn,Dependent_name,Sex,Bdate,Relationship)

DEPT_LOCATIONS (Dnumber,Dlocation)

WORKS_ON (Essn,Pno,Hours)

Lab Sheet-3: Use Draw.io CASE Tool to draw the E-R database of COMPANY database and import
in word document.

Lab Sheet-4: Prepare the requirements (miniworld) of Patan Multiple Campus Library and draw
E-R diagram and their relational database Tables.

Lab Sheet-5: Prepare the requirements (miniworld) of Students database of Patan Multiple
Campus BIT Department . Draw E-R diagram and their relational database Tables.

Lab Sheet-6: Use of SQL Constraints – The following constraints are commonly used in SQL
 NOT NULL - Ensures that a column cannot have a NULL value
 UNIQUE - Ensures that all values in a column are different
 PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
 FOREIGN KEY – create foreign key to reference another table column value. The FOREIGN KEY constraint
prevents invalid data from being inserted into the foreign key column
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified

Lab Sheet-7: What is Referential Integrity?

Example: CASCADE Referential Integrity Constraints in MySQL

Step1: Create two Tables Employees and Department

Employees (Id, Name, DepartmentID) where Id is primary Key and DepartmentID is foreign key
Department(Id, Dname) where ID is primary key
CREATE TABLE Employees(
Id INT NOT NULL,
Name varchar(25),
DepartmentID INT,
PRIMARY KEY (Id));

CREATE TABLE Department(


Id INT NOT NULL,
Dname varchar(25),
PRIMARY KEY (Id));

Step2: Alter Employees table to create foreign key with referential integrity

Example as
ALTER TABLE Employees ADD FOREIGN KEY(DepartmentID) REFERENCES Department (Id)
ON DELETE CASCADE
ON UPDATE CASCADE;
Step-3: Now, insert some data into the Employees and Department tables by executing the below SQL
statement.

Insert into Department values (10, 'IT');


Insert into Department values (20, 'HR');
Insert into Department values (30, 'INFRA');

INSERT into Employees VALUES (101, 'Anurag', 10);


INSERT into Employees VALUES (102, 'Pranaya', 20);
INSERT into Employees VALUES (103, 'Hina', 30);
------------------------------------------------------------------

Step-4: Now, delete the Department whose Id is 10 from the Department table by executing the below SQL
Statement.

DELETE FROM Department WHERE Id = 10;

Now, you can see the above DELETE statement executed successfully, and further if you notice the employees
whose DepartmentId is 10 are also deleted from the Employees table automatically.

Step-5: Now, execute the below UPDATE statement.

UPDATE Department SET Id = 40 WHERE Id = 30;

Now, you can see the above UPDATE statement also executed successfully, and further if you notice the
employees whose DepartmentId is 30 are also updated as 40 in the Employees table automatically.

Example: SET NULL Referential Integrity Constraints in MySQL

Step-1: First, delete the Employees table by executing the below SQL Statement.
DROP TABLE Employees;

Step-2: Then truncate the Department table and add the three records by executing the below SQL
Statement.
TRUNCATE TABLE Department;
Insert into Department values (10, 'IT');
Insert into Department values (20, 'HR');
Insert into Department values (30, 'INFRA');

Step-3: Now, create the Employees table by executing the below SQL Statement.
CREATE TABLE Employees(
Id INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
DepartmentID INT,
FOREIGN KEY (DepartmentId) REFERENCES Department(Id)
ON DELETE SET NULL
ON UPDATE SET NULL
);

Step-4:Now, insert the following records into the Employees table by executing the below INSERT
Statements.
INSERT into Employees VALUES (101, 'Anurag', 10);
INSERT into Employees VALUES (102, 'Pranaya', 20);
INSERT into Employees VALUES (103, 'Hina', 30);

Step-5: Now, delete the Department whose Id is 10 from the Department table by executing the
below SQL Statement.

DELETE FROM Department WHERE Id = 10;


Now, you can see the above DELETE statement executed successfully, and further if you notice the
Employees table, those employees whose DepartmentId is 10 are set to NULL automatically.

Step-5: Now, execute the below UPDATE statement.


UPDATE Department SET Id = 40 WHERE Id = 30;
Now, you can see the above UPDATE statement also executed successfully, and further if you
notice the employees whose DepartmentId is 30 are also updated as NULL in the Employees table
automatically.
Example: NO ACTION Referential Integrity Constraints in MySQL

Step-1: First, delete the Employees table by executing the below SQL Statement.
DROP TABLE Employees;
Step-2: Then truncate the Department table and add the three records by executing the below SQL
Statement.
TRUNCATE TABLE Department;
Insert into Department values (10, 'IT');
Insert into Department values (20, 'HR');
Insert into Department values (30, 'INFRA');

Step-3:Now, create the Employees table by executing the below SQL Statement.
CREATE TABLE Employees(
Id INT PRIMARY KEY,
Name VARCHAR(100) NOT NULL,
DepartmentID INT,
FOREIGN KEY (DepartmentId) REFERENCES Department(Id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
);

Step-4:Now, insert the following records into the Employees table by executing the below INSERT
Statements.
INSERT into Employees VALUES (101, 'Anurag', 10);
INSERT into Employees VALUES (102, 'Pranaya', 20);
INSERT into Employees VALUES (103, 'Hina', 30);

Step-5: Now, delete the Department whose Id is 10 from the Department table by executing the
below SQL Statement.
DELETE FROM Department WHERE Id = 10;
Now, when you try to execute the above DELETE statement, you will get the following error
message, and the delete operation is rollback.
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails
(`employeedb`.`employees`, CONSTRAINT `employees_ibfk_1` FOREIGN KEY
(`DepartmentID`) REFERENCES `department` (`Id`))

Step-6:Now, execute the below UPDATE statement.


UPDATE Department SET Id = 40 WHERE Id = 30;
Now, when you try to execute the above UPDATE statement then you will get the following error
message and the update operation is rollback.
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails
(`employeedb`.`employees`, CONSTRAINT `employees_ibfk_1` FOREIGN KEY
(`DepartmentID`) REFERENCES `department` (`Id`))

Lab Sheet-8: What is domain constraints? Explain the difference between PRIMARY and UNIQUE
constraunts. Use following constraints to create EMPLOYEE table.
EMPLOYEE(Ssn,Fname,Mnit,Lname,Bdate,Address,Sex,Salary,Super_ssn,Dno)

 NOT NULL - Ensures that a column cannot have a NULL value


 UNIQUE - Ensures that all values in a column are different
 CHECK - Ensures that the values in a column satisfies a specific condition
 DEFAULT - Sets a default value for a column if no value is specified
Lab Sheet-9: Create all the COMPANY Database Tables with all various constrains (Primary,
Domain, Foreign and Referential integrity) and insert 5 records in each tables and then use the
following SQL commands.

Lab Sheet-9_1: Use of SELECT and FROM clauses.


a) Retrieve the SSN values for all employees.
b) Retrieve all the attribute values of EMPLOYEE.

Lab Sheet-9_2 Use of SELECT, FROM and WHERE clauses.


a) Retrieve the name and address of all employees who work for the 'Research' department.
b) For every project located in 'Kathmandu', list the project number, the controlling department number, and
the department manager's last name, address, and birthdate.

Lab Sheet-9_3: Use of GROUP BY clause


a) For each department, retrieve the department number, the number of employees in the department, and
their average salary.
b) For each project, retrieve the project number, project name, and the number of employees who work on
that project.

Lab Sheet-9_4: Use of HAVING clause


a) For each project on which more than two employees work, retrieve the project number, project name, and
the number of employees who work on that project.

Lab Sheet-9_5: Use of ORDER BY clause


a) Retrieve a list of employees and the projects each works in, ordered by the employee's department, and
within each department ordered alphabetically by employee last name.

You might also like