数据库表间数据复制分类
在利用数据库开发时,常常会将一些表之间的数据互相导入。当然可以编写程序实现,但是,程序常常需要开发环境,不方便。最方便是利用sql语言直接导入。既方便而修改也简单。以下就是导入的方法。1、 表结构相同的表,且在同一数据库(如,table1,table2)
Sql :insert into table1 select * fromtable2 (完全复制)insert into table1 select distinct * fromtable2(不复制重复纪录)insert into table1 select top 5 * fromtable2 (前五条纪录)2、 不在同一数据库中(如,db1 table1,db2 table2)
sql:insert into db1.table1 select * fromdb2.table2 (完全复制)insert into db1.table1 select distinct * fromdb2table2(不复制重复纪录)insert into tdb1.able1 select top 5 * fromdb2table2 (前五条纪录)3、 表结构不同的表或复制部分纪录(如,dn_user,dn_user2)
a. 建一个新表[DN_UserTemp](在老表dn_user上增加一列)CREATE TABLE [DN_UserTemp] ( [Num] [numeric](18, 0) IDENTITY (1, 1) NOT NULL)[Id] [idtype] NOT NULL,[Name] [fntype] NOT NULL,[Descript] [dstype] NULL,[LogonNm] [idtype] NOT NULL,[Password] [idtype] NULL,[Gender] [char] (1) NULL,[Quited] [booltype] NOT NULL,[OffDuty] [booltype] NOT NULL,[Stopped] [booltype] NOT NULL,[OSBind] [booltype] NOT NULL,[Domain] [idtype] NULL,[EMail] [fntype] NULL,[UnitId] [idtype] NULL,[BranchId] [idtype] NULL,[DutyId] [idtype] NULL,[LevelId] [idtype] NULL,[ClassId] [idtype] NULL,[TypeId] [idtype] NULL,[IP] [varchar] (15) COLLATE Chinese_PRC_CI_AS NULL,[ExpireDT] [datetime] NULL,[Sort] [int] NOT NULL,[AllowDel] [booltype] NOT NULL,[UnitChief] [booltype] NOT NULL,[BranchChief] [booltype] NOT NULL,[UnitDeputy] [booltype] NOT NULL,[BranchDeputy] [booltype] NOT NULL,[Num] [numeric](18, 0) IDENTITY (1, 1) NOT NULL)ON [PRIMARY]b. 将dn_uer2的数据拷入dn_usertemp
sql:insert into dn_usertemp select * fromdn_user2
c.将dn_usertemp 拷入dn_user
sql:declare @i int
declare @j int
declare @Namefntypeset @i=1
select @j=count(*) fromdn_usertempwhile @i
begin
select @Name=Name from dn_usertemp where Num=@i
print @Name
insert into dn_user (Name) values (@Name) where Num=@i
select @i=@i 1
endMySql数据库复制表数据
将 production 数据库中的 mytbl 表快速复制为 mytbl_new,2个命令如下:CREATE TABLE mytbl_new LIKEproduction.mytbl;INSERT mytbl_new SELECT * FROMproduction.mytbl;
第一个命令是创建新的数据表 mytbl_new ,并复制 mytbl 的数据表结构。
第二个命令是讲数据表 mytbl 中的数据复制到新表 mytbl_new 。
注:production.mytbl是指定要复制表的数据库名称为 production 。它是可选的。
假如没有production. ,MySQL数据库将会假设mytbl在当前操作的数据库。
另外:在mysql数据库中复制数据为:select * into desTable fromsourceTable在mssql中支持,在mysql中不支持insert into desTable select * from sourceTable