Primary Key 생성 문법 - table 생성시
primary key 삭제 문법
alter table tablename
drop primary key;
alter table tablename
add primary key(col1, col2,...);
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)
);
# constraint 생략하면 이름 자동생성됨
create table job
-> (
-> jid int not null,
-> name varchar(16),
-> pid int,
-> primary key (jid),
-> foreign key (pid) references person(pid)
-> );
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 |
+-------+--------------------------------------------------------------
alter table tablename
drop foreign key FK_constraint;
alter table tablename
add foreign key (col) references REF_tablename(REF_col);
alter table orders
add foreign key (pid) references person(pid);