I want to get all the columns (MSSQL SERVER) of a particular Table Along with whether it is _PK or Not. So far I have Tried:
SELECT TABLE_CATALOG AS Database_Name, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'TableName'
And its Out Put is:
Database_Name TABLE_SCHEMA TABLE_NAME COLUMN_NAME IS_NULLABLE
------------- ------------ ---------- ----------- -----------
Now I want one More Column to above Table Say 'IS_PRIMARYKEY', means I want the output as:
Database_Name TABLE_SCHEMA TABLE_NAME COLUMN_NAME IS_NULLABLE IS_PRIMARYKEY
------------- ------------ ---------- ----------- ----------- -------------
How ths can be Achieved in One Query/Procedure ?
Thanks
CONSTRAINT DATA IS STORED IN DIFFERENT TABLES HENCE YOU NEED TO APPLY A JOIN TO MATCH TABLE CONSTRAINT AND COLUMN USAGE CONSTRAINT TO GET THE PRIMARY KEY COLUMN.
BUT SINCE YOU NEED THE DETAILS IN A SINGLE QUERY, I HAVE CREATED A ALIAS OF MY QUERY TO GENERATE THE DESIRED OUTPUT. BELOW QUERY WILL GIVE YOU APPROPRIATE ANSWER.
CANNOT CREATE FIDDLE AS THIS IS ONLY HAVING SYSTEM TABLES.
I HAVE DONE THIS FOR MY TEMP TABLE tbl_primaryKey YOU CAN REPLACE THE TABLE NAME AS PER YOUR NEED.
SELECT Database_Name,TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,IS_NULLABLE, CASE WHEN (IS_PRIMARYKEY IS NULL) THEN '' ELSE 'YES' END AS [IS_PRIMARYKEY]
FROM
(SELECT cols.TABLE_CATALOG AS Database_Name, cols.TABLE_SCHEMA, cols.TABLE_NAME, cols.COLUMN_NAME, cols.IS_NULLABLE,
(SELECT Col.Column_Name from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS Tab,
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE Col
WHERE
Col.Constraint_Name = Tab.Constraint_Name
AND Col.Table_Name = Tab.Table_Name
AND Constraint_Type = 'PRIMARY KEY'
AND Col.Table_Name = 'tbl_primaryKey'
AND cols.COLUMN_NAME = Col.COLUMN_NAME
) AS IS_PRIMARYKEY
FROM INFORMATION_SCHEMA.COLUMNS cols
WHERE
cols.TABLE_NAME = 'tbl_primaryKey' ) A
SELECT TABLE_CATALOG AS Database_Name, TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE, if(COLUMN_KEY = 'PRI', 'YES', 'NO') AS IS_PRIMARYLEY
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'TableName'
You can do this by using If condition, for number's of Columns use *
Related
I have two tables, table1 and table2, that have columns common to both.
The queries that obtain the column names of each table are given below,
Query to get columns from first table:
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1';
Query to get columns from second table:
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2';
I need to select data from table2, only the columns which are also in table1.
I do not have a postrgesql intace at the moment but Dynamic SQL is what you need.
The following query will get you the column names that appear in both table 1 and table 2.
select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
)
And you need to build
EXECUTE 'select ' || select string_agg(column_name, ',') FROM (
select column_name from information_schema.columns
where table_schema = 'schema1'
and table_name = 'table1'
intersect
select column_name from information_schema.columns
where table_schema = 'schema2'
and table_name = 'table2'
) || ' from schema2.table2 '
Sory if there any syntax error as writing from mobile app, you can join both result set to get common data.
select column_name from information_schema.columns T2
JOIN (select column_name from information_schema.columns where table_schema = 'schema1' and table_name = 'table1') T1
ON T2.COLUMN_NAME = T1.COLUMN_NAME Where T2.table_schema = 'schema2' and T2.table_name = 'table2';
Aim is to compare tables column_name with the views columns_name to check if they have the same columns , however, getting them with different orders. Wanted to see them both with same order
select t1.COLUMN_NAME, t2.COLUMN_NAME from
(select * from information_schema.columns where TABLE_SCHEMA = 'dmt' and table_name = '#tablename' ) as t1
left join
(select * from information_schema.columns where TABLE_SCHEMA = 'models' and table_name = 'viewName') as t2
on t1.ORDINAL_POSITION = t2.ORDINAL_POSITION
I want get the output like in the same order, for example:
Column_Name 1 Column_Name 2
cat cat
dog dog
You can use the following to get the comparison of the columns.
WITH ColumnsCTE (TABLE_NAME, COLUMN_NAME) AS
(
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dmt' AND TABLE_NAME = 'TableName'
UNION ALL
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'models' AND TABLE_NAME = 'ViewName'
)
-- get the different columns of the tables / views.
SELECT t1.TABLE_NAME, t1.COLUMN_NAME AS [COLUMN_NAME 1], ISNULL(t2.COLUMN_NAME, 'missing on other table / view') AS [COLUMN_NAME 2]
FROM ColumnsCTE AS t1 LEFT JOIN ColumnsCTE AS t2 ON t1.COLUMN_NAME = t2.COLUMN_NAME AND t1.TABLE_NAME <> t2.TABLE_NAME
WHERE t2.COLUMN_NAME IS NULL
demo on dbfiddle.uk
You can't use the ORDINAL_POSITION to get the expected result since the order of the columns can be different. See the following example where the ORDINAL_POSITION is different but the columns are available in both tables / views:
CREATE TABLE Test (
id INT,
name VARCHAR(100)
);
CREATE VIEW ViewTest AS
SELECT name, id FROM Test;
If the order of the attributes is identitcal (you can hardcode which attributes to select and in which order)
i strongly recommend
"intersect" or "except" command
increddibly strong function offering almost anything you could wish for here.
I am using the following SQL to grab index columns for a table:
SELECT DISTINCT COLUMN_NAME
FROM DBA_IND_COLUMNS
WHERE TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'";
I want to adjust this SQL such that I grab the index columns and their data type:
SELECT DISTINCT COLUMN_NAME, DATA_TYPE
FROM DBA_IND_COLUMNS
WHERE TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'";
But this gives an invalid identifier error for "DATA_TYPE". Is there a way to do this without creating another query?
You need to add DBA_TAB_COLUMNS to your query:
SELECT DISTINCT COL.COLUMN_NAME, COL.DATA_TYPE
FROM DBA_IND_COLUMNS IND
INNER JOIN DBA_TAB_COLUMNS COL
ON ( IND.TABLE_OWNER = COL.OWNER AND IND.TABLE_NAME = COL.TABLE_NAME AND IND.COLUMN_NAME = COL.COLUMN_NAME)
WHERE IND.TABLE_NAME = 'MY_TABLE' AND TABLE_OWNER = 'SCHEMA'
I have names of primary keys in a variable and I need to find the table to which they belong. The db has many table so linear search is not an option.
You can use the information_schema tables. If the primary key name is the first column in the table, you can just do:
select table_name
from information_schema.columns
where column_name in (<your list here>) and
ordinal_position = 1;
Otherwise, you have to go through the constraints to get what you want. Something like:
select kcu.table_name, kcu.column_name
from information_schema.table_constraints tc join
information_schema.key_column_usage kcu
on tc.contraint_name = kcu.contraint_name and
tc.table_name = kcu.table_name
where tc.contraint_type = 'PRIMARY KEY' and
column_name in (<your list here>);
You can also do this using the system tables and views.
you can try this out ::
SELECT table_name
FROM information_Schema.columns
WHERE column_name='dept_id'
and ordinal_position = 1;
This should work, please try it:
SELECT table_name
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE OBJECTPROPERTY(OBJECT_ID(constraint_name), 'IsPrimaryKey') = 1
AND column_name in (select column_name from <your list here>)
Below solution wouldn't work if the primary key constraint is set to any column other than that of the first column in `CREATE TABLE' query.
select table_name
from information_schema.columns
where column_name in (select column_name from <your list here>) and
ordinal_position = 1
For example, it wouldn't work if we create table like this:
create table sample1
(
field1 int,
field2 int primary key
)
Please correct me if I am wrong.
The below query gives tablename based on constraint name
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
where CONSTRAINT_NAME ='<ConstraintName>'
To find table name using primary key name follow this below query
select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS where
CONSTRAINT_NAME =<ConstraintName>
---Find the below views to get constraint details in a db select * from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS select * from
INFORMATION_SCHEMA.TABLE_CONSTRAINTS select * from
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
improved from above answer
select kcu.table_name, kcu.column_name, *
from information_schema.table_constraints tc join
information_schema.key_column_usage kcu
on tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME and
tc.table_name = kcu.table_name
where tc.CONSTRAINT_NAME = 'PK__DC_INPUT__7EA5540496A9FD5D'
SELECT [TABLE_NAME]
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS
WHERE CONSTRAINT_NAME = 'YourPrimaryKey';
How to know the Column position in a table in MS-SQL.
Eg: if a table consists of 3 columns namely column1, column2 and column3.
I should write a query so that i can get the position of column3 as 3
You will get all these from information_schema.
select ordinal_position from information_schema.columns
where schema_name = 'databasename'
and table_name = 'tablename'
and column_name = 'column name'
There're two ways to do this:
select colid
from sys.syscolumns
where id = object_id('schemaname.tablename') and name = 'column3'
and
select ordinal_position
from information_schema.columns
where
schema_name = 'schemaname' and
table_name = 'tablename' and
column_name = 'column3'
Here's an article about why you have to avoid information_schema views - The case against INFORMATION_SCHEMA views, I don't have to write this types of query often, so I don't really care about it, but sys.syscolumns tends to be a bit faster because it doesn't have many redundant joins which you may not need.
OTOH, information_schema views are ISO standard - here's dicussion about this - SQL Server: should I use information_schema tables over sys tables?
try :
SELECT ORDINAL_POSITION
FROM information_schema.columns
WHERE table_name = 'YourTableName' AND COLUMN_NAME = 'YourColumnName'
Try the query and check for the result.
select column_name,ordinal_position
from information_schema.columns
where table_catalog = 'yourdatabasename'
and table_schema = 'yourschemaname'
and table_name = 'yourtablename'