Home > Enterprise >  Show SQL constraints in multiple tables
Show SQL constraints in multiple tables

Time:09-30

Let say I have these 3 constraint given:

ALTER TABLE actor ADD CONSTRAINT PK_ACTORID PRIMARY KEY (actor_id);
ALTER TABLE film ADD CONSTRAINT PK_FILMID PRIMARY KEY (film_id);
ALTER TABLE film_actor ADD CONSTRAINT FK_FILMID1 FOREIGN KEY (film_id) REFERENCES film;

I need to write sql to show these table constraints:

-- Check which constraints added in ACTOR table
SELECT OWNER, CONSTRAINT_NAME, TABLE_NAME, SEARCH_CONDITION, INDEX_NAME 
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'ACTOR';

-- Check which constraints added in FILM_ACTOR table
SELECT OWNER, CONSTRAINT_NAME, TABLE_NAME, SEARCH_CONDITION, INDEX_NAME 
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'FILM_ACTOR';

And the result end up like:

enter image description here

and

enter image description here

My question is, how can I combine two sql statements I wrote as 1 sql and also formatting the result displayed.

CodePudding user response:

Would changing your where statement work?

Something like this:

SELECT OWNER, CONSTRAINT_NAME, TABLE_NAME, SEARCH_CONDITION, INDEX_NAME 
FROM USER_CONSTRAINTS
WHERE TABLE_NAME = 'ACTOR' or table_name = 'film' or table_name ='film_actor';
  • Related