DDL : Data Definition Language
1. create
CREATE DATABASE db_name;
CREATE TABLE table_name(
column_name type constraint
);
2. alter
ALTER TABLE persons ADD email VARCHAR(255);
ALTER TABLE emp_sub ADD deptno int DEFAULT 10;
ALTER TABLE tcons ADD CONSTRAINT tcons_no_pk PRIMARY KEY(NO);
ALTER TABLE persons MODIFY COLUMN city VARCHAR(255);
ALTER TABLE emp_sub RENAME COLUMN deptno TO dcode;
ALTER TABLE persons DROP COLUMN email;
3. drop
DROP DATABASE db_name;
DROP TABLE table_name;
4. truncate
SELECT * FROM emp_10;
TRUNCATE TABLE emp_10;
DML : Data Manipulation Language
1. insert
INSERT INTO user VALUES('park', '박길동');
INSERT INTO user (id, NAME) VALUES ('kong', '공길동');
INSERT INTO user (NAME, id) VALUES ('공길동', 'kong');
INSERT INTO emp_sub (id, NAME)
SELECT empno, ename FROM emp WHERE deptno = 10;
2. delete
DELETE FROM emp WHERE ename = 'hong';
3. update
UPDATE emp SET job = 'CLERK', mgr = 7782 WHERE ename='hong';
4. select
DCL : Data Controll Language
- grant
CREATE user kosta IDENTIFIED BY '1234';
ALTER user kosta IDENTIFIED BY '2345';
DROP user kosta;
GRANT SELECT,INSERT, UPDATE ON kotest.* TO kosta;
GRANT ALL PRIVILEGES ON kotest.* TO kosta;
grant ALL PRIVILEGES ON *.* TO kosta;
- revoke
REVOKE SELECT, INSERT, UPDATE ON kotest.* FROM 'kosta';
REVOKE UPDATE ON kotest.* FROM 'kosta';
REVOKE ALL PRIVILEGES ON *.* from 'kosta';
- roll
CREATE ROLL roll_name;
GRANT 권한 TO roll_name;
GRANT roll_name TO 사용자;
TCL : Transaction Controll Language
- commit : 변경사항 확정, 반영
- rollback : 변경사항 취소
- savepoint : rollback 범위 지정
START TRANSACTION;
DELETE FROM emp WHERE deptno = 40;
UPDATE emp SET job = 'CLERK', mgr = 7782 WHERE ename='hong';
SAVEPOINT S1;
UPDATE emp SET comm = 100 WHERE comm IS NULL OR comm = 0;
ROLLBACK TO S1;
UPDATE emp SET comm = comm+sal*0.1 WHERE deptno = 10;
COMMIT;
DROP vs. TRUNCATE vs. DELETE
DROP | TRUNCATE | DELETE |
---|
DDL | DDL/DML | DML |
Auto-Commit / Rollback 안 됨 | Auto-Commit / Rollback 안 됨 | User-Commit / Rollback 가능 |
log 남지 않음 | log 남지 않음 | log 남음 |
테이블의 정의 삭제 | 최초의 상태, 디스크 초기화, 테이블 재사용 | 데이터만 삭제, 용량 유지 |