
- ํ ์ด๋ธ์ ๊ฐ ๋ ์ฝ๋๋ฅผ ์๋ณ
- ์ค๋ณต๋์ง ์์ ๊ณ ์ ๊ฐ์ ํฌํจ
- NULL ๊ฐ์ ํฌํจํ ์ ์์
- ํ ์ด๋ธ ๋น ํ๋์ ๊ธฐ๋ณธํค๋ฅผ ๊ฐ์ง
- ํ ํ ์ด๋ธ์ ๋ค๋ฅธ ํ ์ด๋ธ๊ณผ ์ฐ๊ฒฐํด์ฃผ๋ ์ญํ
- ์ฐธ์กฐ๋๋ ํ ์ด๋ธ์ ํญ๋ชฉ์ ๊ทธ ํ ์ด๋ธ์ ๊ธฐ๋ณธํค (ํน์ ๋จ์ผ๊ฐ)
Primary Key ๋ฌธ๋ฒ
//create table ์์ Primary key๋ฅผ ์ค์
create table tablename
(
column1 datatype not null,
column2 datatype not null,
...
constraint constraint_name
primary key (column1, column2, ...)
)
// ํ
์ด๋ธ ์์ฑ ํ Primary Key ์ถ๊ฐ
alter table tablename
add primary key (column1, column2, ...)
Primary Key ์ญ์ ๋ฌธ๋ฒ
alter table tablename
drop primary key(column)
Foreign Key ๋ฌธ๋ฒ
//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 (column3, column4, ...) references ref_tablename(ref_column)
)
// table ์์ฑ ํ foreign key ์ถ๊ฐ
alter table tablename
add foreign key(column) references ref_tablename(ref_column)
์๋ ์์ฑ๋ CONSTRAINT ๋ฅผ ํ์ธ
show create table tablename
Foreign Key ์ญ์ ๋ฌธ๋ฒ
alter table tablename
drop foreign key constraint_name
- orders table
create table orders ( order_id int not null primary key, user_id int, product varchar(32) );
- users table
create table users ( id int not null primary key, pw varchar(16) not null, name varchar(16) not null, email varchar(32) );
alter table orders
drop primary key;
orders ํ ์ด๋ธ์ foreign key ์ญ์
alter table orders
drop foreign key orders_ibfk_1; // ์๋์ผ๋ก ์์ฑ๋ constraint_name
alter table orders
add foreign key (user_id) references users (id);
alter table orders
add primary key(order_id);
์ค์ตํ์ธ
orders![]() | users![]() |
|---|
constraint_name ํ์ธ![]() |
|---|