sql server - Retrieving column information (composite key) in SQL -
i have large sql database need verify structure of tables , columns (not data itself). need generate list of of tables, each table, of columns, each column, data type, length/precision, ordinal position, , whether it's part of primary key table.
i can of need following query:
select table_name, ordinal_position, column_name, data_type, character_maximum_length, numeric_precision, numeric_scale information_schema.columns
however, i'm not sure how check whether column part of primary key. additionally, tables pk consists of more 1 column, want know ordinal position of each column within key. information i've found far relates setting key rather reading it.
i'm interested in doing in both sql server , oracle.
in sql server can
select k.table_catalog, k.table_name, k.column_name, k.ordinal_position information_schema.key_column_usage k inner join information_schema.table_constraints tc on k.table_catalog = tc.table_catalog , k.table_schema = tc.table_schema , k.constraint_name = tc.constraint_name tc.constraint_type = 'primary key'
or
select object_name(c.object_id) table_name, c.name, ic.index_column_id sys.key_constraints k inner join sys.index_columns ic on k.parent_object_id = ic.object_id , k.unique_index_id = ic.index_id inner join sys.columns c on ic.object_id = c.object_id , ic.column_id = c.column_id k.type = 'pk'
in oracle
select k.owner, k.table_name, k.index_name, c.column_name, c.column_position all_constraints k inner join all_ind_columns c on k.owner = c.index_owner , k.table_name = c.table_name , k.index_name = c.index_name k.constraint_type = 'p'
Comments
Post a Comment