alter table dept80
add job_id varchar(9);
새롭게 추가된 컬럼은 기본적으로 마지막 컬럼으로 추가됨.
새롭게 추가된 컬럼에는 기본적으로 null값이 저장됨.
dept80 테이블에 default값이 있는 email 컬럼 추가
alter table dept80
add email varchar(30) default '미입력';
desc dept80;
select *
from dept80;
alter table dept80
add emp_number int first;
alter table dept80
add salary int default 300 not null after last_name;
alter table dept80
add salary1 int not null after last_name;
desc dept80;
자동으로 null값 대신 0이 저장됨
select *
from dept80;
alter table dept80
modify salary bigint;
desc dept80;
- bigint로 바뀌면서 default값도 사라짐
alter table dept80
modify last_name varchar(30) not null;
desc dept80;
alter table dept80
modify salary bigint default 500 not null;
desc dept80;
- 저장된 default값이 변경되는게 아니라 변경 이후 데이터 삽입 시부터 적용됨.
alter table dept80
rename column hire_date to start_date;
alter table dept80
drop emp_number;
desc dept80;
pk, uk, ck, fk 제약조건 추가 : alter table 테이블명 add ---;
not null 제약조건 추가 : alter table 테이블명 modify ---;
primary key 제약조건 추가하기
alter table dept80
add primary key(employee_id);
desc dept80;
alter table dept80
add unique(job_id);
desc dept80;
alter table dept80
add check(salary>100);
alter table dept80
add mgr_id int default 150;
alter table dept80
add foreign key(mgr_id) references dept80(employee_id);
desc dept80;
alter table dept80
modify annsal double(22,0) not null;
desc dept80;
use information_schema;
select *
from table_constraints
where table_name = 'dept80';
use hr;
alter table dept80
drop foreign key dept80_ibfk_1;
use information_schema;
select *
from table_constraints
where table_name = 'dept80';
foreign key 제약조건 삭제됨
use hr;
alter table dept80
drop primary key;
-- 자식이 있는 pk는 삭제 안됨.
-- fk부터 삭제
desc dept80;
check 제약조건 삭제 문법
alter table 테이블명
drop check check제약조건명;
unique 제약조건 삭제 문법
alter table 테이블명
drop index unique제약조건명;
not null 제약조건 삭제 문법
alter table dept80
modify annsal double(22,0) null;
(==)
alter table dept80
modify annsal double(22,0);
drop table dept80;
desc dept80;
select *
from copy_dept;
truncate table copy_dept;
(==)
delete from copy_dept;
select *
from copy_dept;
delete 구문은 where절 작성 가능
[문법] create view 뷰명
as select 컬럼명1, 컬럼명2
from 테이블명
where 조건문;
---> 뷰의 base table
create view empvu80
as select employee_id, last_name, salary, department_id
from employees
where department_id = 80;
desc empvu80;
select *
from empvu80;
create view deptvu
as select *
from departments
where department_id > 200;
desc deptvu;
select *
from empvu80;
연계된 employees 테이블에서 데이터가 검색됨.
insert into deptvu
values (320, 'AAA', null, 1700);
select *
from deptvu;
select *
from departments;
- 뷰에 삽입해도 departments 테이블에도 같이 삽입됨
use shopdb;
create view member_vu
as select member_id, member_name, birth, job
from members;
desc member_vu;
select *
from member_vu;
use hr;
create view dept_sal_vu
as select d.department_name, sum(e.salary) as "급여 합계",
avg(e.salary) as "급여 평균",
min(e.salary) as "최소 급여",
max(e.salary) as "최대 급여"
from employees e join departments d
on e.department_id = d.department_id
group by d.department_name
order by d.department_name;
desc dept_sal_vu;
- 계속 긴 구문 써서 조회하기 어렵기 때문에 view 테이블 만들어 놓음
select *
from dept_sal_vu;
alter view empvu80
as select employee_id, last_name, salary, email, department_id
from employees
where department_id = 80;
desc empvu80;
select *
from empvu80;
drop view empvu80;
select *
from empvu80;
select *
from employees;
- 뷰를 삭제해도 base table에 미치는 영향은 없다
use information_schema;
show tables;
select *
from views
where table_schema = 'hr';