제약이란? 제약은 테이블에 설정하는 것이다. CREATE로 테이블 생성시 정의하거나 ALTER로 변경할 수 있다.
# 열 제약 정의
mysql> CREATE TABLE sample631 (
-> a INTEGER NOT NULL,
-> b INTEGER NOT NULL UNIQUE,
-> c VARCHAR(30)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> DESC sample631;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int | NO | | NULL | |
| b | int | NO | PRI | NULL | |
| c | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# 테이블 제약 정의
mysql> CREATE TABLE sample632 (
-> no INTEGER NOT NULL,
-> sub_no INTEGER NOT NULL,
-> name VARCHAR(30),
-> PRIMARY KEY(no, sub_no)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> DESC sample632;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| no | int | NO | PRI | NULL | |
| sub_no | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> CREATE TABLE sample633(
-> no INTEGER NOT NULL,
-> sub_no INTEGER NOT NULL,
-> name VARCHAR(30),
-> CONSTRAINT pkey_sample PRIMARY KEY (no, sub_no) 👉 제약 이름 지정
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> DESC sample633;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| no | int | NO | PRI | NULL | |
| sub_no | int | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# c 열에 NOT NULL 제약 걸기
mysql> ALTER TABLE sample631 MODIFY c VARCHAR(30) NOT NULL;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC sample631;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int | NO | | NULL | |
| b | int | NO | PRI | NULL | |
| c | varchar(30) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# 기본키 제약 추가
mysql> ALTER TABLE sample631 ADD CONSTRAINT pkey_sample631 PRIMARY KEY(a);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC sample631;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int | NO | PRI | NULL | |
| b | int | NO | UNI | NULL | |
| c | varchar(30) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# c 열의 NOT NULL 제약 삭제
mysql> ALTER TABLE sample631 MODIFY c VARCHAR (30);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC sample631;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int | NO | PRI | NULL | |
| b | int | NO | UNI | NULL | |
| c | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
# 기본키 제약 삭제
mysql> ALTER TABLE sample631 DROP PRIMARY KEY;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC sample631;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| a | int | NO | | NULL | |
| b | int | NO | PRI | NULL | |
| c | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> CREATE TABLE sample634(
-> p INTEGER NOT NULL,
-> a VARCHAR (30),
-> CONSTRAINT pkey_sample634 PRIMARY KEY(p)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> DESC sample634;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| p | int | NO | PRI | NULL | |
| a | varchar(30) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> INSERT INTO sample634 VALUES (3, '셋째줄');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM sample634;
+---+-----------+
| p | a |
+---+-----------+
| 1 | 첫째줄 |
| 2 | 둘째줄 |
| 3 | 셋째줄 |
+---+-----------+
3 rows in set (0.00 sec)
mysql> INSERT INTO sample634 VALUES (2, '넷째줄');
ERROR 1062 (23000): Duplicate entry '2' for key 'sample634.PRIMARY'
mysql> UPDATE sample634 SET p = 2 WHERE p = 3;
ERROR 1062 (23000): Duplicate entry '2' for key 'sample634.PRIMARY'
mysql> SELECT a, b FROM sample635;
+---+---+
| a | b |
+---+---+
| 1 | 1 | 👉 a 열만 봤을 때는 중복 값이 있지만 b 열이 다르기때문에 키 전체로서는 중복되지 않는다.
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
+---+---+
5 rows in set (0.01 sec)