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;
Related
Suppose there are 2 catalogs. First is 'master', second is 'test'. I want to see columns of test catalog's tables when my current catalog is master.
There is command is like:
SELECT
sys.columns.name AS ColumnName
FROM
sys.columns
JOIN
sys.tables ON sys.columns.object_id = tables.object_id
This command getting current schemas columns. It means I have to change my catalog master to test. Is there any way to show columns without USE test command?
You could use 3-part name:
SELECT c.name AS ColumnName
FROM master.sys.columns c
JOIN master.sys.tables t
ON c.object_id = t.object_id
UNION ALL
SELECT c.name AS ColumnName
FROM test.sys.columns c
JOIN test.sys.tables t
ON c.object_id = t.object_id
I have a table which has a default value in a column. That default value is visible in column_default column of information_schema.columns table, but that doesn't show up in sys.default_constraints view.
Can anybody say why it is so?
If the default is showing in INFORMATION_SCHEMA.COLUMNS as something like create default X as Y then it exists as part of a bound default, a deprecated means of applying default values.
It will not show up in sys.default_constraints because it isn't part of the more "modern" constraint system.
The default constraint details are showing in the sys.default_constraints. But it doesn't contains which column it belongs to.
Need to join with sys.columns, sys.objects, sys.schemas you will get the actual details. I'm passing the table name in the WHERE clause to get the default constraints details from the both tables:
SELECT c.[name], col.[name], c.[definition]
FROM sys.default_constraints c
INNER JOIN sys.columns col ON col.default_object_id = c.object_id
INNER JOIN sys.objects o ON o.object_id = c.parent_object_id
INNER JOIN sys.schemas s ON s.schema_id = o.schema_id
WHERE o.[name] = 'TableName'
GO
SELECT column_name, column_default
FROM information_schema.columns
WHERE Table_name = 'TableName'
AND column_default IS NOT NULL
GO
sys.default_constraints related code used from this post
I came across this page when trying to find answers and wanted to share my solution for anyone else in the same boat:
My problem was that I was trying to match the sys.tables.object_id with sys.default_constraints.object_id, when I should have been trying to match with sys.default_constraints.parent_object_id. Make sure you have the right column in your query.
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
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
I'm collecting metadata using the sys.* views, and according to the documentation, the sys.identity_columns view will return the seed and increment values like so.
CREATE TABLE ident_test (
test_id int IDENTITY(1000,10),
other int
)
SELECT name, seed_value, increment_value
FROM sys.identity_columns
WHERE object_id = OBJECT_ID( 'ident_test' )
However, the above query just returns one column. Is it just me?
(Note: I've had to change this question somewhat from its earlier version.)
Shouldn't you reverse the from and join, like this:
SELECT c.name, i.seed_value, i.increment_value
from sys.identity_columns i
join sys.columns c
ON i.object_id = c.object_id
AND i.column_id = c.column_id
You are missing the Where clause. Your query is effectively saying 'Give me all of sys.columns and any matching rows from sys.identity_columns you have (but give me null if there are no matching rows)'.
By adding the Where clause below you'll change it to only return where an exact match is returned, which is the same as an inner join in this instance really.
SELECT
c.name, i.seed_value, i.increment_value
FROM
sys.columns c
LEFT OUTER JOIN sys.identity_columns i
ON i.object_id = c.object_id
AND i.column_id = c.column_id
Where I.seed_value is not null
So I think your data is correct, there are no results to view though.
Are you sure you are running this in a database with tables with IDENTITY columns?
SELECT c.name, i.seed_value, i.increment_value
FROM sys.columns c
INNER JOIN sys.identity_columns i
ON i.object_id = c.object_id
AND i.column_id = c.column_id
Returns rows for me in a regular production database with a few identities.
Using a LEFT JOIN returns these rows as well as many which are not IDENTITY
I ran this on another database, and I noticed some NULLs are returned (even in the INNER JOIN case). This is because some of the columns are in VIEWs.
Try adding:
INNER JOIN sys.tables t
ON t.object_id = c.object_id
To filter only to actual IDENTITY columns in tables.
your query returns what I'd expect [see below]; it returns the single meta-data row about the single identity column (test_ID) in table (ident_test), the oter column (other) has no meta-data in the sys.identity_column as is is not an identity.
SELECT name, seed_value, increment_value
FROM sys.identity_columns
WHERE object_id = OBJECT_ID( 'ident_test' )
select name, is_identity, is_nullable
from sys.columns
WHERE object_id = OBJECT_ID( 'ident_test' )
Which gives
name seed_value increment_value
-----------------------------------------
test_id 1000 10
(1 row(s) affected)
name is_identity is_nullable
-------------------------------------
test_id 1 0
other 0 1
(2 row(s) affected)