day8

์ƒ์€๐Ÿ‘ธยท2024๋…„ 1์›” 14์ผ
0

๋šœ๋ฒ…๋šœ๋ฒ… ์„ธ๋ฒˆ์งธ

๋ชฉ๋ก ๋ณด๊ธฐ
8/11
post-thumbnail

๐Ÿ“– MERGE : ๋ณ‘ํ•ฉ

๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด ์ €์žฅํ•˜๊ณ , ์žˆ์œผ๋ฉด ์—…๋ฐ์ดํŠธ ํ•ด๋ผ!

MERGE INTO ํ…Œ์ด๋ธ”๋ช…(์ตœ์ข…์ €์žฅํ•  ๋Œ€์ƒ)  --์ „์ฒด ์ง์›
USING ๊ฐ€์ ธ์˜ฌ๋ฐ์ดํ„ฐํ…Œ์ด๋ธ”๋ช…           --๊ณ„์—ด์‚ฌ ์ง์›
ON ( ํ•ฉ์น˜๋Š” ์กฐ๊ฑด )
WHEN MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์žˆ๋‹ค -> ์ˆ˜์ •
    UPDATE SET ์ปฌ๋Ÿผ๋ช… = ๊ฐ’
    WHERE
WHEN NOT MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์—†๋‹ค -> ์‚ฝ์ž…
    INSERT VALUES ( ์ปฌ๋Ÿผ๋ช… );

emp_b1 (๊ณ„์—ด์‚ฌ1)
emp_b2 (๊ณ„์—ด์‚ฌ2)
emp_all (์ตœ์ข… ์ง‘๊ณ„ ์ „์ฒด ์ง์› ๋ชฉ๋ก)

๐ŸŽˆ ํ…Œ์ด๋ธ” ์ƒ์„ฑ - CREATE

CREATE TABLE emp_b1
(
    empno NUMBER(3),
    name VARCHAR2(16),
    pay NUMBER(10)
);

CREATE TABLE emp_b2
(
    empno NUMBER(3),
    name VARCHAR2(16),
    pay NUMBER(10)
);

CREATE TABLE emp_all
(
    empno NUMBER(3),
    name VARCHAR2(16),
    type NUMBER(1),    --1 ๊ณ„์—ด์‚ฌ, 2 ๊ณ„์—ด์‚ฌ
    pay NUMBER(10)
);

๐ŸŽˆ ํ…Œ์ด๋ธ” ์‚ฝ์ž… - INSERT

--emp_b1
INSERT INTO emp_b1 VALUES (1, '1์ด๋ฆ„', 100);
INSERT INTO emp_b1 VALUES (2, '2์ด๋ฆ„', 200);
INSERT INTO emp_b1 VALUES (3, '3์ด๋ฆ„', 400);

--emp_b2
INSERT INTO emp_b2 VALUES (4, '4์ด๋ฆ„', 400);
INSERT INTO emp_b2 VALUES (5, '5์ด๋ฆ„', 500);
INSERT INTO emp_b2 VALUES (6, '6์ด๋ฆ„', 1000);

๐ŸŽˆ emb_b1 ๊ณผ emp_b2๋ฅผ emp_all์— ๋ณ‘ํ•ฉํ•˜๊ธฐ!

--emp_b1 ๋ณ‘ํ•ฉ
    MERGE INTO emp_all ea --์ „์ฒด ์ง์›
    USING emp_b1 e1       --๊ณ„์—ด์‚ฌ ์ง์›
    ON ( ea.empno = e1.empno ) --ํ•ฉ์น˜๋Š” ์กฐ๊ฑด
    WHEN MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์žˆ๋‹ค -> ์ˆ˜์ •
        UPDATE SET ea.pay = e1.pay
        --WHERE
    WHEN NOT MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์—†๋‹ค -> ์‚ฝ์ž…
        INSERT VALUES ( e1.empno, e1.name, 1, e1.pay );

