ITPub博客

首页 > 数据库 > Oracle > oracle主外键相关

oracle主外键相关

原创 Oracle 作者:XmIssU 时间:2015-09-23 16:19:27 0 删除 编辑

<!--[if !supportLists]-->1.<!--[endif]-->建主键约束(表名为AN)

Alter table an add constraint PK_AN primary key(ID);

<!--[if !supportLists]-->2.<!--[endif]-->建联合主键

Alter table an add constraint PK_AN primary key(ID,NAME);

<!--[if !supportLists]-->3.<!--[endif]-->创建表描述

Comment on table an is ‘表含义’;

<!--[if !supportLists]-->4.<!--[endif]-->创建字段描述

Comment on column an.id is ‘字段含义’;

<!--[if !supportLists]-->5.<!--[endif]-->建外键

Alter table an_1 add constraint FK_AN_1 foreign key(an_id) references AN(ID);

<!--[if !supportLists]-->6.<!--[endif]-->查看表的主键

SELECT * FROM USER_CONS_COLUMNS cc,USER_CONSTRAINTS c WHERE c.constraint_name=cc.constraint_name 

   AND c.constraint_type='P' AND cc.table_name='AN';

<!--[if !supportLists]-->7.<!--[endif]-->查看外键

SELECT r.constraint_name,r.r_constraint_name,rc.column_name,pc.column_name AS PK_COLUMN_NAME,pc.table_name AS PK_TABLE_NAME

  FROM USER_CONSTRAINTS r,USER_CONS_COLUMNS rc,USER_CONSTRAINTS p,USER_CONS_COLUMNS pc

    WHERE r.constraint_type='R' AND r.constraint_name=rc.constraint_name AND pc.constraint_name=p.constraint_name

      AND p.constraint_type='P' AND r.r_constraint_name=p.constraint_name AND r.table_name='APAS_SERVICE';

<!--[if !supportLists]-->7.<!--[endif]-->删除主键

alter table an drop constraint constraint_name;

<!--[if !supportLists]-->8.<!--[endif]-->使主键失效

alter table an disable constraint constraint_name;

<!--[if !supportLists]-->9.<!--[endif]-->使主键生效

alter table an ensable constraint constraint_name;

 

 

 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/30461945/viewspace-1807360/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 反向索引
请登录后发表评论 登录
全部评论

注册时间:2015-09-07

  • 博文量
    24
  • 访问量
    34789