-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabo2.sql
196 lines (180 loc) · 5.28 KB
/
labo2.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
-- region: company schema
create schema if not exists company;
-- endregion: company schema
-- region: company tables
create table company.employee
(
fname varchar(15) not null,
minit char(1),
lname varchar(15) not null,
ssn char(9) not null,
bdate date,
address varchar(30),
sex char(1),
salary decimal(10, 2),
super_ssn char(9),
dno integer not null,
primary key (ssn)
);
create table company.department
(
dname varchar(15) not null,
dnumber integer not null,
mgr_ssn char(9),
mgr_start_date date,
primary key (dnumber)
);
create table company.dept_locations
(
dnumber integer not null,
dlocation integer not null,
primary key (dnumber, dlocation)
);
create table company.project
(
pname varchar(15) not null,
pnumber integer not null,
plocation integer,
dnum integer not null,
primary key (pnumber)
);
create table company.works_on
(
essn char(9) not null,
pno integer not null,
hours decimal(3, 1) not null,
primary key (essn, pno)
);
create table company.dependent
(
essn char(9) not null,
dependent_name varchar(15) not null,
sex char(1),
bdate date,
relationship varchar(8),
primary key (essn, dependent_name)
);
create table company.location
(
lnumber integer not null,
lname varchar(15) not null,
primary key (lnumber)
);
-- endregion: company tables
-- region: data insert on works_on
insert into company.works_on (essn, pno, hours)
values ('123456789', 3, 10),
('123456789', 4, 10),
('123456789', 5, 10);
-- > there is no project with number 3 and 4
delete
from company.department
where dnumber = 5;
-- > deletes the project that is linked to a works_on tuple
-- endregion: data insert on works_on
-- region: clear tables
truncate table company.department;
truncate table company.dependent;
truncate table company.dept_locations;
truncate table company.employee;
truncate table company.location;
truncate table company.project;
truncate table company.works_on;
-- endregion: clear tables
-- region: add foreign keys
alter table company.dept_locations
add foreign key (dnumber) references company.department (dnumber);
alter table company.dept_locations
add foreign key (dlocation) references company.location (lnumber);
alter table company.department
add foreign key (mgr_ssn) references company.employee (ssn);
alter table company.employee
add foreign key (dno) references company.department (dnumber);
alter table company.employee
add foreign key (super_ssn) references company.employee (ssn);
alter table company.dependent
add foreign key (essn) references company.employee (ssn);
alter table company.works_on
add foreign key (essn) references company.employee (ssn);
alter table company.works_on
add foreign key (pno) references company.project (pnumber);
alter table company.project
add foreign key (dnum) references company.department (dnumber);
alter table company.project
add foreign key (plocation) references company.location (lnumber);
-- endregion: add foreign keys
-- region: triggers to insert data
alter table company.department
disable trigger all;
alter table company.dependent
disable trigger all;
alter table company.dept_locations
disable trigger all;
alter table company.employee
disable trigger all;
alter table company.location
disable trigger all;
alter table company.project
disable trigger all;
alter table company.works_on
disable trigger all;
alter table company.department
enable trigger all;
alter table company.dependent
enable trigger all;
alter table company.dept_locations
enable trigger all;
alter table company.employee
enable trigger all;
alter table company.location
enable trigger all;
alter table company.project
enable trigger all;
alter table company.works_on
enable trigger all;
-- endregion: triggers to insert data
-- region: insertion département "IT" dnumber = 10
with employee as (
insert into company.employee (fname, lname, ssn, dno)
values ('Steve', 'Jobs', '555444333', 10)
returning ssn)
insert
into company.department (dname, dnumber, mgr_ssn, mgr_start_date)
values ('IT', 10, (select ssn from employee), now());
-- endregion: insertion département "IT" dnumber = 10
-- region: various updates
update company.employee
set dno = 7
where ssn = '999887777';
--
delete
from company.employee
where ssn = '999887777';
--
insert into company.works_on (essn, pno, hours)
values ('123456789', 3, 10),
('123456789', 4, 10),
('123456789', 5, 10);
--
delete
from company.department
where dnumber = 5;
-- endregion: various updates
-- region: attempts on deletion
delete from company.employee
where ssn = '987654321';
alter table company.employee
drop constraint employee_super_ssn_fkey;
alter table company.employee
add foreign key (super_ssn) references company.employee (ssn)
on delete set null;
--
update company.department
set dnumber = 7
where dnumber = 4;
alter table company.employee
drop constraint employee_dno_fkey;
alter table company.employee
add foreign key (dno) references company.department (dnumber)
on update cascade;
-- endregion: attempts on deletion