1. transaction
데이터베이스에서 데이터 처리의 한 단위를 트랜젝션이라고 부른다.
대부분의 데이터베이스는 데이터를 저장, 수정, 삭제하는 등의 작업을 물리적인 하드디스크에 저장된 데이터에 반영하지 않는다.
개발자의 실수로 잘못된 명령문을 실행하였을 때 되돌릴 수 있어야 하기 때문이다.
이러한 안전장치로 커밋이라는 절차가 존재하며, 커밋이라는 작업 전에는 실행한 명령문이 메모리 상에서만 적용되고 물리적인 하드디스크에 저장된 데이터에는 반영되지 않는다.
데이터 작업을 위한 명령문 입력부터 커밋까지를 하나의 트랜젝션이라고 부른다.
2. rollback
데이터 저장, 수정, 삭제 등의 작업을 하고 난 뒤, 원래의 데이터로 되돌리는 작업을 의미한다.
커밋 후에는 rollback 작업을 해도 원래의 데이터로 돌아오지 않는다.
주의할 점은 workbench 등의 프로그램에서는 자동으로 커밋을 실행하기 때문에 rollback을 해도 되돌아오지 않는다.
별도로 자동 커밋 옵션을 해제 해줘야 rollback이 정상적으로 작동한다.
use test_db;
select * from test_table2;
delete from test_table2;
select * from test_table2;
rollback;
select * from test_table2;
3. commit
하나의 트랜젝션을 물리적인 데이터베이스에 적용하는 작업이다.
이 작업을 실행하면 rollback을 해도 데이터가 되돌아오지 않는다.
-- rollback 가능
delete from test_table2;
select * from test_table2;
rollback;
select * from test_table2;
-- rollback 불가능
delete from test_table2;
commit;
select * from test_table2;
rollback;
select * from test_table2;
savepoint를 통해 rollback할 수 있는 지점을 지정해줄 수 있다.
insert into test_table2 (data1, data2, data3) values (100, '문자열1', 11.11);
insert into test_table2 (data1, data2, data3) values (200, '문자열2', 22.22);
insert into test_table2 (data1, data2, data3) values (300, '문자열3', 33.33);
commit;
select * from test_table2;
update test_table2 set data2 = '새로운문자열', data3 = 44.44 where data1 = 100; -- 되돌아오는 데이터 상태
savepoint aa;
select * from test_table2;
delete from test_table2 where data1 = 100;
select * from test_table2;
rollback to aa;
select * from test_table2;
4. truncate
truncate는 지정된 테이블의 모든 로우를 삭제한다.
delete는 rollback이 가능하지만, truncate는 rollback이 불가능하다.
-- rollback 가능
delete from test_table2;
select * from test_table2;
rollback;
select * from test_table2;
-- rollback 불가능
truncate test_table2;
rollback;
select * from test_table2;
5. 테이블변경
아래의 구문을 통해 테이블의 이름, 속성 등을 변경할 수 있다.
- rename table old_name to new_name: 테이블명 변경
- alter table table_name modify column type: 컬럼의 타입 등 변경
- alter table table_name change old_column new_column type: 컬럼의 이름과 속성을 변경, 속성을 유지하고 싶을 때는 기존 속성을 그대로 작성
- alter table table_name add new_column type: 컬럼 추가
- alter table table_name drop column_name: 컬럼 삭제
- drop table table_name: 테이블 삭제
use test_db;
show tables;
rename table test_table1 to test_table3;
show tables;
alter table test_table3 modify data1 float;
desc test_table3;
alter table test_table3 modify data1 int;
alter table test_table3 change data1 data10 int;
desc test_table3;
alter table test_table3 add data4 int;
desc test_table3;
alter table test_table3 drop data4;
desc test_table3;
drop table test_table3;
'DataBase > MySQL' 카테고리의 다른 글
sequence, limit (0) | 2021.08.10 |
---|---|
constraint (0) | 2021.08.09 |
데이터베이스 관리 (0) | 2021.08.06 |
set (0) | 2021.08.04 |
서브 쿼리문 (0) | 2021.08.03 |