-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_script.sql
74 lines (64 loc) · 1.63 KB
/
db_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
CREATE TYPE dept_Type
/
CREATE TYPE employee_Type AS OBJECT (
empno CHAR(6),
firstName VARCHAR(12),
lastName VARCHAR(15),
workDept REF dept_Type,
sex CHAR(1),
birthDate Date,
salary NUMBER(8, 2)
)
/
CREATE TYPE dept_Type AS OBJECT (
deptNo CHAR(3),
deptName VARCHAR(36),
mgrNo REF employee_Type,
admrDept REF dept_Type
)
/
-- CREATE Table
CREATE TABLE EMPLOYEE_TABLE OF employee_Type (
CONSTRAINT tblemp_PK PRIMARY KEY (empno),
CONSTRAINT tblemp_fname firstName NOT NULL,
CONSTRAINT tblemp_lname lastName NOT NULL,
CONSTRAINT tblemp_check_sex CHECK(sex = 'M' OR sex = 'm' OR sex = 'F' OR sex = 'f')
)
/
CREATE TABLE DEPARTMENT_TABLE OF dept_Type (
CONSTRAINT tbldept_PK PRIMARY KEY (deptNo),
CONSTRAINT tbldept_deptName deptName NOT NULL,
CONSTRAINT tbldept_mgrNo_FK FOREIGN KEY (mgrNo) REFERENCES EMPLOYEE_TABLE,
CONSTRAINT tbldept_admin_FK FOREIGN KEY (admrDept) REFERENCES DEPARTMENT_TABLE
)
/
-- Inset Values
INSERT INTO DEPARTMENT_TABLE VALUES (
dept_Type('A00', 'SPIFFY', NULL, NULL)
)
/
INSERT INTO DEPARTMENT_TABLE VALUES (
dept_Type('B01', 'Planning', NULL, (SELECT REF(d) FROM DEPARTMENT_TABLE d WHERE d.deptNo = 'A00'))
)
/
INSERT INTO DEPARTMENT_TABLE VALUES (
dept_Type('C001', 'Information Centre', NULL, (SELECT REF(d) DEPARTMENT_TABLE d WHERE d.deptNo = 'A00'))
)
/
-- Updates
UPDATE DEPARTMENT_TABLE d
SET d.admrDept = (
SELECT REF(d)
FROM DEPARTMENT_TABLE d
WHERE d.deptNo = 'A00'
)
/
-- Complex Updates
UPDATE DEPARTMENT_TABLE d
SET d.mgrNo = (
SELECT REF(d)
FROM EMPLOYEE_TABLE e
WHERE e.empno = '00010'
)
WHERE d.deptNo = 'A00'
/