¿À¶óŬÀº Á¾¼ÓµÈ
Å×À̺íÀÇ »èÁ¦¸¦ ¹æÁöÇÏ°í Å×ÀÌºí¿¡ À¯È¿ÇÏÁö ¾ÊÀº µ¥ÀÌŸ°¡ ÀԷµǴ °ÍÀ» ¹æÁöÇϱâ
À§ÇÏ¿© constraint¸¦ »ç¿ëÇÕ´Ï´Ù.
1. constraint
Áöħ
1)
Á¦¾àÁ¶°Ç¿¡ À̸§À» ÁöÁ¤ÇÏÁö ¾ÊÀ¸¸é Oracle server°¡ SYS_CnÀÇ Çü½ÄÀ¸·Î ÀÚµ¿À¸·Î
À̸§À» »ý¼ºÇÕ´Ï´Ù.
2)
Á¦¾àÁ¶°ÇÀº Å©°Ô Å×ÀÌºí ·¹º§°ú ¿ ·¹º§·Î Á¤ÀÇÇÒ ¼ö ÀÖ½À´Ï´Ù.
2. constraint
Á¤ÀÇ ¿¹Á¦
SQL>
CREATE TABLE EXAMPLE(
ID
NUMBER(6),
NAME
VARCHAR2(20) [CONSTRAINT
NAME_CTR] NOT NULL,
---->
¿ ·¹º§(Á¦¾àÁ¶°Ç À̸§À» »ý·«ÇÏ¸é ½Ã½ºÅÛÀÌ ÀÚµ¿À¸·Î À̸§ »ý¼º
...
CONSTRAINT
EXAMPLE_ID_PK PRIMARY KEY(ID));
---->
Å×ÀÌºí ·¹º§(Á¦¾àÁ¶°Ç À̸§ÀÌ EXAMPLE_ID_PK·Î »ý¼º)
3. constraint
À¯Çü
1)
NOT NULL : ÇØ´ç¿¿¡ ³Î°ªÀÌ ¾øµµ·Ï Çϱâ À§ÇÑ Á¦¾àÁ¶°Ç
2)
UNIQUE : ÁöÁ¤µÈ ¿¿¡ ´ëÇØ ³Î°ªÀº Çã¿ëÇϳª, µ¿ÀÏÇÑ °ªÀº Çã¿ëµÇÁö ¾Ê´Â Á¦¾àÁ¶°Ç
¿À¶óŬ
¼¹ö´Â ÁöÁ¤µÈ ¿¿¡ ´ëÇØ ÀÚµ¿À¸·Î À妽º¸¦ »ý¼º
[¿¹Á¦]
SQL> ...CONSTRAINT EXAMPLE_ID_UK UNIQUE(ID)
3)
PRIMARY KEY : Å×À̺íÀÇ °¢ ÇàÀ» ½Äº°Çϱâ À§ÇØ Å×À̺í´ç ÇϳªÀÇ ±âº»Å°¸¦ »ý¼º
Áߺ¹µÈ
°ª°ú ³Î°ªÀ» °¡Áú ¼ö ¾ø´Ù
¿À¶óŬ
¼¹ö´Â ÁöÁ¤µÈ ¿¿¡ ´ëÇØ ÀÚµ¿À¸·Î À妽º¸¦ »ý¼º
4)
FOREIGN KEY : µ¿ÀÏÇÑ Å×ÀÌºí ¶Ç´Â ´Ù¸¥ Å×ÀÌºí¿¡¼ ±âº»Å° ¶Ç´Â °íÀ¯Å°¸¦ ÂüÁ¶ÇÏ´Â
Á¦¾àÁ¶°Ç
ºÎ¸ð
Å×À̺íÀÇ °ª°ú ÀÏÄ¡Çϰųª ³Î°ªÀ» °¡Á®¾ß ÇÑ´Ù.
ON
DELETE CASCADE : ºÎ¸ð Å×À̺íÀÇ ÇàÀÌ »èÁ¦µÇ´Â °æ¿ì ÀÚ½Ä Å×À̺íÀÇ Á¾¼Ó ÇàÀ» »èÁ¦
ON
DELETE SET NULL : ºÎ¸ð Å×À̺íÀÇ ÇàÀÌ »èÁ¦µÇ´Â °æ¿ì Á¾¼Ó ¿Ü·¡Å° °ªÀ» ³Î·Î º¯È¯
[¿¹Á¦]
...CONSTRAINT EXAMPLE_ID_FK FOREIGN KEY(ID) REFERENCES DEPT(ID)
5)
CHECK : °¢ ÇàÀÌ ¸¸Á·½ÃÄÑ¾ß ÇÏ´Â Á¶°ÇÀ» Á¤ÀÇ
[¿¹Á¦]
SAL NUMBER(10) CONSTRAINT
EXAMPLE_SAL_CK
CHECK(SAL
> 1000)
4. constraint¸¦
Á¶È¸ÇÏ´Â µ¥ÀÌŸ µñ¼Å³Ê¸®
1)
USER_CONSTRAINTS
Å×À̺í
»ý¼º ÈÄ DESC·Î Å×ÀÌºí ±¸Á¶¸¦ º¸¸é NOT NULLÁ¦¾àÁ¶°Ç¸¸ º¸ÀÌ°í, ´Ù¸¥ Á¦¾à Á¶°ÇÀº
º¸ÀÌÁö ¾Ê°Ô µÈ´Ù.
À̶§ ´Ù¸¥ Á¦¾àÁ¶°ÇÀ» º¸±â À§ÇØ USER_CONSTRAINTS¶ó´Â µ¥ÀÌŸ
µñ¼Å³Ê¸®°¡ Á¦°øµÈ´Ù.
¿©·¯ ÇʵåÁß
CONSTRAINT_TYPEÀ̶ó´Â Çʵ常 »ìÆ캸µµ·Ï ÇÏ°Ú´Ù. ³ª¸ÓÁö´Â ½±°Ô ÀÌÇØ°¡ µÉ °ÍÀÌ´Ù.
P : PRIMARY
KEY, R : FOREIGN KEY, C : CHECK ¶Ç´Â NOT NULL, U : UNIQUE
2)
USER_CONS_COLUMNS
Á¦¾à
Á¶°Ç À̸§°ú ¿¬°üµÈ Ä÷³À» º¼ ¼ö ÀÖ°Ô ÇÏ´Â µ¥ÀÌŸ µñ¼Å³Ê¸®
5. constraint
Ãß°¡ / »èÁ¦
1)
constraint Ãß°¡
EMP_TESTÅ×À̺íÀÇ
DEPT_IDÇʵ尡 DETP_TESTÀÇ ID°ªÀ» ÂüÁ¶ÇÏ´Â Á¦¾àÁ¶°ÇÀ» Ãß°¡ÇÏ´Â ¿¹¹®
SQL>
SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
CONSTRAINT_NAME
------------------------------
-------------
EMP_TEST
EMP_TEST_PK
1)
DETP_TESTÅ×À̺íÀÇ IDÇʵ带 PRIMARY KEY°ªÀ¸·Î ¼³Á¤
SQL>
ALTER TABLE DETP_TEST
ADD CONSTRAINT DETP_TEST_PK PRIMARY KEY(ID);
Table
altered.
2)
EMP_TESTÅ×À̺íÀÇ DEPT_IDÇʵ带 DETP_TESTÀÇ ID°ªÀ» ÂüÁ¶ÇÏ´Â FOREIGN KEY°ªÀ¸·Î
¼³Á¤
ALTER
TABLE EMP_TEST
ADD CONSTRAINT EMP_TEST_FK FOREIGN KEY(DEPT_ID) REFERENCES DETP_TEST(ID);
Table
altered.
3)
constraint Ãß°¡ ¿©ºÎ È®ÀÎ
SQL>
SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
CONSTRAINT_NAME
------------------------------
------------------------------
DETP_TEST
DETP_TEST_PK
EMP_TEST
EMP_TEST_FK
EMP_TEST
EMP_TEST_PK
2)
constraint »èÁ¦
EMP_TESTÅ×À̺íÀÇ
DEPT_ID(FOREIGN KEY)ÇʵåÀÇ Á¦¾àÁ¶°ÇÀ» »èÁ¦
SQL>
ALTER TABLE EMP_TEST
DROP CONSTRAINT EMP_TEST_FK;
Table
altered.
SQL>
SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
CONSTRAINT_NAME
------------------------------
------------------------------
DETP_TEST
DETP_TEST_PK
EMP_TEST
EMP_TEST_PK
DETP_TESTÅ×À̺íÀÇ
IDÇʵåÀÇ Á¦¾àÁ¶°Ç(PRIMARY KEY)À» »èÁ¦
SQL>
ALTER TABLE DETP_TEST
DROP PRIMARY KEY CASCADE; ---->
CASCADE¸¦ ºÙÀ̸é FOREIGN KEY±îÁö µ¿½Ã¿¡ »èÁ¦°¡ µÊ
Table
altered.
SQL>
SELECT TABLE_NAME, CONSTRAINT_NAME FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
CONSTRAINT_NAME
------------------------------
------------------------------
EMP_TEST
EMP_TEST_PK
6. constraintÀÇ
enable/disable : Á¦¾àÁ¶°ÇÀ» »èÁ¦/»ý¼ºÇÏÁö ¾Ê°íµµ Á¦¾àÁ¶°ÇÀ» ºñÈ°¼ºÈ ÇÒ ¼ö ÀÖ´Ù.
1)
constraintÀÇ disable
DETP_TESTÅ×À̺íÀÇ
IDÇʵåÀÇ Á¦¾àÁ¶°Ç(PRIMARY KEY)À» disable
SQL>
ALTER TABLE DETP_TEST
DISABLE CONSTRAINT DETP_TEST CASCADE; --->
CASCADE´Â FOREIGN KEY±îÁö µ¿½Ã¿¡ disableµÊ
Table
altered.
SQL>
SELECT TABLE_NAME, STATUS FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
STATUS
------------------------------
--------
DETP_TEST
DISABLED
EMP_TEST
DISABLED
EMP_TEST
ENABLED
2)
constraintÀÇ enable
disableµÈ
Á¦¾àÁ¶°ÇÀ» enable½ÃÅ°±â À§Çؼ´Â PRIMARY KEY¸¦ enable½ÃŲ ÈÄ, FOREIGN KEY¸¦
enable ½ÃÄÑ
¾ß
ÇÑ´Ù.l
SQL>
ALTER TABLE DETP_TEST
ENABLE CONSTRAINT DETP_TEST;
Table
altered.
SQL>
ALTER TABLE EMP_TEST
ENABLE CONSTRAINT EMP_TEST_FK;
Table
altered.
SQL>
SELECT TABLE_NAME, STATUS FROM USER_CONSTRAINTS
WHERE TABLE_NAME IN('EMP_TEST','DETP_TEST');
TABLE_NAME
STATUS
------------------------------
--------
DETP_TEST
ENABLED
EMP_TEST
ENABLED
EMP_TEST
ENABLED
* iWiz´Ô¿¡ ÀÇÇؼ °Ô½Ã¹° À̵¿µÇ¾ú½À´Ï´Ù (2010-02-03 17:06)
|