CONSTRAINTS : ๋ฐ์ดํฐ ๋ฌด๊ฒฐ์ฑ ๋ณด์ฅ์ด ์ฃผ ๋ชฉ์ ์ด๋ค. ์ ๋ ฅ ๋ฐ์ดํฐ์ ๋ฌธ์ ๊ฐ ์๋์ง ๊ฒ์ฌ์ ๋ฐ์ดํฐ์ ์์ /์ญ์ ๊ฐ๋ฅ ์ฌ๋ถ ๊ฒ์ฌ ๋ฑ์ ์ํด ์ฌ์ฉํ๋ค.
-- 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 ํ
์ด๋ธ ์์ฑ
-- ์ค๋ณต ๊ธ์ง
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;
-- 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;
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 ์ ์ฝ์กฐ๊ฑด
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 ์ธ๋ํค ์ ์ฝ์กฐ๊ฑด
-- ์ฐธ์กฐ๋๋ ํ
์ด๋ธ / ๋ถ๋ชจ ํ
์ด๋ธ
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 ์ฐ๋ ค๋ฉด ํ์ํจ
๊ธฐ๋ณธ ์ต์ : 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
๋ ๊ธฐ๋ณธ ์ต์
์ผ๋ก, ์ฐธ์กฐ๋ ์์์ด ์์ ๊ฒฝ์ฐ, ๋ถ๋ชจ ํ
์ด๋ธ์ด ์ญ์ ๋์ง ์๋๋ค.
CREATE TABLE USER_FOREIGNKEY(
...
GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE) ON DELETE CASCADE
);
ON DELETE CASCADE
๋ ์ฐธ์กฐ๋ ์์์ด ์์ด๋, ๋ถ๋ชจ์ ์์ ๋ชจ๋์๊ฒ์ ์ฐ๊ด๋ ๋ชจ๋ ๊ฒ์ ์ญ์
ํ๋ ์ต์
์ด๋ค.
CREATE TABLE USER_FOREIGNKEY(
...
GRADE_CODE NUMBER REFERENCES USER_GRADE(GRADE_CODE) ON DELETE SET NULL
);
ON DELETE SET NULL
์ ๋ถ๋ชจ๊ฐ ์ญ์ ๋ ์, ์์์ ํด๋น ๊ฐ์ NULL
๋ก ๋ง๋ค์ด์ฃผ๋ ์ต์
์ด๋ค.