标签:
【外键--通过子表查询】
SELECT ‘父表:‘||USER_INDEXES.TABLE_NAME||‘ ‘||‘父表列:‘||USER_IND_COLUMNS.COLUMN_NAME||‘ ‘||‘子表:‘||USER_CONS_COLUMNS.TABLE_NAME||‘ ‘||‘子表列:‘||USER_CONS_COLUMNS.COLUMN_NAME
FROM USER_CONSTRAINTS,USER_CONS_COLUMNS,USER_INDEXES,USER_IND_COLUMNS where USER_CONSTRAINTS.CONSTRAINT_NAME = USER_CONS_COLUMNS.CONSTRAINT_NAME and
USER_CONSTRAINTS.R_CONSTRAINT_NAME = USER_INDEXES.INDEX_NAME and USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME and CONSTRAINT_TYPE = ‘R‘ and USER_CONS_COLUMNS.TABLE_NAME=‘YX_PRIZE_WIN_AREA‘;
【外键--通过父表查询】
SELECT ‘父表:‘||USER_INDEXES.TABLE_NAME||‘ ‘||‘父表列:‘||USER_IND_COLUMNS.COLUMN_NAME||‘ ‘||‘子表:‘||USER_CONS_COLUMNS.TABLE_NAME||‘ ‘||‘子表列:‘||USER_CONS_COLUMNS.COLUMN_NAME
FROM USER_CONSTRAINTS,USER_CONS_COLUMNS,USER_INDEXES,USER_IND_COLUMNS where USER_CONSTRAINTS.CONSTRAINT_NAME = USER_CONS_COLUMNS.CONSTRAINT_NAME and
USER_CONSTRAINTS.R_CONSTRAINT_NAME = USER_INDEXES.INDEX_NAME and USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME and CONSTRAINT_TYPE = ‘R‘ and USER_INDEXES.TABLE_NAME=‘YX_AC_LOTTERY_MODEL‘;
【查询主键】
SELECT
‘约束名:‘||USER_CONS_COLUMNS.CONSTRAINT_NAME||‘ ‘||
‘表名:‘||USER_CONS_COLUMNS.TABLE_NAME||‘ ‘||
‘列名:‘||USER_CONS_COLUMNS.COLUMN_NAME||‘ ‘||
‘位置:‘||USER_CONS_COLUMNS.POSITION
FROM USER_CONSTRAINTS,USER_CONS_COLUMNS
WHERE USER_CONSTRAINTS.CONSTRAINT_NAME = USER_CONS_COLUMNS.CONSTRAINT_NAME and CONSTRAINT_TYPE = ‘P‘
and USER_CONS_COLUMNS.TABLE_NAME=‘‘;
其他约束
SELECT
‘约束名:‘||USER_CONS_COLUMNS.CONSTRAINT_NAME||‘ ‘||
‘表名:‘||USER_CONS_COLUMNS.TABLE_NAME||‘ ‘||
‘列名:‘||USER_CONS_COLUMNS.COLUMN_NAME||‘ ‘||
‘位置:‘||USER_CONS_COLUMNS.POSITION||‘ ‘||
CONSTRAINT_TYPE,
SEARCH_CONDITION
FROM
USER_CONSTRAINTS
JOIN USER_CONS_COLUMNS
ON (USER_CONSTRAINTS.CONSTRAINT_NAME
= USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
CONSTRAINT_TYPE IN (‘C‘, ‘V‘, ‘O‘);
【让约失效、生效】
alter table EMP disable constraint PK_EMP_EMPNO;
alter table EMP enable constraint PK_EMP_EMPNO;
【查询某表下约束】
select ‘CONSTRAINT_NAME:‘||CONSTRAINT_NAME||‘,‘||‘CONSTRAINT_TYPE:‘||CONSTRAINT_TYPE||‘,‘||‘TABLE_NAME:‘||TABLE_NAME||‘,‘||
‘STATUS:‘||STATUS from all_constraints WHERE table_name = ‘YX_ACTIVITY‘ and owner = ‘HUODONGPLATNEW‘;
-----------去掉外键约束---------------
select ‘alter table ‘||u1.TABLE_NAME||‘ disable constraint ‘||u1.CONSTRAINT_NAME||‘;‘ from USER_CONS_COLUMNS u1,USER_CONSTRAINTS u2 where u1.CONSTRAINT_NAME=u2.CONSTRAINT_NAME and u2.CONSTRAINT_TYPE=‘R‘ and u1.TABLE_NAME=‘B‘;
create table a ( id int,name varchar2(2));
create table b ( id int,name varchar2(2));
ALTER TABLE b ADD CONSTRAINT FK_name FOREIGN KEY(id) REFERENCES a(id);
alter table b add constraint pk_student1 primary key(id);
alter table a add constraint pk_student2 primary key(id);
---------------------生产B2B------------------------
DUMP_DIR2 /export/home/oracle/b2b_test
grant read, write on directory DUMP_DIR2 to eygle;
-----------------------63测试-----------------------
DUMP_DIR2 /export/home/oracle/b2b_test
grant read, write on directory DUMP_DIR2 to eygle;
day5_查看表主外键关系
标签:
原文地址:http://www.cnblogs.com/xiaoxiao5ya/p/d63baaad1d59130ba17aaeb13a624e95.html