Oracle #4 - ์ œ์•ฝ ์กฐ๊ฑด

ennakoidaยท2023๋…„ 6์›” 15์ผ
0

Oracle

๋ชฉ๋ก ๋ณด๊ธฐ
4/13

๐Ÿ“ ์ œ์•ฝ ์กฐ๊ฑด

CONSTRAINTS : ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ ๋ณด์žฅ์ด ์ฃผ ๋ชฉ์ ์ด๋‹ค. ์ž…๋ ฅ ๋ฐ์ดํ„ฐ์— ๋ฌธ์ œ๊ฐ€ ์—†๋Š”์ง€ ๊ฒ€์‚ฌ์™€ ๋ฐ์ดํ„ฐ์˜ ์ˆ˜์ •/์‚ญ์ œ ๊ฐ€๋Šฅ ์—ฌ๋ถ€ ๊ฒ€์‚ฌ ๋“ฑ์„ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.

NOT NULL

-- NOT NULL ํ…Œ์ด๋ธ” ์ƒ์„ฑ

CREATE TABLE USER_CONSTRAINT (
    USER_NO NUMBER,
    USER_ID VARCHAR2(20) NOT NULL,
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50)
);
SELECT * FROM USER_CONSTRAINT;
INSERT INTO USER_CONSTRAINT VALUES(1,'user01','pass01', '์ผ์šฉ์ž', '๋‚จ', '01029023849', 'user01@naver.com');
INSERT INTO USER_CONSTRAINT VALUES(1,'NULL','NULL', 'NULL', '๋‚จ', '01029023849', 'user01@naver.com');

SELECT * FROM USER_NO_CONSTRAINT;
-- OPA-00942 : table or view does not exist : ํ…Œ์ด๋ธ”๋ช… ์˜คํƒ€
-- OPA-00904 : "USER_PW" " : invalid identifier : ์นผ๋Ÿผ๋ช… ์˜คํƒ€
-- ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€( DML : insert, update, delete, select )
INSERT INTO USER_NO_CONSTRAINT VALUES (2, 'user02', 'pass02', '์ด์šฉ์ž', '๋‚จ', '01049844544', 'user02@nate.com');

UNIQUE

-- UNIQUE ํ…Œ์ด๋ธ” ์ƒ์„ฑ 
-- ์ค‘๋ณต ๊ธˆ์ง€

CREATE TABLE USER_UNIQUE (
    USER_NO NUMBER,
    USER_ID VARCHAR2(20) UNIQUE,
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50)
);
INSERT INTO USER_UNIQUE VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01068546222', 'user01@kakao.com');
INSERT INTO USER_UNIQUE VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01068546222', 'user01@kakao.com');
-- ORA-00001: unique constraint (USER01.SYS_C007002) violated
-- ๋™์ผํ•œ INSERT๋ฅผ ํ–ˆ์„ ๋•Œ > ๊ฐ™์€ ๊ฐ’์„ ๋„ฃ์œผ๋ ค๊ณ  ํ•จ!!! ์˜ ์˜ค๋ฅ˜๋ณด๊ณ 
INSERT INTO USER_UNIQUE VALUES (1, null, 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01068546222', 'user01@kakao.com');

SELECT * FROM USER_UNIQUE;

PRIMARY KEY : ๊ธฐ๋ณธํ‚ค

-- PK

CREATE TABLE USER_PRIMARYKEY (
    USER_NO NUMBER UNIQUE,
    USER_ID VARCHAR2(20) CONSTRAINT USER_ID_PRIMARY_KEY PRIMARY KEY,
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50)
);
INSERT INTO USER_PRIMARYKEY VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@kakao.com');
INSERT INTO USER_PRIMARYKEY VALUES (2, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@kakao.com');
-- ORA-00001: unique constraint (USER01.SYS_C007007) violated
-- ํ…Œ์ด๋ธ”์˜ ์ œ์•ฝ์กฐ๊ฑด์˜ ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. (USER01.SYS_C007007)

SELECT * FROM USER_PRIMARYKEY;

CHECK

CREATE TABLE USER_CHECK (
    USER_NO NUMBER UNIQUE,
    USER_ID VARCHAR2(20) CONSTRAINT U_ID_PRIMARY_KEY PRIMARY KEY, 
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10) CHECK(USER_GENDER IN('M', 'F')),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50)
);
INSERT INTO USER_CHECK VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@kakao.com');
-- ORA-02290: check constraint (USER01.SYS_C007021) violated
-- '๋‚จ' ์•ˆ๋จ!! ํ…Œ์ด๋ธ” ์ƒ์„ฑ ๋•Œ IN('M', 'F')๋ฅผ ์คฌ์Œ!!
INSERT INTO USER_CHECK VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', 'M', '01023445678', 'user01@kakao.com');

