Beh*_*hti 1 oracle unique-constraint
如何获得Oracle数据库中对应列的唯一的constraint_name?
换句话说,我想按列名获取约束名称。
伪查询此问题:
select constraint_name from table_name tname where tname.column_name = 'columnName';
Run Code Online (Sandbox Code Playgroud)
这可能吗?
使用 Oracle 字典视图ALL_CONS_COLUMNS或USER_CONS_COLUMNS(如果您想通过列类型限制它 - 即唯一或主键约束 - 那么您可以加入ALL_CONSTRAINTS或USER_CONSTRAINTS):
SELECT acc.constraint_name
FROM ALL_CONS_COLUMNS acc
INNER JOIN ALL_CONSTRAINTS ac
ON ( acc.CONSTRAINT_NAME = ac.CONSTRAINT_NAME )
WHERE ac.OWNER = 'YOUR_SCHEMA_NAME'
AND ac.TABLE_NAME = 'YOUR_TABLE_NAME'
AND acc.COLUMN_NAME = 'YOUR_COLUMN_NAME'
AND ac.CONSTRAINT_TYPE IN ( 'U', 'P' ) -- Unique or primary key constraints
Run Code Online (Sandbox Code Playgroud)
或者:
SELECT ucc.constraint_name
FROM USER_CONS_COLUMNS ucc
INNER JOIN ALL_CONSTRAINTS uc
ON ( ucc.CONSTRAINT_NAME = uc.CONSTRAINT_NAME )
WHERE uc.OWNER = 'YOUR_SCHEMA_NAME'
AND uc.TABLE_NAME = 'YOUR_TABLE_NAME'
AND ucc.COLUMN_NAME = 'YOUR_COLUMN_NAME'
AND uc.CONSTRAINT_TYPE IN ( 'U', 'P' ) -- Unique or primary key constraints
Run Code Online (Sandbox Code Playgroud)
尝试这个。
select CONSTRAINT_NAME from USER_CONS_COLUMNS
where table_name='YOUR_TABLE'
and column_name='YOUR_COLUMN'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4838 次 |
| 最近记录: |