create table dept80
as
select employee_id, last_name, salary*12 ANNSAL, hire_date
from employees
where department_id=80;
desc dept80;
create table copy_dept
as
select * from departments;
desc copy_dept;
alter table dept80
add (job_id varchar2(10));
desc dept80;
alter table dept80
modify (job_id varchar2(20));
alter table dept80
modify (last_name varchar2(20));
alter table dept80
drop column job_id;
desc dept80;
=> pk, fk, uk, ck
alter table 테이블명
add constraint 제약조건명 제약조건유형(컬럼명);
=> pk, fk, uk, ck, nn
alter table 테이블명
modify 컬럼명 constraint 제약조건명 제약조건유형
create table emp2
as
select * from employees;
not null 제약조건만 옮겨짐
제약조건명은 오라클에서 자동으로 부여되는 이름
alter table emp2
add constraint e2_eid_pk primary key(employee_id);
alter table emp2
add constraint e2_sal_ck check(salary>0);
alter table emp2
modify department_id constraint e2_did_fk references departments(department_id);
alter table emp2
modify manager_id constraint e2_mid_fk references emp2(employee_id);
alter table emp2
drop constraint e2_sal_ck;
삭제됨
alter table emp2
drop primary key cascade;
cascade 자식도 같이 삭제
alter table emp2
rename column hire_date to start_date;
alter table emp2
rename constraint sys_c007452 to e2_jid_nn;
rename 테이블명 to 바꿀 테이블명
rename emp2 to emp5;
drop table 테이블명 [purge] ;
실수로 테이블을 삭제한 경우 휴지통(recyclebin)으로부터 되살리는 작업
휴지통 조회
select original_name, operation, droptime from recyclebin;
(==) show recyclebin
휴지통으로부터 테이블 되돌리기
flashback table 테이블명 to before drop;
select original_name, operation, droptime from recyclebin;
show recyclebin;
drop table emp5;
select original_name, operation, droptime from recyclebin;
flashback table emp5 to before drop;
purge 옵션 붙이기
truncate table 테이블명;
(==) delete from 테이블명;
DELETE | TRUNCATE | |
---|---|---|
개념 | 테이블의 특정 행 삭제 (단, WHERE절 생략 시 모든 행 삭제됨) | 테이블의 모든 행 절단(삭제) |
문법 | SQL> delete from 테이블명; | SQL> truncate table 테이블명; |
차이점 | DML(데이터조작어) 저장공간 반납 X ROLLBACK O | DDL(데이터정의어) 저장공간 반납 O ROLLBACK X |
1번
select location_id, street_address, city, state_province, country_name
from locations natural join countries;
2번
select e.last_name, e.job_id, d.department_id, d.department_name
from employees e join departments d
on e.department_id = d.department_id
join locations l
on l.location_id = d.location_id
where l.city = 'Toronto';
3번
select e1.last_name "Employee", e1.employee_id "EMP#", e2. last_name "Manager",
e1.manager_id "Mgr#"
from employees e1 left join employees e2
on e1.manager_id = e2.employee_id
order by EMP#;
4번
select e1.last_name, e1.hire_date, e2.last_name, e2.hire_date
from employees e1 join employees e2
on e1.manager_id = e2.employee_id
where e1.hire_date<e2.hire_date;
5번
select d.department_id, d.department_name, d.location_id, count(e.employee_id)
from employees e right join departments d
on e.department_id = d.department_id
group by d.department_id, d.department_name, d.location_id;