首页 > 数据库 >47. SQL--克隆表(复制表)

47. SQL--克隆表(复制表)

时间:2022-09-05 16:48:19浏览次数:43  
标签:author -- 47 create tbl SQL table null tutorial

1. 前言

在某些情况下,您可能需要一个表的完整、精确副本,也即克隆表(复制表)。我们首先想到的是使用 create table 命令创建一张新表,然后使用 select 命令从旧表中选取所有数据,并使用 insert 命令插入到新表中。但是这种做法可能达不到您的目的,因为副本必须包含相同的索引、约束、默认值等。

如果您使用 mysql 数据库,可以借助 show create table 命令,该命令用来展示表结构的全部细节(包括字段名称、字段类型、索引、约束等),也即创建表的 create table 语句。

克隆表的大概步骤为:

  • 使用 show create table 命令获取创建表的 create table 语句;
  • 将 create table 语句中的表名改成新表的名字,然后执行该语句,这样您将拥有一个精确的克隆表;
  • 如果您还希望复制表中的数据,可以使用 insert into 和 select 命令。

2. 示例

尝试为 tutorials_tbl 表创建一个名为 clone_tbl 的克隆表。

步骤 1:获取表的完整结构。

sql> show create table tutorials_tbl \g;
*************************** 1. row ***************************
      table: tutorials_tbl
create table: create table 'tutorials_tbl' (
  'tutorial_id' int(11) not null auto_increment,
  'tutorial_title' varchar(100) not null default '',
  'tutorial_author' varchar(40) not null default '',
  'submission_date' date default null,
  primary key  ('tutorial_id'),
  unique key 'author_index' ('tutorial_author')
) type = myisam
1 row in set (0.00 sec)

步骤2:重命名并创建一个新表。

sql> create table `clone_tbl` (
  -> 'tutorial_id' int(11) not null auto_increment,
  -> 'tutorial_title' varchar(100) not null default '',
  -> 'tutorial_author' varchar(40) not null default '',
  -> 'submission_date' date default null,
  -> primary key  (`tutorial_id'),
  -> unique key 'author_index' ('tutorial_author')
-> ) type = myisam;
query ok, 0 rows affected (1.80 sec) 

步骤3:执行完步骤 2 之后,您已经在数据库中创建了一张克隆表。如果您需要从旧表中复制数据,则可以使用 insert into ... select 语句来完成。

sql> insert into clone_tbl (tutorial_id,
   ->                        tutorial_title,
   ->                        tutorial_author,
   ->                        submission_date)
   -> select tutorial_id,tutorial_title,
   ->        tutorial_author,submission_date,
   -> from tutorials_tbl;
query ok, 3 rows affected (0.07 sec)
records: 3  duplicates: 0  warnings: 0

最终,您拥有了一张结构精确、数据完整的克隆表。

标签:author,--,47,create,tbl,SQL,table,null,tutorial
From: https://www.cnblogs.com/jiajunling/p/16658675.html

相关文章

  • js实现切换页面清除定时器的函数
    背景:我在切换页面的时候,发现切换回原来的页面,定时器会叠加而不会清除原来的定时器解决方法:1.定义全局变量,通过js遍历清除(不会用,但性能好)varpageTimer={};/......
  • C# ADO.NET 数据库访问的基础类
    1usingSystem;2usingSystem.Data;3usingSystem.Data.SqlClient;45namespaceSQLServerAccess6{7///<summary>8///数据库访......
  • TS创建接口和接口类型数组,并统一导入后引用
    在app.ts中引用import{users,IRoute,IUser,routes}from"./data";在index.ts中统一导入后导出export*from"./routes";export*from"./users";importro......
  • [个人用] 常用工具
    对象合并constobj={id:null,count:null,completed:null,};//假设这是接口返回的对象constdata={id:1,count:2,completed:3,otherPa......
  • 1048. 最长字符串链
    给出一个单词数组 words ,其中每个单词都由小写英文字母组成。如果我们可以 不改变其他字符的顺序 ,在wordA 的任何地方添加恰好一个字母使其变成 wordB ,那么我们......
  • PostgreSQL-数据类型1
    一、数字类型整数类型:SQL仅指定整数类型integer(或int)、smallint和bigint。类型名称int2、int4和int8是扩展,其他一些SQL数据库系统也使用它们。数值类型num......
  • codeforces.ml/contest/932/problem/D 树上找最长上升子序列长度 倍增算法
    D.Treetimelimitpertest2secondsmemorylimitpertest512megabytesinputstandardinputoutputstandardoutputYouaregivenanodeofthetreew......
  • CF1453D Checkpoints(期望)
    Gildongisdevelopingagameconsistingof......
  • 系统优化软件MacPilot
    还在为系统太卡,找不到好用的系统优化软件而苦恼吗?今天小编就为大家带来了MacPilot,MacPilotMac是一款能够为您修复、检查系统所有垃圾文件,浏览历史等,并为您清理所有不需要......
  • Retinaface ONNX 如何正确导出
    Retinaface是一个人脸检测器,人脸检测天生存在强先验知识,比如近场人脸识别,人脸较大,监控视角下人脸识别通常人脸较小,两者天生对输入的分辨率有个假设,如果人脸很大,不需要大的......