Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL : Test if a column has the "Not Null" property

Is there a simple query to return whether a specific column allows nulls?
I want to change this as part of a DB upgrade script.

Alternatively, is it better to just change it, even if its already set?

Edit : This is for SQL Server (needs to support 2000 or later)

like image 836
Abort Avatar asked Feb 09 '11 17:02

Abort


People also ask

IS NOT NULL () in SQL?

The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

Is is not NULL and != The same in SQL?

<> is Standard SQL-92; != is its equivalent. Both evaluate for values, which NULL is not -- NULL is a placeholder to say there is the absence of a value. Which is why you can only use IS NULL / IS NOT NULL as predicates for such situations.

Is not null or exists SQL?

The IS NOT NULL condition is used in SQL to test for a non-NULL value. It returns TRUE if a non-NULL value is found, otherwise it returns FALSE. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.


1 Answers

Any particular RDBMS?

In SQL Server

use master

SELECT COLUMNPROPERTY( OBJECT_ID('dbo.spt_values'),'number','AllowsNull')

Or (more standard)

select IS_NULLABLE 
from INFORMATION_SCHEMA.COLUMNS 
where TABLE_SCHEMA='dbo' 
      AND TABLE_NAME='spt_values' 
      AND COLUMN_NAME='number'
like image 168
Martin Smith Avatar answered Oct 09 '22 06:10

Martin Smith