Вы находитесь на странице: 1из 11

create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar (8),

semester varchar (6), year numeric (4,0), primary key (ID, course_id, sec_id,
semester, year), foreign key (course_id, sec_id, semester, year) references
section(course_id, sec_id, semester, year) on delete cascade, foreign key (ID)
references instructor(ID) on delete cascade); create table student (ID varchar
(5), name varchar (20) not null, dept_name varchar (20), tot_cred numeric (3,0)
check (tot_cred >= 0), primary key (ID), foreign key (dept_name) references
department(dept_name) on delete set null);
create table takes (ID varchar (5), course_id varchar (8), sec_id varchar (8),
semester varchar (6), year numeric (4,0), grade varchar (2), primary key (ID,
course_id, sec_id, semester, year), foreign key (course_id, sec_id, semester, year)
references section(course_id, sec_id, semester, year) on delete cascade, foreign
key (ID) references student(ID) on delete cascade);

A.2 DDL ays


create table advisor (sJD varchar (5), iJD varchar (5), primary key (sJD), foreign
key (iJD) references instructor (1D) on delete set null, foreign key (s_10)
references student (ID) on delete cascade);
create table prereq (courseid varchar(8), prereivid varchar(8), primary key
(coursejd, prereq_id), foreign key (courseid) references course on delete cascade,
foreign key (prereq_id) references course); -� The following create table statement
for the table timeslot can be run on most database systems, but does not work on
Oracle (at least as of Oracle version 11), since Oracle does not support the SQL
standard type time.
creat (timtabOhstghapRF Enhancer e, day varchar (1) check (day in (1.4', 'T , 'W ,
R , F, '0', 'U')), starlike time, end_thne time, primary key (time_s)otid, day,
start_timeTh
The syntax for specifying time in SQL is illustrated by these examples: '08:30',
'13:55', and '5:30 PM'. Since Oracle does not support the time type, for Oracle we
use the following schema instead:
create table timeslot (time_slot_id varchar (4), day varchar (1), start_hr numeric
(2) check (start_hr >= 0 and end_hr <24), start_min numeric (2) check (start_min >.
0 and startittin < 60), end_hr numeric (2) check (endiir 0 and endiir < 24),
end_min numeric (2) check (etidinin >. 0 and end_min < 60), primary key
(tinleslot_id, day, start_hr, start_min));
The difference is that start_time has been replaced by two attributes startJir and
startmin, and similarly end_titne has been replaced by attributes end_hr and
endanin. These attributes also have constraints that ensure that only numbers
representing valid time values appear in those attributes. This version of the

create table department(dept_name varchar (20), building varchar(15), budget


numeric (12,2) check (budget>0), primary key(dept_name));
create table course (course_id varchar(8), title varchar(50), dept_name varchar
(20), credits numeric (2,0) check (credits > 0), primary key (course_id), foreign
key(dept_name) references department(dept_name) on delete set null);
create table instructor(ID varchar (5), name varchar (20) not null, dept_name
varchar (20), salary numeric (8,2) check (salary > 29000), primary key (ID),foreign
key(dept_name) references department(dept_name) on delete set null);
create table section (course_id varchar (8), sec_id varchar (8), semester varchar
(6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), year numeric (4,0)
check (year > 1701 and year < 2100), building varchar (15), room_number varchar
(7), time_slot_id varchar (4), primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course(course_id) on delete cascade, foreign
key(building, room_number) references classroom(building, room_number) on delete
set null);
In the above DM we add the on delete cascade specification to a foreig !),
constraint if the existence of the tuple depends on the referenced tuple. PI ,ample
we add the on delete cascade specification to the foreign key constrai: mt suction
(which was generated from weak entity section), to course (which w.
mysql> create table section (course_id varchar(8), sec_id varchar(8), semester
varchar(6) check(semester in ('fall','winter','spring','summer')), year
numeric(4,0) check(year>1701 and year<2100), building varchar(15), room_number
varchar(7),time_slot_id varchar(4), primary key(course_id,sec_id,semester,year),
foreign key(course_id) references course(course_id) on delete cascade, foreign
key(building,room_number) references classroom(building,room_number) on delete set
null);
ERROR 1046 (3D000): No database selected
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| UNIVERSITY |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.07 sec)

