您的位置:首页 > 数据库

SQL_如何实现用户A的表A数据插入到用户B的表B中?

2014-10-08 22:23 477 查看
***********************************************声明***********************************************************************

原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任。
深蓝的blog:/article/1336910.html
****************************************************************************************************************************
如何实现用户A的表A数据插入到用户B的表B中?

情况一:表A、表B数据结构完全相同。在用户B下通过用户A的表创建属于用户B的表,以达到两表数据相同。

例:hyl用户根据scott的dept表数据创建新表A,要求A表数据与scott的DEPT表数据相同

SQL> create user hyl identified by hyl;

User created.

--创建实验用户

SQL> grant connect to hyl;

Grant succeeded.

--授予相应权限

SQL> grant select any table to hyl;

Grant succeeded.

SQL> grant resource to hyl;

Grant succeeded.

SQL> conn hyl/hyl

Connected.

SQL> select * from tab;

no rows selected

SQL> create table A as select * from scott.dept;

Table created.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID

------------------------------ ------- ----------

A TABLE

SQL> select * from A;

--根据scott的dept的数据创建了表A

DEPTNO DNAME LOC

---------- -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

情况二:表A与表B列名不同,但要求表B的数据内容涵盖表A的数据

例:hyl用户的表newdept,scott表dept,要求按新字段的格式将dept表的数据插入到newdept表中去

SQL> create table newdept (newdeptno number(2),olddeptno number(2),newdname varchar2(14),olddname varchar2(14),newloc varchar2(13),oldloc varchar2(13) );

Table created.

--创建实验表

SQL> select * from tab;

--查看hyl目前拥有的表

TNAME TABTYPE CLUSTERID

------------------------------ ------- ----------

A TABLE

NEWDEPT TABLE

SQL> select * from newdept;

--查询实验表,目前没有数据

no rows selected

SQL> desc newdept;

--查询新建表的表结构

Name Type Nullable Default Comments

--------- ------------ -------- ------- --------

NEWDEPTNO NUMBER(2) Y

OLDDEPTNO NUMBER(2) Y

NEWDNAME VARCHAR2(14) Y

OLDDNAME VARCHAR2(14) Y

NEWLOC VARCHAR2(13) Y

OLDLOC VARCHAR2(13) Y

--对照新表的结构,向新表中插入数据

SQL> insert into newdept(olddeptno,olddname,oldloc) select t.deptno as olddeptno,t.dname as olddname,t.loc as oldloc from scott.dept t where t.deptno not in (select olddeptno from newdept);

--将scott的数据插入到hyl用户下,要求新字段的位置上,并且使用where限制条件,要求插入的新数据在新表的olddeptno列上是不存在的

4 rows inserted

SQL> select * from newdept;

--查询现在newdept表的数据

NEWDEPTNO OLDDEPTNO NEWDNAME OLDDNAME NEWLOC OLDLOC

--------- --------- -------------- -------------- ------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

SQL> select * from scott.dept;

--对照的看一下scott的dept表里的数据

DEPTNO DNAME LOC

------ -------------- -------------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

--对比,hyl用户下的newdept表和scott用户下的dept表,已经按照要求将dept的数据导入到hyl用户下的新表newdept表中了。

--核实两张表的数据量

SQL> select count(*) from scott.dept;

COUNT(*)

----------

4

SQL> select count(*) from newdept;

COUNT(*)

----------

4

***********************************************声明***********************************************************************

原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处,否则追究版权法律责任。
深蓝的blog:/article/1336910.html
****************************************************************************************************************************
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: