East West University
Department of Computer Science and Engineering
CSE 302 LAB 02 (Handout)
Course Instructor: Dr. Mohammad Rezwanul Huq
Intermediate level DDL and Simple DML statements
Lab Objective
Familiarize students with intermediate level DDL commands and simple DML statements in SQL.
Lab Outcome
After completing this lab successfully, students will be able to:
1. Understand and execute DDL commands to define integrity constraints and modify the database
schema.
2. Construct DML statements to perform queries involving distinct keyword, generalized projection,
simple multi-table queries and so on.
Psychomotor Learning Levels
This lab involves activities that encompass the following learning levels in psychomotor domain.
Level Category Meaning Keywords
P1 Imitation Copy action of Relate, Repeat, Choose, Copy,
another; observe and Follow, Show, Identify, Isolate.
replicate.
P2 Manipulation Reproduce activity Copy, response, trace, Show,
from instruction or Start, Perform, Execute,
memory Recreate.
Lab Activities
1. Schema Definition along with Integrity Constraints
CREATE TABLE <table_name>
(
<attribute_name1> <datatype> [NOT NULL],
<attribute_name2> <datatype> [NOT NULL],
...,
[constraint <constraint_name>] primary key (<attribute_name,…>),
[constraint <constraint_name>] foreign key (<attribute_name,…>)
references <parent_table_name>(<attribute>) [ON DELETE CASCADE]),
[constraint <constraint_name>] check (<condition>)
);
Task:
• Create department relation with dept_name, building and budget attributes where dept_name
must be the primary key and budget must be positive.
• Create another relation course with course_id, title, dept_name and credits where course_id
is the primary key, dept_name is the foreign key and credits must be greater than or equal to
1.
2. Schema Modification
Adding a new attribute:
ALTER TABLE <table_name> ADD <attribute_name> <datatype>;
Dropping an attribute:
ALTER TABLE <table_name> DROP column <attribute_name>;
Modifying data type of an attribute (Column must be empty/has no values):
ALTER TABLE <table_name> MODIFY <attribute_name> <new_type>;
Renaming an attribute:
ALTER TABLE <table_name> RENAME column <attribute_name> to
<new_attribute_name>;
Renaming a table:
ALTER TABLE <table_name> RENAME TO <new_table_name>;
Adding a constraint into a table (primary key constraint, foreign key constraint):
ALTER TABLE <table_name> ADD CONSTRAINT <constraint_name>
<constraint>;
Deleting a constraint from a table:
ALTER TABLE <table_name> DROP CONSTRAINT <constraint_name>;
Checking all constraints:
SELECT * FROM user_cons_columns WHERE TABLE_NAME =
<table_name>;
Dropping a Table (both data and schema):
DROP TABLE <table_name>;
3. Manipulating Data (DML)
Basic Query Structure
SELECT A1, A2, ..., An [list of attributes]
FROM r1, r2, ..., rm [list of relations]
WHERE P [condition]
Inserting records into a table:
INSERT INTO <table_name> VALUES (…, …, …);
Deleting records from a table:
DELETE FROM <table_name> WHERE <condition>;
Updating values of a record in a table:
UPDATE <table_name>
SET <attribute_name> = <value>
WHERE <condition>;
Multi-table queries
Cartesian product:
select *
from instructor, department;
This generates many tuples which are not meaningful. To get the meaningful tuples, you need to
write:
select *
from instructor, department;
where instructor.dept_name = department.dept_name;
Natural join:
select * from instructor natural join department;
East West University
Department of Computer Science and Engineering
CSE 302: LAB 02 (Exercise - Offline)
Course Instructor: Dr. Mohammad Rezwanul Huq
Write all SQL statements in a notepad (text document) before execution.
Lab Task # 01 (Schema Definition):
Write SQL statements to create the following tables with the given constraints.
i) account
account_no char(5) primary key
balance number Not null and cannot be less than 0
ii) customer
customer_no char(5) primary key
customer_name varchar2(20) Not null
customer_city varchar2(10)
iii) depositor
account_no char(5)
customer_no char(5)
primary key (account_no, customer_no)
Lab Task # 02 (Schema Modification):
After executing each of these SQL statements execute the command – desc <table_name> to confirm the
changes.
i. Write SQL statement to add a new attribute ‘date_of_birth’ (date type) in customer table.
ii. Write SQL statement to drop the attribute ‘date_of_birth’ from customer table.
iii. Write SQL statement to rename the attribute account_no, customer_no from depositor table to a_no
and c_no, respectively.
iv. Write SQL statements to add two foreign key constraints ‘depositor_fk1’ and ‘depositor_fk2’ which
identifies a_no and c_no as a foreign key.
Lab Task # 03 (Inserting Records into Tables):
Write appropriate SQL statements to insert the records as shown below.
Account Customer Depositor
Lab Task # 04 (Queries):
i. Find account numbers with balance more than 5000.
ii. Find customer number and customer name who live in Dhaka.
iii. Find customer number and customer name who do not live in Dhaka.
iv. Find customer name and customer city who have accounts with balance more than 5000.
v. Find customer name and customer city who have accounts with balance more than 5000 and do
not live in Dhaka.
Submission
Take screenshots of the execution and result of your queries in SQLPlus Tool and insert the captured image
in a doc file for each and every question. Name the file as your_student_id_lab02 and submit the file in the
following link: [Link]