tags:

views:

85

answers:

3

Hi, I want to know which table's field are required or not required so I have to get "Allow nulls" state. How to do that?

+3  A: 

I will assume you are talking about SQL Server.

There is a table, INFORMATION_SCHEMA.COLUMNS, that contains meta-data about the columns in the database.

You can do this:

SELECT TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
ORDER BY TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME

IS_NULLABLE gives you the "Allow Nulls" value used in the designer.

Kevin
+1: I didn't knew about this view, ty
Rubens Farias
A: 

If your in MySQL use the sql command

DESCRIBE Table;

Where table is the name of the table you want to examine

Mohgeroth
A: 

Try this (SQL Server)

select sysobjects.name, syscolumns.name, syscolumns.isnullable
from   sysobjects join syscolumns
   on  sysobjects.id    = syscolumns.id
  and  sysobjects.xtype = 'U'
  and  sysobjects.name  = 'your table name'
Rubens Farias
As of SQL Server 2005, you should stop using "sysobjects" and start using the proper and more focused catalog views in the "sys" schema, e.g. "sys.tables", "sys.columns" and so forth
marc_s
+1: It's a great tip marc_s, ty
Rubens Farias