--emp_b2 ๋ณ‘ํ•ฉ
    MERGE INTO emp_all ea --์ „์ฒด ์ง์›
    USING emp_b2 e2       --๊ณ„์—ด์‚ฌ ์ง์›
    ON ( ea.empno = e2.empno ) --ํ•ฉ์น˜๋Š” ์กฐ๊ฑด
    WHEN MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์žˆ๋‹ค -> ์ˆ˜์ •
        UPDATE SET ea.pay = e2.pay
        --WHERE
    WHEN NOT MATCHED THEN --๋ถ€ํ•ฉํ•˜๋Š”๊ฒŒ ์—†๋‹ค -> ์‚ฝ์ž…
        INSERT VALUES ( e2.empno, e2.name, 2, e2.pay );

๐Ÿ’ป

๐Ÿ“– ํ‚ค ์ œ์•ฝ์กฐ๊ฑด

NOT NULL : ๋„ ์•ˆ๋จ. ๊ฐ’์ด ๋ฌด์กฐ๊ฑด ์กด์žฌ / NULL X
UNIQUE : ์œ ์ผํ•ด์•ผํ•œ๋‹ค. ์ค‘๋ณต๊ฐ’ ์ €์žฅ XXXX / ์ค‘๋ณต X
CHECT : ํ•ด๋‹น ์กฐ๊ฑด์— ๋งž๋Š” ๊ฐ’๋งŒ ํ—ˆ์šฉ ex) ์ ์ˆ˜ NUMBER(3) 999 0~100

PRIMARY KEY : PK ๊ธฐ๋ณธํ‚ค์„ค์ •! (NOT NULL + UNIQUE), ํ…Œ์ด๋ธ”์— 1๊ฐœ๋งŒ ์„ค์ •!
FOREIGN KEY : FK ์™ธ๋ž˜ํ‚ค์„ค์ •! ๋‹ค๋ฅธ ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ์„ ์ฐธ์กฐ

๐Ÿ“Œ test001 ํ…Œ์ด๋ธ” ์ƒ์„ฑ -> UNIQUE, NOT NULL


CREATE TABLE test001
(
    no NUMBER(3) UNIQUE,
    id VARCHAR2(30) NOT NULL,
    name VARCHAR2(30) NOT NULL
);

--๋ฐ์ดํ„ฐ ์‚ฝ์ž…
INSERT INTO test001
VALUES (1, 'myid1', null); --์ƒ์„ฑํ• ๋•Œ name NOT NULL! -> null ์ €์žฅ๋ถˆ๊ฐ€

INSERT INTO test001
VALUES (1, 'myid1', 'name111');

INSERT INTO test001
VALUES (1, 'myid1', 'name111'); --no 1 ์ด unique ๊ทผ๋ฐ ์œ„์— ์ด๋ฏธ ๋งŒ๋“ค์–ด์ง„ ์ƒํƒœ์—์„œ 1์„ ๋˜ ๋„ฃ์œผ๋ ค๊ณ  ํ•˜๋ฉด -> ๊ณ ์œ  ๋ฌด๊ฒฐ์„ฑ ์ œ์•ฝ์กฐ๊ฑด ์œ„๋ฐฐ

INSERT INTO test001
VALUES (2, 'myid1', 'name111');

INSERT INTO test001
VALUES (null, 'myid1', 'name111');

==> name์€ NOT NULL ๋กœ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— null๋กœ ์ €์žฅX
==> no๋Š” UNIQUE ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— 1์ด ์ด๋ฏธ ์‚ฝ์ž… ๋˜์–ด์žˆ๋‹ค๋ฉด ๋‹ค์Œ์— 1์„ ๋‹ค์‹œ ์‚ฝ์ž…ํ•˜๋ฉด ์˜ค๋ฅ˜!

๐Ÿ’ป

๐Ÿ“Œ test002 ํ…Œ์ด๋ธ” ์ƒ์„ฑ -> PRIMARY KEY, UNIQUE, NOT NULL

CREATE TABLE test002
(
    no NUMBER(3) PRIMARY KEY, --ํ…Œ์ด๋ธ”์—๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋ณธํ‚ค๋งŒ!
    id VARCHAR2(30) UNIQUE NOT NULL,
    name VARCHAR2(30) NOT NULL --๋™๋ช…์ด์ธ์ด ์žˆ์„์ˆ˜๋„ ์žˆ์œผ๋‹ˆ๊นŒ ๊ธฐ๋ณธํ‚คX
);

