Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL for Oracle to check if a constraint exists

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

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.

like image 994
Ryan Rodemoyer Avatar asked Dec 08 '10 15:12

Ryan Rodemoyer


People also ask

How do you check if a constraint exists or not?

The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a column it will allow only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

How can check constraint in SQL query?

The syntax for enabling a check constraint in SQL Server (Transact-SQL) is: ALTER TABLE table_name WITH CHECK CHECK CONSTRAINT constraint_name; table_name. The name of the table that you wish to enable the check constraint.

How do you check if data exists in a table in Oracle?

Type a short Oracle program, using the following code as a guide: DECLARE record_exists INTEGER; BEGIN SELECT COUNT(*) INTO record_exists FROM your_table WHERE search_field = 'search value' AND ROWNUM = 1; IF record_exists = 1 THEN DBMS_OUTPUT. put_line('Record Exists') ELSE DBMS_OUTPUT.

Can we use if exists in Oracle?

The Oracle EXISTS condition is used in combination with a subquery and is considered "to be met" if the subquery returns at least one row. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.


1 Answers

SELECT * FROM USER_CONSTRAINTS WHERE CONSTRAINT_NAME = 'CONSTR_NAME';

THE CONSTRAINT_TYPE will tell you what type of contraint it is

  • R - Referential key ( foreign key)
  • U - Unique key
  • P - Primary key
  • C - Check constraint

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

like image 139
Sathyajith Bhat Avatar answered Sep 18 '22 08:09

Sathyajith Bhat