数据表新增自增一主键能加快数据表的访问速度,而且是整形的索引速度最快。本程序适合在导入oracle数据库时删除不存在主键的情况
应用场合:数据表新增自增一主键能加快数据表的访问速度,而且是整形的索引速度最快。本程序适合在导入oracle数据库时删除不存在主键的情况下运行。
代码说明:所有的表主键字段名都设置为id,如果已存在id字段,则判断是否是整形,如果不是就重命名字段为[表名id],然后新增id,,如果不存在则直接添加自增一id的主键
操作说明:打开pqsql连接数据库后直接执行下面的详细脚本代码运行即可,脚本有风险(会删除原来的索引跟主键约束),请不要轻易在正式运行的数据库上直接执行
--oracle使用游标为所有用户表创建主键语句
--参考语句如下:
--查询所有主键约束select * from user_constraints
--查询所有序列select * from user_sequences;
--查询所有触发器select * from user_triggers;
--查询触发器的用户select distinct(table_owner) from user_triggers;
declare
addstring nvarchar2(2000):=' '; --定义添加字段变量
renamestring nvarchar2(2000):=' '; --定义重命名字段变量
tablestring nvarchar2(2000):=' '; --定义序列变量
keyidname nvarchar2(255):='id'; --定义主键字段名变量
tableidname nvarchar2(255):=' '; --定义新的字段名变量
trigerstring nvarchar2(2000):=' '; --定义创建触发器字符串变量
trgname nvarchar2(255):=' '; --定义触发器名称变量
seqstring nvarchar2(2000):=' '; --定义创建序列字符串变量
seqname nvarchar2(255):=' '; --定义序列名称变量
pkname nvarchar2(255):=' '; --定义主键索引名称变量
constring nvarchar2(2000):=' '; --定义索引变量
notnullstring nvarchar2(2000):=' '; --定义主键不为空变量
cursor mycursor is select * from user_tables where tablespace_name='szgabl' order by table_name; --定义游标获取所所有用户数据表名称
myrecord mycursor%rowtype; --定义游标记录类型
countername int :=0; --定义是否存在对应的列名变量
counterdata int :=0; --定义是否存在对应的数据类型
begin
dbms_output.put_line('declare counter int :=0;begin ');
open mycursor; --打开游标
if mycursor%isopen then --判断打开成功
loop --循环获取记录集
fetch mycursor into myrecord; --获取游标中的记录
if mycursor%found then --游标的found属性判断是否有记录
begin
--获取有效的数据表名
select replace(myrecord.table_name,'tb_','') into tablestring from dual;
select 'seq_'||tablestring into seqname from dual;
select 'trg_'||tablestring into trgname from dual;
select 'pk_'||tablestring into pkname from dual;
select tablestring||upper(keyidname) into tableidname from dual;
--判断当前数据表是否包含字段名为id的列
select count(*) into countername from dual where exists(select * from user_tab_cols where lower(column_name)=lower(keyidname) and table_name=myrecord.table_name);
if countername=0 then
begin
dbms_output.put_line('--当前数据表'||myrecord.table_name||'不存在字段名为id的列');
--添加主键字段
addstring:='execute immediate ''alter table '||myrecord.table_name||' add '||keyidname||' number'';';
dbms_output.put_line(addstring);
--execute immediate addstring;
--创建一个序列
seqstring:='select count(*) into counter from dual where exists(select * from user_sequences where sequence_name='''||seqname||''');if counter>0 then execute immediate ''drop sequence '||seqname||'''; end if; execute immediate '' create sequence seq_'||tablestring||' increment by 1 start with 1 nomaxvalue nocycle nocache'';';
dbms_output.put_line(seqstring);
--execute immediate seqstring;
--创建一个触发器
trigerstring:='select count(*) into counter from dual where exists(select * from user_triggers where trigger_name='''||trgname||''');if counter>0 then execute immediate ''drop trigger '||trgname||'''; end if; execute immediate '' create trigger trg_'||tablestring||' before insert on '||myrecord.table_name||' for each row when (new.'||keyidname||' is null) begin select '||seqname||'.nextval into: new.'||keyidname||' from dual; end'';';
dbms_output.put_line(trigerstring);
--execute immediate trigerstring;
--添加主键约束
constring:='select count(*) into counter from dual where exists(select * from user_constraints where constraint_name='''||pkname||''');if counter>0 then execute immediate ''drop constraint '||pkname||'''; end if; execute immediate ''alter table '||myrecord.table_name||' add constraint '||pkname||' primary key('||keyidname||')'';';
dbms_output.put_line(constring);
--execute immediate constring;
--更新主键不为空
notnullstring:='select count(*) into counter from dual where exists(select * from user_tab_cols where table_name='''||myrecord.table_name||''' and column_name='''||keyidname||''' and nullable=''y'' );if counter>0 then execute immediate ''alter table '||myrecord.table_name||' modify '||keyidname||' not null''; end if;';
dbms_output.put_line(notnullstring);
--execute immediate notnullstring;
end;
else
begin
--判断当前数据表是否包含字段名为id且数据类型为number
select count(*) into counterdata from dual where exists(select * from user_tab_cols where lower(column_name)=lower(keyidname) and data_type='number' and table_name=myrecord.table_name);
if counterdata=0 then
begin
dbms_output.put_line('--当前数据表'||myrecord.table_name||'存在字段名为id,但数据类型不为number的列');
--先重命名字段,然后添加主键字段
renamestring:='execute immediate ''alter table '||myrecord.table_name||' rename column '||keyidname||' to '||tableidname||''';';
dbms_output.put_line(renamestring);
--execute immediate renamestring;
--添加主键字段
addstring:='execute immediate ''alter table '||myrecord.table_name||' add '||keyidname||' number'';';
dbms_output.put_line(addstring);