[๋ฌธ๋ฒ] create table ํ
์ด๋ธ๋ช
ย ย ย ย ย ย ย ย (์ปฌ๋ผ๋ช
1 ๋ฐ์ดํฐํ์
(์ปฌ๋ผ์ฌ์ด์ฆ),
ย ย ย ย ย ย ย ย ย ์ปฌ๋ผ๋ช
2 ๋ฐ์ดํฐํ์
(์ปฌ๋ผ์ฌ์ด์ฆ) ์ ์ฝ์กฐ๊ฑด,
ย ย ย ย ย ย ย ย ย ์ปฌ๋ผ๋ช
3 ๋ฐ์ดํฐํ์
(์ปฌ๋ผ์ฌ์ด์ฆ) default ๊ธฐ๋ณธ๊ฐ,
ย ย ย ย ย ย ย ย ย ์ปฌ๋ผ๋ช
4 ๋ฐ์ดํฐํ์
(์ปฌ๋ผ์ฌ์ด์ฆ));
โ ํ ์ด๋ธ ์์ฑ
๐ธ default๊ฐ์ด ํฌํจ๋ ํ ์ด๋ธ ์์ฑ ๋ฐ ํ์ฉ
create table dept
(deptno int,
dname varchar(14),
loc varchar(13),
create_date datetime default now());
desc dept;
๐ฅ ์๋์ปค๋ฐ ์ค์ ํ๊ธฐ : [Query] - [Auto-Commit Transactions] ์ฒดํฌํ๊ธฐ
insert into dept
values (10,'AAA','A100','2022-10-25 13:51:05');
default๊ฐ์ด ์ ์ธ๋์ด ์์ง ์์ ์ปฌ๋ผ์ ์๋ต์ null๊ฐ ์ฝ์
๋จ.
default๊ฐ์ด ์ ์ธ๋ ์ปฌ๋ผ์ ์๋ต์ default๊ฐ ์ฝ์
๋จ.
๐ธ ์์์ (์๋)์ผ๋ก default๊ฐ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ
insert into dept(deptno, dname)
values (20,'BBB');
๐ธ ๋ช ์์ (์๋)์ผ๋ก default๊ฐ ์ฝ์ ํ๋ ๋ฐฉ๋ฒ
insert into dept
values (30,'CCC','C100',default);
insert into dept
values (40,'DDD','D100',null);
insert into dept
values (50,'EEE',default, default);
update dept
set create_date = default
where deptno = 40;
select*
from dept;
โ ์ ์ฝ์กฐ๊ฑด : ํ ์ด๋ธ์ ๋ถ์ ํฉํ ๋ฐ์ดํฐ๊ฐ ์ฝ์ /์์ /์ญ์ ๋๋ ๊ฒ์ ๋ง์์ค.
๐ธ [์ ์ฝ์กฐ๊ฑด1] not null
ย ย ย ย null๊ฐ์ด ์ฝ์
/์์ ๋ ์ ์๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย ย ํ์ ์ปฌ๋ผ์ ์ ์ธํจ.
ย ย ย ย (์) ํ์๋ช
, ์ง์์ด๋ฆ, ์ฃผ๋ฏผ๋ฒํธ, ์๋
์์ผ, ์ ํ๋ฒํธ ๋ฑ
create table test1
(id int not null,
name varchar(30) not null,
jumin varchar(13) not null,
job varchar(20),
email varchar(20),
phone varchar(20) not null,
start_date date);
desc test1;
๐ธ [์ ์ฝ์กฐ๊ฑด2] unique
ย ย ย ์ค๋ณต๊ฐ์ด ์ฝ์
/์์ ๋๋ ๊ฒ์ ๋ง์์ฃผ๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย (๋จ, not null ์ ์ฝ์กฐ๊ฑด์ด ์ ์ธ๋์ด ์์ง ์๋ค๋ฉด null๊ฐ์ ํ์ฉํจ)
ย ย ย ๊ณ ์ ํ ๊ฐ์ด ๋ค์ด์์ผ ํ๋ ์ปฌ๋ผ์ ์ฌ์ฉํจ.
ย ย ย (์) ์ฃผ๋ฏผ๋ฒํธ, ์ ํ๋ฒํธ, ๋ฉ์ผ ๋ฑ
create table test2
(id int not null unique,
name varchar(30) not null,
jumin varchar(13) not null unique,
job varchar(20),
email varchar(20) unique,
phone varchar(20) not null unique,
start_date date);
desc test2;
๐ธ [์ ์ฝ์กฐ๊ฑด3] primary key(๊ธฐ๋ณธํค)
ย ย ย not null + unique์ ์ฑ๊ฒฉ์ ๊ฐ์ง๊ณ ์๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย null๊ฐ ๋ฐ ์ค๋ณต๊ฐ์ด ์ฝ์
/์์ ๋๋ ๊ฒ์ ๋ง์์ฃผ๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย ๋จ, ํ
์ด๋ธ ๋น ํ๋ฒ๋ง ์ ์ธํ ์ ์์!!!
ย ย ย (์) ํ๋ฒ, ์ฌ๋ฒ, ํ์๋ฒํธ, ์ ํ๋ฒํธ ๋ฑ
create table test3
(id int primary key,
name varchar(30) not null,
jumin varchar(13) not null unique,
job varchar(20),
email varchar(20) unique,
phone varchar(20) not null unique,
start_date date);
desc test3;
๐ธ [์ ์ฝ์กฐ๊ฑด4] foreign key(์ธ๋ํค)
ย ย ย ์๊ธฐ ์์ ํ
์ด๋ธ์ด๋ ๋ค๋ฅธ ํ
์ด๋ธ์ ํน์ ์ปฌ๋ผ์ ์ฐธ์กฐํ๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย FK ์ ์ฝ์กฐ๊ฑด์ด ์ ์ธ๋ ์ปฌ๋ผ : ์์์ปฌ๋ผ
ย ย ย FK ์ ์ฝ์กฐ๊ฑด์ด ์ฐธ์กฐํ๋ ์ปฌ๋ผ : ๋ถ๋ชจ์ปฌ๋ผ
ย ย ย ์์ ์ปฌ๋ผ์๋ ๋ถ๋ชจ ์ปฌ๋ผ์ ์๋ ๊ฐ ์ค ํ๋๋ง ์ฝ์
/์์ ๋ ์ ์์!
ย ย ย ย (๋จ, not null ์ ์ฝ์กฐ๊ฑด์ด ์ ์ธ๋์ด ์์ง ์์ ๊ฒฝ์ฐ null๊ฐ์ ํ์ฉํจ)
create table test4
(t_num int primary key,
t_id int,
title varchar(20) not null,
story varchar(100) not null,
foreign key(t_id) references test3(id));
desc test4;
๐ธ [์ ์ฝ์กฐ๊ฑด5] check
ย ย ย ํด๋น ์ปฌ๋ผ์ด ๋ง์กฑํด์ผ ํ๋ ์กฐ๊ฑด๋ฌธ์ ์์ ๋กญ๊ฒ ์ง์ ํ๋ ์ ์ฝ์กฐ๊ฑด
ย ย ย ย (์) salary int check (salary > 0)
ย ย ย ย ย ย ย ย ์ฑ๋ณ varchar(10) check (์ฑ๋ณ in ('๋จ','์ฌ'))
ย ย ย ย ย ย ย ย jumin varchar(13) check (length(jumin)=13)
ย ย ย ย ย ย ย ย email varchar(50) check (email like '%@%')
create table test5
(id int(10) primary key,
name varchar(30) not null,
jumin varchar(13) not null unique check (length(jumin)=13),
job varchar(20),
email varchar(20) unique,
phone varchar(20) not null unique,
start_date date check (start_date >= '2005-01-01'));
desc test5;
show databases;
use information_schema;
show tables;
desc table_constraints;
select table_schema, table_name, constraint_type
from table_constraints
where table_schema = 'hr'
order by table_name;
desc check_constraints;
select *
from check_constraints
where constraint_schema = 'hr';
โ ์๋ธ์ฟผ๋ฆฌ๋ฅผ ํ์ฉํ ํ ์ด๋ธ ์์ฑ
use hr;
๐ธ (์์ 1) ๊ธฐ์กด ํ ์ด๋ธ(์๋ธ์ฟผ๋ฆฌ ํ ์ด๋ธ)์ ๋ณต์ฌ๋ณธ ํ ์ด๋ธ์ด ์์ฑ๋จ.
create table dept80
as select employee_id, last_name, salary*12 as annsal, hire_date
from employees
where department_id = 80;
desc dept80;
select*
from dept80;
๐ธ (์์ 2) ๋ฐฑ์ ์ฉ ๋๋ ํ ์คํธ์ฉ์ผ๋ก ๋ณต์ฌ๋ณธ ํ ์ด๋ธ ์์ฑ ๋ง์ดํจ.
create table copy_dept
as select*
from departments;
desc copy_dept;
shopdb ๋ฐ์ดํฐ๋ฒ ์ด์ค(์คํค๋ง)์์ ํ ์ด๋ธ ์์ฑ ์ฐ์ต๋ฌธ์ ์์ ํ๊ธฐ!
use shopdb;
create table title
(TITLE_ID int primary key,
TITLE varchar(60) not null,
DESCRIPTION varchar(400) not null,
RATING varchar(4) check (RATING in ('G','PR','R','NC17','NR')),
CATEGORY varchar(20) check (CATEGORY in ('DRAMA','COMEDY','ACTION',
'CHILD','SCIFI','DOCUMENTARY')),
RELEASE_DATE date);
desc title;
create table title_copy
(COPY_ID int,
TITLE_ID int,
STATUS varchar(15) not null check
(STATUS in ('AVAILABLE','DESTROYED','RENTED','RESERVED')),
primary key(COPY_ID, TITLE_ID),
foreign key(TITLE_ID) references title(TITLE_ID));
desc title_copy;
-- DB ์ฌ์ ์กฐํ
use information_schema;
select*
from check_constraints
where constraint_schema = 'shopdb';