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

CSE302_Lab02_final

Uploaded by

Shoeb Khandaker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CSE302_Lab02_final

Uploaded by

Shoeb Khandaker
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

East West University

Department of Computer Science and Engineering

CSE 302 LAB 02 (Handout)


Course Instructor: Mahmuda Rawnak Jahan

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 Follow, Show, Identify,
and replicate. Isolate.

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: Mahmuda Rawnak Jahan

You must write all SQL statements in notepad first and save them with .sql extension.
Then execute your SQL scripts.

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. Display customer name and customer city only.


ii. Display the unique customer city. No repetitions are allowed.
iii. Find account numbers with balance more than 7000.
iv. Find customer number and customer name who live in Khulna.
v. Find customer number and customer name who do not live in Dhaka.
vi. Find customer name and customer city who have accounts with balance more than 7000. vii.
Find customer name and customer city who have accounts with balance more than 7000 and do not
live in Khulna.
viii. Find account number and balance for those accounts which belong to a customer with id ‘C-102’.
ix. Find all account number and balance for those accounts which belong to customers of Dhaka and
Khulna city.
x. Find the customer who have no accounts. [Result of this query will be empty for this dataset.
However, you must write the correct SQL]

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 . Submit both doc and sql file in the given submission link in the
Classroom. Submit files separately. Name the file as per the following format: 2022-1-60-001_LAB02.pdf
and 2022-1-60-001.sql_LAB02.

You might also like