复制表中的字段到另一张表

a                                                         b

id    name     sex                                 id    name   age   sex    

1      ww        m                                  1      ww      18    

2      kk          w                                  2      kk        21

3      cc          m                                   3      cc        19

怎么把表a中sex字段的值复制到b?

 

该题解题思路如下:

 

一、建表a,b,插入两行数据

 

create table a(

id smallint primary key,

name varchar2(20),

sex varchar2(2)

);

 

create table b(

id smallint primary key,

name varchar2(20),

age smallint,

sex varchar2(2)

);

 

 

insert into a values(1,'ww','m');

insert into a values(2,'kk','w');

 

 

 

insert into b values(1,'ww',20,'');

insert into b values(2,'kk',19,'');

 
二、测试创建表c,同a
1.若表字段相同
1)创建表c的同时将a中的数据导入c中
create table c as select * from a;
 
2)表c创建完毕将a中的数据导入c中

insert into c select * from a;

 

2.若表字段不同,如将a中的数据插入b中

insert into b select id,name,null,sex from a;

 

三、将a表中的sex字段插入到b表中

update b set b.sex = (select sex from a where a.id = b.id);

或update b set b.sex = (select sex from a where a.id = b.id)where exists(select 1 from a where a.id = b.id);

 

引自:http://blog.csdn.net/icearmour/article/details/6359123

posted @ 2013-10-18 16:07  IT_菜鸟  阅读(658)  评论(0编辑  收藏  举报