I have found that how to determine what columns are primary key column of a given table by using this query:
SELECT CONSTRAINT_NAME, COLUMN_NAME
FROM
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
WHERE TABLE_NAME='tablename_here' AND
CONSTRAINT_NAME LIKE 'PK_%'
I can find , what the identity seed and increment is by using this query:
SELECT IDENT_SEED('tablename_here'), IDENT_INCR('tablename_here')
I cant use the constraint information, because a primary key constraint can be across multiple columns. And i cant seem to find any Transact SQL function to give my the identity information.
Can anybody help me to understand how to find the identity information?
I am using SQL Server 2000.
To find the IDENTITY column in a given table you can use this:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='tablename_here'
AND COLUMNPROPERTY(OBJECT_ID('tablename_here'),COLUMN_NAME,'IsIdentity') = 1
You can use the COLUMNPROPERTY function to check whether a column uses the identity property.
SELECT sys.tables.name, sys.columns.name
FROM sys.tables
INNER JOIN sys.columns
ON sys.tables.object_id = sys.columns.object_id
WHERE is_identity = 1
AND sys.tables.name = 'MyTable'
Related
What is the most efficient way in SQL Server to identify all tables that have a unique index?
This has been addressed elsewhere for Oracle: https://stackoverflow.com/a/28740458/3112914
My end goal is to identify tables in a database that can not be compared using SSDT Data Compare tool. For that tool to work "Tables must have the same primary key, unique index, or unique constraint." I can identify those with primary key or unique constraint using OBJECTPROPERTY, e.g.
SELECT
SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName,
OBJECTPROPERTY(OBJECT_ID,'TableHasPrimaryKey') AS HasPrimaryKey,
OBJECTPROPERTY(OBJECT_ID,'TableHasUniqueCnst') AS HasUniqueConstraint
FROM
sys.tables
There is an IsIndexed property but that doesn't say that is a unique index. https://learn.microsoft.com/en-us/sql/t-sql/functions/objectproperty-transact-sql?view=sql-server-2017
If you want to list all the tables that have a unique index you can join to sys.indexes and filter using column is_unique.
e.g.
-- list of tables that have a unique index
SELECT SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName
FROM
sys.tables
WHERE EXISTS (SELECT *
FROM sys.indexes i
WHERE i.object_id = tables.object_id AND is_unique = 1)
If you want to extend the show every table, along with a value indicating whether or not it has a unique index (ie extend the start you had above), you can wrap that EXISTS statement in an IIF or CASE, like this :
SELECT SCHEMA_NAME(schema_id) AS SchemaName,
name AS TableName,
OBJECTPROPERTY(OBJECT_ID, 'TableHasPrimaryKey') AS HasPrimaryKey,
OBJECTPROPERTY(OBJECT_ID, 'TableHasUniqueCnst') AS HasUniqueConstraint,
CASE
WHEN EXISTS (
SELECT *
FROM sys.indexes i
WHERE i.object_id = tables.object_id
AND is_unique = 1
)
THEN 1
ELSE 0
END AS HasUniqueIndex
FROM sys.tables
I need to make some mechanism that checks if a table X is columnstored or not and if it is not to convert it.
I am aware that to convert a table X for example to clustered columnstore index we can use the following code:
CREATE CLUSTERED COLUMNSTORE INDEX MyColumnStoreIndex
ON Table_X
I would like to make a query that checks if a table contains columnstore index.
Thanks in advance.
You can query sys.indexes and check the type and type_desc columns. According to the documentation values 5 and 6 hold information whether an index is clustered or nonclustered columnstore:
select *
from sys.indexes
where type in (5, 6)
you can query the sys.indexes as mentioned in Rigerta's answer.
To also get the tablename that belongs to found indexes you can do following query
select i.name as indexname,
t.name as tablename
from sys.indexes i
join sys.tables t on i.object_id = t.object_id
where i.type in (5, 6)
I am working on an application that we will be using to maintain our database dictionary (description of each column in each table) and would like to build a "Refresh" function that can go to the database and get the updated column list/names.
What I'm having issues with is when a column is renamed/moved, I would like to track if there is way to update the database with the new details. I tried using the Column_ID from syscolumns, but this changes when the column is moved so seems to be an arbitrary number.
Is there any unique ID that SQL Server generates for a column that doesn't change?
My alternative is to add the new column and mark the old one as removed.
Thanks
Why don't you store the description in the column's description property. Then when the column is updated the developer would be responsible for updating this.
I wrote a web font-end to this over 10 years ago.... I could send you the code if you want? Looked like:
You can try this... its a composite of two queries:
This will get you the list of coumns inside a table:
SELECT c.object_id, OBJECT_NAME(c.object_id) AS OBJECT_NAME
,c.NAME AS COLUMN_NAME
,t.NAME AS DATA_TYPE
,c.max_length AS MAX_LENGTH
FROM sys.all_columns c
INNER JOIN sys.types t ON t.system_type_id = c.system_type_id
WHERE object_name(c.object_id) = 'TableName'
You can generate a HashCode of the column name and combine it with object_id... which should be unique unless the column name changed
Hope this will help.
I would add a new column and mark the old one as removed.
[edit]
I thought that what SQL server deleted the table and create it in the first place, and after your question I went to investigate:
If you mark the option of "Prevent saving changes that require table re creation" (in Options -> Designers -> Table and database designers) and then create the following table:
CREATE TABLE [dbo].[testTbl](
[colA] [nchar](10) NULL,
[colB] [smallint] NOT NULL
) ON [PRIMARY]
and even insert some values:
INSERT INTO dbo.testTbl
VALUES ('text1', 12)
,('text2', 22)
,('text3', 32)
,('text4', 42)
,('text5', 52)
And then run the code from above you will get:
SELECT c.object_id, OBJECT_NAME(c.object_id) AS OBJECT_NAME
,c.NAME AS COLUMN_NAME
,t.NAME AS DATA_TYPE
,c.max_length AS MAX_LENGTH
FROM sys.all_columns c
INNER JOIN sys.types t ON t.system_type_id = c.system_type_id
WHERE object_name(c.object_id) = 'testTbl'
You will get the following values:
If you try to do now the following changes (in the table designer)you will find out that all require drop and re create from the database:
Change a NULLABLE column to non-nullable
change non-nullable column to a nullable
(which is to be expected; those changes require the columns to move around the page)
change the datatype of a column (nchar(10) to more or less, smallint to tinyint or int)
but if you want to change the column from B to C, SQL can manage it, and the results would be:
The object id refers to the table id, and therefore the table stayed with the same object id.
I have a database with about 200 tables with many foreign keys constraints. There is no data in the database and I need to insert rows into a specified table X.
Because of the many foreign keys I don't know the order of how to insert in other tables so that I can insert into table X. Some of foreign key constraint are hierarchical.
How can I find out the order of inserting so that I can successfully insert data into table X? Is there any SQL query that help me?
Edited
I want result in a table with column "TableName" ,"ParentTableDependece" that show tree view and can get select from it
Using this snippet, you can find all other tables and the foreign keys involved that reference your own table X:
;WITH ReferencingFK AS
(
SELECT
fk.Name AS 'FKName',
OBJECT_NAME(fk.parent_object_id) 'ParentTable',
cpa.name 'ParentColumnName',
OBJECT_NAME(fk.referenced_object_id) 'ReferencedTable',
cref.name 'ReferencedColumnName'
FROM
sys.foreign_keys fk
INNER JOIN
sys.foreign_key_columns fkc ON fkc.constraint_object_id = fk.object_id
INNER JOIN
sys.columns cpa ON fkc.parent_object_id = cpa.object_id AND fkc.parent_column_id = cpa.column_id
INNER JOIN
sys.columns cref ON fkc.referenced_object_id = cref.object_id AND fkc.referenced_column_id = cref.column_id
)
SELECT
FKName,
ParentTable,
ParentColumnName,
ReferencedTable,
ReferencedColumnName
FROM
ReferencingFK
WHERE
ReferencedTable = 'X' -- <=== put your table name here!
ORDER BY
ParentTable, ReferencedTable, FKName
Once you have all the tables that reference X, you might also need to repeat this for other tables (if they in turn depend on foreign key references)
Why not this?
EXEC sp_msdependencies #objname = 'X'
Raj
You can try:
EXEC sp_help 'TableName'
This will give you the whole details of the table i.e you can get the relations, indexes and other information related with the object you pass with it.
Is there any way given a column, which table it belongs using SQL Query?
yes, Assuming this is SQL server db, you can check the below query -
select [name], object_name(id) from sys.columns where [name] like '%columnname%'
the object_name(id) will give you the table name for your columnname specified.
You can try something like this using Sql Server 2005+
SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns c
WHERE c.name = '<column name>'
Try
SELECT OBJECT_NAME(id)
FROM syscolumns
WHERE [name] = 'mycolumn'
Using the syscolumns table (and assuming you just have the column name), you can figure out which tables contain a column with that name. But beyond that you might be stuck.
If you are trying to figure out which table a particular column in a query came from then the best bet is to alias all columns at the time you write queries. I would not accept any code in a code review that doesn't do this because it is a pain to figure out later.