0% found this document useful (0 votes)
166 views14 pages

Database Lab 5

This document describes a lab assignment on database design and constraints in SQL. The objectives are to create relations with constraints like primary keys and foreign keys. Students are instructed to populate tables with data, add and modify constraints, and perform queries. The lab aims to demonstrate how constraints enforce data integrity when records are inserted, updated, and deleted from relations.

Uploaded by

Mydah Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
166 views14 pages

Database Lab 5

This document describes a lab assignment on database design and constraints in SQL. The objectives are to create relations with constraints like primary keys and foreign keys. Students are instructed to populate tables with data, add and modify constraints, and perform queries. The lab aims to demonstrate how constraints enforce data integrity when records are inserted, updated, and deleted from relations.

Uploaded by

Mydah Nasir
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 14

Department of Computing

CS220: Database Systems

Class: BESE 11AB


Lab 05: DDL and Constraints

Date: October 25, 2021


Time: 9:00 – 12:00 & 2:00-05:00
Instructor: Dr. Shah Khalid & Dr. Muneer Ahmed

Lab Engineer: Anum Asif

CLO-1: Create a database schema that incorporates keys and integrity


constraints.
Lab 05: DDL and Constraints

Introduction
This lab will focus on the DDL (Data Definition Language) part of SQL. It discusses how to
create relations and discusses different types of constraints defined in SQL. Constraints are
specific set of rules defined for SQL objects. In this lab you will implement constraints and then
observe how a constraint ensures that valid data is inserted into the relations.

Objectives
After completing this lab, you should be able to do the following:
 Create relations in a database
 Create constraints including NOT NULL, Unique Key Constraint, Primary Key, Foreign
Key

Tools/Software Requirement
 MySQL Community Server 5.6
 MySQL Workbench 6.1

Description

Actions taken when DELETE or UPDATE operation is performed

When a referential integrity constraint violation occurs in case of delete or update operation, you
can specify one the operations as a response.

 Restrict
 Set NULL, SET DEFAULT
 Cascade (Propagate the change)
CREATE TABLE employees(

employee_id INT,
first_name VARCHAR(20),
job_id INT NOT NULL,
dept_id INT,
CONSTRAINT emp_emp_id_pk
PRIMARY KEY (EMPLOYEE_ID),
CONSTRAINT emp_dept_id_fk
FOREIGN KEY (dept_id) references departments
(department_id)

ON DELETE SET NULL

ON UPDATE CASCADE);

Inserting Values
The syntax to insert values into a table is as follows:

INSERT INTO employees

Values(2,'John',10,2);

There is an implicit and explicit method to add values to a table.

 Implicit method: Omit the column from the column list.

INSERT INTO employees (employee_id, first_name)

Values(2,'John');

 Specify the NULL keyword in the VALUES clause.


INSERT INTO employees

Values(2,'John',NULL, NULL);

 Adding multiple rows at a time:

INSERT INTO employees

Values(2,'John',10, NULL),(3,'Smith',12, NULL);

Deleting Values
The syntax to delete values from a table is as follows:

DELETE FROM table_name

[WHERE condition];

Updating Values
The syntax to insert values into a table is as follows:
UPDATE table
SET column = value [, column = value, ...]
[WHERE condition];

Constraints Table

All constraints are stored in relation known as table_constraint. This relation is stored in the
system schema “Information_schema” and can be extracted using the following query.

select *
from information_schema.table_constraints;

Dropping a Constraint
To drop a constraint, you can identify the constraint name from the TABLE_CONSTRAINTS.
Then use the ALTER TABLE statement with the DROP clause.

Syntax

ALTER TABLE table


DROP CONSTRAINT constraint;

In the syntax:
table is the name of the table
constraint is the name of the constraint

Example:

ALTER TABLE employees


DROP CONSTRAINT emp_manager_fk;
Lab Task
Write SQL expressions for each of the following queries and execute them:

1. After creating the database in Lab 04 using your SQL statements, populate the database according
to the data given in text files using the SQL INSERT commands.

Inserting values in table Student:

insert into student values(060839453,'Charles Harris','Architecture','SR',22),


(099354543,'Susan Martin','Law','JR',20),
(112348546,'Joseph Thompson','Computer Science','SO',19),
(115987938,'Christopher Garcia','Computer Science','JR',20),
(132977562,'Angela Martinez','History','SR',20),
(269734834,'Thomas Robinson','Psychology','SO',18),
(280158572,'Margaret Clark','Animal Science','FR',18),
(301221823,'Juan Rodriguez','Psychology','JR',20),
(318548912,'Dorthy Lewis','Finance','FR',18),
(320874981,'Daniel Lee','Electrical Engineering','FR',17),
(322654189,'Lisa Walker','Computer Science','SO',17),
(348121549,'Paul Hall','Computer Science','JR',18),
(351565322,'Nancy Allen','Accounting','JR',19),
(451519864,'Mark Young','Finance','FR',18),
(455798411,'Luis Hernandez','Electrical Engineering','FR',17),
(462156489,'Donald King','Mechanical Engineering','SO',19),
(550156548,'George Wright','Education','SR',21),
(552455318,'Ana Lopez','Computer Engineering','SR',19),
(556784565,'Kenneth Hill','Civil Engineering','SR',21),
(567354612,'Karen Scott','Computer Engineering','FR',18),
(573284895,'Steven Green','Kinesiology','SO',19),
(574489456,'Betty Adams','Economics','JR',20),
(578875478,'Edward Baker','Veterinary Medicine','SR',21);

