- 论坛徽章:
- 0
|
如何找数据库表的主键字段的名称?
SELECT * FROM user_constraints WHERE CONSTRAINT_TYPE='P' and table_name='AAA';
select * from dba_cons_columns where CONSTRAINT_NAME='SYS_AAA';
Oracle 在创建主键(可以不加constraint SYS_AAA),会为库表自动创建索引,索引的列为主键列。
并且当库表某些列名或者库表名改变时候,Oracle自动创建的索引SYS_AAA,中的索引列也会自动更新(类似于视图),并且SYS_AAA会与名字更改后的库表还是保持索引关系。
关键系统库表:
desc dba_constraints
desc dba_cons_columns
desc dba_indexes
desc dba_ind_columns
desc DBA_TAB_COLUMNS
例子1:更改库表的列名
ALTER TABLE AAA RENAME COLUMN ID TO AAA_ID;
create table AAA (
ID NUMBER(8),
NAME CHAR(20),
constraint SYS_AAA primary key(ID)
);
//查找约束名字
select c.CONSTRAINT_NAME,c.table_name,cc.COLUMN_NAME from user_constraints c, user_cons_columns cc
where c.constraint_name=cc.constraint_name and c.table_name ='AAA'
AND C.CONSTRAINT_TYPE='P';
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
------------------------------ ------------ -------------
SYS_AAA AAA ID
//查找索引
select index_name,index_type,uniqueness from user_indexes where table_name='AAA';
INDEX_NAME INDEX_TYPE UNIQUENES
------------------------------ --------------------------- ---------
SYS_AAA NORMAL UNIQUE
//查找索引有那些列
SQL> select INDEX_NAME,column_name from dba_ind_columns where table_name ='AAA';
INDEX_NAME COLUMN_NAME
------------------------ --------------
SYS_AAA ID
ALTER TABLE AAA RENAME COLUMN ID TO AAA_ID;
//查找约束名字
select c.CONSTRAINT_NAME,c.table_name,cc.COLUMN_NAME from user_constraints c, user_cons_columns cc
where c.constraint_name=cc.constraint_name and c.table_name ='AAA'
AND C.CONSTRAINT_TYPE='P';
CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
------------------------------ ------------ -------------
SYS_AAA AAA AAA_ID
SQL> select INDEX_NAME,column_name from dba_ind_columns where table_name ='AAA';
INDEX_NAME COLUMN_NAME
------------------------ --------------
SYS_AAA AAA_ID
例子2:更改库表的表名
ALTER TABLE AAA RENAME TO AAA_AAA;
select INDEX_NAME,column_name from dba_ind_columns where table_name ='AAA_AAA';
INDEX_NAME COLUMN_NAME
-------------------- -------------------------------
SYS_AAA AAA_ID
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/35012/showart_430607.html |
|