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

Lab 07 DB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 07 DB

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Computing

CS220: Database Systems

Class: Fall 2024


Lab 07: DDL and Constraints

Date: October 21, 2024


Time: 10:00-12:50/ 02:00-04:50
Instructor: Dr. Bilal Ali/ Dr. Shah Khalid

Lab Engineer: Ms. Ayesha Asif


Lab 07: 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 07 using your SQL statements, populate the database according
to the data given in text files using the SQL INSERT commands.
2. Drop foreign key constraints in Enrolled table.
3. Add foreign key constraints in Enrolled table, with set null on delete, cascade on update.
4. Update a class name “Operating System Design” to “Operating Systems” in Class table.
5. Retrieve class name: “Operating System Design” in Enrolled table, show row numbers.
6. Retrieve class name: “Operating Systems” in Enrolled table, show row numbers.
7. Delete class name: “Patent Law” from Class table.
8. Retrieve class name: “Patent Law” from Enrolled table, show row numbers.
9. Change constraints option on delete to Restrict on snum in Enrolled table.
10. Delete level “JR” tuples from Student table, show row numbers.
11. Change constraints option on delete to No Action on snum in Enrolled table

Deliverables

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

You might also like