Oracle #3 - DDL

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

Oracle

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

๐Ÿ“ DDL

๋ฐ์ดํ„ฐ ์ •์˜์–ด (Data Definition Language) : ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œํ•œ๋‹ค.

CREATE TABLE

-- ํ…Œ์ด๋ธ” ์ƒ์„ฑ (์ •์˜)
-- * ์ƒ์„ฑ์„ ์œ„ํ•ด์„œ๋Š” ์šฐ์„  ๊ด€๋ฆฌ์ž ๊ณ„์ •์—์„œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์ค˜์•ผํ•จ (GRANT)

-- STUDENT_TBL ์„ ์ƒ์„ฑํ•˜๋ผ

CREATE TABLE STUDENT_TBL (
    STUDENT_NAME VARCHAR2(20),
    STUDENT_AGE NUMBER, 
    STUDENT_GRADE NUMBER,
    STUDENT_ADDR VARCHAR2(100)
);

ALTER TABLE

-- ํ…Œ์ด๋ธ”์— ์นผ๋Ÿผ ์ถ”๊ฐ€
ALTER TABLE USER_TABLE ADD USER_DATE DATE;

-- ํ…Œ์ด๋ธ”์— ์นผ๋Ÿผ ์‚ญ์ œ
ALTER TABLE USER_TABLE DROP COLUMN USER_DATE;
-- ํ…Œ์ด๋ธ” ์ปฌ๋Ÿผ ์ˆ˜์ •
ALTER TABLE USER_TABLE MODIFY USER_POINT NUMBER;
-- ์ œ์•ฝ์กฐ๊ฑด ์ถ”๊ฐ€
-- ALTER TABLE (ํ…Œ์ด๋ธ”๋ช…) ADD CONSTRAINT (์ œ์•ฝ์กฐ๊ฑด ์ด๋ฆ„) (์ œ์•ฝ์กฐ๊ฑด) ((์นผ๋Ÿผ๋ช…))
ALTER TABLE USER_TABLE ADD CONSTRAINT UNQ_USER_NO UNIQUE(USER_NO);
ALTER TABLE SHOP_BUY ADD CONSTRAINT FK_USER_ID FOREIGN KEY(USER_ID) REFERENCES SHOP_MEMBER(USER_ID);

-- * NOT NULL, DEFAULT ์ถ”๊ฐ€๋Š” ์กฐ๊ธˆ ๋‹ค๋ฆ„
ALTER TABLE SHOP_MEMBER MODIFY USER_PWD NOT NULL;
ALTER TABLE SHOP_BUY MODIFY REG_DAGE DEFAULT SYSDATE;
-- ์ œ์•ฝ์กฐ๊ฑด ์‚ญ์ œ
-- ์ œ์•ฝ์กฐ๊ฑด์„ ํ™•์ธํ•˜์—ฌ ์ด๋ฆ„์„ ์•Œ์•„์•ผํ•จ (CONSTRAINT_NAME)
ALTER TABLE SHOP_MEMBER DROP CONSTRAINT SYS_C007082;
-- ์ œ์•ฝ์กฐ๊ฑด ํ™œ์„ฑํ™” / ๋น„ํ™œ์„ฑํ™”
ALTER TABLE SHOP_BUY DISABLE CONSTRAINT FK_USER_ID; -- ๋น„ํ™œ์„ฑํ™”
ALTER TABLE SHOP_BUY ENABLE CONSTRAINT FK_USER_ID; -- ํ™œ์„ฑํ™”
-- ์ œ์•ฝ์กฐ๊ฑด๋ช… ๋ณ€๊ฒฝ
ALTER TABLE SHOP_BUY RENAME CONSTRAINT PK_BUY_NO TO BUY_NO_PK;
-- ์ปฌ๋Ÿผ๋ช… ๋ณ€๊ฒฝ
ALTER TABLE SHOP_BUY RENAME COLUMN REG_DATE TO BUY_DATE;
-- ํ…Œ์ด๋ธ”๋ช… ๋ณ€๊ฒฝ
ALTER TABLE SHOP_BUY RENAME TO SHOP_BUY_TBL;
RENAME SHOP_BUY_TBL TO SHOP_BUY;
-- ์ œ์•ฝ์กฐ๊ฑด ํ™•์ธ
SELECT * FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'USER_TABLE';

DROP TABLE

-- STUDENT_TBL ์„ ์‚ญ์ œํ•˜๋ผ

DROP TABLE STUDENT_TBL; 

-- ๋กค๋ฐฑ ๋ถˆ๊ฐ€. ํ…Œ์ด๋ธ” ์ „์ฒด๋ฅผ ์‚ญ์ œ

DROP ์€ DELETE ์™€ ๋‹ค๋ฅด๊ฒŒ ํ…Œ์ด๋ธ” ์ž์ฒด๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

ROLLBACK์ด ๋ถˆ๊ฐ€๋Šฅ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฃจ๋Š” ๋ฐ์— ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

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