Database Lab 5
Database Lab 5
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
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 UPDATE CASCADE);
Inserting Values
The syntax to insert values into a table is as follows:
Values(2,'John',10,2);
Values(2,'John');
Values(2,'John',NULL, NULL);
Deleting Values
The syntax to delete values from a table is as follows:
[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
In the syntax:
table is the name of the table
constraint is the name of the constraint
Example:
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.
Screenshot:
Inserting values in table Faculty:
Screenshot:
Inserting values in table Class:
Screenshot:
Inserting values in table Enrolled:
Screenshot:
2. Drop foreign key constraints in Enrolled table.
Command:
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.