-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_creation_azure_company_script.sql
99 lines (81 loc) · 2.63 KB
/
db_creation_azure_company_script.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
create schema if not exists azure_company;
use azure_company;
select * from information_schema.table_constraints
where constraint_schema = 'azure_company';
CREATE TABLE 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 INT NOT NULL DEFAULT 1,
CONSTRAINT chk_salary_employee CHECK (Salary > 2000.0),
CONSTRAINT pk_employee PRIMARY KEY (Ssn)
);
ALTER TABLE employee
ADD CONSTRAINT fk_employee
FOREIGN KEY (Super_ssn) REFERENCES employee(Ssn)
ON DELETE SET NULL
ON UPDATE CASCADE;
alter table employee modify Dno int not null default 1;
desc employee;
create table departament(
Dname varchar(15) not null,
Dnumber int not null,
Mgr_ssn char(9) not null,
Mgr_start_date date,
Dept_create_date date,
constraint chk_date_dept check (Dept_create_date < Mgr_start_date),
constraint pk_dept primary key (Dnumber),
constraint unique_name_dept unique(Dname),
foreign key (Mgr_ssn) references employee(Ssn)
);
alter table departament drop departament_ibfk_1;
alter table departament
add constraint fk_dept foreign key(Mgr_ssn) references employee(Ssn)
on update cascade;
desc departament;
create table dept_locations(
Dnumber int not null,
Dlocation varchar(15) not null,
constraint pk_dept_locations primary key (Dnumber, Dlocation),
constraint fk_dept_locations foreign key (Dnumber) references departament (Dnumber)
);
alter table dept_locations drop fk_dept_locations;
alter table dept_locations
add constraint fk_dept_locations foreign key (Dnumber) references departament(Dnumber)
on delete cascade
on update cascade;
create table project(
Pname varchar(15) not null,
Pnumber int not null,
Plocation varchar(15),
Dnum int not null,
primary key (Pnumber),
constraint unique_project unique (Pname),
constraint fk_project foreign key (Dnum) references departament(Dnumber)
);
create table works_on(
Essn char(9) not null,
Pno int not null,
Hours decimal(3,1) not null,
primary key (Essn, Pno),
constraint fk_employee_works_on foreign key (Essn) references employee(Ssn),
constraint fk_project_works_on foreign key (Pno) references project(Pnumber)
);
drop table dependent;
create table dependent(
Essn char(9) not null,
Dependent_name varchar(15) not null,
Sex char,
Bdate date,
Relationship varchar(8),
primary key (Essn, Dependent_name),
constraint fk_dependent foreign key (Essn) references employee(Ssn)
);
show tables;
desc dependent;