137

I have defined a name for each of the constraint for the multiple tables that I have created in Oracle SQL.

The problem is that to drop a constraint for the column of a particular table I need to know the name that I have supplied for each constraints, which I have forgotten.

How do I list out all the names of constraints that I have specified for each column of a table?

Is there any SQL statement for doing so?

Jeris
  • 2,355
  • 4
  • 26
  • 38

8 Answers8

193

You need to query the data dictionary, specifically the USER_CONS_COLUMNS view to see the table columns and corresponding constraints:

SELECT *
  FROM user_cons_columns
 WHERE table_name = '<your table name>';

FYI, unless you specifically created your table with a lower case name (using double quotes) then the table name will be defaulted to upper case so ensure it is so in your query.

If you then wish to see more information about the constraint itself query the USER_CONSTRAINTS view:

SELECT *
  FROM user_constraints
 WHERE table_name = '<your table name>'
   AND constraint_name = '<your constraint name>';

If the table is held in a schema that is not your default schema then you might need to replace the views with:

all_cons_columns

and

all_constraints

adding to the where clause:

   AND owner = '<schema owner of the table>'
Bob
  • 5,510
  • 9
  • 48
  • 80
Ollie
  • 17,058
  • 7
  • 48
  • 59
  • Typo: `USER_CONS_COLUMNS` – Paul Draper Sep 25 '13 at 03:54
  • 3
    is case sensitive, I think; It should be in upper case. – Kanagavelu Sugumar Dec 20 '13 at 06:26
  • 1
    The `owner` field in both (user|all|dba)_constraints and (user|all|dba)_cons_columns is the owner of the constraint, not the owner of the table (per Oracle documentation). The table owner is not an available field in either of these views. Does this mean that the constraint owner and the table owner must be the same? – David Fletcher Dec 08 '19 at 16:32
  • Table name should be in `UPPERCASE` it's mandatory. Otherwise exists table will show error like `no rows selected`. – Sabbir Sobhani Jun 21 '22 at 15:20
18
SELECT * FROM USER_CONSTRAINTS
Paciv
  • 1,487
  • 10
  • 16
14

maybe this can help:

SELECT constraint_name, constraint_type, column_name
from user_constraints natural join user_cons_columns
where table_name = "my_table_name";

cheers

Van Gogh
  • 465
  • 1
  • 7
  • 8
9
select constraint_name,constraint_type 
from user_constraints
where table_name = 'YOUR TABLE NAME';

note: table name should be in caps.

In case you don't know the name of the table then,

select constraint_name,constraint_type,table_name 
from user_constraints;
Noel
  • 10,152
  • 30
  • 45
  • 67
kapil kumar
  • 91
  • 1
  • 1
9

Often enterprise databases have several users and I'm not aways on the right one :

SELECT * FROM ALL_CONSTRAINTS WHERE table_name = 'YOUR TABLE NAME' ;

Picked from Oracle documentation

Gweltaz Niquel
  • 629
  • 4
  • 14
1

Use either of the two commands below. Everything must be in uppercase. The table name must be wrapped in quotation marks:

--SEE THE CONSTRAINTS ON A TABLE
SELECT COLUMN_NAME, CONSTRAINT_NAME FROM USER_CONS_COLUMNS WHERE TABLE_NAME = 'TBL_CUSTOMER';

--OR FOR LESS DETAIL
SELECT CONSTRAINT_NAME FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'TBL_CUSTOMER';
Wachaga Mwaura
  • 3,310
  • 3
  • 28
  • 31
1
select a.constraint_name   as f_key,
   a.owner             as f_owner,
   a.table_name        as f_table,
   a.r_constraint_name as p_key,
   a.r_owner           as p_owner,
   b.table_name        as p_table

from all_constraints a inner join all_constraints b on a.r_constraint_name = b.constraint_name

-1

An easy way to this in MySQL is -

SHOW INDEXES IN <table-name>;

It shows the key names for the constraints

Payel Senapati
  • 1,134
  • 1
  • 11
  • 27