1

So im attempting to get a list of table and any foreign constraints that exist on them for the sake of attempting some automated mariabackup restores in python. im running the following query:

SELECT a.TABLE_NAME, b.CONSTRAINT_NAME from information_schema.tables a
LEFT JOIN information_schema.table_constraints b
    ON a.table_name = b.table_name
WHERE a.table_schema = 'world'
    AND b.constraint_type = 'FOREIGN KEY';

My "world" test table, has 3 columns on it - Country (No FK's), CountryLanguage (one fk) and City (one FK). With the above query i'd be expecting 3 tables, 2 returning the FK name and 1 null however the return i get is only the 2 tables with the FK's on them.

I'm sure im missing something but cant quite pin it.

1 Answer 1

1

You should move the left join condition in the ON clause and not in where

    select a.TABLE_NAME
        , b.CONSTRAINT_NAME 
    from information_schema.tables a
    left join information_schema.table_constraints b
        on a.table_name = b.table_name
            and b.constraint_type = 'FOREIGN KEY';
    where a.table_schema = 'world'

If you use a left join table column in where clause this work as an inner join .. so juts extend the ON condition with the others clause for left joined columns

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.