oracle对表添加主键约束的sql语句

6 篇文章 0 订阅

转自:http://blog.csdn.net/u014455929/article/details/52374523

https://zhidao.baidu.com/question/1958170407835555540.html

–创建主外键(可以在创建表的同时添加主外键约束,也可以 
–创建完成后再添加约束 )

—–方法一:表创建的同时,添加主键约束

语法:

create table 表名 

列名1 数据类型及长度 constraint 主键名称(一般主键名称为”PK_”开头) primary key, 
列名2 数据类型及长度 not null,——-not null 约束该列不为空,不写表示可以为空 
列名3 数据类型及长度

)tablespace 表空间名称;——-指定将该表放在某个表空间里,可以省略不指定

例: 
create table T_DEPOSIT 
(savingid VARCHAR2(20) constraint pk_savingid primary key, 
savingname VARCHAR2(20) not null, 
descrip VARCHAR2(50) 
)tablespace ATM_tablespace;

————注意————- 
CREATE TABLE T_cardInfo –银行卡信息表 

cardID varchar2(19) primary key, 
—— 如果此处这样定义主键,则主键名称系统自己定义设置 
);

—–方法二:表创建后,添加主键约束 
语法: 
alter table 表名 
add constraint 主键名称(一般主键名称为”PK_”开头) primary key(要设为主键的列名); 
例: 
alter table T_Grade 
add constraint pk_gradeId primary key (gradeId);

、、、、、、、、、

查询出所有的用户表。
查所有用户的表在all_tables
主键名称、外键在all_constraints
索引在all_indexes
但主键也会成为索引,所以主键也会在all_indexes里面。
具体需要的字段可以DESC下这几个view,dba登陆的话可以把all换成dba。
查询用户表的索引(非聚集索引):
select  *  from  user_indexes
where  uniqueness  =  'NONUNIQUE '
查询用户表的主键(聚集索引):
select  *  from  user_indexes
where  uniqueness  =  'UNIQUE '
1、查找表的所有索引(包括索引名,类型,构成列):
select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name = 要查询的表
2、查找表的主键(包括名称,构成列):
select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 要查询的表
3、查找表的唯一性约束(包括名称,构成列):
select column_name from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'U' and au.table_name = 要查询的表
4、查找表的外键(包括名称,引用表的表名和对应的键名,下面是分成多步查询):
select * from user_constraints c where c.constraint_type = 'R' and c.table_name = 要查询的表
查询外键约束的列名:
select * from user_cons_columns cl where cl.constraint_name = 外键名称







  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值