mysql資料庫 幾個時間儲存型別

2021-08-03 21:40:06 字數 2707 閱讀 8084

一、timestamp

顯示格式:yyyy-mm-dd hh:mm:ss

時間範圍:[ 『1970-01-01 00:00:00』到』2037-12-31 23:59:59』]

timestamp default current_timestamp on update current_timestamp 在建立新記錄和修改現有記錄的時候都對這個資料列重新整理。

timestamp default current_timestamp 在建立新記錄的時候把這個字段設定為當前時間,但以後修改時,不再重新整理它。

timestamp on update current_timestamp 在建立新記錄的時候把這個字段設定為0,以後修改時重新整理它。

timestamp default 『yyyy-mm-dd hh:mm:ss』 on update current_timestamp 在建立新記錄的時候把這個字段設定為給定值,以後修改時重新整理它

1、timestamp列不為空時,預設值可以為「0000-00-00 00:00:00」,但不能為null。

2、乙個表可以存在多個timestamp列,但乙個表只有乙個timestamp型別的字段可以在預設值或者update部分用current_timestamp,即設定為資料更新而改變為資料庫系統當前值。

3、timestamp列的預設值是current_timestamp常量值。當紀錄資料發生變化的時候,timestamp列會自動將其值設定為current_timestamp。

4、timestamp列建立後的格式是:

alter

table

`course`

addcolumn

`birthday`

timestamp

notnull

default

current_timestamp

onupdate

current_timestamp;

alter

table

`course`

addcolumn

`birthday`

timestamp

notnull

default

'0000-00-00 00:00:00'

onupdate

current_timestamp ;

alter

table

`course`

addcolumn

`birthday`

timestamp

null

after

`cname`;

二、datetime

顯示格式:yyyy-mm-dd hh:mm:ss

時間範圍:[ 『1000-01-01 00:00:00』到』9999-12-31 23:59:59』]

三、date

顯示格式:yyyy-mm-dd

時間範圍:[『1000-01-01』到』9999-12-31』]

四、日期格式轉換

1、字串轉日期

select str_to_date('2013-01-29 13:49:18', '%y-%m-%d %h:%i:%s')
2、日期轉字串

select date_format('2013-01-29 13:49:18', '%y-%m-%d %h:%i:%s')
五、日期的中常用的年月日時分秒星期月份等獲取方法

select

timestamp('2013-01-29 13:50:27');

select

date('2013-01-29 13:50:27');

select

year('2013-01-29 13:50:27');

select

month(('2013-01-29 13:50:27');

select week('2013-01-29 13:50:27');

select

day('2013-01-29 13:50:27');

select

time('2013-01-29 13:50:27');

select curtime();

select curdate();

select

current_date;

select

current_time;

select

current_timestamp;

select now()

六、日期的運算:

select date_add('2013-01-29 13:50:27', interval 1

day);

-> '2013-01-30 13:50:27'

select date_add('2013-01-29 13:50:27', interval 1

hour);

-> '2013-01-29 14:50:27'

select date_add('2013-01-29 13:50:27', interval 1

month);

-> '2013-02-28 13:50:27'

資料庫如何儲存時間

主要會有下面兩個問題 字串占用的空間更大 字串儲存的日期比較效率比較低 逐個字元進行比對 無法用日期相關的 api 進行計算和比較。datetime 和 timestamp 是 mysql 提供的兩種比較相似的儲存時間的資料型別。通常我們都會首選 timestamp。原因如下 2.1 datetim...

mysql資料庫類 MySQL資料庫類的定義

俗話說 好的開始是成功的一半 而php mysql專案中資料庫的操作是重點之一,能否簡化資料庫操作程式的編寫,就成了影響工作效率的關鍵之一。所以小陽並不是一開始就做頁面,而是先建立乙個 dbclass.php 檔案,開始編寫操作mysql資料庫的類 dbclass 即在 dbclass.php 中編...

mysql資料庫時間型別

mysql時間型別 日期時間 datetime 範圍 1000 01 01 00 00 00 9999 12 31 23 59 59 格式 yyyymmddhhmmss 日期時間 timestamp 範圍 1970 01 01 00 00 00 2038 01 19 00 00 00 格式 yyyy...