-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreating and Populating Tables.sql
166 lines (124 loc) · 3.59 KB
/
Creating and Populating Tables.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
-- Part 1
DROP TABLE Enrollment;
DROP TABLE Course;
DROP TABLE Student;
CREATE TABLE Student
(
ID VARCHAR2(5),
Name VARCHAR2(25),
Standing VARCHAR2(8),
CONSTRAINT Student_PK
PRIMARY KEY(ID)
);
CREATE TABLE Course
(
CourseID VARCHAR2(15),
Name VARCHAR2(50),
Credits NUMBER(*,0),
CONSTRAINT Course_PK
PRIMARY KEY(CourseID)
);
CREATE TABLE Enrollment
(
StudentID VARCHAR2(5),
CourseID VARCHAR2(15),
Enrolled DATE,
CONSTRAINT Enrollment_PK
PRIMARY KEY(CourseID, StudentID),
CONSTRAINT Enrollment_FK1
FOREIGN KEY (CourseID)
REFERENCES Course(CourseID),
CONSTRAINT Enrollment_FK2
FOREIGN KEY (StudentID)
REFERENCES Student(ID)
);
INSERT INTO Student VALUES ('12345', 'Paul K', 'Grad');
INSERT INTO Student VALUES ('23456', 'Larry P', 'Grad');
INSERT INTO Student VALUES ('34567', 'Ana B', 'Ugrad');
INSERT INTO Student VALUES ('45678', 'Mary Y', 'Grad');
INSERT INTO Student VALUES ('56789', 'Pat B', 'Ugrad');
INSERT INTO Course VALUES ('CSC211', 'Intro to Java I', 4);
INSERT INTO Course VALUES ('IT130', 'The Internet and the Web', 2);
INSERT INTO Course VALUES ('CSC451', 'Database Design', 4);
INSERT INTO Enrollment VALUES('12345', 'CSC211', '01-Jan-2011');
INSERT INTO Enrollment VALUES('12345', 'CSC451', '02-Jan-2011');
INSERT INTO Enrollment VALUES('23456', 'IT130', '03-Jan-2011');
INSERT INTO Enrollment VALUES('23456', 'CSC211', to_date('January 03, 2019', 'Month dd, YYYY'));
INSERT INTO Enrollment VALUES('34567', 'CSC211', '06-Jan-2011');
INSERT INTO Enrollment VALUES('34567', 'IT130', '07-Jan-2011');
INSERT INTO Enrollment VALUES('34567', 'CSC451', '11-Jan-2011');
INSERT INTO Enrollment VALUES('45678', 'IT130', '02-Jan-2011');
INSERT INTO Enrollment VALUES('45678', 'CSC211', '02-Jan-2011');
SELECT * FROM Student;
SELECT * FROM Course;
SELECT * FROM Enrollment;
-- Part 2
DROP TABLE Ownership;
DROP TABLE Cars;
DROP TABLE People;
CREATE TABLE People
(
ID NUMBER(7),
Name VARCHAR2(25),
Phone VARCHAR2(12),
CONSTRAINT People_PK
PRIMARY KEY(ID)
);
CREATE Table Cars
(
Plate VARCHAR2(10),
Color VARCHAR2(8),
CONSTRAINT Cars_PK
PRIMARY KEY(Plate)
);
CREATE Table Ownership
(
ID NUMBER(7),
CarPlate VARCHAR2(10),
CONSTRAINT Ownership_PK
PRIMARY KEY(ID, CarPlate),
CONSTRAINT Ownership_FK1
FOREIGN KEY (ID)
REFERENCES People(ID),
CONSTRAINT Ownership_FK2
FOREIGN KEY (CarPlate)
REFERENCES Cars(Plate)
);
INSERT INTO People VALUES(1, 'Jane', '222');
INSERT INTO People VALUES(2, 'Mike', '333');
INSERT INTO Cars VALUES('DBA1', 'Red');
INSERT INTO Cars VALUES('486K', 'Black');
INSERT INTO Ownership VALUES(1, 'DBA1');
INSERT INTO Ownership VALUES(2, '486K');
INSERT INTO Ownership VALUES(1, '486K');
SELECT * FROM People;
SELECT * FROM Cars;
SELECT * FROM Ownership;
-- Part 3
DROP TABLE Cars_2;
DROP TABLE People_2;
CREATE TABLE People_2
(
ID NUMBER(7),
Name VARCHAR2(25),
Phone VARCHAR2(12),
CONSTRAINT People_2_PK
PRIMARY KEY(ID)
);
CREATE Table Cars_2
(
Plate VARCHAR2(10),
Color VARCHAR2(8),
Owner NUMBER(7),
CONSTRAINT Cars_2_PK
PRIMARY KEY(Plate),
CONSTRAINT Cars_2_FK
FOREIGN KEY (Owner)
REFERENCES People_2(ID)
);
INSERT INTO People_2 VALUES(1, 'Jane', '222');
INSERT INTO People_2 VALUES(2, 'Mike', '333');
INSERT INTO Cars_2 VALUES('DBA1', 'Red', 1);
INSERT INTO Cars_2 VALUES('486K', 'Black', 2);
SELECT * FROM People_2;
SELECT * FROM Cars_2;