用于 Oracle 检查约束是否存在的 SQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4389228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:31:33  来源:igfitidea点击:

SQL for Oracle to check if a constraint exists

sqlsql-serveroracle

提问by Ryan Rodemoyer

In SQL Server I can use the SQL below to check if a constraint exists and if it's a primary key, trigger, etc.

在 SQL Server 中,我可以使用下面的 SQL 来检查约束是否存在以及它是否是主键、触发器等。

SELECT * 
    FROM dbo.sysobjects 
    WHERE id = OBJECT_ID(N'[SCHEMA].[TABLENAME]') 
        AND OBJECTPROPERTY(id, N'IsPrimaryKey') = 1

What would be the Oracle equivalent because my query uses SQL Server specific tables to find the answer.

什么是 Oracle 等效项,因为我的查询使用 SQL Server 特定表来查找答案。

回答by Sathyajith Bhat

SELECT * FROM USER_CONSTRAINTS WHERE CONSTRAINT_NAME = 'CONSTR_NAME';

THE CONSTRAINT_TYPEwill tell you what type of contraint it is

THECONSTRAINT_TYPE会告诉你它是什么类型的约束

  • R - Referential key ( foreign key)
  • U - Unique key
  • P - Primary key
  • C - Check constraint
  • R - 引用键(外键)
  • U - 唯一键
  • P - 主键
  • C - 检查约束


To find out if an object is a trigger, you can query USER_OBJECTS. OBJECT_TYPEwill tell you if the object's a trigger, view, procedure et al.

要确定对象是否为触发器,您可以查询USER_OBJECTS. OBJECT_TYPE会告诉您对象是否是触发器、视图、过程等。