Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set a existing column of MS SQL table as NOT NULL

How to Set a existing column of MS SQL table as NOT NULL?

like image 271
Vinod Avatar asked Feb 11 '09 09:02

Vinod


People also ask

How do you add not NULL constraint to an existing column?

To add not null constraint to an existing column in MySQL, we will use the ALTER command. This is a type of validation to restrict the user from entering null values.

How do you define a column as not NULL?

By default, a column can hold NULL values. 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.

How do I create an existing column nullable in SQL?

ALTER TABLE table_name ALTER COLUMN column_name DATA_TYPE [(COLUMN_SIZE)] NULL; In this syntax: First, specify the name of the table from which you want to change the column. Second, specify the column name with size which you want to change to allow NULL and then write NULL statement .


1 Answers

ALTER TABLE tablename ALTER COLUMN columnname datatype NOT NULL 

You will obviously have to make sure that the column does not contain any NULL values before doing this.

E.g.

ALTER TABLE orders ALTER COLUMN customer_id INT NOT NULL 
like image 53
Adam Ralph Avatar answered Sep 21 '22 08:09

Adam Ralph