SQL: Exists Condition

The SQL EXISTS condition is a boolean function that returns true if the condition is met. The syntax is pretty simple:

SELECT * FROM TABLE WHERE EXISTS(subquery);

Alternatively, you can use NOT EXISTS(subquery). An example of the function is the following:

SELECT utc.table_name
FROM user_tab_cols utc
WHERE utc.column_name='ACCOUNTID'
AND NOT EXISTS(SELECT uc.table_name
FROM user_constraints uc
WHERE uc.table_name=utc.table_name
AND uc.r_constraint_name='SYS_C00229824')
ORDER BY utc.table_name;

The query looks for all the tables that contain the column ACCOUNTID and from those tables, only get the ones that don’t make reference to the constraint SYS_C00229824.

Advertisements

Software developer. Currently working at MercadoLibre.com and other personal projects.

Tagged with: , ,
Posted in Database Administration

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Error: Please make sure the Twitter account is public.

%d bloggers like this: