-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMyEmployee.sql
72 lines (67 loc) · 2.95 KB
/
MyEmployee.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
CREATE TABLE employees (
emp_no INT NOT NULL, -- UNSIGNED AUTO_INCREMENT??
birth_date DATE NOT NULL,
first_name VARCHAR(14) NOT NULL,
last_name VARCHAR(16) NOT NULL,
gender ENUM ('M','F') NOT NULL, -- Enumeration of either 'M' or 'F'
hire_date DATE NOT NULL,
PRIMARY KEY (emp_no) -- Index built automatically on primary-key column
-- INDEX (first_name)
-- INDEX (last_name)
);
CREATE TABLE departments (
dept_no CHAR(4) NOT NULL, -- in the form of 'dxxx'
dept_name VARCHAR(40) NOT NULL,
PRIMARY KEY (dept_no), -- Index built automatically
UNIQUE KEY (dept_name) -- Build INDEX on this unique-value column
);
CREATE TABLE dept_emp (
emp_no INT NOT NULL,
dept_no CHAR(4) NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
KEY (emp_no), -- Build INDEX on this non-unique-value column
KEY (dept_no), -- Build INDEX on this non-unique-value column
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
-- Cascade DELETE from parent table 'employee' to this child table
-- If an emp_no is deleted from parent 'employee', all records
-- involving this emp_no in this child table are also deleted
-- ON UPDATE CASCADE??
FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE,
-- ON UPDATE CASCADE??
PRIMARY KEY (emp_no, dept_no)
-- Might not be unique?? Need to include from_date
);
CREATE TABLE dept_manager (
dept_no CHAR(4) NOT NULL,
emp_no INT NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
KEY (emp_no),
KEY (dept_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
-- ON UPDATE CASCADE??
FOREIGN KEY (dept_no) REFERENCES departments (dept_no) ON DELETE CASCADE,
PRIMARY KEY (emp_no, dept_no) -- might not be unique?? Need from_date
);
CREATE TABLE titles (
emp_no INT NOT NULL,
title VARCHAR(50) NOT NULL,
from_date DATE NOT NULL,
to_date DATE,
KEY (emp_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
-- ON UPDATE CASCADE??
PRIMARY KEY (emp_no, title, from_date)
-- This ensures unique combination.
-- An employee may hold the same title but at different period
);
CREATE TABLE salaries (
emp_no INT NOT NULL,
salary INT NOT NULL,
from_date DATE NOT NULL,
to_date DATE NOT NULL,
KEY (emp_no),
FOREIGN KEY (emp_no) REFERENCES employees (emp_no) ON DELETE CASCADE,
PRIMARY KEY (emp_no, from_date)
);