Skip to content

Commit

Permalink
SQL์˜ DDL, DML, DCL, TCL
Browse files Browse the repository at this point in the history
  • Loading branch information
SeYun committed Feb 4, 2021
1 parent 5ee560d commit 125eb38
Showing 1 changed file with 160 additions and 0 deletions.
160 changes: 160 additions & 0 deletions SeYunKim/2021-02-04-sql_purpose_kinds.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# SQL์˜ DDL, DML, DCL, TCL

## ์„œ๋ก 

- SQL(Structured Query Language)๋ž€ ๊ตฌ์กฐ์  ์งˆ์˜ ์–ธ์–ด๋ผ๋Š” ์˜๋ฏธ๋กœ ์งˆ์˜ ์–ธ์–ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ œ์–ด, ๊ด€๋ฆฌ ํ•  ์ˆ˜ ์žˆ๋‹ค.
- SQL์€ DDL, DML, DCL๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
- ๊ฐ ์–ธ์–ด์˜ ํŠน์ง•์„ ์‚ดํŽด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
- ํ•ด๋‹น ๋ชจ๋“  ํ…Œ์ŠคํŠธ๋Š” MySQL 8.0.17 ํ™˜๊ฒฝ์—์„œ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

## DDL(Data Definition Language) - ๋ฐ์ดํ„ฐ ์ •์˜ ์–ธ์–ด

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ์–ธ์–ด๋กœ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ƒ์„ฑ, ์ˆ˜์ •, ์‚ญ์ œ ๋“ฑ์˜ ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด

### CREATE

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์„ ๋งŒ๋“œ๋Š” ์—ญํ• 
- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ƒ์„ฑ

```sql
CREATE database test
```

![sql_purpose_kinds-1](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-1.png?raw=true)

- ํ…Œ์ด๋ธ” ์ƒ์„ฑ

```sql
CREATE table user (
user_id int not null,
name char(16) not null,
email char(16) not null,
primary key(user_id)
);
```

![sql_purpose_kinds-2](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-2.png?raw=true)

### ALTER

- ํ…Œ์ด๋ธ”์„ ์ˆ˜์ •ํ•˜๋Š” ์—ญํ• 
- ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์„ ์ถ”๊ฐ€ํ•˜๋Š” SQL

```sql
ALTER TABLE user ADD age int;
```

![sql_purpose_kinds-3](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-3.png?raw=true)

### DROP

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์„ ์‚ญ์ œํ•˜๋Š” ์—ญํ• 
- ํ…Œ์ด๋ธ” ์‚ญ์ œํ•˜๋Š” SQL

```sql
DROP TABLE user;
```

![sql_purpose_kinds-4](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-4.png?raw=true)

### TRUNCATE

- ํ…Œ์ด๋ธ”์„ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ์—ญํ• 

## DML(Data Manipulation Language) - ๋ฐ์ดํ„ฐ ์กฐ์ž‘ ์–ธ์–ด

- ์ •์˜๋œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ž…๋ ฅ๋œ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…,์กฐํšŒ,์ˆ˜์ •,์‚ญ์ œ๋ฅผ ํ•˜๋Š” ์–ธ์–ด

### SELECT

- ๋ ˆ์ฝ”๋“œ๋ฅผ ์กฐํšŒํ•˜๋Š” ์—ญํ• 
- ๋ ˆ์ฝ”๋“œ ์ „์ฒด ์กฐํšŒ SQL

```sql
SELECT * FROM user
```

![sql_purpose_kinds-2](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-2.png?raw=true)

### INSERT

- ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ์—ญํ• 
- ํ•œ๊ฐœ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” SQL

```sql
INSERT INTO user(user_id, name, email) VALUES (1, 'rutgo', '[email protected]');
```

![sql_purpose_kinds-5](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-5.png?raw=true)

### UPDATE

- ๋ ˆ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•˜๋Š” ์—ญํ• 
- email์„ ๋ณ€๊ฒฝํ•˜๋Š” SQL

```sql
UPDATE user
SET email='[email protected]'
WHERE user_id = 1;
```

![sql_purpose_kinds-6](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-6.png?raw=true)

### DELETE

- ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์—ญํ• 

```sql
DELETE FROM user
WHERE user_id = 1;
```

![sql_purpose_kinds-7](https://github.com/ksy90101/TIL/blob/master/database/image/sql_purpose_kinds-7.png?raw=true)

## DCL(Data Control Language) - ๋ฐ์ดํ„ฐ ์ œ์–ด ์–ธ์–ด

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘๊ทผํ•˜๊ฑฐ๋‚˜ ๊ฐ์ฒด์— ๊ถŒํ•œ์„ ์ฃผ๋Š” ์—ญํ• ์„ ํ•˜๋Š” ์–ธ์–ด

### GRANT

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ถŒํ•œ์„ ์ฃผ๋Š” ์—ญํ• 

### REVOKE

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์‚ฌ์šฉ์ž์—๊ฒŒ ๊ถŒํ•œ์„ ๋ฐ•ํƒˆํ•˜๋Š” ์—ญํ• 

### COMMIT

- ํŠธ๋žœ์žญ์…˜์˜ ์ž‘์—…์ด ์ •์ƒ์ ์ด๋ผ๋ฉด ๋ฐ˜์˜ํ•˜๋Š” ์—ญํ• 

### ROLLBACK

- ํŠธ๋žœ์žญ์…˜ ์ž‘์—…์ด ๋น„์ •์ƒ์ ์ด๋ผ๋ฉด ๋‹ค์‹œ ๋˜๋Œ๋ฆฌ๋Š” ์—ญํ• 

### SET TRANSACTION

- ํŠธ๋žœ์žญ์…˜ ๋ชจ๋“œ ์„ค์ •ํ•˜๋Š” ์—ญํ• 
- ์˜ˆ๋ฅผ๋“ค๋ฉด ํŠธ๋žœ์žญ์…˜ ๊ฒฉ๋ฆฌ ์ˆ˜์ค€๋“ฑ์„ ์„ค์ •ํ•œ๋‹ค

### BEGIN

- ํŠธ๋žœ์žญ์…˜ ์‹œ์‚ญ

### SAVEPOINT

- ์ €์žฅ ์ง€์ ์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ž„์‹œ ์ €์žฅ์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹์„๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค.

### LOCK

- ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋ฆฌ์†Œ์Šค ์ ‘๊ทผ ์ œํ•œ์„ ํ•˜๊ธฐ ์œ„ํ•œ ๋ช…๋ น์–ด

## TCL(Transaction Control Language)

- DCL์—์„œ COMMIT๊ณผ ROLLBACK, SAVEPONT์„ ์ง€์นญํ•œ๋‹ค.
- ์ฆ‰, ํŠธ๋žœ์žญ์…˜์„ ์ œ์–ดํ•˜๋Š” ์–ธ์–ด์ด๋‹ค.

## ๊ฒฐ๋ก 

- DDL์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค, ํ…Œ์ด๋ธ”์„ ์ƒ์„ฑ,๋ณ€๊ฒฝ,์ œ๊ฑฐ๋ฅผ ํ•˜๊ณ 
- DML์„ ํ†ตํ•ด ๋ ˆ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑ,๋ณ€๊ฒฝ,์กฐํšŒ,์ œ๊ฑฐ๋ฅผ ํ•˜๊ณ 
- DCL์„ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ ‘๊ทผ ๊ถŒํ•œ์ด๋‚˜ ์ž‘์—… ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜์˜, ๋ฆฌ์…‹์„ ์ง„ํ–‰ํ•œ๋‹ค.

0 comments on commit 125eb38

Please sign in to comment.