공부/TIL

[SQL 첫걸음] 14강 - 16강

Ail_ 2024. 1. 5. 09:03

14강 : 날짜 연산

1. SQL에서의 날짜

DB에 있는 날짜 데이터는 서식 지정 가능

mysql> select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2024-01-05 08:43:19 |
+---------------------+
1 row in set (0.00 sec)

 

2. 날짜의 덧셈과 뺄셈

# 1일 후
mysql> select current_date + interval 1 day;
+-------------------------------+
| current_date + interval 1 day |
+-------------------------------+
| 2024-01-06                    |
+-------------------------------+
1 row in set (0.00 sec)

# 뺄셈
DATEDIFF('2014-02-24', '2024-01-01')

 

 

15강 : CASE 문으로 데이터 변환하기

1. CASE 문

간단한 함수를 작성하는 대신 CASE 문으로 처리 가능

mysql> select a from sample37;
+------+
| a    |
+------+
|    1 |
|    2 |
| NULL |
+------+
3 rows in set (0.01 sec)

# null -> 0
mysql> select a, case when a is null then 0 else a end "a(null=0)" from sample37;
+------+-----------+
| a    | a(null=0) |
+------+-----------+
|    1 |         1 |
|    2 |         2 |
| NULL |         0 |
+------+-----------+
3 rows in set (0.00 sec)

 

COALESCE

null 값 변환 시 사용

인수 여러개 지정 가능

mysql> select a, coalesce(a, 0) from sample37;
+------+----------------+
| a    | coalesce(a, 0) |
+------+----------------+
|    1 |              1 |
|    2 |              2 |
| NULL |              0 |
+------+----------------+
3 rows in set (0.00 sec)

 

2. 또 하나의 CASE 문

디코드 : 숫자인 코드를 알아보기 좋게 문자열로 변환

인코드 : 디코드와 반대로 문자열을 수치화 하는 것

CASE 사용 시 ELSE 생략하면 NULL 반환 -> 생략 지양

WHEN NULL THEN '데이터 없음' -> 데이터 = NULL -> 해당 비교 연산자(=)로는 비교 불가능 -> IS NULL 사용

# 검색 CASE
mysql> select a as '코드',
    -> case
    -> when a = 1 then '남자'
    -> when a = 2 then '여자'
    -> else '미지정'
    -> end as '성별' from sample37;
+------+--------+
| 코드 | 성별   |
+------+--------+
|    1 | 남자   |
|    2 | 여자   |
| NULL | 미지정 |
+------+--------+
3 rows in set (0.00 sec)

# 단순 CASE
mysql> select a as '코드',
    -> case a
    -> when 1 then '남자'
    -> when 2 then '여자'
    -> else '미지정'
    -> end as '성별' from sample37;
+------+--------+
| 코드 | 성별   |
+------+--------+
|    1 | 남자   |
|    2 | 여자   |
| NULL | 미지정 |
+------+--------+
3 rows in set (0.00 sec)

 

 

4장 : 데이터의 추가, 삭제, 갱신

16강 : 행 추가하기 - INSERT

1. INSERT로 행 추가하기

mysql> select * from sample41;
Empty set (0.01 sec)

mysql> desc sample41;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| no    | int         | NO   |     | NULL    |       |
| a     | varchar(30) | YES  |     | NULL    |       |
| b     | date        | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql>  insert into sample41 values(1, 'abc', '2014-02-02');
Query OK, 1 row affected (0.01 sec)

mysql> select * from sample41;
+----+------+------------+
| no | a    | b          |
+----+------+------------+
|  1 | abc  | 2014-02-02 |
+----+------+------------+
1 row in set (0.00 sec)

# 열 지정
mysql> insert into sample41(a, no) values('xyz', 2);
Query OK, 1 row affected (0.00 sec)

mysql> select * from sample41;
+----+------+------------+
| no | a    | b          |
+----+------+------------+
|  1 | abc  | 2014-02-02 |
|  2 | xyz  | NULL       |
+----+------+------------+
2 rows in set (0.00 sec)

# null 추가
mysql> insert into sample41(no, a, b) values(null, null, null);
ERROR 1048 (23000): Column 'no' cannot be null

mysql> insert into sample41(no, a, b) values(3, null, null);
Query OK, 1 row affected (0.00 sec)

mysql> select * from sample41;
+----+------+------------+
| no | a    | b          |
+----+------+------------+
|  1 | abc  | 2014-02-02 |
|  2 | xyz  | NULL       |
|  3 | NULL | NULL       |
+----+------+------------+
3 rows in set (0.00 sec)

 

2. DEFAULT

mysql> desc sample411;
+-------+------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------+------+-----+---------+-------+
| no    | int  | NO   |     | NULL    |       |
| d     | int  | YES  |     | 0       |       |
+-------+------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> insert into sample411(no, d) values(1, 1);
Query OK, 1 row affected (0.01 sec)

mysql> select * from sample411;
+----+------+
| no | d    |
+----+------+
|  1 |    1 |
+----+------+
1 row in set (0.00 sec)

mysql> insert into sample411(no, d) values(2, default);
Query OK, 1 row affected (0.00 sec)

mysql> select * from sample411;
+----+------+
| no | d    |
+----+------+
|  1 |    1 |
|  2 |    0 |
+----+------+
2 rows in set (0.00 sec)

# 암묵적으로 생략 시 default 저장
mysql> insert into sample411(no) values(3);
Query OK, 1 row affected (0.00 sec)

mysql> select * from sample411;
+----+------+
| no | d    |
+----+------+
|  1 |    1 |
|  2 |    0 |
|  3 |    0 |
+----+------+
3 rows in set (0.00 sec)