您的位置:首页 > 数据库

关于结构相同,数据不同的数据库进行一个表数据合并入另一个数据库中的表

2015-12-15 10:50 681 查看
首先,进行数据库的操作,这里使用的是ado+access进行的数据库的操作,目前来看,ado这种方式还算是比较简单。

而且网络上关于数据库进行连接的博客有一大推,这里就不做探讨了,这里仅仅说明的是,一个数据库的表复制进入另一个数据库中。

1、关于数据库的复制有两种情况

  1)insert into table1 select * from table2

  2) select into table 1 select *from table2

这两种写法有什么区别呐,首先insert into这种写法必须有table1这个表格,select则是在access中必须不能存在table1,否则在ado接口中会出现报错的现象。

2、一个数据库对另一个数据库进行复制操作,

sql语句有如下写法:

insert into table1 select distinct * from (select * from table1 union select * from table1 in 'D:\\record1.mdb')a where id =1

这样的意思就是 向已经连接的数据库中的table1 插入(不同的)来自(table1 以及 record1中的table1)a  加上条件的记录

select into

其用法与insert into 相同 不做描述

3、增加点难度,这里说一下where 后面跟两个条件的例子

sql: insert into table1 select distinct * from(selcet * from table1 union selcet *from table1  in 'D:\\record1.mdb')a where id=1 and name=fd

这是两种条件的情况 条件中间必须要加上 and  or  ,如果有非操作 那么可以加上括号,并且在括号之前加上not

4、继续增加点难度,我们向一个表中复制另一个数据库中的表中的数据,并且不能出现重复的,接下来我们应该怎么做?

根据以上的经验 我们是不是可以用一个条件,这个条件是什么?

sql:insert into table1 select distinct * from(select * from table1 union select * from table1 in 'D:\\record1.mdb')a where not exists (select 1 from table1 where id = a.id)

这里的id为主键,那么我们就可以得到了从另一个数据库中的不重复的记录。

5、接着增加难度,如果我们的数据库当中存在两个主键我们应该怎么办???(并且我们要增加不重复的记录)

根据之前的经验我们是不是可以得出结论了

sql: insert into table select distinct * from(select * from table1 union select * from table1 in 'D:\\record1.mdb')a where not exists (select 1 from table1 where id=a.id and name = a.name)

这就是关于从另一个结构相同的数据库中复制带有两个主键的表中的数据的sql 语句。希望对之后的热能有帮助
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  access 数据库 合并