zoukankan      html  css  js  c++  java
  • 数据库表将历史数据分表存储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年的数据(不好,其他方法之后找来写)

  • 相关阅读:
    第二个周六——3.9
    女王节,很开心——3.8
    女生节——3.7
    尴尬的一批——3.6
    周二——3.5
    周一——3.4
    Java基本语法_循环练习系列(二)——万年历
    Java基本语法_循环练习系列(一)——模拟双色球
    《剩女郎》的艺术魅力
    剩女郎剧评
  • 原文地址:https://www.cnblogs.com/xlj227/p/14839760.html
Copyright © 2011-2022 走看看