INSERT INTO test002
VALUES (1, 'myid1', null); --์ƒ์„ฑํ• ๋•Œ name NOT NULL! -> null ์ €์žฅ๋ถˆ๊ฐ€

INSERT INTO test002
VALUES (1, 'myid1', 'name111'); --๊ธฐ๋ณธํ‚ค ์ค‘๋ณตX

INSERT INTO test002
VALUES (null, 'myid1', 'name111'); --๊ธฐ๋ณธํ‚ค NULLX, id ์ค‘๋ณตX

INSERT INTO test002
VALUES (3, 'myid222', 'name111');

==> name์€ NOT NULL ๋กœ ์ƒ์„ฑํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— null๋กœ ์ €์žฅX
==> no๋Š” ๊ธฐ๋ณธํ‚ค! ๊ทธ๋ž˜์„œ ์ค‘๋ณตx
==> no๋Š” ๊ธฐ๋ณธํ‚ค! ๊ทธ๋ž˜์„œ nullx , id๋Š” UNIQUE๋ผ ์ค‘๋ณตX

๐Ÿ“Œ test003 ํ…Œ์ด๋ธ” ์ƒ์„ฑ -> PRIMARY KEY, UNIQUE, NOT NULL, CHECK

CREATE TABLE test003
(
    no NUMBER(3) CONSTRAINT test003_no_pk PRIMARY KEY, --SYS_C007501 ์ด๋Ÿฐ์‹์œผ๋กœ ๋œจ๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ test003_no_pk ์ด๋ ‡๊ฒŒ ๋œธ
    id VARCHAR2(30) 
        CONSTRAINT test003_id_uk UNIQUE 
        CONSTRAINT test003_id_nn NOT NULL,
    name VARCHAR2(30) 
        CONSTRAINT test003_name_nn NOT NULL,
    score NUMBER(3) -- 0~100
        CONSTRAINT test003_score_ck CHECK (score BETWEEN 0 AND 100)
);

SELECT * FROM test003;

INSERT INTO test003
VALUES (1, 'id1', 'name1', 50); --๋‘๋ฒˆ ๋„ฃ์„์ˆ˜๋Š” ์—†์–ด ๊ธฐ๋ณธํ‚ค๋Š” ์ค‘๋ณตX

INSERT INTO test003
VALUES (2, 'id2', 'name1', 50);

INSERT INTO test003
VALUES (3, 'id3', 'name1', 100); 

==> CONSTRAINT test003_no_pk : ์–˜๋กœ ์ง€์ • ์•ˆํ•ด์ฃผ๋ฉด CONSTRAINT name์€ SYS_C007501 ์ด๋Ÿฐ์‹์œผ๋กœ ์ €์žฅ๋˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ test003_no_pk๋กœ ์ €์žฅ๋œ๋‹ค!

==> ๊ทธ๋ฆฌ๊ณ  CHECK ๋กœ score์— ๋“ค์–ด์˜ค๋Š” ๊ฐ’์˜ ๋ฒ”์œ„ ์ง€์ •!

๐Ÿ“Œ test004 ํ…Œ์ด๋ธ” ์ƒ์„ฑ -> PRIMARY KEY, UNIQUE, NOT NULL / ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ธฐ๋ณธํ‚ค ์ง€์ •!

CREATE TABLE test004
(
    no NUMBER(3) PRIMARY KEY, --ํ…Œ์ด๋ธ”์—๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋ณธํ‚ค๋งŒ! --no๋„ PK
    id VARCHAR2(30) PRIMARY KEY,                       --id๋„ PK
    name VARCHAR2(30) NOT NULL --๋™๋ช…์ด์ธ์ด ์žˆ์„์ˆ˜๋„ ์žˆ์œผ๋‹ˆ๊นŒ ๊ธฐ๋ณธํ‚คX
);

--๋‘๊ฐœ ์กฐํ•ฉ PK VS ํ…Œ์ด๋ธ”์—๋Š” ํ•œ๊ฐœ์˜ PK๋งŒ ์„ค์ •๊ฐ€๋Šฅ!


