日期:2014-05-17 浏览次数:21513 次
--给你个例子
--建表
create table test(
name varchar2(20));
--测试数据
insert into test values('name1');
insert into test values('name2');
insert into test values('name3');
--加列
alter table test add id integer;
--创建含主键表,用于更新
create table temprt(
rd varchar2(18) primary key,
id int
);
--将目的表的值插入到创建的表
insert into temprt select rowid,rownum from test;
--更新列值
update (select A.id Aid,A.rowid Ard,B.rd,B.id Bid from test A,temprt B where A.rowid=B.rd)
set Aid=Bid;
--设置不空
alter table test modify id int not null;
--设置主键
alter table test add constraint pk_test primary key(id);
------解决方案--------------------
Tested Under Oracle 11gR2
create table test1(name1 varchar2(40),city varchar2(40));
insert into test1 values('name1','nanjing');
insert into test1 values('name1','nanjing');
insert into test1 values('name2','nanjing1');
insert into test1 values('name3','nanjing2');
insert into test1 values('name4','nanjing3');
insert into test1 values('name5','nanjing4');
insert into test1 values('name6','nanjing5');
insert into test1 values('name7','nanjing6');
insert into test1 values('name8','nanjing7');
insert into test1 values('name9','nanjing8');
insert into test1 values('name10','nanjing9');
insert into test1 values('name10','nanjing9');
insert into test1 values('name12','nanjing11');
insert into test1 values('name13','nanjing12');
insert into test1 values('name14','nanjing13');
commit;
alter table TEST1 add id number(10);
create sequence SEQ_ID
minvalue 1
maxvalue 999999999
start with 1;
Update test1 set id=seq_id.nextval;
commit;
alter table TEST1
add constraint PK_TEST1 primary key (ID);
select ID,Name1,CITY from TEST1;
------解决方案--------------------
先增加一列,然后update table t set t.col=rownum ;
alter table t add contstraint pk_t primary key (col) ;
------解决方案--------------------
alter table tabName add id number(8); update tabName set id = rownum
------解决方案--------------------
楼上不少的正确答案
先加一列,更新值,再加约束