設置mysql日期時間類型字段默認爲當前時間

  1. 直接在創建表時添加該列並聲明默認值,如下
CREATE TABLE `table1` (
  `id` int(11) NOT NULL,
  `createtime` timestamp NULL default CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. 在現有表中添加新列
ALTER TABLE table1
ADD COLUMN  `createtime` timestamp NULL DEFAULT CURRENT_TIMESTAMP
  1. 修改某一列爲時間格式並添加默認值
alter table table1 
 change createtime newtime timestamp null default current_timestamp
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章