Channi Studies

[MySQL] Day 5. CURRENT_DATE() & CURRENT_TIME() 본문

SQL

[MySQL] Day 5. CURRENT_DATE() & CURRENT_TIME()

Chan Lee 2025. 3. 28. 15:41

Let's starty by creating a temporary database for this post. 

CREATE DATABASE test(
    my_date DATE,
    my_time TIME,
    my_datetime DATETIME
);

SELECT * from test;

test table

 

To get current date, time, or date time, there are 3 built-in functions, CURRENT_DATE(), CURRENT_TIME, and NOW()

Let's try inserting a new row of appropriate data obtained by these functions.

 

INSERT INTO test
VALUES  (CURRENT_DATE(), CURRENT_TIME, NOW());

SELECT * FROM test;

 

When we add or subtract an integer from DATE, than it will change the date accordingly.

Same for TIME and DATETIME, each integer added or subtracted will change the time in seconds. 

 

However, the date and time does not automatically updates when it's over the maximum dates in the month or over 60 seconds. 

Any attempt of adding an invalid time or date will result in an error. (ex. 2021-21-42, 2025-03-40, 30:01:21, 01:82: 99, 3000-01-32 29:92:02)

 

For DATE, you have to add 100 for + 1 month, and add 10000 for + 1 year. 

For TIME and DATETIME, you have to add 100 for +1 minute, add 10000 for +1 hour.