MS-SQL > Data Integrity - Constraints
µî·ÏÀÏ : 2017-07-03 19:32
Á¶È¸¼ö : 52,276
¿À´ÃÀÇ µ¿¹° ´º½º ÀÔ´Ï´Ù.
--------------------------------------------------------------------------------
Á·Á¦ºñµéÀÌ Áýȸ¸¦ ¿°í ´ë´ëÀûÀÎ ¹Ý¸ðÇǿÀ» ¹ú¿´½À´Ï´Ù.
Áýȸ ÈÄ¿¡´Â, ÀÚ¼± ¹ÙÀÚȸ¸¦ ¿°í ¹ì Ç㸮¶ì¿Í ¾Ç¾î °¡Á×±¸µÎ ¹ÖÅ©ÄÚÆ® µîÀ» ÆÈ¾Æ ¼öÀÍ±Ý Àü¾×À»
'Á·Á¦ºñ º¸È£¸¦ À§ÇØ ½á´Þ¶ó'¸ç ȯ°æ´Üü¿¡ ±âºÎÇß½À´Ï´Ù.
ÀÌ¿¡ ȯ°æ´Üü´Â °¨»çÆи¦ Àü´ÞÇÏ°í ºÎÁ·ÇÑ ÀÚ±ÝÀº Á·Á¦ºñ¸¦ Àâ¾Æ Ãæ´çÇÏ°Ú´Ù°í ¾à¼Ó Çß½À´Ï´Ù
--------------------------------------------------------------------------------
¾È³çÇϼ¼¿ä.... ±Í¿°µÕÀÌ ¾È±Ô¶ø´Ï´Ù.
Àú¹ø ½Ã°£¿¡´Â °³³ä¿¡ ´ëÇؼ °£´ÜÈ÷ ¾Ë¾Æº¸¾ÒÁö¿©????
Ȥ½Ã Àß ÀÌÇØ°¡ °¡Áö ¾ÊÀ¸½Ã¸é,, ´Ù½Ã õõÈ÷ ÀÐ¾î º¸¼¼¿ä. ±âº»ÀÌ Áß¿äÇÑ°Ç ¾Ë°í °è½ÃÁö¿ä?
Constraints ¶ó´Â°ÍÀº ¿ì¸®¸»·Î Á¦¾à Á¶°ÇÀ̶ó°í ºÎ¸¥´ä´Ï´Ù. ANSI Ç¥ÁØÀÇ µ¥ÀÌŸ ¹«°á¼º ¹æ¹ýÁß
ÇϳªÀÌÁö¿ä.
ÀÌ ÆäÀÌÁö¿¡¼´Â Contraints¸¦ ¾î¶»°Ô Á¤ÀÇÇϸç Á¤Àǽà °í·ÁÇÒ »çÇ׵鿡´Â ¹«¾ùÀÌ Àִ°¡¸¦
»ìÆì º¸°íÀÚ ÇÕ´Ï´Ù.
¿©·¯ºÐÀÌ »ç¿ëÇϽô MS_SQL¼¹ö¿¡¼´Â Constraints ¹æ¹ýÀº µðÆúÆ®,üũ,ÇÁ¶óÀ̸Ӹ® Å°, À¯´ÏÅ©,
Æ÷¸°Å° µîÀÌ Àִµ¥¿©.. ÀÌ·±°ÍµéÀº Â÷ÈÄ ½Ç¹«À§ÁÖ·Î ÀÚ¼¼È÷ ´Ù·ç¾î º¸°íÀÚ ÇÕ´Ï´Ù.
Constraints¸¦ Á¤ÀÇÇϱâ À§Çؼ´Â CREATE TABLE ¶Ç´Â ALTER TABLE ¹®À» »ç¿ëÇϸç Àû¿ëÄ÷³ÀÇ
¼ö³ª ¼±¾ð ¹æ¹ý¿¡ µû¶ó¼ Constraints¿Í Å×À̺í Constraints·Î ±¸ºÐÀÌ µË´Ï´Ù.
1. CREATE TABLE ¹®¿¡¼ ConstraintsÀÇ Á¤ÀÇ
CREATE TABLE table_name
(
{ [{column_name data_type}[]][,...n]
|
}[,...n]
)
::[CONSTRAINT constraint_name]
{
[NULL | NOT NULL]
[DEFAULT constant_expression]
| [ {PRIMARY KEY | UNIQUE}
[CLUSTERED | NONCLUSTERED]
[WITH FILLAFACTOR=fillfactor]
[ON {filegroup | DEFAULT}]]
]
| [ [FOREIGN KEY]
REFERENCES ref_table[(ref_column)]
[NOT FOR REPLICATION]
]
| CHECK [NOT FOR REPLICATION]
(logical_expression)
}
::[CONSTRAINT constraint_name]
{
[ {PRIMARY KEY | UNIQUE}
[ CLUSTERED | NONCLUSTERED]
{(column[,..n])}
[WITH FILLFACTOR=fillfactor]
[ON {FILLEGROUP | DEFAULT}]
]
| FOREIGN KEY
[(column[,....n])]
REFERENCES ref_table[(ref_column[,..])]
[NOT FOR REPLICATION]
| CHECK [NOT FOR REPLICATION]
(search_conditions)
}
Ä÷³ Constraints¿Í Å×À̺í Constraints´Â °ÅÀÇ °°À¸¸ç ¾Æ·¡¿Í °°ÀÌ ±¸ºÐÀÌ µÉ¼ö ÀÖ½À´Ï´Ù.
1) Ä÷³ Á¦¾àÀº ÇϳªÀÇ Ä÷³¿¡ ´ëÇؼ¸¸ Àû¿ëµÇ´Â Á¦¾àÀ» Á¤ÈñÇÒ¶§ »ç¿ëÇϹǷΠÄ÷³ÀÇ
Á¤ÀÇ µÚ¿¡ °ð¹Ù·Î ¼±¾ðÇϸç, null°ª Çã¿ë¿©ºÎ, default,¿Í °°Àº Á¦¾à°ú ÇÔ²² Á¤Àǵ˴ϴÙ.
2) Å×À̺í Á¦¾àÀº ÁÖ·Î Çϳª ÀÌ»óÀÇ Ä÷³¿¡ ´ëÇؼ Àû¿ëµÇ´Â Á¦¾àÀÔ´Ï´Ù. ƯÁ¤ Ä÷³°ú
µ¶¸³ÀûÀ¸·Î Á¤ÀǵǹǷΠ¹Ýµå½Ã Á¦¾àÀÌ Àû¿ëµÇ´Â Ä÷³À» °ýÈ£¸¦ Ãļ ¸í½ÃÇØ¾ß ÇÕ´Ï´Ù.
À§ÀÇ ¹®¹ýÀº ¾îÄÉ º¸¸é ¹«Àð°Ô º¹ÀâÇÏ°Ô µÇ¾î ÀÖÁö¸¸, ¿©·¯ºÐÀÌ ÀÚ¼¼È÷ º¸¸é ±×¸® ¾î·ÆÁö ¾Ê°í
°¢ Á¾·ùº° ConstraintsÀÇ »ç¿ë¹ýÀ» ÀÍÈ÷°Ô µÇ¸é ¾ÆÁÖ ½±°Ô ÀÀ¿ëÇÒ ¼ö ÀÖ½À´Ï´Ù.
2. ALTER TABLE ¹®¿¡¼ ConstraintsÀÇ Á¤ÀÇ
ALTER TABLE table
{
| ADD { column_name data_type []}[,...n]
| [WITH CHECK | WITH NOCHECK] ADD
{}[,...n]
| DROP
{[CONSTRAINT] constraint_name}[,..n]
| {CHECK | NOCHECK} CONSTRAINT
{ALL | constraint_name[,...n]}
}
(
::[CONSTRAINT constraint_name]
{
[NULL | NOT NULL]
[DEFAULT constant_expression]
| [ {PRIMARY KEY | UNIQUE}
[CLUSTERED | NONCLUSTERED]
[WITH FILLAFACTOR=fillfactor]
[ON {filegroup | DEFAULT}]]
]
| [ [FOREIGN KEY]
REFERENCES ref_table[(ref_column)]
[NOT FOR REPLICATION]
]
| CHECK [NOT FOR REPLICATION]
(logical_expression)
}
::[CONSTRAINT constraint_name]
{
[ {PRIMARY KEY | UNIQUE}
[ CLUSTERED | NONCLUSTERED]
{(column[,..n])}
[WITH FILLFACTOR=fillfactor]
[ON {FILLEGROUP | DEFAULT}]
]
| FOREIGN KEY
[(column[,....n])]
REFERENCES ref_table[(ref_column[,..])]
[NOT FOR REPLICATION]
| CHECK [NOT FOR REPLICATION]
(logical_expression)
}
À§ÀÇ ¹®¹ýÀº ALTER TABLE¹®À» »ç¿ëÇÏ¿© Constraints¸¦ Á¤ÀÇÇÒ¶§ »ç¿ëµÇ´Â ¹®¹ýÀÔ´Ï´Ù.
¾Æ·¡ÀÇ »çÇ×À» °í·ÁÇϱ⸸ ÇÏ¸é µË´Ï´Ù.
1) ALTER TABLE ¹®À» »ç¿ëÇϸé Å×À̺íÀ» º¯°æÇÏÁö ¾Ê°í Constraints¸¦ Ãß°¡Çϰųª
º¯°æ,Á¦°ÅÇÒ¼ö ÀÖ½À´Ï´Ù.
2) Á¦¾àÀ» Ãß°¡ÇÒ¶§´Â Á¦¾àÀÇ À̸§À» ¸í½ÃÇϴ°ÍÀÌ ÁÁ½À´Ï´Ù. ±×·¸°Ô ÇÏÁö ¾ÊÀ¸¸é
SQL ¼¹ö¿¡¼ Constraints¸¦ Ãß°¡ÇÒ¶§¸¶´Ù Á¾·ù¿¡ µû¶ó¼ Áߺ¹µÇÁö ¾Ê°Ô²û ÀϺ¯¹øÈ£
¸¦ ºÙ¿©¼ À̸§À» ¸¸µé°Ô µÇ´Âµ¥ ³ªÁß¿¡´Â ¾Ë¾Æº¸±â Èûµé¾î Áø´ä´Ï´Ù.
¿À´ÃÀº °£´ÜÈ÷ ¹®¹ý¸¸À» Áý°í ³Ñ¾î °¬±¸¿ä... ´ÙÀ½½Ã°£ºÎÅÍ´Â ½ÇÁ¦·Î Çϳª Çϳª Å×½ºÆ®
ÇÑ °á°ú°ú ÇÔ²² Á÷Á¢ °°ÀÌ ÇØ º¸¸é¼ ÀÍÈ÷´Â °É·ç ÁøÇàÇØ ³ª°¡¾ß ÇÒµíÇÕ´Ï´Ù.
¿À´Ã ,, ¸¹ÀÌ µ¤´ç,,, ±×ÃÄ??? ¸Ó¸® ¾ÆÇÁ°Ô ¿©±â±îÁö Àоî Áּż ³Ê¹« °¨»ç Çϱ¸¿©.... °Ç°Çϼ¼¿ä....