-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
101 lines (94 loc) · 3.94 KB
/
schema.sql
File metadata and controls
101 lines (94 loc) · 3.94 KB
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
-- =============================================================
-- Section 03 — Normalization: STARTING SCHEMA
-- =============================================================
-- Unlike most schema.sql files in this repo, this one is NOT a
-- reference. It is the STARTING POINT.
--
-- It contains three deliberately BAD, denormalized tables. You
-- will spend this section critiquing them, spotting anomalies,
-- and rewriting them into 1NF, then 2NF, then 3NF.
--
-- Load this file first, then work through exercises.md:
--
-- psql -U postgres -d sql_exercise \
-- -f 03-database-design/03-normalization/schema.sql
-- psql -U postgres -d sql_exercise \
-- -f 03-database-design/03-normalization/seed.sql
--
-- The normalized reference schemas live in:
-- solutions/03-database-design/03-normalization/solutions.sql
--
-- It drops the schema first, so it's safe to re-run.
-- =============================================================
DROP SCHEMA IF EXISTS dd_normalization CASCADE;
CREATE SCHEMA dd_normalization;
SET search_path TO dd_normalization;
-- -------------------------------------------------------------
-- bad_student_courses
-- -------------------------------------------------------------
-- A single table tracking which students are enrolled in which
-- courses, plus the instructor, and the student's phone numbers.
--
-- Look at everything wrong with this:
-- * student_phones holds a COMMA-SEPARATED LIST of phone numbers
-- * Student info (name, major) repeats for every enrollment
-- * Course info (title, credits) repeats for every enrollment
-- * Instructor info (name, dept) repeats for every enrollment
-- * No primary key
-- * Everything is TEXT, including credits
-- -------------------------------------------------------------
CREATE TABLE bad_student_courses (
student_id TEXT,
student_name TEXT,
student_major TEXT,
student_phones TEXT, -- e.g. '555-0100, 555-0200'
course_code TEXT,
course_title TEXT,
course_credits TEXT,
instructor_name TEXT,
instructor_dept TEXT,
grade TEXT
);
-- -------------------------------------------------------------
-- bad_orders
-- -------------------------------------------------------------
-- Every line item of every order is one row, but the row also
-- carries customer info and product info. Classic 2NF violation:
-- customer_name depends only on customer_id (part of the PK),
-- and product_name/price depend only on product_id.
-- -------------------------------------------------------------
CREATE TABLE bad_orders (
order_id INTEGER,
product_id INTEGER,
-- order-level (depends on order_id only)
order_date DATE,
customer_id INTEGER,
customer_name TEXT,
customer_city TEXT,
customer_zip TEXT,
-- product-level (depends on product_id only)
product_name TEXT,
product_category TEXT,
product_unit_price NUMERIC(10, 2),
-- line-level (depends on the full PK)
quantity INTEGER,
line_total NUMERIC(10, 2)
);
-- -------------------------------------------------------------
-- bad_employees
-- -------------------------------------------------------------
-- One row per employee. Each employee has a department, and each
-- department has a location and a manager. That's a transitive
-- dependency: employee_id -> dept_code -> dept_location.
-- -------------------------------------------------------------
CREATE TABLE bad_employees (
employee_id INTEGER PRIMARY KEY,
first_name TEXT NOT NULL,
last_name TEXT NOT NULL,
email TEXT NOT NULL,
salary NUMERIC(10, 2) NOT NULL,
dept_code TEXT NOT NULL,
dept_name TEXT NOT NULL, -- depends on dept_code, not employee_id
dept_location TEXT NOT NULL, -- depends on dept_code, not employee_id
dept_manager TEXT NOT NULL -- depends on dept_code, not employee_id
);