forked from anthonydb/practical-sql-2
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathChapter_08.sql
More file actions
202 lines (149 loc) · 5.93 KB
/
Chapter_08.sql
File metadata and controls
202 lines (149 loc) · 5.93 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
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
---------------------------------------------------------------------------
-- 실용 SQL
-- by Anthony DeBarros
----------------------------------------------------------------------------
-- 코드 8-1: 단일 열 자연 키를 기본 키로 선언
-- 열 제약조건 구문 CONSTRAINT를 사용
CREATE TABLE natural_key_example (
license_id text CONSTRAINT license_key PRIMARY KEY,
first_name text,
last_name text
);
-- 테이블 삭제
DROP TABLE natural_key_example;
-- 테이블 제약 선언
CREATE TABLE natural_key_example (
license_id text,
first_name text,
last_name text,
CONSTRAINT license_key PRIMARY KEY (license_id)
);
-- 코드 8-2: 기본 키 위반의 예
-- 코드 8-1의 두 코드 중 하나를 사용해 natural_key_example를 만든 뒤 사용하세요.
INSERT INTO natural_key_example (license_id, first_name, last_name)
VALUES ('T229901', 'Gem', 'Godfrey');
INSERT INTO natural_key_example (license_id, first_name, last_name)
VALUES ('T229901', 'John', 'Mitchell');
-- 코드 8-3: 복합 기본 키를 자연 키로 생성
CREATE TABLE natural_key_composite_example (
student_id text,
school_day date,
present boolean,
CONSTRAINT student_key PRIMARY KEY (student_id, school_day)
);
-- 코드 8-4: 복합 기본 키 위반의 예
INSERT INTO natural_key_composite_example (student_id, school_day, present)
VALUES(775, '2022-01-22', 'Y');
INSERT INTO natural_key_composite_example (student_id, school_day, present)
VALUES(775, '2022-01-23', 'Y');
INSERT INTO natural_key_composite_example (student_id, school_day, present)
VALUES(775, '2022-01-23', 'N');
-- 코드 8-5: IDENTITY를 사용하여 인조 키로 bigint 열 선언
CREATE TABLE surrogate_key_example (
order_number bigint GENERATED ALWAYS AS IDENTITY,
product_name text,
order_time timestamp with time zone,
CONSTRAINT order_number_key PRIMARY KEY (order_number)
);
INSERT INTO surrogate_key_example (product_name, order_time)
VALUES ('Beachball Polish', '2020-03-15 09:21-07'),
('Wrinkle De-Atomizer', '2017-05-22 14:00-07'),
('Flux Capacitor', '1985-10-26 01:18:00-07');
SELECT * FROM surrogate_key_example;
-- 코드 8-6: IDENTITY 시퀀스 다시 시작
INSERT INTO surrogate_key_example
OVERRIDING SYSTEM VALUE
VALUES (4, 'Chicken Coop', '2021-09-03 10:33-07');
ALTER TABLE surrogate_key_example ALTER COLUMN order_number RESTART WITH 5;
INSERT INTO surrogate_key_example (product_name, order_time)
VALUES ('Aloe Plant', '2020-03-15 10:09-07');
SELECT * FROM surrogate_key_example;
-- 코드 8-7: 외래 키 예시
CREATE TABLE licenses (
license_id text,
first_name text,
last_name text,
CONSTRAINT licenses_key PRIMARY KEY (license_id)
);
CREATE TABLE registrations (
registration_id text,
registration_date timestamp with time zone,
license_id text REFERENCES licenses (license_id),
CONSTRAINT registration_key PRIMARY KEY (registration_id, license_id)
);
INSERT INTO licenses (license_id, first_name, last_name)
VALUES ('T229901', 'Steve', 'Rothery');
INSERT INTO registrations (registration_id, registration_date, license_id)
VALUES ('A203391', '2022-03-17', 'T229901');
INSERT INTO registrations (registration_id, registration_date, license_id)
VALUES ('A75772', '2022-03-17', 'T000001');
-- 코드 8-8: CHECK 제약조건 예시
CREATE TABLE check_constraint_example (
user_id bigint GENERATED ALWAYS AS IDENTITY,
user_role text,
salary numeric(10,2),
CONSTRAINT user_id_key PRIMARY KEY (user_id),
CONSTRAINT check_role_in_list CHECK (user_role IN('Admin', 'Staff')),
CONSTRAINT check_salary_not_below_zero CHECK (salary >= 0)
);
-- 아래 두 명령어는 실행에 실패합니다
INSERT INTO check_constraint_example (user_role)
VALUES ('admin');
INSERT INTO check_constraint_example (salary)
VALUES (-10000);
-- 코드 8-9: UNIQUE 제약조건 예시
CREATE TABLE unique_constraint_example (
contact_id bigint GENERATED ALWAYS AS IDENTITY,
first_name text,
last_name text,
email text,
CONSTRAINT contact_id_key PRIMARY KEY (contact_id),
CONSTRAINT email_unique UNIQUE (email)
);
INSERT INTO unique_constraint_example (first_name, last_name, email)
VALUES ('Samantha', 'Lee', 'slee@example.org');
INSERT INTO unique_constraint_example (first_name, last_name, email)
VALUES ('Betty', 'Diaz', 'bdiaz@example.org');
INSERT INTO unique_constraint_example (first_name, last_name, email)
VALUES ('Sasha', 'Lee', 'slee@example.org');
-- 코드 8-10: NOT NULL 제약조건 예시
CREATE TABLE not_null_example (
student_id bigint GENERATED ALWAYS AS IDENTITY,
first_name text NOT NULL,
last_name text NOT NULL,
CONSTRAINT student_id_key PRIMARY KEY (student_id)
);
-- This will fail:
INSERT INTO not_null_example (first_name, last_name)
VALUES ('Sting', NULL);
-- 코드 8-11: 기본 키 및 NOT NULL 제약조건 삭제 및 추가하기
-- 삭제
ALTER TABLE not_null_example DROP CONSTRAINT student_id_key;
-- 추가
ALTER TABLE not_null_example ADD CONSTRAINT student_id_key PRIMARY KEY (student_id);
-- 삭제
ALTER TABLE not_null_example ALTER COLUMN first_name DROP NOT NULL;
-- 추가
ALTER TABLE not_null_example ALTER COLUMN first_name SET NOT NULL;
-- 코드 8-12: 뉴욕 도시 데이터 가져오기
CREATE TABLE new_york_addresses (
longitude numeric(9,6),
latitude numeric(9,6),
street_number text,
street text,
unit text,
postcode text,
id integer CONSTRAINT new_york_key PRIMARY KEY
);
COPY new_york_addresses
FROM 'C:\YourDirectory\city_of_new_york.csv'
WITH (FORMAT CSV, HEADER);
-- 코드 8-13: 인덱스 성능을 위한 벤치마크 쿼리
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
WHERE street = 'BROADWAY';
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
WHERE street = '52 STREET';
EXPLAIN ANALYZE SELECT * FROM new_york_addresses
WHERE street = 'ZWICKY AVENUE';
-- 코드 8-14: new_york_addresses 테이블에 B-Tree 인덱스 생성하기
CREATE INDEX street_idx ON new_york_addresses (street);