📖 Primary Key
🌟 Primary Key (기본키)
- 테이블의 각 레코드를 식별
- 중복되지 않은 고유값을 포함
- NULL 값을 포함할 수 없음
- 테이블 당 하나의 기본키를 가짐
문법
🌟 PK 생성
CREATE TABLE tablename
(
column1 datatype NOT NULL,
column2 datatype NOT NULL,
...
CONSTRAINT constraint_name
PRIMERY KEY (colum1)
PRIMERY KEY (colum1, colum2, ...)
);
🌟 삭제
ALTER TABLE tablename
DROP PRIMARY KEY;
🌟 이미 있는 table에 PK 추가
ALTER TABLE tablename
ADD PRIMARY KEY (colum1, colum2, ...);
ADD CONSTRANINT constraint_name
PRIMERY KEY (colum1, colum2, ...)
실습
💭 PK 생성
mysql> create table person
-> (
-> pid int NOT NULL,
-> name varchar(16),
-> age int,
-> sex char,
-> primary key(pid)
-> );
Query OK, 0 rows affected (0.07 sec)
mysql> desc person;
+
| Field | Type | Null | Key | Default | Extra |
+
| pid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+
4 rows in set (0.03 sec)
mysql> create table animal
-> (
-> name varchar(16) NOT NULL,
-> type varchar(16) NOT NULL,
-> age int,
-> primary key (name ,type)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc animal;
+
| Field | Type | Null | Key | Default | Extra |
+
| name | varchar(16) | NO | PRI | NULL | |
| type | varchar(16) | NO | PRI | NULL | |
| age | int | YES | | NULL | |
+
3 rows in set (0.01 sec)
💭 PK 삭제
mysql> alter table person drop primary key;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table animal drop primary key;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
💭 이미 생성된 테이블에 PK 추가
mysql> alter table person
-> add primary key(pid);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc person;
+
| Field | Type | Null | Key | Default | Extra |
+
| pid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| age | int | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
+
4 rows in set (0.00 sec)
mysql> alter table animal
-> add constraint PK_animal
-> primary key (name,type);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc animal;
+
| Field | Type | Null | Key | Default | Extra |
+
| name | varchar(16) | NO | PRI | NULL | |
| type | varchar(16) | NO | PRI | NULL | |
| age | int | YES | | NULL | |
+
3 rows in set (0.01 sec)
📖 Foreign Key
🌟 Foreign Key (외래키)
- 한 테이블을 다른 테이블과 연결해주는 역할
- 참조되는 테이블의 항목은 그 테이블의 기본키 (혹은 단일값)
문법
🌟 FK 생성
- CREATE TABLE 에서 FOREIGN KEY를 지정하는 경우
CREATE TABLE tablename
(
column1 datatype NOT NULL,
column2 datatype NOT NULL,
column3 datatype,
column4 datatype,
...
CONSTRAINT constraint_name
PRIMARY KEY (column1,column2, ...)
CONSTRAINT constraint_name
foreign KEY (column1,column2, ...) REFERENCES REF_tablename(REF_column)
);
🌟 COSNTRAINT 확인 문법
SHOW CREATE Table tablename;
🌟 FK 삭제
ALTER TABLE tablename
DEOP FOREGIN KEY FK_constraint;
🌟 이미 있는 table에 FK 추가
ALTER TABLE tablename
ADD FOREIGN KEY (column) REFERENCES REF_tablename(REF_column);
실습
💭 FK 생성
- CREATE TABLE 에서 FOREIGN KEY를 지정하는 경우
mysql> create table orders
-> (
-> oid int not null,
-> order_no varchar(16),
-> pid int,
-> primary key(oid),
-> constraint FK_person foreign key (pid) references person(pid)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc orders;
+
| Field | Type | Null | Key | Default | Extra |
+
| oid | int | NO | PRI | NULL | |
| order_no | varchar(16) | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+
3 rows in set (0.00 sec)
- CREATE TABLE 에서 FOREIGN KEY를 지정하는 경우 - constraint 생략
mysql> create table job
-> (
-> jid int not null,
-> name varchar(16),
-> pid int,
-> primary key (jid),
-> foreign key (pid) references person(pid)
-> );
Query OK, 0 rows affected (0.04 sec)
mysql> desc job;
+
| Field | Type | Null | Key | Default | Extra |
+
| jid | int | NO | PRI | NULL | |
| name | varchar(16) | YES | | NULL | |
| pid | int | YES | MUL | NULL | |
+
3 rows in set (0.00 sec)
💭 COSNTRAINT 확인
- 자동 생성된 COSNTRAINT의 이름을 확인 후 나중에 FK 설정을 위해 확인
mysql> show create table job;
+
| Table | Create Table |
+
| job | CREATE TABLE `job` (
`jid` int NOT NULL,
`name` varchar(16) DEFAULT NULL,
`pid` int DEFAULT NULL,
PRIMARY KEY (`jid`),
KEY `pid` (`pid`),
CONSTRAINT `job_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `person` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+
1 row in set (0.01 sec)
💭 FK 삭제
mysql> alter table orders drop foreign key FK_person;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
show create table orders;
+
| Table | Create Table |
+
| orders | CREATE TABLE `orders` (
`oid` int NOT NULL,
`order_no` varchar(16) DEFAULT NULL,
`pid` int DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `FK_person` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+
1 row in set (0.00 sec)
💭 이미 있는 table에 FK 추가
mysql> alter table orders
-> add foreign key (pid) references person(pid);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table orders;
+
| Table | Create Table |
+
| orders | CREATE TABLE `orders` (
`oid` int NOT NULL,
`order_no` varchar(16) DEFAULT NULL,
`pid` int DEFAULT NULL,
PRIMARY KEY (`oid`),
KEY `pid` (`pid`),
CONSTRAINT `orders_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `person` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
+
1 row in set (0.00 sec)