-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture Notes.sql
286 lines (196 loc) · 6.54 KB
/
lecture Notes.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
-- PRACTICAL LECTURE SESSION
CREATE DATABASE school_db;
-- create database cpen_207;
-- How to create tables and functions
-- ER Diagrams
create table student (
StudentPID VARCHAR(50),
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
Address VARCHAR(50)
);
INSERT INTO Student (StudentPID, first_name, last_name,Address ) values (1098633, 'Evans', 'Acheampong ', 'LegonHallAA');
INSERT INTO Student (StudentPID, first_name, last_name,Address ) values (1095632, 'Edward', 'Acquah ', 'LegonHallAA');
SELECT DISTINCT * FROM Student;
create table Professor (
ProfessorPID INT NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
Office VARCHAR(50),
Age INT,
DepartmentName VARCHAR(50) NOT NULL
);
INSERT INTO Professor (ProfessorPID, first_name, last_name, Office, Age, DepartmentName ) values (1986882, 'Kenneth', 'Broni', 'SES-O233', 39, 'CPEN' );
SELECT DISTINCT * FROM Professor;
create table Course (
Number INT NOT NULL,
DepartmentName VARCHAR(50),
CourseName VARCHAR(50),
Classroom VARCHAR(50),
Enrollment INT
);
INSERT INTO Course (Number, DepartmentName, CourseName,Classroom, Enrollment ) values (203, 'CPEN', 'Programming For Engineers', '09', 1);
SELECT * FROM Course;
create table Teach (
ProfessorPID VARCHAR(50) NOT NULL,
Number INT NOT NULL,
DepartmentName VARCHAR(50)
);
INSERT INTO Teach (ProfessorPID, Number, DepartmentName ) values (20835560, 2, 'CPEN');
SELECT * FROM Course;
create table Take (
StudentPID VARCHAR(50) NOT NULL,
Number INT NOT NULL,
DepartmentName VARCHAR(50),
Grade VARCHAR(50),
ProfessorEvaluation INT
);
INSERT INTO Take (StudentPID, Number, DepartmentName, Grade, ProfessorEvaluation) values (305112, 12, 'CPEN', 'B', 83);
SELECT * FROM Course;
create table Department (
Name VARCHAR(50),
ChairmanPID VARCHAR(50)
);
INSERT INTO Department (Name, ChairmanPID) values ('CPEN', 8558892);
SELECT * FROM Department;
create table PreReq (
Number INT NOT NULL,
DepartmentName VARCHAR(50),
PreReqNumber INT,
PreReqDeptName VARCHAR(50)
);
INSERT INTO PreReq (Number, DepartmentName, PreReqNumber, PreReqDeptName) values (15, 'CPEN', 19, 13);
SELECT * FROM PreReq;
-- Creating the orders table
CREATE TABLE orders (
order_id INT NOT NULL,
product VARCHAR(50) NOT NULL,
total BIGINT NOT NULL,
customer_id INT NOT NULL
);
-- Inserting values into the orders table
INSERT INTO orders (order_id, product, total, customer_id)
VALUES(1, 'Paper', 500, 5);
INSERT INTO orders (order_id, product, total, customer_id)
VALUES(2, 'Pen', 10, 2);
INSERT INTO orders (order_id, product, total, customer_id)
VALUES(3, 'Marker', 120, 3);
INSERT INTO orders (order_id, product, total, customer_id)
VALUES(4, 'Books', 1000, 1);
INSERT INTO orders (order_id, product, total, customer_id)
VALUES(5, 'Erasers', 20, 4);
-- Selecting Values from the orders table
SELECT * FROM orders;
SELECT * FROM orders WHERE total > 500;
CREATE TABLE customers (
customer_id INT NOT NULL,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL,
phone VARCHAR(50) NOT NULL,
country TEXT NOT NULL
);
-- Inserting values into the cuatomers table
INSERT INTO customers (customer_id, first_name, last_name, phone, country)
VALUES(1, 'John', 'Doe', '817-646-8833', 'USA');
INSERT INTO customers (customer_id, first_name, last_name, phone, country)
VALUES(2, 'Robert', 'Luna', '412-862-0502', 'USA');
INSERT INTO customers (customer_id, first_name, last_name, phone, country)
VALUES(3, 'David', 'Robinson', '208-340-7906', 'UK');
INSERT INTO customers (customer_id, first_name, last_name, phone, country)
VALUES(4, 'John', 'Reinhardt', '307-232-6285', 'UK');
INSERT INTO customers (customer_id, first_name, last_name, phone, country)
VALUES(5, 'Betty', 'Doe', '806-749-2958', 'UAE');
-- Selecting Values from the customers table
SELECT * FROM customers;*
SELECT * FROM customers WHERE country = 'UK';
SELECT phone, first_name FROM customers WHERE country = 'UK';
SELECT DISTINCT country FROM customers;
SELECT * FROM customers WHERE last_name = 'Doe' ORDER BY first_name;
-- FUNCTIONS
CREATE TABLE student_info (
st_id BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
student_name VARCHAR(50)
);
INSERT INTO student_info (student_id, student_name)
VALUES(10864522, 'Mark');
INSERT INTO student_info (student_id, student_name)
VALUES(10639236, 'Jane');
INSERT INTO student_info (student_id, student_name)
VALUES(10945385, 'Nathaniel');
INSERT INTO student_info (student_id, student_name)
VALUES(10856789, 'Evans');
INSERT INTO student_info (student_id, student_name)
VALUES(10678906, 'Hammond');
INSERT INTO student_info (student_id, student_name)
VALUES(10967845, 'Edward');
INSERT INTO student_info (student_id, student_name)
VALUES(10443735, 'Dennis');
INSERT INTO student_info (student_id, student_name)
VALUES(10856789, 'Ken');
INSERT INTO student_info (student_id, student_name)
VALUES(10544774, 'Ama');
INSERT INTO student_info (student_id, student_name)
VALUES(10766553, 'Kofi');
INSERT INTO student_info (student_id, student_name)
VALUES(10964532, 'Theophilus');
INSERT INTO student_info (student_id, student_name)
VALUES(10964088, 'Nate');
UPDATE student_info
SET student_name = 'Bright'
WHERE st_id = 2;
CREATE TABLE student_info_audit(
st_id BIGSERIAL PRIMARY KEY,
student_id INT NOT NULL,
student_name VARCHAR(50),
t_date TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
-- CREATING A TRIGGER FUNCTION
CREATE OR REPLACE FUNCTION generate_audit_trail_on_student()
RETURNS TRIGGER
LANGUAGE 'plpgsql'
COST 100
AS $BODY$
DECLARE
--v_region TEXT DEFAULT";
BEGIN
INSERT INTO
student_info_audit(student_id, student_name, t_date)
VALUES(NEW.student_id, NEW.student_name,current_date);
RETURN NEW;
END;
$BODY$;
-- CREATING A TRIGGER
CREATE TRIGGER generate_audit_trail_on_student_trigger
AFTER INSERT
ON student_info
FOR EACH ROW
EXECUTE PROCEDURE
generate_audit_trail_on_student();
-- TRIGGER AND TRIGGER FUNCTION FOR UPDATING VALUES
-- CREATING A TRIGGER FUNCTION
CREATE OR REPLACE FUNCTION update_audit_on_student()
RETURNS TRIGGER
LANGUAGE 'plpgsql'
COST 100
AS $BODY$
DECLARE
--v_region TEXT DEFAULT";
BEGIN
INSERT INTO
student_info_audit(student_id, student_name, t_date)
VALUES(NEW.student_id, NEW.student_name,current_date);
INSERT INTO
student_info_audit(student_id, student_name, t_date)
VALUES(OLD.student_id, OLD.student_name,current_date);
RETURN NEW;
END;
$BODY$;
-- CREATING A TRIGGER FUNCTION
CREATE TRIGGER updatee_audit_on_student_trigger
AFTER UPDATE
ON student_info
FOR EACH ROW
EXECUTE PROCEDURE
update_audit_on_student();
SELECT * FROM student_info;