0% found this document useful (0 votes)
345 views3 pages

Assignment 3

The document discusses modifying database schemas and adding constraints. It includes: 1) Adding a teaching_assistant table to relate teaching assistants to courses and sections. 2) Allowing a student to have multiple advisors by modifying the advisor table's primary key. Sample queries on the modified schema are provided. 3) Writing queries to delete old data over 10 years, delete a course and related data, and find students with certain advisor relationships. 4) Creating tables for phones, networks, and deals along with constraints to check dates and network types. Cascade constraints are added to delete related deal records.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
0% found this document useful (0 votes)
345 views3 pages

Assignment 3

The document discusses modifying database schemas and adding constraints. It includes: 1) Adding a teaching_assistant table to relate teaching assistants to courses and sections. 2) Allowing a student to have multiple advisors by modifying the advisor table's primary key. Sample queries on the modified schema are provided. 3) Writing queries to delete old data over 10 years, delete a course and related data, and find students with certain advisor relationships. 4) Creating tables for phones, networks, and deals along with constraints to check dates and network types. Cascade constraints are added to delete related deal records.
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1/ 3

1. Each offering of a course (i.e.

a section) can have many Teaching assistants; each


teaching assistant is a student. Extend the existing schema by adding or altering
tables to accommodate this requirement.
Sol: Create a table teaching_assistant(ID, course_id, sec_id, semester, year), with ID
referencing student, and (course_id,sec_id, semester,year) referencing section.

2. According to the existing schema, one student can have only one advisor. 
1. Alter the schema to allow a student to have multiple advisors and make sure
that you are able to insert multiple advisors for a student.
Sol: alter table advisor drop constraint advisor_pkey;
alter table advisor add constraint advisor_pkey primary key (s_id, i_id);

2. Write SQL queries on the modified schema. You will need to insert data to
ensure the query results are not empty.
1. Find all students who have more than 3 advisors
Sol: select s_id, name 
from advisor,student
where advisor.s_ID=student.ID 
group by s_id, name 
having count(i_ID)>3;

2. Find all students who are co-advised by Prof. Srinivas and Prof. Ashok.
Sol: select name 
from student 
where ID in
(select s_id from advisor,instructor 
where advisor.i_id = instructor.ID 
and instructor.name='Srinivasan') 
and ID in 
(select s_id from advisor,instructor 
where advisor.i_id = instructor.ID 
and instructor.name='Ashok');

3. Find students advised by instructors from different departments. etc.


Sol: select distinct student.ID, student.name
from student, advisor A1, instructor I1, advisor A2, instructor I2
where student.ID = A1.s_id 
and student.ID = A2.s_id 
and A1.i_id = I1.ID 
and A2.i_id = I2.ID 

3. Write SQL queries for the following:


1. Delete all information in the database which is more than 10 years old. Add
data as necessary to verify your query.
Sol: delete from takes where year < (year(CURRENT_DATE) - 10);
delete from teaches where year < (year(CURRENT_DATE) - 10);
delete from section where year < (year(CURRENT_DATE) - 10);

2. Delete the course CS 101. All courses which have this as a prereq should
remove this from its prereq set. Create a cascade constraint and verify.
Sol: delete from course where course_id='CS-101'
4. Write the create table SQL commands to create the following tables:
1. phone(phone_id, brand, model, price) 
 This table represents a mobile phone.
 brand is some string like 'Nokia', 'Samsung' etc.
 model is a string such as 'N95', 'Galaxy3' etc.
Sol: create table phone
(phone_id varchar(20),
brand varchar(20),
model varchar(20),
price numeric(12,2),
primary key (phone_id)
);

2. network(network_id, name, type)


 This table represents mobile phone networks such as 'Airtel', 'BSNL'
etc.
Sol: create table network
(network_id varchar(20), 
name varchar(20), 
type varchar(20),
primary key (network_id)
);

3. deal(id, phone_id, network_id, deal_price, start_date, end_date)


 Deal is an offer for a phone when bought along with a network
 The deal price is valid only between start_date and end_date
 The phone_id and network_id attributes are foreign keys to the phone
and the network table respectively
Sol: create table deal
(id varchar(20), 
phone_id varchar(20), 
network_id varchar(20),
deal_price numeric(12,2),
start_date date,
end_date date,
primary key (id),
foreign key (phone_id) references phone,
foreign key (network_id) references network
);

2. Create constraints to check the following:


1. The start_date of a deal should always be <= its end_date.
Sol: alter table deal add check(start_date <= end_date)

2. The network type should be one of the following values: 'GSM', 'CDMA',
'BOTH'
Sol: alter table network add check (type in ('GSM', 'CDMA', 'BOTH'))

3. Modify the create table script and add a cascade constraint so that, when a phone is
deleted (i.e., no longer being manufactured), all deals for that phone are deleted.
Sol: alter table deal drop constraint "deal_phone_id_fkey"
alter table deal add constraint "deal_phone_id_fkey" foreign
key(phone_id) references phone on delete cascade
4. Try to insert inconsistent data and verify the constraints. Provide insert statements
that fail, along with the error message.
Sol: Check deal start_date <= end_date:
insert into phone values(1, 'Nokia', 'N95', 10000);
insert into network values(1, 'Airtel', 'GSM');

insert into deal values(12345, 1, 1, 11000, '2010-12-14', '2010-12-02');


ERROR: new row for relation "deal" violates check constraint "deal_check
Check Network type:

insert into network values(2, 'Airtel', 'GSM and CDMA');

ERROR: new row for relation "network" violates check constraint "network_type_check"

You might also like