1. INSERT문
1.1 문법
- INSERT INTO 테이블명 VALUES(v1,v2,...)
- INSERT INTO 테이블명(칼럼명1, 칼럼명2,..) VALUES(v1, v2,...)
1.2 예제
CREATE TABLE LINK (
ID SERIAL PRIMARY KEY
, URL VARCHAR (255) NOT NULL
, NAME VARCHAR (255) NOT NULL
, DESCRIPTION VARCHAR (255)
, REL VARCHAR (50)
);
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
INSERT INTO LINK (URL, NAME)
VALUES ('http://naver.com','Naver');
COMMIT;
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
0 |
1 |
http://naver.com |
Naver |
None |
None |
INSERT INTO LINK(URL, NAME)
VALUES ('''http://naver.com''', '''Naver''');
COMMIT;
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
0 |
1 |
http://naver.com |
Naver |
None |
None |
1 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
INSERT INTO LINK(URL, NAME)
VALUES ('http://www.google.com','Google')
, ('http://www.yahoo.com' ,'Yahoo')
, ('http://www.bing.com' ,'Bing')
;
COMMIT;
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
0 |
1 |
http://naver.com |
Naver |
None |
None |
1 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
2 |
3 |
http://www.google.com |
Google |
None |
None |
3 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
4 |
5 |
http://www.bing.com |
Bing |
None |
None |
- 다른 테이블을 이용해 스키마만 있는 빈 껍데기 테이블 생성
CREATE TABLE LINK_TMP AS
SELECT * FROM LINK WHERE 0=1;
COMMIT
SELECT * FROM LINK_TMP
|
id |
url |
name |
description |
rel |
- 다른 테이블로부터 SELECT 하여 데이터 삽입
INSERT INTO LINK_TMP
SELECT * FROM LINK;
COMMIT;
SELECT * FROM LINK_TMP
|
id |
url |
name |
description |
rel |
0 |
1 |
http://naver.com |
Naver |
None |
None |
1 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
2 |
3 |
http://www.google.com |
Google |
None |
None |
3 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
4 |
5 |
http://www.bing.com |
Bing |
None |
None |
2. UPDATE문
- 테이블 안에 데이터를 편집할 때 사용하는 명령어
2.1 문법
- UPDATE 테이블명 SET 컬럼명 = 값 WHERE 조건
2.2 예제
- name 이 'Naver'인 데이터의 description을 name 컬럼값으로 수정
UPDATE LINK SET DESCRIPTION = NAME
WHERE NAME = 'Naver'
COMMIT;
SELECT * FROM LINK
|
id |
url |
name |
description |
rel |
0 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
1 |
3 |
http://www.google.com |
Google |
None |
None |
2 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
3 |
5 |
http://www.bing.com |
Bing |
None |
None |
4 |
1 |
http://naver.com |
Naver |
Naver |
None |
3. UPDATE JOIN 문
- 다른 테이블의 값을 참조하여 한 테이블의 값을 편집할 때 사용하는 명령어
3.1 문법
- UPDATE 테이블1 A SET A.컬럼 = 값 FROM 테이블2 B WHERE A.컬럼 = B.컬럼
3.2 예제
-- PRODUCT_SEGMENT 테이블 생성 및 삽입
CREATE TABLE PRODUCT_SEGMENT
(
ID SERIAL PRIMARY KEY
, SEGMENT VARCHAR NOT NULL
, DISCOUNT NUMERIC (4, 2)
);
INSERT INTO PRODUCT_SEGMENT (SEGMENT, DISCOUNT)
VALUES
('Grand Luxury', 0.05)
, ('Luxury', 0.06)
, ('Mass', 0.1);
COMMIT;
-- PRODUCT 테이블 생성 및 삽입
CREATE TABLE PRODUCT(
ID SERIAL PRIMARY KEY
, NAME VARCHAR NOT NULL
, PRICE NUMERIC(10, 2)
, NET_PRICE NUMERIC(10, 2)
, SEGMENT_ID INT NOT NULL
, FOREIGN KEY(SEGMENT_ID)
REFERENCES PRODUCT_SEGMENT(ID)
);
NSERT INTO PRODUCT (NAME, PRICE, SEGMENT_ID)
VALUES
('K5', 804.89, 1)
, ('K7', 228.55, 3)
, ('K9', 366.45, 2)
, ('SONATA', 145.33, 3)
, ('SPARK', 551.77, 2)
, ('AVANTE', 261.58, 3)
, ('LOZTE', 519.62, 2)
, ('SANTAFE', 843.31, 1)
, ('TUSON', 254.18, 3)
, ('TRAX', 427.78, 2)
, ('ORANDO', 936.29, 1)
, ('RAY', 910.34, 1)
, ('MORNING', 208.33, 3)
, ('VERNA', 985.45, 1)
, ('K8', 841.26, 1)
, ('TICO', 896.38, 1)
, ('MATIZ', 575.74, 2)
, ('SPORTAGE', 530.64, 2)
, ('ACCENT', 892.43, 1)
, ('TOSCA', 161.71, 3);
COMMIT;
SELECT * FROM PRODUCT_SEGMENT;
|
id |
segment |
discount |
0 |
1 |
Grand Luxury |
0.05 |
1 |
2 |
Luxury |
0.06 |
2 |
3 |
Mass |
0.10 |
SELECT * FROM PRODUCT;
|
id |
name |
price |
net_price |
segment_id |
0 |
1 |
K5 |
804.89 |
None |
1 |
1 |
2 |
K7 |
228.55 |
None |
3 |
2 |
3 |
K9 |
366.45 |
None |
2 |
3 |
4 |
SONATA |
145.33 |
None |
3 |
4 |
5 |
SPARK |
551.77 |
None |
2 |
5 |
6 |
AVANTE |
261.58 |
None |
3 |
6 |
7 |
LOZTE |
519.62 |
None |
2 |
7 |
8 |
SANTAFE |
843.31 |
None |
1 |
8 |
9 |
TUSON |
254.18 |
None |
3 |
9 |
10 |
TRAX |
427.78 |
None |
2 |
10 |
11 |
ORANDO |
936.29 |
None |
1 |
11 |
12 |
RAY |
910.34 |
None |
1 |
12 |
13 |
MORNING |
208.33 |
None |
3 |
13 |
14 |
VERNA |
985.45 |
None |
1 |
14 |
15 |
K8 |
841.26 |
None |
1 |
15 |
16 |
TICO |
896.38 |
None |
1 |
16 |
17 |
MATIZ |
575.74 |
None |
2 |
17 |
18 |
SPORTAGE |
530.64 |
None |
2 |
18 |
19 |
ACCENT |
892.43 |
None |
1 |
19 |
20 |
TOSCA |
161.71 |
None |
3 |
- PRODUCT_SEGMENT의 값을 참조하여 NET_PRICE 계산
UPDATE PRODUCT A
SET NET_PRICE = A.PRICE - (A.PRICE * B.DISCOUNT)
FROM PRODUCT_SEGMENT B
WHERE A.SEGMENT_ID = B.ID;
COMMIT;
SELECT * FROM PRODUCT;
|
id |
name |
price |
net_price |
segment_id |
0 |
1 |
K5 |
804.89 |
764.65 |
1 |
1 |
2 |
K7 |
228.55 |
205.70 |
3 |
2 |
3 |
K9 |
366.45 |
344.46 |
2 |
3 |
4 |
SONATA |
145.33 |
130.80 |
3 |
4 |
5 |
SPARK |
551.77 |
518.66 |
2 |
5 |
6 |
AVANTE |
261.58 |
235.42 |
3 |
6 |
7 |
LOZTE |
519.62 |
488.44 |
2 |
7 |
8 |
SANTAFE |
843.31 |
801.14 |
1 |
8 |
9 |
TUSON |
254.18 |
228.76 |
3 |
9 |
10 |
TRAX |
427.78 |
402.11 |
2 |
10 |
11 |
ORANDO |
936.29 |
889.48 |
1 |
11 |
12 |
RAY |
910.34 |
864.82 |
1 |
12 |
13 |
MORNING |
208.33 |
187.50 |
3 |
13 |
14 |
VERNA |
985.45 |
936.18 |
1 |
14 |
15 |
K8 |
841.26 |
799.20 |
1 |
15 |
16 |
TICO |
896.38 |
851.56 |
1 |
16 |
17 |
MATIZ |
575.74 |
541.20 |
2 |
17 |
18 |
SPORTAGE |
530.64 |
498.80 |
2 |
18 |
19 |
ACCENT |
892.43 |
847.81 |
1 |
19 |
20 |
TOSCA |
161.71 |
145.54 |
3 |
4. DELETE 문
4.1 문법
4.2 예제
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
0 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
1 |
3 |
http://www.google.com |
Google |
None |
None |
2 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
3 |
5 |
http://www.bing.com |
Bing |
None |
None |
4 |
1 |
http://naver.com |
Naver |
Naver |
None |
SELECT * FROM LINK_TMP;
|
id |
url |
name |
description |
rel |
0 |
1 |
http://naver.com |
Naver |
None |
None |
1 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
2 |
3 |
http://www.google.com |
Google |
None |
None |
3 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
4 |
5 |
http://www.bing.com |
Bing |
None |
None |
DELETE FROM LINK WHERE ID = 5;
COMMIT;
SELECT * FROM LINK;
|
id |
url |
name |
description |
rel |
0 |
2 |
'http://naver.com' |
'Naver' |
None |
None |
1 |
3 |
http://www.google.com |
Google |
None |
None |
2 |
4 |
http://www.yahoo.com |
Yahoo |
None |
None |
3 |
1 |
http://naver.com |
Naver |
Naver |
None |
DELETE FROM LINK_TMP A USING LINK B
WHERE A.ID = B.ID
COMMIT;
SELECT * FROM LINK_TMP;
|
id |
url |
name |
description |
rel |
0 |
5 |
http://www.bing.com |
Bing |
None |
None |
5. UPSERT 문
- INSERT 시도 시에 조건에 따라 UPDATE를 할 수 있는 구문
5.1 문법
- INSERT INTO 테이블명(컬럼) VALUES(값) ON CONFLICT(타켓컬럼) 액션
- 액션 종류
- DO NOTHING : 충돌 시 아무 것도 하지 말라
- DO UPDATE SET ... : 충돌 시 업데이트 하라
5.2 예시
- 실습 테이블 생성(NAME 컬럼에 UNIQUE 제약 조건 설정)
CREATE TABLE CUSTOMERS
(
CUSTOMER_ID SERIAL PRIMARY KEY
, NAME VARCHAR UNIQUE
, EMAIL VARCHAR NOT NULL
, ACTIVE BOOL NOT NULL DEFAULT TRUE
);
INSERT INTO CUSTOMERS (NAME, EMAIL)
VALUES
('IBM', 'contact@ibm.com'),
('Microsoft', 'contact@microsoft.com'),
('Intel', 'contact@intel.com');
COMMIT;
SELECT * FROM CUSTOMERS;
|
customer_id |
name |
email |
active |
0 |
1 |
IBM |
contact@ibm.com |
True |
1 |
2 |
Microsoft |
contact@microsoft.com |
True |
2 |
3 |
Intel |
contact@intel.com |
True |
- 이미 있는 NAME 값을 추가 시 아무것도 하지 않음
INSERT INTO CUSTOMERS (NAME, EMAIL)
VALUES
(
'Microsoft', 'hotline@microsoft.com'
)
ON CONFLICT (NAME) DO NOTHING;
COMMIT;
|
customer_id |
name |
email |
active |
0 |
1 |
IBM |
contact@ibm.com |
True |
1 |
2 |
Microsoft |
contact@microsoft.com |
True |
2 |
3 |
Intel |
contact@intel.com |
True |
- 이미 있는 NAME 값을 추가 시 email 컬럼에 내용 추가
- EXCLUDED.EMAIL 은 위에서 INSERT 시도한 EMAIL을 가리킴
INSERT INTO CUSTOMERS (NAME, EMAIL)
VALUES ('Microsoft', 'hotline@microsoft.com')
ON CONFLICT (NAME) DO UPDATE
SET EMAIL = EXCLUDED.EMAIL || ';' || CUSTOMERS.EMAIL;
|
customer_id |
name |
email |
active |
0 |
1 |
IBM |
contact@ibm.com |
True |
1 |
3 |
Intel |
contact@intel.com |
True |
2 |
2 |
Microsoft |
hotline@microsoft.com;contact@microsoft.com |
True |
참고