people2 테이블

DATE는 날짜 TIME은 시간 DATETIME은 날짜와 시각이다. 아래와 같이 임의의 데이터와 현재 날짜와 시간을 받아오는 함수를 통해 insert 한다.

insert into people2
(name, birthdate, birthtime, birthdt)
values
('Padma', '1988-11-11', '10:07:35', '1988-11-11 10:07:35'),
('Larry', '1994-04-22', '04:10:42', '1994-04-22 04:10:42');

insert into people2
(name, birthdate, birthtime, birthdt)
values
('Harry', curdate(), curtime(), now());

select *
from people2;

 

날짜와 시간에서 특정 년도나 몇시인지 아래처럼 불러올 수 있다.

select name, year(birthdate)
from people2;

select name, month(birthdate)
from people2;

select name, day(birthdate)
from people2;

select name, dayofweek(birthdate)
from people2;

select name, dayname(birthdate)
from people2;

select name, hour(birthtime)
from people2;

select name, minute(birthtime)
from people2;

select name, second(birthtime)
from people2;

 

아래는 날짜 포맷과 시간 계산방법이다.

-- 2000-11-11 03:50 에 태어났습니다.
select date_format(birthdt, '%Y-%m-%d %H:%i 에 태어났습니다.')
from people2;

-- birthdate 컬럼과 현재시간의 차이를 가져오세요
select datediff(now(), birthdate)
from people2;

-- birthdate에 36일 후는??
select date_add(birthdate, interval 36 day);

 

아래처럼 데이터를 insert하면 created_at에 timestmap로 현재시간이 저장된다.

timestamp는 1970년 1월 1일 자정을 0으로 시작해서(정확하게 표현하면 00:00:00 UTC on January 1, 1970) ~

특정 시점까지 얼마나 많은 초(second) 단위의 시간이 지났는지를 통해 특정 시점을 표기하는 방식이다.

create table comments (
	id int unsigned not null auto_increment primary key,
    content varchar(100),
    created_at timestamp default now()
);

insert into comments
(content)
values
('사과 진짜 맛있나요??????????');

+ Recent posts