--no + id ๊ฒฐํ•ฉํ•ด์„œ PK ์„ค์ • ํ•˜๊ณ ์‹ถ์œผ๋ฉด?
CREATE TABLE test004
(
    no NUMBER(3),     --no๋„ PK
    id VARCHAR2(30),  --id๋„ PK                     
    name VARCHAR2(30) NOT NULL, --๋™๋ช…์ด์ธ์ด ์žˆ์„์ˆ˜๋„ ์žˆ์œผ๋‹ˆ๊นŒ ๊ธฐ๋ณธํ‚คX
    
    --no + id ๊ฒฐํ•ฉํ•ด์„œ PK ์„ค์ •
    CONSTRAINT test004_pk PRIMARY KEY (no, id)
);

==> ํ…Œ์ด๋ธ”์—๋Š” ํ•˜๋‚˜์˜ ๊ธฐ๋ณธํ‚ค๋งŒ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค!
==> ๊ทผ๋ฐ ์—ฌ๋Ÿฌ ์ปฌ๋Ÿผ์„ ๊ฒฐํ•ฉํ•ด์„œ ๊ธฐ๋ณธํ‚ค๋กœ ์ง€์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด?
CONSTRAINT test004_pk PRIMARY KEY (no, id) ์ด๋Ÿฐ์‹์œผ๋กœ ๋ฐ‘์—์„œ ๋”ฐ๋กœ ์ด๋ฆ„ test004_pk ์„ค์ •ํ•ด์ฃผ๊ณ  no, id ๋‘๊ฐœ๋ฅผ ๋™์‹œ์— ๊ธฐ๋ณธํ‚ค๋กœ ์ง€์ •ํ•ด์ค€๋‹ค~!

๐Ÿ“Œ ์™ธ๋ž˜ํ‚ค ์„ค์ • -> ์ข€๋ฒˆ๊ฑฐ๋กœ์›Œ์„œ ์ž˜ ์‚ฌ์šฉ์•ˆํ•œ๋‹ค!

--mystu myprof
CREATE TABLE mystu
(
    stuno NUMBER(4) PRIMARY KEY,
    name VARCHAR2(30) NOT NULL,
    profno NUMBER(4) CONSTRAINT mystu_fk REFERENCES myprof(profno)
--  profno NUMBER(4) CONSTRAINT mystu_fk REFERENCES myprof(profno) ON DELETE CASCADE; -> ์ฐธ์กฐ๊ฐ’์ด ์ง€์›Œ์งˆ๋•Œ ๊ฐ™์ด ์ง€์›Œ์ง€๋Š” => ์—ฐ์‡„์‚ญ์ œ
--  profno NUMBER(4) CONSTRAINT mystu_fk REFERENCES myprof(profno) ON DELETE SET NULL; -> ์ฐธ์กฐ๊ฐ’์ด ์ง€์›Œ์งˆ๋•Œ NULL๋กœ ์„ธํŒ… => NULL๋กœ ๋ฐ”๋€œ
);

CREATE TABLE myprof
(
    profno NUMBER(4) PRIMARY KEY,
    name VARCHAR2(30) NOT NULL
);

INSERT INTO myprof VALUES (1, '๊ต์ˆ˜1');
INSERT INTO myprof VALUES (2, '๊ต์ˆ˜2');
INSERT INTO myprof VALUES (3, '๊ต์ˆ˜3');

SELECT * FROM myprof;
COMMIT;

INSERT INTO mystu VALUES (101, 'ํ•™์ƒ1', null);
INSERT INTO mystu VALUES (102, 'ํ•™์ƒ2', 1);
INSERT INTO mystu VALUES (103, 'ํ•™์ƒ3', 3);
INSERT INTO mystu VALUES (104, 'ํ•™์ƒ4', 8); --myprof prof 1,2,3๋งŒ! ์™ธ๋ž˜ํ‚ค ์„ค์ •ํ•œ ์ปฌ๋Ÿผ์— ์กด์žฌํ•˜๋Š” ๊ฐ’๋งŒ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ!
INSERT INTO mystu VALUES (104, 'ํ•™์ƒ4', 2); --myprof prof์— 1,2,3์žˆ์œผ๋‹ˆ๊นŒ 1,2,3๋งŒ ๋“ค์–ด๊ฐˆ์ˆ˜์žˆ์–ด!

