网站建设资讯

NEWS

网站建设资讯

mysql怎么建表年月日,mysql建表时间

MySQL如何每个月自动创建一张表,以年月做为

这个要建立一个作业啊

成都创新互联是一家集网站建设,旅顺口企业网站建设,旅顺口品牌网站建设,网站定制,旅顺口网站建设报价,网络营销,网络优化,旅顺口网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

作业运行时间放在月初,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

试一试,如有疑问,及时沟通!

MySql建表时日期类型的出理

mysql(5.5)所支持的日期时间类型有:DATETIME、 TIMESTAMP、DATE、TIME、YEAR。

1.DATETIME 用于表示 年月日 时分秒,是 DATE 和 TIME 的组合,并且记录的年份比较长久。如果实际应用中有这样的需求,就可以使用 DATETIME 类型。

2.TIMESTAMP

TIMESTAMP 用于表示 年月日 时分秒,但是记录的年份比较短暂。

TIMESTAMP 和时区相关,更能反映当前时间。当插入日期时,会先转换为本地时区后再存放;当查询日期时,会将日期转换为本地时区后再显示。所以不同时区的人看到的同一时间是  不一样的。

表中的第一个 TIMESTAMP 列自动设置为系统时间(CURRENT_TIMESTAMP)。当插入或更新一行,但没有明确给 TIMESTAMP 列赋值,也会自动设置为当前系统时间。如果表中有第二个 TIMESTAMP 列,则默认值设置为0000-00-00 00:00:00。

TIMESTAMP 的属性受 Mysql 版本和服务器 SQLMode 的影响较大。

如果记录的日期需要让不同时区的人使用,最好使用 TIMESTAMP。

3.DATE

DATE 用于表示 年月日,如果实际应用值需要保存 年月日 就可以使用 DATE。

4.TIME

TIME 用于表示 时分秒,如果实际应用值需要保存 时分秒 就可以使用 TIME。

5.YEAR

YEAR 用于表示 年份,YEAR 有 2 位(最好使用4位)和 4 位格式的年。 默认是4位。如果实际应用只保存年份,那么用 1 bytes 保存 YEAR 类型完全可以。不但能够节约存储空间,还能提高表的操作效率。

资料拓展:

每种日期时间类型都有一个有效值范围,如果超出这个范围,在默认的SQLMode下会报错,并以零值存储。

插入或更新时,日期时间类型允许“不严格”语法,以DATETIME为例(其他日期时间类型雷同):

YYYY-MM-DD HH:MM:SS 或 YY-MM-DD HH:MM:SS 格式的字符串。任何符号都可以用作日期部分或时间部分的间隔符。例如:“14-06-18 14:54:10”、“14*06*18 14.54.10”、“14+06+18 14=54=10”是等价的。对于包含日期时间的字符串值,如果月、日、时、分、秒的值小于10,不需要指定两位数。例如:“2014-2-3 2:3:6”、“2014-02-03 02:03:06”是等价的。

YYYYMMDDHHMMSS 或 YYMMDDHHMMSS 格式的字符串。如果字符串对于日期时间类型是合法的就可以解释为日期时间类型。例如:“20140618145410” 和 “140618145410”将被解释为 “2014-06-18 14:54:10” ,但是 “20140618145480” 是不合法的(秒数不合法),将被解释为 “0000-00-00 00:00:00”。

YYYYMMDDHHMMSS 或 YYMMDDHHMMSS 格式的数字。如果该数字对日期时间类型是合法的就可以解释为日期时间类型。例如:“20140618145410” 和 “140618145410” 将被解释为 “2014-06-18 14:54:10” 。数值的长度应为6、8、12、14。如果数值长度是 8 或 14 位长,则假定为 YYYYMMDD 或 YYYYMMDDHHMMSS 格式。如果数值为 6 或 12 位长,则假定为 YYMMDD 或 YYMMDDHHMMSS 格式。

MySQL如何每个月自动创建一张表,以年月做为表名

我正好有楼主类似的需求,每个季度为几个表增加一个分区,表的基本名称是在一个叫设备类型的表里,每天计划执行一个过程,在过程里从系统表中判断是否已经创建了相关的分区,如果没创建就创建它

楼主可以参考一下,记得在my.ini 文件里配置event_scheduler=on

/**

定时每天检查各个设备类型的历史数据表,如果历史数据表的所在分区已经

接近当前日期,则为此设备类型追加分区

*/

-- 得到按月分区的日期值

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;;

-- 得到按季度分区的日期值

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;;

-- 得到按半年分区的日期值

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;;

-- 维护按年分区

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;

-- 数字量表

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;

MYSQL数据库中怎么建立一个表呢?

1、打开Navicat for MySQL,找到要创建数据库中数据表

2、接着我们在“表”上面单击鼠标右键,然后点击“新建表”

3、然后,右边就会出现设计表的界面,这里可以设置表的字段名,类型,长度以及是否为null等

4、设计完数据表之后,点击“保存”按钮就OK了。

5、我们在其中输入表名就点击确定就可以了,表名可以根据自己的需求来设置


分享标题:mysql怎么建表年月日,mysql建表时间
当前地址:http://cdweb.net/article/dsggpeh.html