【1】MySQL中的日期時間型別
在MySQL中,date、datetime、time、year和timestamp是常用的時間型別
#資料型別 | 佔用位元組 | 最小值 | #零值表示 | |
---|---|---|---|---|
##date | 4 | 1000-01-01 | 9999-12-31 | |
#datetime | 8 | 1000-01-01 00:00:00 | 9999-12 -31 23:59:59 | |
timestamp | #4 | #19700101080001 | 2038年的某個時刻 | |
time | 3 | -838:59: 59 | 838:59:59 | |
year | 1 | #1901 | 2155 |
詳細解釋
datetime
: 時間日期型,格式是YYYY-mm-dd HH:ii:ss ,表示的範圍是從1000到9999。但有零值,0000-00-00 00:00:00;date
:日期,就是datetime中的date部分;time
:時間(段),指定的某個區間之間,從-時間到時間(有負時間表示);timestamp
:時間戳,並不是常規意義時間戳(如:14253685),範圍是'1970-01-01 00:00:00'到2037年。格式為YYYY-mm-dd HH:ii:ss,格式與datetime完全一致;year
:yy和yyyy,yyyy的範圍是1901-2155,yy的範圍是1970-2069。
兩位year(00-69表示2000-2069,70-99表示1970~1999)。當應用只需要記錄年份時,year比date更省空間
SQL語句實例
create table my_date( d1 datetime, d2 date, d3 time, d4 timestamp, d5 year )charset utf8; desc my_date
如下圖所示:year預設為4位,即YYYY;timestamp不能為空,有預設值,在建立新記錄和修改現有記錄的時候都對這個資料列刷新。
如下分別插入幾個資料並對time做差異分析:
insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','2015'); insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-11:50:54','2015-09-28 11:51:08','2015');-- -11 insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-211:50:54','2015-09-28 11:51:08','2015');-- -2 11 insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','-2 11:50:54','2015-09-28 11:51:08','2015');-- -2过去两天 #year用69标识-2069 insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','69');-- 69 #year用70标识-1970 insert into my_date VALUES('2015-09-28 11:50:36','2015-09-28','11:50:54','2015-09-28 11:51:08','70');-- 70
timestamp欄位
預設只要目前所在的記錄被更新,該欄位一定會自動更新成目前時間。
update my_date set d1 = SYSDATE() where d5=69; select * from my_date
那麼MySQL可以拿到真正的時間戳記嗎?當然可以!
select UNIX_TIMESTAMP();
測試實例
#4.1 查詢目前時間
SELECT SYSDATE() from dual;
#4.2 將目前時間插入以上幾種型別列中
insert INTO `user` (name,number,date,datetime,timestamp,time,year) VALUES ( 'Loum',3,SYSDATE(),SYSDATE(),SYSDATE(),SYSDATE(),2016 );
4.3 mysql中datetime類型的長度位數字
如下所示,通常我們MySQL中設計datetime類型長度都預設為0:
`work_time` datetime(0) DEFAULT NULL COMMENT '清收时间',
常見的時間格式通常是2020-08-29 12:52:16 ,其中插入當前時間。但是如果datetime(n)中的n不為0呢?
`work_time` datetime(2) DEFAULT NULL COMMENT '清收时间', # datetime(n)中的n最大值为6 `work_time` datetime(6) DEFAULT NULL COMMENT '清收时间',
這時在MySQL中會分別顯示如下:
2020-08-29 12:52:16.01
2020-08-29 12:52:16.014057
會發現最後有一個小數點且小數點後面會分別對應對應位數的數字–這稱為奈秒。
總結如下:
date
: 只有日期,沒有時間;#datetime
:有時間,有日期;time
:只有時間,精確到分秒;timestamp
:時間戳,精確到分秒;#year
:年,如2002,如果寫為2002-01-15,將會進行計算,插入結果為1986
【2】日期時間型別格式化
DATE_FORMAT( )函數
可以使用date_format( )函數進行時間的轉換。
SELECT DATE_FORMAT(SYSDATE(),'%Y-%m-%d %H:%i:%s') from dual;
date_format( ) 转换格式
格式 | 描述 |
---|---|
%a | 缩写星期名 |
%b | 缩写月名 |
%c | 月,数值 |
%D | 带有英文前缀的月中的天 |
%d | 月的天,数值(00-31) |
%e | 月的天,数值(0-31) |
%f | 微秒 |
%H | 小时 (00-23) |
%h | 小时 (01-12) |
%I | 小时 (01-12) |
%i | 分钟,数值(00-59) |
%j | 年的天 (001-366) |
%k | 小时 (0-23) |
%l | 小时 (1-12) |
%M | 月名 |
%m | 月,数值(00-12) |
%p | AM 或 PM |
%r | 时间,12-小时(hh:mm:ss AM 或 PM) |
%S | 秒(00-59) |
%s | 秒(00-59) |
%T | 时间, 24-小时 (hh:mm:ss) |
%U | 周 (00-53) 星期日是一周的第一天 |
%u | 周 (00-53) 星期一是一周的第一天 |
%V | 周 (01-53) 星期日是一周的第一天,与 %X 使用 |
%v | 周 (01-53) 星期一是一周的第一天,与 %x 使用 |
%W | 星期名 |
%w | 周的天 (0=星期日, 6=星期六) |
%X | 年,其中的星期日是周的第一天,4 位,与 %V 使用 |
%x | 年,其中的星期一是周的第一天,4 位,与 %v 使用 |
%Y | 年,4 位 |
%y | 年,2 位 |
str_to_date()函数
字符串转换为date:
str_to_date( '2016-12-15 16:48:40', '%Y-%m-%d %H:%i:%S' )
以上是MySQL中的日期時間類型與格式化方式是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

todropaviewInmySQL,使用“ dropviewifexistsview_name;” andTomodifyAview,使用“ createOrreplaceViewViewViewview_nameAsSelect ...”。 whendroppingaview,asew dectivectenciesanduse和showcreateateviewViewview_name;“ tounderStanditSsstructure.whenModifying

mySqlViewScaneFectectialized unizedesignpatternslikeadapter,Decorator,Factory,andObserver.1)adapterPatternadaptSdataForomDifferentTablesIntoAunifiendView.2)decoratorPatternenhancateDataWithCalcalcualdCalcalculenfields.3)fieldfields.3)

查看InMysqlareBeneForsImplifyingComplexqueries,增強安全性,確保dataConsistency,andOptimizingPerformance.1)他們simimplifycomplexqueriesbleiesbyEncapsbyEnculatingThemintoreusableviews.2)viewsEnenenhancesecuritybyControllityByControllingDataAcces.3)

toCreateAsimpleViewInmySQL,USEthecReateaTeviewStatement.1)defitEtheetEtheTeViewWithCreatEaTeviewView_nameas.2)指定usethectstatementTorivedesireddata.3)usethectStatementTorivedesireddata.3)usetheviewlikeatlikeatlikeatlikeatlikeatlikeatable.views.viewssimplplifefifydataaccessandenenanceberity but consisterfort,butconserfort,consoncontorfinft

1)foralocaluser:createUser'localuser'@'@'localhost'Indidendify'securepassword'; 2)foraremoteuser:creationuser's creationuser'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Remoteer'Rocaluser'@'localhost'Indidendify'seceledify'Securepassword'; 2)

mysqlviewshavelimitations:1)他們不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinsOrsubqueries.2)他們canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

porthusermanagementinmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)複雜的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

禪工作室 13.0.1
強大的PHP整合開發環境

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境