Skip to content
Advertisement

Can’t return column table when the table have just FK?

When I choose a table when it doesn’t have any Primary Key, my query returns nothing. So what’s the problem in this query?

This is result when I have pkin my table:

enter image description here

And this is result when I haven’t pkin my table:

enter image description here

Here is the query:

select 
    col.name as [Nom de Colone],
        typ.DATA_TYPE + CASE WHEN typ.DATA_TYPE IN('char', 'nchar', 'varchar', 'nvarchar', 'binary', 'varbinary')AND typ.CHARACTER_MAXIMUM_LENGTH > 0 THEN COALESCE('(' + 
        CONVERT(varchar, typ.CHARACTER_MAXIMUM_LENGTH) + ')','') ELSE '' END + CASE WHEN typ.DATA_TYPE IN('decimal', 'numeric') THEN COALESCE('(' + CONVERT(varchar, typ.NUMERIC_PRECISION) + ',' + CONVERT(varchar, typ.NUMERIC_SCALE) + ')','') 
    ELSE '' END AS [Type],
    CASE WHEN col.IS_NULLABLE = 0 THEN 'NOT ' ELSE '' END + 'NULL' AS Valeur,
    case when KU.COLUMN_NAME=col.name then cast(1 as bit) else cast(0 as bit) end as [PK],
    case when fk.object_id is not null then cast(1 as bit) else cast(0 as bit) end as [fk],
    case when pk_tab.name is not null then pk_tab.name else ''end as [Ref_Tab_FK],
    case when pk_col.name is not null then pk_col.name else ''end as [Ref_Col_FK]
from sys.tables tab 
    inner join sys.columns col  
        on col.object_id = tab.object_id 
    left outer join sys.foreign_key_columns fk_cols 
        on fk_cols.parent_object_id = tab.object_id and fk_cols.parent_column_id = col.column_id
    left outer join sys.foreign_keys fk  
        on fk.object_id = fk_cols.constraint_object_id left outer join sys.tables pk_tab 
        on pk_tab.object_id = fk_cols.referenced_object_id 
    left outer join sys.columns pk_col
        on pk_col.column_id = fk_cols.referenced_column_id and pk_col.object_id = fk_cols.referenced_object_id 
    inner join INFORMATION_SCHEMA.COLUMNS typ 
        on typ.COLUMN_NAME=col.name and tab.name=typ.TABLE_NAME 
    inner join INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC 
    on tc.TABLE_NAME=typ.TABLE_NAME and TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
        ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME     
where tab.name='TABLE_NAME' 
order by col.column_id

Advertisement

Answer

As you are putting INNER JOIN with PRIMARY KEY constraint, as given below, only the tables with PRIMARY KEY are returned in the result set.

    inner join INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC 
    on tc.TABLE_NAME=typ.TABLE_NAME and TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
    INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
        ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME     

If you change them to LEFT OUTER JOIN, you will get tables without PRIMARY KEY and just FOREIGN KEY also

    LEFT OUTER join INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS TC 
    on tc.TABLE_NAME=typ.TABLE_NAME and TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
    LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KU
        ON TC.CONSTRAINT_TYPE = 'PRIMARY KEY'
        AND TC.CONSTRAINT_NAME = KU.CONSTRAINT_NAME  
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement