Oracle SQL-将主键添加到表 [英] Oracle SQL - Add Primary Key to table
问题描述
我有一些没有主键的列,并且想添加一个主键列.
I have some columns with no primary key and want to add a primary key column.
NAME Age
-------------
Peter 45
Bob 25
John 56
Peter 45
一些同事建议添加带有序列和触发器的PK : 将自动增量主键添加到的现有表中甲骨文
Some collegues suggest to add a PK with a sequences and triggers: Add a auto increment primary key to existing table in oracle
这很好,但是 我的客户使用的数据库用户无权添加序列或触发器 . 我想避免与数十名DBA管理员联系以更改用户权限或运行我的脚本.
This is nice, but my customers use a Database User with no rights to add sequences or triggers. I want to prevent to contact dozens of DBA administrators to alter user rights or to run my scripts.
这是我的建议,仅添加一条更新语句即可添加PK :(在步骤2中需要帮助)
This is my suggestion to add a PK with only an update statement: (I need help in Step 2)
步骤1:创建ID列(我对此具有数据库权限)
ALTER TABLE PERSON ADD ID NUMBER(10,0);
第2步: 问题:我可以根据行的顺序或其他方式使用唯一值初始化ID列吗?怎么样?
UPDATE PERSON SET ID = something-unique
步骤3:添加主键约束后缀:(数据库管理员对此拥有权利)
Step 3: Add the primary key contraint afterwords: (I DB have rights for this)
ALTER TABLE PERSON ADD CONSTRAINT PK_ID PRIMARY KEY(ID);
步骤4:后记:主键由我的应用程序管理和添加.
Step 4: Afterwords: the primary key is managed and added by my application.
这将是结果:
ID(PK) NAME Age
---------------------
1 Peter 45
2 Bob 25
3 John 56
4 Peter 45
谢谢大家!
推荐答案
Update person set id = rownum;
这篇关于Oracle SQL-将主键添加到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!