Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a column has not null constraint?

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..

like image 596
Dev Avatar asked Nov 18 '13 09:11

Dev


2 Answers

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)

like image 52
Ric Avatar answered Oct 14 '22 08:10

Ric


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

like image 45
Ocaso Protal Avatar answered Oct 14 '22 06:10

Ocaso Protal