228k views
2 votes
Write a SELECT statement that selects all of the columns for the catalog view that returns information about foreign keys. How many foreign keys are defined in the AP database?

1 Answer

1 vote

Answer:

SELECT COUNT (DISTICT constraint_name)

FROM apd_schema.constraint_column_usage

RUN

Step-by-step explanation:

General syntax to return a catalog view of information about foreign keys.

SELECT DISTINCT PARENT_TABLE =

RIGHT(Replace(DC.constraint_name, 'fkeys_', ''),

Len(Replace(DC.constraint_name, 'fkeys_', '')) - Charindex('_', Replace(DC.constraint_name, 'fkeys_', ''))),

CHILD_TABLE = DC.table_name,

CCU.column_name,

DC.constraint_name,

DC.constraint_type

FROM apd_schema.table_constraints DC

INNER JOIN apd_schema.constraint_column_usage CCU

ON DC.constraint_name = CCU.constraint_name

WHERE DC.constraint_type LIKE '%foreign'

OR DC.constraint_type LIKE '%foreign%'

OR DC.constraint_type LIKE 'foreign%'

RUN

User Unacorn
by
5.0k points