How can we check that table have index or not ? if have how to find that index for a particular column for a table?
Regards,
kumar
In SQL Server Management Studio you can navigate down the tree to the table you're interested in and open the indexes node. Double clicking any index in that node will then open the properties dialog which will show which columns are included in the index.
If you would like to use T-SQL, this might help:
SELECT
sys.tables.name,
sys.indexes.name,
sys.columns.name
FROM sys.indexes
INNER JOIN sys.tables ON sys.tables.object_id = sys.indexes.object_id
INNER JOIN sys.index_columns ON sys.index_columns.index_id = sys.indexes.index_id
AND sys.index_columns.object_id = sys.tables.object_id
INNER JOIN sys.columns ON sys.columns.column_id = sys.index_columns.column_id
AND sys.columns.object_id = sys.tables.object_id
WHERE sys.tables.name = 'TABLE NAME HERE'
ORDER BY
sys.tables.name,
sys.indexes.name,
sys.columns.name
ordering by column name is wrong, you need to order by the position in the index, so order by clause should be tabname, indname and sys.index_columns.index_column_id...
Try
select object_name(object_id),* from sys.indexes
where object_name(object_id) = 'your table name'
Related
enter image description here
I am new to databases, I just created a table using "New Table", but I want to list of columns and their properties as shown in the screenshot.
What is the SQL command for this? I googled it before coming here but it's of no use, any help is appreciated. Thanks
You Can Use This Query:
SELECT * from INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='YourTableName'
and this link may be helpful.
visit https://learn.microsoft.com/en-us/sql/relational-databases/system-information-schema-views/columns-transact-sql?view=sql-server-ver15
You can use this Query
SELECT TAB.name AS TableName, COL.name AS ColumnName, TYP.name AS DataTypeName,
TYP.max_length AS MaxLength
From sys.columns COL
INNER JOIN sys.tables TAB
On COL.object_id = TAB.object_id
INNER JOIN sys.types TYP
ON TYP.user_type_id = COL.user_type_id
I am using SQL Server 2008 R2.
I have a table in which I have a column that have a not null constraint.
Now, what if I want to check if column has not null constraint defined or not for specific column?
Is there any query to find out it?
Thanks in advance..
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
This query will show all columns from all tables and a whole host of information on them. The column you would want is: IS_NULLABLE that can have the value 'YES' or 'NO'
COLUMNS (Transact-SQL)
Something like
SELECT o.name AS tab, c.name AS col, c.is_nullable
FROM sys.objects o
INNER JOIN sys.columns c ON c.object_id = o.object_id
WHERE o.name like '%yourtable%' and type = 'U'
See sys.columns and sys.objects
There's some catalog views you can use:
// information about check constraints
select * from sys.check_constraints
// information about specific columns
select name, is_nullable from sys.columns
// information about tables
select * from sys.tables
The sys.columns is_nullablefield holds information about nullability.
there is a table sys.all_columns
and a column in this table called is_nullable
http://technet.microsoft.com/en-us/library/ms177522(v=sql.105).aspx
select s.name, c.name, c.is_nullable from sys.tables s, sys.all_columns c
where s.object_id = c.object_id
and s.type = 'U' -- USER_TABLE
and c.is_nullable = 1
It is a simple command that will list - Field, Type, Null, Key, Default
SHOW FIELDS FROM Your_Table_Name;
I have a database and a lot of tables inside it. I wrote some information into the each table and column's decription part. And now using query i want to see all table and columns descriptions.
Note: DATABASE -> ms sql server
Can you please help me ?
You can see that using INFORMATION_SCHEMA
To get columns for each table you can do:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
To get table information you can do:
SELECT * FROM INFORMATION_SCHEMA.TABLES
Check this query:
SELECT
t.name AS TableName
, td.value AS TableDescription
, c.name AS ColumnName
, cd.value AS ColumnDescription
FROM sys.tables t
INNER JOIN sys.columns c ON t.object_id = c.object_id
LEFT JOIN sys.extended_properties td
ON td.major_id = t.object_id
AND td.minor_id = 0
AND td.name = 'MS_Description'
LEFT JOIN sys.extended_properties cd
ON cd.major_id = t.object_id
AND cd.minor_id = c.column_id
AND cd.name = 'MS_Description'
select * from INFORMATION_SCHEMA.TABLES
select * from INFORMATION_SCHEMA.COLUMNS
select * from user_col_comments;
This will display all tables's column with comments for the logged in user.
select * from user_col_comments where table_name = '<table name>';
This will display specified tables's column with comments for the logged in user.
desc table_name query is used to describe the table
Can anyone provide me the query in sql server to extract the tables that have been done indexing, for a particular database....
Your question is somewhat unclear. This will return all tables with at least one index.
select DISTINCT OBJECT_NAME(object_id)
from sys.indexes
where type<>0
Or for SQL Server 2000
select DISTINCT OBJECT_NAME(id)
from sysindexes
where indid<>0
select object_name(object_id),* from sys.indexes where type <> 0
This will return you all the indexes available in your database. But beware, it also lists the system tables.
The sys.indexes DMV should have what you're looking for:
SELECT TableName = object_name(Object_Id)
, IndexName = Name
, IndexType = Type_Desc
FROM sys.indexes
The Type_Desc column will tell you whether you're looking at a heap, a clustered index, or a non-clustered index.
Joining to sys.tables will limit the results to user tables and leave out system tables:
SELECT TableName = st.Name
, IndexName = si.name
, IndexType = si.type_desc
FROM SYS.indexes si
JOIN SYS.tables st
ON si.object_id = st.object_id
How can I search my sql database for a table that contains a field tiEntityId. This field is referenced in a stored procedure, but I am unable to identify which table this id is a primary key for? Any suggestions? I currently look through stored procedure definitions for references to text by using something like this:
Declare #Search varchar(255)
SET #Search='[10.10.100.50]'
SELECT DISTINCT
o.name AS Object_Name,o.type_desc
FROM sys.sql_modules m
INNER JOIN sys.objects o ON m.object_id=o.object_id
WHERE m.definition Like '%'+#Search+'%'
ORDER BY 2,1
Any SQL guru's out there know what I need to use to find the table that contains the field, preferably the table where that field is the Primary Key.
You can do:
select table_name
from INFORMATION_SCHEMA.COLUMNS
where column_name = 'MyColumn'
If you're looking for primary keys that contain a column with a given name (in SQL 2005+), here you go:
select so.name as TableName,
si.name as IndexName,
sc.name as ColumnName
from sys.indexes si
join sys.index_columns sic
on si.object_id = sic.object_id
and si.index_id = sic.index_id
join sys.columns sc
on si.object_id = sc.object_id
and sic.column_id = sc.column_id
join sys.objects so
on si.object_id = so.object_id
where sc.name like '%ColumnName%'
and si.is_primary_key = 1