Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b07e435

Browse files
Add files via upload
1 parent e2ee036 commit b07e435

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

‎DB Requirements.docx

12.5 KB
Binary file not shown.

‎ILS_DB_code.sql

Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
2+
-- Creating the tables --
3+
4+
CREATE TABLE teacher (
5+
teacher_id INT PRIMARY KEY,
6+
first_name VARCHAR(40) NOT NULL,
7+
last_name VARCHAR(40) NOT NULL,
8+
language_1 VARCHAR(3) NOT NULL,
9+
language_2 VARCHAR(3),
10+
dob DATE,
11+
tax_id INT UNIQUE,
12+
phone_no VARCHAR(20)
13+
);
14+
15+
CREATE TABLE client (
16+
client_id INT PRIMARY KEY,
17+
client_name VARCHAR(40) NOT NULL,
18+
address VARCHAR(60) NOT NULL,
19+
industry VARCHAR(20)
20+
);
21+
22+
CREATE TABLE participant (
23+
participant_id INT PRIMARY KEY,
24+
first_name VARCHAR(40) NOT NULL,
25+
last_name VARCHAR(40) NOT NULL,
26+
phone_no VARCHAR(20),
27+
client INT
28+
);
29+
30+
CREATE TABLE course (
31+
course_id INT PRIMARY KEY,
32+
course_name VARCHAR(40) NOT NULL,
33+
language VARCHAR(3) NOT NULL,
34+
level VARCHAR(2),
35+
course_length_weeks INT,
36+
start_date DATE,
37+
in_school BOOLEAN,
38+
teacher INT,
39+
client INT
40+
);
41+
42+
43+
-- Altering the tables to establish the relationships via FOREIGN KEYs
44+
45+
ALTER TABLE participant
46+
ADD FOREIGN KEY(client)
47+
REFERENCES client(client_id)
48+
ON DELETE SET NULL;
49+
50+
ALTER TABLE course
51+
ADD FOREIGN KEY(teacher)
52+
REFERENCES teacher(teacher_id)
53+
ON DELETE SET NULL;
54+
55+
ALTER TABLE course
56+
ADD FOREIGN KEY(client)
57+
REFERENCES client(client_id)
58+
ON DELETE SET NULL;
59+
60+
61+
62+
CREATE TABLE takes_course (
63+
participant_id INT,
64+
course_id INT,
65+
PRIMARY KEY(participant_id, course_id),
66+
FOREIGN KEY(participant_id) REFERENCES participant(participant_id) ON DELETE CASCADE, -- it makes no sense to keep this rtelation when a participant or course is no longer in the system, hence why CASCADE this time
67+
FOREIGN KEY(course_id) REFERENCES course(course_id) ON DELETE CASCADE
68+
);
69+
70+
71+
72+
/*
73+
-- If needed to quickly delete and re-build the tables:
74+
75+
SET FOREIGN_KEY_CHECKS = 0;
76+
77+
DROP TABLE client;
78+
DROP TABLE course;
79+
DROP TABLE participant;
80+
DROP TABLE takes_course;
81+
DROP TABLE teacher;
82+
83+
SET FOREIGN_KEY_CHECKS = 1;
84+
85+
*/
86+
87+
-- Populating the tables
88+
89+
INSERT INTO teacher VALUES
90+
(1, 'James', 'Smith', 'ENG', NULL, '1985年04月20日', 12345, '+491774553676'),
91+
(2, 'Stefanie', 'Martin', 'FRA', NULL, '1970年02月17日', 23456, '+491234567890'),
92+
(3, 'Steve', 'Wang', 'MAN', 'ENG', '1990年11月12日', 34567, '+447840921333'),
93+
(4, 'Friederike', 'Müller-Rossi', 'DEU', 'ITA', '1987年07月07日', 45678, '+492345678901'),
94+
(5, 'Isobel', 'Ivanova', 'RUS', 'ENG', '1963年05月30日', 56789, '+491772635467'),
95+
(6, 'Niamh', 'Murphy', 'ENG', 'IRI', '1995年09月08日', 67890, '+491231231232');
96+
97+
98+
INSERT INTO client VALUES
99+
(101, 'Big Business Federation', '123 Falschungstraße, 10999 Berlin', 'NGO'),
100+
(102, 'eCommerce GmbH', '27 Ersatz Allee, 10317 Berlin', 'Retail'),
101+
(103, 'AutoMaker AG', '20 Künstlichstraße, 10023 Berlin', 'Auto'),
102+
(104, 'Banko Bank', '12 Betrugstraße, 12345 Berlin', 'Banking'),
103+
(105, 'WeMoveIt GmbH', '138 Arglistweg, 10065 Berlin', 'Logistics');
104+
105+
106+
INSERT INTO participant VALUES
107+
(101, 'Marina', 'Berg','491635558182', 101),
108+
(102, 'Andrea', 'Duerr', '49159555740', 101),
109+
(103, 'Philipp', 'Probst', '49155555692', 102),
110+
(104, 'René', 'Brandt', '4916355546', 102),
111+
(105, 'Susanne', 'Shuster', '49155555779', 102),
112+
(106, 'Christian', 'Schreiner', '49162555375', 101),
113+
(107, 'Harry', 'Kim', '49177555633', 101),
114+
(108, 'Jan', 'Nowak', '49151555824', 101),
115+
(109, 'Pablo', 'Garcia', '49162555176', 101),
116+
(110, 'Melanie', 'Dreschler', '49151555527', 103),
117+
(111, 'Dieter', 'Durr', '49178555311', 103),
118+
(112, 'Max', 'Mustermann', '49152555195', 104),
119+
(113, 'Maxine', 'Mustermann', '49177555355', 104),
120+
(114, 'Heiko', 'Fleischer', '49155555581', 105);
121+
122+
123+
INSERT INTO course VALUES
124+
(12, 'English for Logistics', 'ENG', 'A1', 10, '2020年02月01日', TRUE, 1, 105),
125+
(13, 'Beginner English', 'ENG', 'A2', 40, '2019年11月12日', FALSE, 6, 101),
126+
(14, 'Intermediate English', 'ENG', 'B2', 40, '2019年11月12日', FALSE, 6, 101),
127+
(15, 'Advanced English', 'ENG', 'C1', 40, '2019年11月12日', FALSE, 6, 101),
128+
(16, 'Mandarin für Autoindustrie', 'MAN', 'B1', 15, '2020年01月15日', TRUE, 3, 103),
129+
(17, 'Français intermédiaire', 'FRA', 'B1', 18, '2020年04月03日', FALSE, 2, 101),
130+
(18, 'Deutsch für Anfänger', 'DEU', 'A2', 8, '2020年02月14日', TRUE, 4, 102),
131+
(19, 'Intermediate English', 'ENG', 'B2', 10, '2020年03月29日', FALSE, 1, 104),
132+
(20, 'Fortgeschrittenes Russisch', 'RUS', 'C1', 4, '2020年04月08日', FALSE, 5, 103);
133+
134+
135+
INSERT INTO takes_course VALUES
136+
(101, 15),
137+
(101, 17),
138+
(102, 17),
139+
(103, 18),
140+
(104, 18),
141+
(105, 18),
142+
(106, 13),
143+
(107, 13),
144+
(108, 13),
145+
(109, 14),
146+
(109, 15),
147+
(110, 16),
148+
(110, 20),
149+
(111, 16),
150+
(114, 12),
151+
(112, 19),
152+
(113, 19);
153+
154+
155+
-- Now for some fun! It's query-time.
156+
157+
-- Get all the details from the teachers table.
158+
SELECT *
159+
FROM teacher;
160+
161+
-- Get the last names and birthdays of all the teachers.
162+
SELECT last_name, dob
163+
FROM teacher;
164+
165+
-- Get the names and phone numbers of all teachers born before 1990.
166+
SELECT first_name, last_name, phone_no
167+
FROM teacher
168+
WHERE dob < '1990年01月01日';
169+
170+
-- Do the same as before but use aliasing.
171+
SELECT first_name AS First Name, last_name AS 'Last Name', phone_no AS Telephone
172+
FROM teacher
173+
WHERE dob < '1990年01月01日';
174+
175+
176+
-- Get all particpants in classes taught by Niamh Murphy
177+
SELECT participant.first_name, participant.last_name
178+
FROM participant
179+
JOIN takes_course ON takes_course.participant_id = participant.participant_id
180+
JOIN course ON takes_course.course_id = course.course_id
181+
WHERE takes_course.course_id =
182+
(SELECT takes_course.course_id
183+
WHERE course.teacher = 6);
184+
185+
186+
-- Get the course details for all courses which take place at the client's offices.
187+
SELECT course.course_id, course.course_name, course.language, client.client_name, client.address
188+
FROM course
189+
JOIN client
190+
ON course.client = client.client_id
191+
WHERE course.in_school = FALSE;
192+
193+
194+
-- Get the course details for all courses which take place at the client's offices, which are taught
195+
-- by Stefanie Martin (as above, but with added details).
196+
SELECT course.course_id, course.course_name, course.language, client.client_name, client.address
197+
FROM course
198+
JOIN client
199+
ON course.client = client.client_id
200+
WHERE course.in_school = FALSE AND course.teacher = 2;

‎Tables.xlsx

19.1 KB
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /