-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_schema.sql
76 lines (62 loc) · 2.05 KB
/
table_schema.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
-- Exported from QuickDBD: https://www.quickdatabasediagrams.com/
-- NOTE! If you have used non-SQL datatypes in your design, you will have to change these here.
CREATE TABLE "Departments" (
"dept_no" varchar NOT NULL,
"dept_name" varchar NOT NULL,
CONSTRAINT "pk_Departments" PRIMARY KEY (
"dept_no"
)
);
CREATE TABLE "Employees" (
"emp_no" int NOT NULL,
"emp_title" varchar NOT NULL,
"birth_date" date NOT NULL,
"first_name" varchar NOT NULL,
"last_name" varchar NOT NULL,
"gender" varchar NOT NULL,
"hire_date" date NOT NULL,
CONSTRAINT "pk_Employees" PRIMARY KEY (
"emp_no"
)
);
CREATE TABLE "Managers" (
"dept_no" varchar NOT NULL,
"emp_no" int NOT NULL,
CONSTRAINT "pk_Managers" PRIMARY KEY (
"dept_no","emp_no"
)
);
CREATE TABLE "Dept_Emp" (
"emp_no" int NOT NULL,
"dept_no" varchar NOT NULL,
CONSTRAINT "pk_Dept_Emp" PRIMARY KEY (
"emp_no","dept_no"
)
);
CREATE TABLE "Titles" (
-- Adds pk restraint to title column
"title_id" varchar NOT NULL,
"title" varchar NOT NULL,
CONSTRAINT "pk_Titles" PRIMARY KEY (
"title_id"
)
);
CREATE TABLE "Salaries" (
"emp_no" int NOT NULL,
"salary" int NOT NULL,
CONSTRAINT "pk_Salaries" PRIMARY KEY (
"emp_no"
)
);
ALTER TABLE "Employees" ADD CONSTRAINT "fk_Employees_emp_title" FOREIGN KEY("emp_title")
REFERENCES "Titles" ("title_id");
ALTER TABLE "Managers" ADD CONSTRAINT "fk_Managers_dept_no" FOREIGN KEY("dept_no")
REFERENCES "Departments" ("dept_no");
ALTER TABLE "Managers" ADD CONSTRAINT "fk_Managers_emp_no" FOREIGN KEY("emp_no")
REFERENCES "Employees" ("emp_no");
ALTER TABLE "Dept_Emp" ADD CONSTRAINT "fk_Dept_Emp_emp_no" FOREIGN KEY("emp_no")
REFERENCES "Employees" ("emp_no");
ALTER TABLE "Dept_Emp" ADD CONSTRAINT "fk_Dept_Emp_dept_no" FOREIGN KEY("dept_no")
REFERENCES "Departments" ("dept_no");
ALTER TABLE "Salaries" ADD CONSTRAINT "fk_Salaries_emp_no" FOREIGN KEY("emp_no")
REFERENCES "Employees" ("emp_no");