Screenshot:
Inserting values in table Faculty:

insert into faculty values(142519864,'Ivana Teach',20),


(242518965,'James Smith',68),
(141582651,'Mary Johnson',20),
(011564812,'John Williams',68),
(254099823,'Patricia Jones',68),
(356187925,'Robert Brown',12),
(489456522,'Linda Davis',20),
(287321212,'Michael Miller',12),
(248965255,'Barbara Wilson',12),
(159542516,'William Moore',33),
(090873519,'Elizabeth Taylor',11),
(486512566,'David Anderson',20),
(619023588,'Jennifer Thomas',11),
(489221823,'Richard Jackson',33),
(548977562,'Ulysses Teach',20);

Screenshot:
Inserting values in table Class:

insert into class values ('Data Structures','MWF 10','R128',489456522),


('Database Systems','MWF 12:30-1:45','1320 DCL',142519864),
('Operating System Design','TuTh 12-1:20','20 AVW',489456522),
('Archaeology of the Incas','MWF 3-4:15','R128',248965255),
('Aviation','TuTh 1-2:50','Q3',011564812),
('Air Quality Engineering','TuTh 10:30-11:45','R15',011564812),
('Introductory Latin','MWF 3-4:15','R12',248965255),
('American Political Parties','TuTh 2-3:15','20 AVW',619023588),
('Social Cognition','Tu 6:30-8:40','R15',159542516),
('Perception','MTuWTh 3','Q3',489221823),
('Multivariate Analysis','TuTh 2-3:15','R15',090873519),
('Patent Law','F 1-2:50','R128',090873519),
('Urban Economics','MWF 11','20 AVW',489221823),
('Organic Chemistry','TuTh 12:30-1:45','R12',489221823),
('Marketing Research','MW 10-11:15','1320 DCL',489221823),
('Seminar in American Art','M 4','R15',489221823),
('Orbital Mechanics','MWF 8','1320 DCL',011564812),
('Dairy Herd Management','TuTh 12:30-1:45','R128',356187925),
('Communication Networks','MW 9:30-10:45','20 AVW',141582651),
('Optical Electronics','TuTh 12:30-1:45','R15',254099823),
('Intoduction to Math','TuTh 8-9:30','R128',489221823);

Screenshot:
Inserting values in table Enrolled:

insert into enrolled values(112348546,'Database Systems'),


(115987938,'Database Systems'),
(348121549,'Database Systems'),
(322654189,'Database Systems'),
(552455318,'Database Systems'),
(455798411,'Operating System Design'),
(552455318,'Operating System Design'),
(567354612,'Operating System Design'),
(112348546,'Operating System Design'),
(115987938,'Operating System Design'),
(322654189,'Operating System Design'),
(567354612,'Data Structures'),
(552455318,'Communication Networks'),
(455798411,'Optical Electronics'),
(301221823,'Perception'),
(301221823,'Social Cognition'),
(301221823,'American Political Parties'),
(556784565,'Air Quality Engineering'),
(099354543,'Patent Law'),
(574489456,'Urban Economics');

Screenshot:
2. Drop foreign key constraints in Enrolled table.
Command:

alter table enrolled drop constraint enrolled_ibfk_1;


alter table enrolled drop constraint enrolled_ibfk_2;

Screenshot:

3. Add foreign key constraints in Enrolled table, with set null on delete, cascade on update.

Command:

alter
4. table enrolled add constraint foreign key enroll_snum_fk (snum) references student (snum) on
5. set null
delete
on6.update
Screenshot:
cascade;
alter table enrolled add constraint enroll_cname_fk foreign key(cname) references class(cname) on
delete set null
on update cascade;

Screenshot:
4.Update a class name “Operating System Design” to “Operating Systems” in Class table.

Command:

update
7. class set cname = 'Operating System' where cname = 'Operating System Design';

Screenshot:

5. Retrieve class name: “Operating System Design” in Enrolled table, show row numbers.
Command:

select
8. cname from class where cname = 'Operating System Design';

Screenshot:

6. Retrieve class name: “Operating Systems” in Enrolled table, show row numbers.
Command:

select
9. cname from class where cname = 'Operating System';

Screenshot:
7. Delete class name: “Patent Law” from Class table.

Command:

delete
10. from class where cname = 'Patent Law';

Screenshot:

8. Retrieve class name: “Patent Law” from Enrolled table, show row numbers.

Command:

select
11. cname from class where cname = 'Patent Law';

Screenshot:
9. Change constraints option on delete to Restrict on snum in Enrolled table.
Command:

alter
12.table enrolled add constraint enroll_snum_fk foreign key (snum) references student (snum) on
delete restrict
on update cascade;
alter table enrolled add constraint enroll_cname_fk foreign key(cname) references class(cname) on
delete restrict
on update cascade;

Screenshot:

10. Delete level “JR” tuples from Student table, show row numbers.

Command:

delete
13. from student where level='JR';

Screenshot:
11. Change constraints option on delete to No Action on snum in Enrolled table
Command:

alter
14.table enrolled drop constraint enroll_snum_fk;
alter table enrolled add constraint enroll_snum_fk foreign key (snum) references student (snum) on
delete no action
on update cascade;

Screenshot:

Deliverables

Save all queries and their results in the word document that you are executing. Upload this
document to LMS.

You might also like