数据库表将历史数据分表存储SQL语句
1、MySQL
CREATE TABLE newtable LIKE oldtable;-- 创建表结构
INSERT INTO newtable SELECT * FROM oldtable where create_time>='2020-01-01 00:00:00' and create_time<='2020-12-31 23:59:59';-- 将旧表中2020年的数据插入到新表中
CREATE TABLE newtable222 LIKE oldtable;-- 创建表结构
INSERT INTO newtable222 SELECT * FROM oldtable where create_time<'2020-01-01 00:00:00' and create_time>'2020-12-31 23:59:59';-- 将旧表中除了2020年的数据插入到新表中
RENAME TABLE oldtable to oldtable_xxxxx, newtable222 to oldtable; --重命名
2、SQL-SERVER
select * into newtable from oldtable where 1=0;-- 创建表结构
select * into newtable from oldtable where create_time>='2020-01-01 00:00:00' and create_time<='2020-12-31 23:59:59';-- 创建新表,并将旧表中2020年的数据插入到新表中
SET ROWCOUNT 0; - 删除旧表2020年的数据
WHILE 1 = 1
BEGIN
DELETE TOP(500000) FROM oldtable WHERE create_time>='2020-01-01 00:00:00' and create_time<='2020-12-31 23:59:59';
IF @@rowcount < 500000
BREAK;
END
3、ORACLE
create table newtable as select * from oldtable where 1=0;-- 创建表结构
create table newtable as select * from oldtable where create_time>='2020-01-01 00:00:00' and create_time<='2020-12-31 23:59:59';-- 创建新表,并将旧表中2020年的数据插入到新表中
DELETE FROM oldtable where create_time>='2020-01-01 00:00:00' and create_time<='2020-12-31 23:59:59';-- 删除旧表2020年的数据(不好,其他方法之后找来写)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步