관리 메뉴

ruriruriya

[DB] λ°μ΄ν„°λ² μ΄μŠ€ MySQL - λ‚ μ§œ 데이터 μ €μž₯ν•˜λŠ” 데이터 νƒ€μž…(DATE, TIME, DATETIME,TIMESTAMP) λ³Έλ¬Έ

πŸ›’οΈDB/MySQL

[DB] λ°μ΄ν„°λ² μ΄μŠ€ MySQL - λ‚ μ§œ 데이터 μ €μž₯ν•˜λŠ” 데이터 νƒ€μž…(DATE, TIME, DATETIME,TIMESTAMP)

루리야ㅑ 2023. 12. 1. 14:05
λ°˜μ‘ν˜•

MySQLμ—μ„œλŠ” λ‚ μ§œμ™€ μ‹œκ°„μ„ μ €μž₯ν•˜κΈ° μœ„ν•΄ λ‹€μ–‘ν•œ 데이터 νƒ€μž…μ„ μ œκ³΅ν•œλ‹€.

 

  • DATE: λ‚ μ§œ(λ…„, μ›”, 일)λ§Œμ„ μ €μž₯ν•œλ‹€.
    • μ˜ˆμ‹œ: 'YYYY-MM-DD' (예: '2023-12-01')
  • TIME: μ‹œκ°„(μ‹œ, λΆ„, 초)λ§Œμ„ μ €μž₯ν•œλ‹€.
    • μ˜ˆμ‹œ: 'HH:MM:SS' (예: '15:30:00')
  • DATETIME: λ‚ μ§œμ™€ μ‹œκ°„μ„ ν•¨κ»˜ μ €μž₯ν•œλ‹€.
    • μ˜ˆμ‹œ: 'YYYY-MM-DD HH:MM:SS' (예: '2023-12-01 15:30:00')
  • TIMESTAMP: λ‚ μ§œμ™€ μ‹œκ°„μ„ μ €μž₯ν•˜λ©°, 데이터가 μ‚½μž…λ˜κ±°λ‚˜ μ—…λ°μ΄νŠΈλœ μ‹œκ°„μ„ κΈ°λ‘ν•˜λŠ” 데 μ‚¬μš©λœλ‹€. TIMESTAMPλŠ” νŠΉμ • μ‹œκ°„λŒ€λ‘œ ν‘œν˜„λœλ‹€.
    • μ˜ˆμ‹œ: 'YYYY-MM-DD HH:MM:SS' (예: '2023-12-01 15:30:00')

 

1. ν…Œμ΄λΈ” λ§Œλ“€κ³  컬럼 λ°μ΄ν„°νƒ€μž… μ„€μ •ν•˜κΈ°

μƒˆ ν…Œμ΄λΈ”μ„ λ§Œλ“€κ³  컬럼 λ§Œλ“€λ©΄μ„œ 데이터 νƒ€μž…λ„ μ„€μ •ν•œλ‹€.

  • id : INT(PK, NN, UN, AI 체크)
  • name : VARCHAR(10)
  • birthdate : DATE
  • birthtime : TIME
  • birthdt : DATETIME

 

2. ν…Œμ΄λΈ”μ— κ°’ λ„£κΈ°

insert into ν‚€μ›Œλ“œλ‘œ ν…Œμ΄λΈ”μ— λ‚ μ§œμ™€ μ‹œκ°„μ„ λ„£μ–΄λ³΄μž!
λ¨Όμ € 컬럼λͺ…을 κ΄„ν˜Έ μ•ˆμ— λ„£κ³ 
κ·Έ λ‹€μŒμ—” μ»¬λŸΌμˆœμ„œλŒ€λ‘œ 값을 λ„£λŠ”λ‹€.

그리고 select  ~ from ν‚€μ›Œλ“œλ‘œ κ°€μ Έμ™€μ„œ 확인해보기!

insert into people
(name, birthdate, birthtime, birthdt)
values
('κΉ€λ‚˜λ‚˜', '1990-11-11', '10:07:35','1990-11-11 10:07:35'),
('홍길동', '1980-12-25', '04:10:42','1980-12-25 04:10:42');

select *
from people;

 

3. λ‚ μ§œ, μ‹œκ°„ μ •λ³΄λ§Œ κ°€μ Έμ˜€κΈ°

- day() ν•¨μˆ˜ : λ‚ μ§œ 값이 λ“€μ–΄ μžˆλŠ” μ»¬λŸΌμ„ λ„£μ–΄ μ‹€ν–‰ν•˜λ©΄ 일만 κ°€μ Έμ˜¨λ‹€.

select * , day(birthdate)
from people;

- dayname() ν•¨μˆ˜ : λ‚ μ§œ 값이 λ“€μ–΄ μžˆλŠ” μ»¬λŸΌμ„ λ„£μ–΄ μ‹€ν–‰ν•˜λ©΄ ν•΄λ‹Ή μš”μΌ κ°€μ Έμ˜¨λ‹€.

select * , dayname(birthdate)
from people;

- dayofweek ν•¨μˆ˜ : κ·Έ λ‹¬μ˜ λͺ‡ 번 μ§Έ 주인지 μ•Œλ €μ€€λ‹€.

select * , dayofweek(birthdate)
from people;