SELECT * FROM USER_CHECK;

DEFAULT

-- DEFAULT ์ œ์•ฝ์กฐ๊ฑด

CREATE TABLE USER_DEFAULT (
    USER_NO NUMBER UNIQUE,
    USER_ID VARCHAR2(20) PRIMARY KEY, -- ํ•œ๊ธ€(3Byte)๋กœ 6๊ธ€์ž
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10) CHECK(USER_GENDER IN('M', 'F')),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50),
    USER_DATE DATE DEFAULT SYSDATE -- SYSDATE : ์˜ค๋Š˜ ๋‚ ์งœ
);
INSERT INTO USER_DEFAULT VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', 'M', '01023445678', 'user01@nate.com', NULL);
-- DEFAULT : DEFAULT๋กœ ์ •ํ•ด๋‘” ๊ฐ’์„ ์“ฐ๊ฒ ๋‹ค
INSERT INTO USER_DEFAULT VALUES (2, 'user02', 'pass02', '์ผ์šฉ์ž', 'M', '01023445678', 'user01@nate.com', DEFAULT);
INSERT INTO USER_DEFAULT VALUES (3, 'user03', 'pass03', '์ผ์šฉ์ž', 'M', '01023445678', 'user01@nate.com', SYSDATE);

SELECT * FROM USER_DEFAULT;

FOREIGN KEY : ์™ธ๋ž˜ํ‚ค

-- FOREIGN KEY ์™ธ๋ž˜ํ‚ค ์ œ์•ฝ์กฐ๊ฑด
-- ์ฐธ์กฐ๋˜๋Š” ํ…Œ์ด๋ธ” / ๋ถ€๋ชจ ํ…Œ์ด๋ธ”

CREATE TABLE USER_GRADE (
    GRADE_CODE NUMBER PRIMARY KEY,
    GARDE_NAME VARCHAR2(30) NOT NULL
);

INSERT INTO USER_GRADE VALUES (10, '์ผ๋ฐ˜ํšŒ์›');
INSERT INTO USER_GRADE VALUES (20, '์šฐ์ˆ˜ํšŒ์›');
INSERT INTO USER_GRADE VALUES (30, 'ํŠน๋ณ„ํšŒ์›');

SELECT * FROM USER_GRADE;
-- ์ฐธ์กฐํ•˜๋Š” ์ž์‹ ํ…Œ์ด๋ธ”

CREATE TABLE USER_FOREIGNKEY (
    USER_NO NUMBER PRIMARY KEY,
    USER_ID VARCHAR2(20) UNIQUE,
    USER_PWD VARCHAR2(30) NOT NULL,
    USER_NAME VARCHAR2(30) NOT NULL,
    USER_GENDER VARCHAR2(10),
    USER_PHONE VARCHAR2(30),
    USER_EMAIL VARCHAR2(50),
    GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE)
    --FOREIGN KEY (GRADE_CODE) REFERENCES USER_GRADE(GRADE_CODE)
);