mysql> select UNIVERSITY


-> ;
ERROR 1054 (42S22): Unknown column 'UNIVERSITY' in 'field list'
mysql> use UNIVERSITY;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table section (course_id varchar(8), sec_id varchar(8), semester
varchar(6) check(semester in ('fall','winter','spring','summer')), year
numeric(4,0) check(year>1701 and year<2100), building varchar(15), room_number
varchar(7),time_slot_id varchar(4), primary key(course_id,sec_id,semester,year),
foreign key(course_id) references course(course_id) on delete cascade, foreign
key(building,room_number) references classroom(building,room_number) on delete set
null);
ERROR 1050 (42S01): Table 'section' already exists
mysql> show tables;
+----------------------+
| Tables_in_UNIVERSITY |
+----------------------+
| classroom |
| course |
| department |
| instructor |
| section |
+----------------------+
5 rows in set (0.00 sec)

mysql> create table teachers(ID varchar(5), course_id varchar(8),sec_id


varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year),
-> foreign key(course_id,sec_id,semester,year) references section on delete
cascade,foreign key(ID) references instructor on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table teachers(ID varchar(5), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table teachers(ID varchar(5), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table teaches(ID varchar(5), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> show tables
-> ;
+----------------------+
| Tables_in_UNIVERSITY |
+----------------------+
| classroom |
| course |
| department |
| instructor |
| section |
+----------------------+
5 rows in set (0.00 sec)

mysql> describe instructor;


+-----------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------------+------+-----+---------+-------+
| ID | varchar(10) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| dept_name | varchar(20) | YES | MUL | NULL | |
| salary | decimal(10,0) | YES | | NULL | |
+-----------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> describe section;


+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| course_id | varchar(8) | NO | PRI | NULL | |
| sec_id | varchar(8) | NO | PRI | NULL | |
| semester | varchar(6) | NO | PRI | NULL | |
| year | decimal(4,0) | NO | PRI | NULL | |
| building | varchar(15) | YES | MUL | NULL | |
| room_number | varchar(7) | YES | | NULL | |
| time_slot_id | varchar(4) | YES | | NULL | |
+--------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> create table teaches(ID varchar(10), course_id varchar(8),sec_id


varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table teaches(ID varchar(10), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year decimal(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table teaches(ID varchar(10), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year decimal(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> ALTER TABLE Persons
-> ;ADD PRIMARY KEY (ID)
ERROR 1146 (42S02): Table 'UNIVERSITY.Persons' doesn't exist
-> alter table instructor
-> add primary key(ID);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'ADD
PRIMARY KEY (ID)
alter table instructor
add primary key(ID)' at line 1
mysql> ADD PRIMARY KEY (ID) alter table instructor add primary key(ID);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'ADD
PRIMARY KEY (ID) alter table instructor add primary key(ID)' at line 1
mysql> alter table instructor add primary key(ID);
Query OK, 0 rows affected (0.41 sec)
Records: 0 Duplicates: 0 Warnings: 0

mysql>
mysql> create table teaches(ID varchar(10), course_id varchar(8),sec_id
varchar(8),semester varchar(6),year numeric(4,0),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
Query OK, 0 rows affected (0.22 sec)

mysql> create table takes(ID varchar(5), course_id varchar(8),sec_id


varchar(8),semester varchar(6),year numeric(4,0),grade varchar(2),primary
key(ID,course_id,sec_id,semester,year), foreign key(course_id,sec_id,semester,year)
references section(course_id,sec_id,semester,year) on delete cascade,foreign
key(ID) references instructor(ID) on delete cascade);
Query OK, 0 rows affected (0.33 sec)

mysql> create table advisor(s_ID varchar(5),i_ID varchar(5),primary key(s_ID),


foreign key(i_ID) references instructor(ID) on delete set null, foreign key(s_ID)
references student(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table advisor(s_ID varchar(10),i_ID varchar(10),primary key(s_ID),
foreign key(i_ID) references instructor(ID) on delete set null, foreign key(s_ID)
references student(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> describe student;
ERROR 1146 (42S02): Table 'UNIVERSITY.student' doesn't exist
mysql> create table advisor(s_ID varchar(10),i_ID varchar(10),primary key(s_ID),
foreign key(i_ID) references instructor(ID) on delete set null, foreign key(s_ID)
references student(ID) on delete cascade);
ERROR 1215 (HY000): Cannot add foreign key constraint
mysql> create table student(ID varchar(5)name varchar(20) not null,dept_name
varchar(20),tod_card numeric(3,0) check(tod_cred>=0),primary key(ID), foreign
key(dept_name) references department(dept_name) on delete set null);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'name
varchar(20) not null,dept_name varchar(20),tod_card numeric(3,0) check(tod_' at
line 1
mysql> create table student(ID varchar(5),name varchar(20) not null,dept_name
varchar(20),tod_card numeric(3,0) check(tod_cred>=0),primary key(ID), foreign
key(dept_name) references department(dept_name) on delete set null);
Query OK, 0 rows affected (0.37 sec)

mysql> create table advisor(s_ID varchar(10),i_ID varchar(10),primary key(s_ID),


foreign key(i_ID) references instructor(ID) on delete set null, foreign key(s_ID)
references student(ID) on delete cascade);
Query OK, 0 rows affected (0.22 sec)

mysql> create table prereq(course_id varchar(8),prereq_id varchar(8),primary


key(course_id,prereq_id),foreign_key(course_id) references course(course_id) on
delete cascade;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'(course_id) references course(course_id) on delete cascade' at line 1
mysql> create table prereq(course_id varchar(8),prereq_id varchar(8),primary
key(course_id,prereq_id),foreign_key(course_id) references course(course_id) on
delete cascade);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near
'(course_id) references course(course_id) on delete cascade)' at line 1
mysql> describe course;
+-----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| course_id | varchar(8) | NO | PRI | NULL | |
| title | varchar(50) | YES | | NULL | |
| dept_name | varchar(20) | YES | MUL | NULL | |
| credits | decimal(2,0) | YES | | NULL | |
+-----------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> create table prereq(course_id varchar(8),prereq_id varchar(8),primary


key(course_id,prereq_id),foreign key(course_id) references course(course_id) on
delete cascade);
Query OK, 0 rows affected (0.19 sec)

mysql> create table timeslot(time_slot_id varchar(4),day varchar(1) check(day


in('M','T','W','R','F','S','U')),start_time time,end_time time,primary
key(time_slot_id,day,start_time));
Query OK, 0 rows affected (0.21 sec)

mysql> insert into classroom values('Packard','101',500);


Query OK, 1 row affected (0.04 sec)

mysql> insert into classroom values('Painter','514',10);


Query OK, 1 row affected (0.04 sec)

mysql> insert into classroom values('Tylor','3128',70);


Query OK, 1 row affected (0.03 sec)
mysql> insert into classroom values('Watson','100',30);
Query OK, 1 row affected (0.03 sec)

mysql> insert into classroom values('Watson','120',50);


Query OK, 1 row affected (0.03 sec)

mysql> select * from classroom;


+----------+-------------+----------+
| building | room_number | capacity |
+----------+-------------+----------+
| Packard | 101 | 500 |
| Painter | 514 | 10 |
| Tylor | 3128 | 70 |
| Watson | 100 | 30 |
| Watson | 120 | 50 |
+----------+-------------+----------+
5 rows in set (0.00 sec)

mysql> insert into department values('Biology','Watson',90000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('Comp.Sci.','Watson',100000);


Query OK, 1 row affected (0.05 sec)

mysql> insert into department values('Elec.Eng.','Tylor',85000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('Finance','Painter',120000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values('History','Painter',50000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values('Music','Packard',80000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into department values('Physics','Watson',70000);


Query OK, 1 row affected (0.04 sec)

mysql> insert into prereq values('BIO-301','BIO-101');


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`UNIVERSITY`.`prereq`, CONSTRAINT `prereq_ibfk_1` FOREIGN KEY (`course_id`)
REFERENCES `course` (`course_id`) ON DELETE CASCADE)
mysql> insert into course values('BIO-101','Intro. to Biology','Biology',4);
Query OK, 1 row affected (0.04 sec)

mysql> insert into course values('BIO-301','Genetics','Biology',4);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('BIO-399','Computational Biology','Biology',3);


Query OK, 1 row affected (0.04 sec)

mysql> insert into course values('CST-101','Intro. to Computer


Science','Comp.Sci.',4);
Query OK, 1 row affected (0.02 sec)

insert into course values('FIN-201','Investment Banking','Finance',3);


insert into course values('HIS-351','World History','History',3);
insert into course values('MU-199','Music Video Prduction','Music',3);
insert into course values('PHY-101','Physical principles','Physics',4);
insert into course values('EE-181','intro. to digital system','elec.eng.',3);

mysql> show tables;


+----------------------+
| Tables_in_UNIVERSITY |
+----------------------+
| advisor |
| classroom |
| course |
| department |
| instructor |
| prereq |
| section |
| student |
| takes |
| teaches |
| timeslot |
+----------------------+
11 rows in set (0.00 sec)

Enter password: *****


Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.22-rc-community MySQL Community Server (GPL)

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database university;


Query OK, 1 row affected (0.15 sec)

mysql> show databases;


+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| university |
+--------------------+
4 rows in set (0.08 sec)

mysql> use university;


Database changed
mysql> create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar
(8), semester varchar (6), year numeric (4,0), primary key (ID, course_id, sec_id,
semester, year), foreign key (course_id, sec_id, semester, year) references section
on delete cascade, foreign key (ID) references instructor on delete cascade);
ERROR 1005 (HY000): Can't create table 'university.teaches' (errno: 150)
mysql> create table department, (dept_name varchar (20), building varchar(15),
budget numeric (12,2) check (budget>0), primary key(dept_name));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '
(dept_name varchar (20), building varchar(15), budget numeric (12,2) check (bud' at
line 1
mysql>
mysql> create table department(dept_name varchar (20), building varchar(15), budget
numeric (12,2) check (budget>0), primary key(dept_name));
Query OK, 0 rows affected (0.10 sec)

mysql> create table course (course_id varchar(8); title varchar(50), dept_name


varchar (20), credits numeric (2,0) check (credits > 0), primary key (couse_id),
foreign key(dept_name) references department on delete set null);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '' at
line 1
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'title
varchar(50), dept_name varchar (20), credits numeric (2,0) check (credits ' at line
1
mysql> create table course (course_id varchar(8), title varchar(50), dept_name
varchar (20), credits numeric (2,0) check (credits > 0), primary key (couse_id),
foreign key(dept_name) references department on delete set null);
ERROR 1072 (42000): Key column 'couse_id' doesn't exist in table
mysql> create table course (course_id varchar(8), title varchar(50), dept_name
varchar (20), credits numeric (2,0) check (credits > 0), primary key (course_id),
foreign key(dept_name) references department on delete set null);
ERROR 1005 (HY000): Can't create table 'university.course' (errno: 150)
mysql> create table course (course_id varchar(8), title varchar(50), dept_name
varchar (20), credits numeric (2,0) check (credits > 0), primary key (course_id),
foreign key(dept_name) references department(dept_name) on delete set null);
Query OK, 0 rows affected (0.06 sec)

mysql> show * from university;


ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '* from
university' at line 1
mysql> select *from university;
ERROR 1146 (42S02): Table 'university.university' doesn't exist
mysql> show tables;
+----------------------+
| Tables_in_university |
+----------------------+
| course |
| department |
+----------------------+
2 rows in set (0.02 sec)

mysql> create table instructor(ID varchar (5), name varchar (20) not null,
dept_name varchar (20), salary numeric (8,2) check (salary > 29000), primary key
(ID),foreign key(dept_name) references department(dept_name) on delete set null);
Query OK, 0 rows affected (0.08 sec)

mysql> create table section (course_id varchar (8), sec_id varchar (8), semester
varchar (6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), year
numeric (4,0) check (year > 1701 and year < 2100), building varchar (15),
room_number varchar (7), time_slot_id varchar (4), primary key (course_id, sec_id,
semester, year), foreign key (course_id) references course(course_id) on delete
cascade, foreign key(building, room_number) references classroom(building,
room_number) on delete set null);
ERROR 1005 (HY000): Can't create table 'university.section' (errno: 150)
mysql> create table classroom(building varchar (15) ,room_number varchar
(7),capacity numeric(4,0),primary key(building,room_number));
Query OK, 0 rows affected (0.08 sec)

mysql> create table section (course_id varchar (8), sec_id varchar (8), semester
varchar (6) check (semester in ('Fall', 'Winter', 'Spring', 'Summer')), year
numeric (4,0) check (year > 1701 and year < 2100), building varchar (15),
room_number varchar (7), time_slot_id varchar (4), primary key (course_id, sec_id,
semester, year), foreign key (course_id) references course(course_id) on delete
cascade, foreign key(building, room_number) references classroom(building,
room_number) on delete set null);
Query OK, 0 rows affected (0.08 sec)

mysql> create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar
(8), semester varchar (6), year numeric (4,0), primary key (ID, course_id, sec_id,
semester, year), foreign key (course_id, sec_id, semester, year) references section
on delete cascade, foreign k
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'k' at
line 1
mysql> create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar
(8), semester varchar (6), year numeric (4,0), primary key (ID, course_id, sec_id,
semester, year), foreign key (course_id, sec_id, semester, year) references section
on delete cascade, foreign key (ID) references instructor(ID) on delete cascade);
ERROR 1005 (HY000): Can't create table 'university.teaches' (errno: 150)
mysql> create table teaches (ID varchar (5), course_id varchar (8), sec_id varchar
(8), semester varchar (6), year numeric (4,0), primary key (ID, course_id, sec_id,
semester, year), foreign key (course_id, sec_id, semester, year) references
section(course_id, sec_id, semester, year) on delete cascade, foreign key (ID)
references instructor(ID) on delete cascade);
Query OK, 0 rows affected (0.09 sec)

mysql> create table student (ID varchar (5), name varchar (20) not null, dept_name
varchar (20), tot_cred numeric (3,0) check (tot_cred >= 0), primary key (ID),
foreign key (dept_name) references department(dept_name) on delete set null);
Query OK, 0 rows affected (0.06 sec)

mysql> create table takes (ID varchar (5), course_id varchar (8), sec_id varchar
(8), semester varchar (6), year numeric (4,0), grade varchar (2), primary key (ID,
course_id, sec_id, semester, year), foreign key (course_id, sec_id, semester, year)
references section(course_id, sec_id, semester, year) on delete cascade, foreign
key (ID) references student(ID) on delete cascade);
Query OK, 0 rows affected (0.12 sec)

mysql> create table advisor(s_ID varchar(5),i_ID varchar(5),primary key(s_ID),


foreign key(i_ID) references instructor(ID) on delete set null, foreign key(s_ID)
references student(ID) on delete cascade);
Query OK, 0 rows affected (0.09 sec)

mysql> create table prereq(course_id varchar(8),prereq_id varchar(8),primary


key(course_id,prereq_id),foreign key(course_id) references course(course_id) on
delete cascade);
Query OK, 0 rows affected (0.08 sec)

mysql> create table timeslot(time_slot_id varchar(4),day varchar(1) check(day


in('M','T','W','R','F','S','U')),start_time time,end_time time,primary
key(time_slot_id,day,start_time));
Query OK, 0 rows affected (0.06 sec)

mysql> insert into classroom values('Packard','101',500);


Query OK, 1 row affected (0.06 sec)

mysql> insert into classroom values('Painter','514',10);


Query OK, 1 row affected (0.03 sec)
mysql> insert into classroom values('Tylor','3128',70);
Query OK, 1 row affected (0.04 sec)

mysql> insert into classroom values('Watson','100',30);


Query OK, 1 row affected (0.02 sec)

mysql> insert into classroom values('Watson','120',50);


Query OK, 1 row affected (0.02 sec)

mysql> insert into department values('Biology','Watson',90000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('Comp.Sci.','Watson',100000);


Query OK, 1 row affected (0.02 sec)

mysql> insert into department values('Elec.Eng.','Tylor',85000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('Finance','Painter',120000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('History','Painter',50000);


Query OK, 1 row affected (0.05 sec)

mysql> insert into department values('Music','Packard',80000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into department values('Physics','Watson',70000);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('BIO-101','Intro. to Biology','Biology',4);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('BIO-301','Genetics','Biology',4);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('BIO-399','Computational Biology','Biology',3);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('CST-101','Intro. to Computer


Science','Comp.Sci.',4);
Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('CS-190','Game Design','Comp.Sci.',4);


Query OK, 1 row affected (0.02 sec)

mysql> insert into course values('CS-315','robotics','Comp.Sci.',3);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('CS-319','image processing','Comp.Sci.',3);


Query OK, 1 row affected (0.02 sec)

mysql> insert into course values('CS-347','database system


concepts','Comp.Sci.',3);
Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('EE-181','intro. to digital


system','elec.eng.',3);
Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('FIN-201','Investment Banking','Finanace',3);


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`university`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`dept_name`)
REFERENCES `department` (`dept_name`) ON DELETE SET NULL)
mysql> insert into course values('HIS-351','World History','History',3);
Query OK, 1 row affected (0.02 sec)

mysql> insert into course values('MU-199','Music Video Prduction','Music',3);


Query OK, 1 row affected (0.03 sec)

mysql> insert into course values('PHY-101','Physical principles','Physics',4);


Query OK, 1 row affected (0.05 sec)

mysql> insert into course values('FIN-201','Investment Banking','Finanace',3);


ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint
fails (`university`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`dept_name`)
REFERENCES `department` (`dept_name`) ON DELETE SET NULL)
mysql> select * from course;
+-----------+----------------------------+-----------+---------+
| course_id | title | dept_name | credits |
+-----------+----------------------------+-----------+---------+
| BIO-101 | Intro. to Biology | Biology | 4 |
| BIO-301 | Genetics | Biology | 4 |
| BIO-399 | Computational Biology | Biology | 3 |
| CS-190 | Game Design | Comp.Sci. | 4 |
| CS-315 | robotics | Comp.Sci. | 3 |
| CS-319 | image processing | Comp.Sci. | 3 |
| CS-347 | database system concepts | Comp.Sci. | 3 |
| CST-101 | Intro. to Computer Science | Comp.Sci. | 4 |
| EE-181 | intro. to digital system | elec.eng. | 3 |
| HIS-351 | World History | History | 3 |
| MU-199 | Music Video Prduction | Music | 3 |
| PHY-101 | Physical principles | Physics | 4 |
+-----------+----------------------------+-----------+---------+
12 rows in set (0.02 sec)

mysql> select * from department;


+-----------+----------+-----------+
| dept_name | building | budget |
+-----------+----------+-----------+
| Biology | Watson | 90000.00 |
| Comp.Sci. | Watson | 100000.00 |
| Elec.Eng. | Tylor | 85000.00 |
| Finance | Painter | 120000.00 |
| History | Painter | 50000.00 |
| Music | Packard | 80000.00 |
| Physics | Watson | 70000.00 |
+-----------+----------+-----------+
7 rows in set (0.00 sec)

mysql> insert into course values('FIN-201','Investment Banking','Finance',3);


Query OK, 1 row affected (0.03 sec)