SELECT * FROM mystu;
COMMIT;

SELECT *
FROM mystu s, myprof p
WHERE s.profno = p.profno(+); --stu์— ์žˆ๋Š”๊ฒŒ ๋‹ค๋‚˜์˜ค๋Š” ์•„์šฐํ„ฐ์กฐ์ธ

--3๊ต์ˆ˜๊ฐ€ ํ‡ด์‚ฌ -> 3๊ต์ˆ˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œ
DELETE FROM myprof 
WHERE profno = 3; --์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์œผ๋ฉด, ์‚ญ์ œ ๋ถˆ๊ฐ€! -> ์ง€์šฐ๋Š” ์ˆœ๊ฐ„ mystu prof 3์ด ์กฐ๊ฑด ์œ„๋ฐฐ!
                  --๊ทธ๋ž˜์„œ mystu prof 3์„ ์•„์˜ˆ ์—†์• ์•ผ myprof prof 3์„ ์ง€์šธ ์ˆ˜ ์žˆ์Œ
                  --๊ทผ๋ฐ ์ƒ์„ฑํ• ๋•Œ CASCADE ๊ฑธ์–ด๋‘๋ฉด myprof์—์„œ ์ง€์šฐ๋ฉด mystu์—์„œ๋„ ์‹น ์ง€์›Œ์ง!

DELETE FROM mystu
WHERE profno = 3; --๊ทธ๋ž˜์„œ mystu prof 3์„ ์•„์˜ˆ ์—†์• ์•ผ myprof prof 3์„ ์ง€์šธ ์ˆ˜ ์žˆ์Œ

==> myprof ํ…Œ์ด๋ธ”์˜ profno๋ฅผ ์™ธ๋ž˜ํ‚ค๋กœ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์œผ๋ฉด profno NUMBER(4) CONSTRAINT mystu_fk REFERENCES myprof(profno) ๋กœ ์ฐธ์กฐํ•˜๋Š” ์™ธ๋ž˜ํ‚ค๋กœ ์ง€์ •!
=> ๊ทผ๋ฐ ์กฐ๊ฑด์„ ๋” ๊ฑธ์ˆ˜ ์žˆ๋‹ค! ON DELETE CASCADE;๋Š” ์ฐธ์กฐ๊ฐ’์„ ์ง€์šธ๋•Œ ๊ฐ™์ด ์ง€์›Œ์ง€๋Š” ์—ฐ์‡„์‚ญ์ œํšจ๊ณผ, ON DELETE SET NULL;๋Š” ์ฐธ์กฐ๊ฐ’์ด ์ง€์›Œ์งˆ๋•Œ mystu๋Š” null๋กœ ๋ฐ”๋€Œ๋Š” ํšจ๊ณผ

==> INSERT INTO mystu VALUES (104, 'ํ•™์ƒ4', 8); ๋ฅผ ํ•  ์ˆ˜์—†๋‹ค! myprof prof์—๋Š” 1,2,3๋งŒ ์กด์žฌ! ์™ธ๋ž˜ํ‚ค ์„ค์ •ํ•œ ์ปฌ๋Ÿผ์— ์กด์žฌํ•˜๋Š” ๊ฐ’๋งŒ ์ถ”๊ฐ€ ๊ฐ€๋Šฅ!

==> myprof์—์„œ prof 3์„ ์‚ญ์ œ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ์™ธ๋ถ€์—์„œ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์–ด์•ผ ์‚ญ์ œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค!
=> ๊ทธ๋ž˜์„œ mystu์—์„œ prof 3์„ ์•„์˜ˆ ๋‹ค์—†์• ์•ผ myprof์—์„œ prof 3์„ ์‚ญ์ œ ๊ฐ€๋Šฅ
๐Ÿšจ ๊ทผ๋ฐ !!!!! ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์—์„œ CASCADE๋ฅผ ๊ฑธ์–ด๋†จ๋‹ค๋ฉด myprof์—์„œ ์ง€์›Œ๋„ mystu์—์„œ๋„ ์‹น ์ง€์›Œ์ง!

profile
๋’ค์ฃฝ๋ฐ•์ฃฝ ๋ฒจ๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€