INSERT INTO USER_FOREIGNKEY VALUES (1, 'user01', 'pass01', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@nate.com', 10);
INSERT INTO USER_FOREIGNKEY VALUES (2, 'user02', 'pass02', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@nate.com', 20);
INSERT INTO USER_FOREIGNKEY VALUES (3, 'user03', 'pass03', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@nate.com', 30);
INSERT INTO USER_FOREIGNKEY VALUES (4, 'user04', 'pass04', '์ผ์šฉ์ž', '๋‚จ', '01023445678', 'user01@nate.com', 40);
-- FOREIGNKEY ์˜ค๋ฅ˜ : ๋ถ€๋ชจํ‚ค ์—†์Œ! (๋ถ€๋ชจํ‚ค์˜ ๊ฐ’๋งŒ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ. ๋งŒ์•ฝ ๋„ฃ๊ณ ์‹ถ๋‹ค๋ฉด ๋ถ€๋ชจํ‚ค๋ถ€ํ„ฐ INSERT ํ•ด์ค˜์•ผํ•จ)
-- ORA-02291: integrity constraint (USER01.SYS_C007053) violated - parent key not found

SELECT * FROM USER_FOREIGNKEY;
-- ๋ถ€๋ชจ ํ…Œ์ด๋ธ”(USER_GRADE)

INSERT INTO USER_GRADE VALUES (40, 'VIPํšŒ์›'); -- ์•„๋ž˜์— FOREIGNKEY ์“ฐ๋ ค๋ฉด ํ•„์š”ํ•จ

FOREIGN KEY - ์‚ญ์ œ ์˜ต์…˜

๊ธฐ๋ณธ ์˜ต์…˜ : ON DELETE RESTRICTED

-- ๋ถ€๋ชจ ํ…Œ์ด๋ธ”(USER_GRADE)์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๊ณ  ํ•  ๋•Œ

DELETE FROM USER_GRADE WHERE GRADE_CODE = 40;
-- ORA-02292: integrity constraint (USER01.SYS_C007053) violated - child record found
-- ์ž์‹ ์žˆ์–ด์„œ ๋ชป์ง€์›€ (์ž์‹์„ ์ง€์šฐ๊ณ  ๋ถ€๋ชจ๋ฅผ ์ง€์šฐ๋ฉด ์ง€์›Œ์ง„๋‹ค.)
CREATE TABLE USER_FOREIGNKEY(
    ...
    GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE) ON DELETE RESTRICTED
);

ON DELETE RESTRICTED๋Š” ๊ธฐ๋ณธ ์˜ต์…˜์œผ๋กœ, ์ฐธ์กฐ๋œ ์ž์‹์ด ์žˆ์„ ๊ฒฝ์šฐ, ๋ถ€๋ชจ ํ…Œ์ด๋ธ”์ด ์‚ญ์ œ๋˜์ง€ ์•Š๋Š”๋‹ค.

CASCADE

CREATE TABLE USER_FOREIGNKEY(
    ...
    GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE) ON DELETE CASCADE
);

ON DELETE CASCADE๋Š” ์ฐธ์กฐ๋œ ์ž์‹์ด ์žˆ์–ด๋„, ๋ถ€๋ชจ์™€ ์ž์‹ ๋ชจ๋‘์—๊ฒŒ์„œ ์—ฐ๊ด€๋œ ๋ชจ๋“  ๊ฒƒ์„ ์‚ญ์ œํ•˜๋Š” ์˜ต์…˜์ด๋‹ค.

SET NULL

CREATE TABLE USER_FOREIGNKEY(
    ...
    GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE) ON DELETE SET NULL
);

ON DELETE SET NULL์€ ๋ถ€๋ชจ๊ฐ€ ์‚ญ์ œ๋  ์‹œ, ์ž์‹์˜ ํ•ด๋‹น ๊ฐ’์€ NULL๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์˜ต์…˜์ด๋‹ค.

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