暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

oracle 表添加主键 primary key

原创 Anbob 2010-12-07
1577
根据数据库三范式就是每个表建议要有主键,每个字段不可再分,表与之间不存在部分依赖
数据库的优化首先考虑的也是主键
主键会在对应的字段加唯一约束、加not null,建 索引
怎么为表加主键呢
1,建表同时
SQL> create table test13 (id int primary key);
Table created.
2,建表后加字段时
SQL> alter table test12 add id2 int primary key;
Table altered.
3,主键字段存在时
SQL> select constraint_name from user_constraints where table_name='TEST12';
CONSTRAINT_NAME
------------------------------------------------------------
SYS_C0010610
SQL> alter table test12 drop constraint SYS_C0010610
2  ;
Table altered.
SQL> alter table test12 add id1 int;
Table altered.
SQL> alter table test12 add constraint pk_id1_t12  primary key(id1);
Table altered.
完成!
ps:如果在要加主键的字段的值中有空值或不唯一,就会报错,有空值时报
SQL> alter table test12 add constraint pk_id1_t12  primary key(id1);
alter table test12 add constraint pk_id1_t12  primary key(id1)
*
ERROR at line 1:
ORA-01449: column contains NULL values; cannot alter to NOT NULL
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论