這個(gè)要建立一個(gè)作業(yè)啊

成都創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),旅順口企業(yè)網(wǎng)站建設(shè),旅順口品牌網(wǎng)站建設(shè),網(wǎng)站定制,旅順口網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,旅順口網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
作業(yè)運(yùn)行時(shí)間放在月初,sql腳本如下所示:
---sqlserver?
declare?@name?varchar(10)
set?@name?=?convert(varchar(2),year(getdate()))+convert(varchar(2),month(getdate()))
if?not?exists(select?null?from?sysobjects?where?xtype='u'?and?name?=@name?)
begin
---建表
exec?
('
create?table?'+@name+'?(
列名?...
)
');
end?
go
試一試,如有疑問,及時(shí)溝通!
mysql(5.5)所支持的日期時(shí)間類型有:DATETIME、?TIMESTAMP、DATE、TIME、YEAR。
1.DATETIME 用于表示 年月日 時(shí)分秒,是 DATE 和 TIME 的組合,并且記錄的年份比較長久。如果實(shí)際應(yīng)用中有這樣的需求,就可以使用 DATETIME 類型。
2.TIMESTAMP
TIMESTAMP 用于表示 年月日 時(shí)分秒,但是記錄的年份比較短暫。
TIMESTAMP 和時(shí)區(qū)相關(guān),更能反映當(dāng)前時(shí)間。當(dāng)插入日期時(shí),會(huì)先轉(zhuǎn)換為本地時(shí)區(qū)后再存放;當(dāng)查詢?nèi)掌跁r(shí),會(huì)將日期轉(zhuǎn)換為本地時(shí)區(qū)后再顯示。所以不同時(shí)區(qū)的人看到的同一時(shí)間是? 不一樣的。
表中的第一個(gè)?TIMESTAMP?列自動(dòng)設(shè)置為系統(tǒng)時(shí)間(CURRENT_TIMESTAMP)。當(dāng)插入或更新一行,但沒有明確給 TIMESTAMP 列賦值,也會(huì)自動(dòng)設(shè)置為當(dāng)前系統(tǒng)時(shí)間。如果表中有第二個(gè) TIMESTAMP 列,則默認(rèn)值設(shè)置為0000-00-00 00:00:00。
TIMESTAMP 的屬性受 Mysql 版本和服務(wù)器 SQLMode 的影響較大。
如果記錄的日期需要讓不同時(shí)區(qū)的人使用,最好使用 TIMESTAMP。
3.DATE
DATE 用于表示 年月日,如果實(shí)際應(yīng)用值需要保存 年月日 就可以使用 DATE。
4.TIME
TIME 用于表示 時(shí)分秒,如果實(shí)際應(yīng)用值需要保存 時(shí)分秒 就可以使用 TIME。
5.YEAR
YEAR 用于表示 年份,YEAR 有 2 位(最好使用4位)和 4 位格式的年。 默認(rèn)是4位。如果實(shí)際應(yīng)用只保存年份,那么用 1 bytes 保存 YEAR 類型完全可以。不但能夠節(jié)約存儲(chǔ)空間,還能提高表的操作效率。
資料拓展:
每種日期時(shí)間類型都有一個(gè)有效值范圍,如果超出這個(gè)范圍,在默認(rèn)的SQLMode下會(huì)報(bào)錯(cuò),并以零值存儲(chǔ)。
插入或更新時(shí),日期時(shí)間類型允許“不嚴(yán)格”語法,以DATETIME為例(其他日期時(shí)間類型雷同):
YYYY-MM-DD HH:MM:SS?或?YY-MM-DD HH:MM:SS 格式的字符串。任何符號(hào)都可以用作日期部分或時(shí)間部分的間隔符。例如:“14-06-18 14:54:10”、“14*06*18 14.54.10”、“14+06+18 14=54=10”是等價(jià)的。對(duì)于包含日期時(shí)間的字符串值,如果月、日、時(shí)、分、秒的值小于10,不需要指定兩位數(shù)。例如:“2014-2-3 2:3:6”、“2014-02-03 02:03:06”是等價(jià)的。
YYYYMMDDHHMMSS 或?YYMMDDHHMMSS 格式的字符串。如果字符串對(duì)于日期時(shí)間類型是合法的就可以解釋為日期時(shí)間類型。例如:“20140618145410” 和 “140618145410”將被解釋為 “2014-06-18 14:54:10” ,但是?“20140618145480” 是不合法的(秒數(shù)不合法),將被解釋為 “0000-00-00 00:00:00”。
YYYYMMDDHHMMSS 或?YYMMDDHHMMSS 格式的數(shù)字。如果該數(shù)字對(duì)日期時(shí)間類型是合法的就可以解釋為日期時(shí)間類型。例如:“20140618145410” 和 “140618145410” 將被解釋為 “2014-06-18 14:54:10” 。數(shù)值的長度應(yīng)為6、8、12、14。如果數(shù)值長度是 8 或 14 位長,則假定為 YYYYMMDD 或?YYYYMMDDHHMMSS?格式。如果數(shù)值為 6 或 12 位長,則假定為 YYMMDD 或?YYMMDDHHMMSS?格式。
我正好有樓主類似的需求,每個(gè)季度為幾個(gè)表增加一個(gè)分區(qū),表的基本名稱是在一個(gè)叫設(shè)備類型的表里,每天計(jì)劃執(zhí)行一個(gè)過程,在過程里從系統(tǒng)表中判斷是否已經(jīng)創(chuàng)建了相關(guān)的分區(qū),如果沒創(chuàng)建就創(chuàng)建它
樓主可以參考一下,記得在my.ini?文件里配置event_scheduler=on
/**
定時(shí)每天檢查各個(gè)設(shè)備類型的歷史數(shù)據(jù)表,如果歷史數(shù)據(jù)表的所在分區(qū)已經(jīng)
接近當(dāng)前日期,則為此設(shè)備類型追加分區(qū)
*/
--?得到按月分區(qū)的日期值
delimiter?;
drop?function?if?exists?fnGetPartitionDateForMonth;?
delimiter?;;
create?function?fnGetPartitionDateForMonth()?returns?INT
begin
declare?v_today?datetime?default?date_add(now(),?INTERVAL?2?month);
return?year(v_today)?*?100?+?month(v_today);
end;;
--?得到按季度分區(qū)的日期值
delimiter?;
drop?function?if?exists?fnGetPartitionDateForQuarter;
delimiter?;;
create?function?fnGetPartitionDateForQuarter()?returns?int
begin
declare?v_today?datetime?default?date_add(now(),?interval?3?month);
declare?v_month?int;
set?v_month?=?month(v_today);
if?v_month?=?1?or?v_month?=?2?or?v_month?=?3?then?
set?v_today?=?DATE_ADD(v_today,?INTERVAL?(4?-?v_month)?month);
elseif?v_month?=?4?or?v_month?=?5?or?v_month?=?6?THEN
set?v_today?=?DATE_ADD(v_today,?INTERVAL?(7?-?v_month)?month);
elseif?v_month?=?7?or?v_month?=?8?or?v_month?=?9?THEN
set?v_today?=?date_add(v_today,?INTERVAL?(10?-?v_month)?month);
ELSE
set?v_today?=?date_add(v_today,?INTERVAL?(13?-?v_month)?month);
end?if;
return?year(v_today)?*?100?+?month(v_today);
end;;
--?得到按半年分區(qū)的日期值
delimiter?;
drop?function?if?exists?fnGetPartitionDateForHalfYear;
delimiter?;;
create?function?fnGetPartitionDateForHalfYear()?returns?int
begin
declare?v_today?datetime?default?date_add(now(),?interval?6?month);
declare?v_month?int;
set?v_month?=?month(v_today);
if?v_month?=?6?THEN
set?v_today?=?date_add(v_today,?INTERVAL?(7?-?v_month)?month);
else
set?v_today?=?DATE_ADD(v_today,?INTERVAL?(13?-?v_month)?month);
end?if;
return?year(v_today)?*?100?+?month(v_today);
end;;
--?維護(hù)按年分區(qū)
delimiter?;
drop?function?if?exists?fnGetPartitionDateForYear;
delimiter?;;
create?function?fnGetPartitionDateForYear()?returns?int
begin
declare?v_today?datetime?default?date_add(now(),?INTERVAL?2?year);
return?year(v_today)?*?100;
end;;
delimiter?;
drop?procedure?if?exists?spMaintainPartitions;
delimiter?;;
create?procedure?spMaintainPartitions()
BEGIN
declare?v_sql?varchar(2000);
declare?v_cnt?int;
declare?v_deviceTypeId?int;
declare?v_tablename?varchar(50);
declare?v_tablename_analog?varchar(50);
declare?v_tablename_digital?varchar(50);
declare?v_partitionType?int;
declare?v_fileDir?varchar(1000);
declare?v_tablenames?varchar(1000)?default?'';
declare?v_intDate?int;
declare?v_partitionName?varchar(100);
declare?done?int?default?0;
declare?c_deviceType?cursor?
for?select?Id,?TableName,?PartitionType,?DataFileDir
from?tbDeviceType?
where?Generated?=?1;
declare?continue?handler?for?not?found?set?done?=?1;
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)
Values(Now(),?'spMaintainPartitions?start......');
open?c_deviceType;
deviceType_loop:?LOOP
fetch?c_deviceType?into?v_deviceTypeId,?v_tablename,?v_partitionType,?v_fileDir;
set?v_fileDir?=?replace(v_fileDir,?'\\',?'/');
if?locate(':',?v_fileDir)??0?and?locate(':/',?v_fileDir)?=?0?then
set?v_fileDir?=?replace(v_fileDir,?':',?':/');
end?if;
if?done?=?1?then?
leave?deviceType_loop;
end?if;
set?v_intDate?=?null;
if?v_partitionType?=?1?then?
set?v_intDate?=?fnGetPartitionDateForMonth();
ELSEIF?v_partitionType?=?2?THEN
set?v_intDate?=?fnGetPartitionDateForQuarter();
ELSEIF?v_partitionType?=?3?then?
set?v_intDate?=?fnGetPartitionDateForHalfYear();
elseif?v_partitionType?=?4?then?
set?v_intDate?=?fnGetPartitionDateForYear();
end?if;
if?v_intDate?is?null?then
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)?
values(Now(),?Concat('DeviceTypeId?=?',?cast(v_deviceTypeId?As?char(10)),?'?did?not?define?paritition?schedule'));
else?
set?v_partitionName?=?concat('p',?cast(v_intDate?as?char(6)));
--?模擬量表
set?v_tablename_analog?=?concat(v_tablename,?'_Analog');
select?count(*)?into?v_cnt
from?information_schema.`TABLES`?where?`TABLE_SCHEMA`?=?database()?and?`table_name`?=?v_tablename_analog;
if?v_cnt??0?then
select?count(*)?into?v_cnt
from?
information_schema.`PARTITIONS`?
where?
TABLE_SCHEMA?=?database()?and?table_name?=?v_tablename_analog?and?partition_name?=?v_partitionName;
if?v_cnt?=?0?then
set?v_sql?=?CONCAT('alter?table?',?v_tablename_analog,?'?add?partition?(partition?',?v_partitionName,?'?values?less?than?(',?cast(v_intDate?as?char(6)),?')?data?directory?=?''',?v_fileDir,?'''?index?directory?=?''',?v_fileDir?,?''')');
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)
Values(Now(),?concat('sql?=?''',?v_sql));
set?@sql?=?v_sql;
prepare?cmd?from?@sql;
execute?cmd;
deallocate?prepare?cmd;
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)?
values(Now(),?concat('execute?complete:?',?v_sql));
end?if;
end?if;
--?數(shù)字量表
set?v_tablename_digital?=?concat(v_tablename,?'_Digital');
select?count(*)?into?v_cnt
from?information_schema.`TABLES`?where?`TABLE_SCHEMA`?=?database()?and?`table_name`?=?v_tablename_digital;
if?v_cnt??0?then
select?count(*)?into?v_cnt
from?
information_schema.`PARTITIONS`
where?
TABLE_SCHEMA?=?database()?and?table_name?=?v_tablename_digital?and?partition_name?=?v_partitionName;
if?v_cnt?=?0?then?
set?v_sql?=?CONCAT('alter?table?',?v_tablename_digital,?'?add?partition?(partition?',?v_partitionName,?'?values?less?than?(',?cast(v_intDate?as?char(6)),?')?data?directory?=?''',?v_fileDir,?'''?index?directory?=?''',?v_fileDir?,?''')');
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)
Values(Now(),?concat('sql?=?''',?v_sql));
set?@sql?=?v_sql;
prepare?cmd?from?@sql;
execute?cmd;
deallocate?prepare?cmd;
insert?into?tbPartitionMaintainLog(`CreatedDateTime`,?`LogContent`)?
values(Now(),?concat('execute?complete:?',?v_sql));
end?if;
end?if;
end?if;
end?loop?deviceType_loop;
close?c_deviceType;
END;;
delimiter?;
drop?event?if?exists?e_DataPartitionMaintain;
create?event?e_DataPartitionMaintain
on?SCHEDULE?every?60?Second
on?completion?PRESERVE
do?call?spMaintainPartitions();
set?global?event_scheduler?=?on;
1、打開Navicat for MySQL,找到要?jiǎng)?chuàng)建數(shù)據(jù)庫中數(shù)據(jù)表
2、接著我們?cè)凇氨怼鄙厦鎲螕羰髽?biāo)右鍵,然后點(diǎn)擊“新建表”
3、然后,右邊就會(huì)出現(xiàn)設(shè)計(jì)表的界面,這里可以設(shè)置表的字段名,類型,長度以及是否為null等
4、設(shè)計(jì)完數(shù)據(jù)表之后,點(diǎn)擊“保存”按鈕就OK了。
5、我們?cè)谄渲休斎氡砻忘c(diǎn)擊確定就可以了,表名可以根據(jù)自己的需求來設(shè)置
本文標(biāo)題:mysql怎么建表年月日,mysql建表時(shí)間
文章鏈接:http://chinadenli.net/article49/dsggpeh.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、微信小程序、電子商務(wù)、小程序開發(fā)、服務(wù)器托管、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)