- dayofyear() ν•¨μˆ˜ : ν•΄λ‹Ή μ—°κ°„ μ§€λ‚œ λ‚ μ§œλ₯Ό μ„Έμ–΄μ€€λ‹€.

select * , dayofyear(birthdate)
from people;

- month() ν•¨μˆ˜ : ν•΄λ‹Ή μ›”λ§Œ λ”°λ‘œ 좜λ ₯ν•΄μ€€λ‹€.

select * , month(birthdate)
from people;

- hour(), minute(), second() ν•¨μˆ˜ : μ‹œκ°„, λΆ„, 초 좜λ ₯ν•΄μ€€λ‹€.

select * , hour(birthtime), minute(birthtime), second(birthtime)
from people;

4. now() ν•¨μˆ˜

ν˜„μž¬μ‹œκ°„μ„ κ°€μ Έμ˜€κ³  μ‹Άμ„ λ•Œ

select now();

5. curdate() ν•¨μˆ˜

ν˜„μž¬ λ…„μ›”μΌλ§Œ κ°€μ Έμ˜€κ³  μ‹Άμ„ λ•Œ

select curdate();

6. curtime() ν•¨μˆ˜

ν˜„μž¬ μ‹œλΆ„μ΄ˆλ§Œ κ°€μ Έμ˜€κ³  μ‹Άμ„ λ•Œ,
SQL λ‚΄μ—μ„œ ν˜„μž¬ μ‹œκ°„μ˜ 기쀀은 κΈ€λ‘œλ²Œ ν‘œμ€€ μ‹œκ°„μ΄λ‹€.
κ·Έλž˜μ„œ ν•œκ΅­μ€ κ·Έ μ‹œκ°„μ— 9μ‹œκ°„μ„ 더 더해쀀닀 생각해야 ν•œλ‹€.

select curtime();

7. datediff() ν•¨μˆ˜

μ§€λ‚œ λ‚ μ§œ μ„ΈλŠ” ν•¨μˆ˜

-- now() ν˜„μž¬ μ‹œκ°„μ—μ„œ μž…λ ₯ν•΄μ€€ birthdt μ»¬λŸΌμ˜ κ°’을 κΈ°μ€€μœΌλ‘œ λ‚ μ§œ(일)λ₯Ό μ„Έμ€€λ‹€.

select *, datediff(now(), birthdt) 
from people;

8. date_add() ν•¨μˆ˜

λ‚ μ§œ, μ‹œκ°„ 등을 λ”ν•΄μ£ΌλŠ” ν•¨μˆ˜.

select * , date_add(birthdt, interval 100 day)
from people;

select * , date_add(birthdt, interval 100 hour)
from people;


-- ν•¨μˆ˜ 없이 λ”ν•˜λŠ” 방법
select *, birthdt + interval 100 day
from people;

100일 λ”ν•˜κΈ°
100μ‹œκ°„ λ”ν•˜κΈ°

9. date_sub() ν•¨μˆ˜

λ‚ μ§œ, μ‹œκ°„ 등을 λΉΌμ£ΌλŠ” ν•¨μˆ˜.

select * , date_sub(birthdt, interval 100 day)
from people;

select * , date_sub(birthdt, interval 100 hour)
from people;


-- ν•¨μˆ˜ 없이 λΉΌλŠ” 방법
select *, birthdt - interval 100 hour
from people;

100일 빼기
100μ‹œκ°„ λΉΌκΈ°

10. λ‚ μ§œμ™€ μ‹œκ°„ μ—°μ‚°

λ‚ μ§œμ™€ μ‹œκ°„μ€ 연산이 κ°€λŠ₯ν•˜λ‹€.

select *, birthdt + interval 100 day - interval 2 month
from people;

 

이듀 각각은 λ‹€μ–‘ν•œ ν˜•μ‹μ˜ λ‚ μ§œμ™€ μ‹œκ°„μ„ μ €μž₯ν•  수 있으며, 각각의 νŠΉμ§•μ— 맞게 μ‚¬μš©λœλ‹€.
DATEλŠ” 일반적으둜 생일, 이벀트 λ‚ μ§œ λ“± λ‚ μ§œ 정보가 ν•„μš”ν•œ κ²½μš°μ— μ‚¬μš©λ˜κ³ ,
TIME은 νŠΉμ • 이벀트 μ‹œκ°„μ΄λ‚˜ μž‘μ—… μ‹œκ°„ 등을 μ €μž₯ν•˜λŠ” 데 μ‚¬μš©λœλ‹€.
DATETIME은 λ‚ μ§œμ™€ μ‹œκ°„μ΄ λͺ¨λ‘ ν•„μš”ν•œ κ²½μš°μ— μ‚¬μš©λ˜λ©°,
TIMESTAMPλŠ” 주둜 데이터가 λ³€κ²½λœ μ‹œκ°„μ„ μΆ”μ ν•˜λŠ” 데 μ‚¬μš©λœλ‹€.

각 데이터 νƒ€μž…μ€ λ°μ΄ν„°λ² μ΄μŠ€ μ„€κ³„μ—μ„œ λ‚ μ§œ 및 μ‹œκ°„ 정보λ₯Ό μ μ ˆν•˜κ²Œ μ €μž₯ν•˜κ³  κ²€μƒ‰ν•˜κΈ° μœ„ν•΄ μ„ νƒλ˜μ–΄μ•Ό ν•œλ‹€.

λ°˜μ‘ν˜•