将MS SQL表的现有列设置为NOT NULL

时间:2022-06-27 01:57:36

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

如何将MS SQL表的现有列设置为非空?

2 个解决方案

#1


80  

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

#2


11  

Firstly ensure that the fields have non null values. In this case I'm working with a field that has a GUID nvarchar so I'll do

首先确保字段具有非空值。在本例中,我使用的是一个具有GUID nvarchar的字段,我将这样做

UPDATE tablename 
SET    fieldname = Newid() 
WHERE  fieldname IS NULL; 

Then as Adam Ralph says

就像亚当·拉尔夫说的那样

ALTER TABLE tablename ALTER COLUMN fieldname datatype NOT NULL 

#1


80  

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

#2


11  

Firstly ensure that the fields have non null values. In this case I'm working with a field that has a GUID nvarchar so I'll do

首先确保字段具有非空值。在本例中,我使用的是一个具有GUID nvarchar的字段,我将这样做

UPDATE tablename 
SET    fieldname = Newid() 
WHERE  fieldname IS NULL; 

Then as Adam Ralph says

就像亚当·拉尔夫说的那样

ALTER TABLE tablename ALTER COLUMN fieldname datatype NOT NULL