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

Lab 05 DB

The document discusses creating a database schema that incorporates keys and integrity constraints. It describes creating tables, adding constraints like primary keys and foreign keys, and performing operations like inserts, updates, deletes while observing the effects of constraints.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Lab 05 DB

The document discusses creating a database schema that incorporates keys and integrity constraints. It describes creating tables, adding constraints like primary keys and foreign keys, and performing operations like inserts, updates, deletes while observing the effects of constraints.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Department of Mathematics

CS220: Database Systems

Class: BS Mathematics Semester 4


Lab 05: DDL and Constraints

Date: March 26, 2021


Time: 02:00-05:00
Instructor: Ms. Naheeda Parveen

Lab Engineer: Ayesha 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.
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. Update the age to 25 years of those students who have age 21 years.
11. Delete level “JR” tuples from Student table, show row numbers.
12. 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