将可空列更改为具有默认值的 NOT NULL

发布于 2024-09-08 12:09:17 字数 263 浏览 1 评论 0原文

我今天遇到一个旧表,其中有一个名为“Created”的日期时间列,它允许空值。现在,我想更改它,使其不为 NULL,并且还包含一个添加默认值 (getdate()) 的约束。

到目前为止,我已经得到了以下脚本,只要我事先清除了所有空值,该脚本就可以正常工作:

ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL 

有没有办法在 ALTER 语句上也指定默认值?

I came across an old table today with a datetime column called 'Created' which allows nulls. Now, I'd want to change this so that it is NOT NULL, and also include a constraint to add in a default value (getdate()).

So far I've got the following script, which works fine provided that I've cleaned up all the nulls beforehand:

ALTER TABLE dbo.MyTable ALTER COLUMN Created DATETIME NOT NULL 

Is there any way to also specify the default value as well on the ALTER statement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逐鹿 2024-09-15 12:09:17

我认为您需要将其作为三个单独的语句来执行。我环顾四周,我所看到的一切似乎都表明,如果您要添加一列,则可以做到这一点,但如果您要更改一列,则不行。

ALTER TABLE dbo.MyTable
ADD CONSTRAINT my_Con DEFAULT GETDATE() for created

UPDATE MyTable SET Created = GetDate() where Created IS NULL

ALTER TABLE dbo.MyTable 
ALTER COLUMN Created DATETIME NOT NULL 

I think you will need to do this as three separate statements. I've been looking around and everything I've seen seems to suggest you can do it if you are adding a column, but not if you are altering one.

ALTER TABLE dbo.MyTable
ADD CONSTRAINT my_Con DEFAULT GETDATE() for created

UPDATE MyTable SET Created = GetDate() where Created IS NULL

ALTER TABLE dbo.MyTable 
ALTER COLUMN Created DATETIME NOT NULL 
水水月牙 2024-09-15 12:09:17

您可能必须首先将所有为空的记录更新为默认值,然后使用 alter table 语句。

Update dbo.TableName
Set
Created="01/01/2000"
where Created is NULL

You may have to first update all the records that are null to the default value then use the alter table statement.

Update dbo.TableName
Set
Created="01/01/2000"
where Created is NULL
够钟 2024-09-15 12:09:17

您需要执行两个查询:

一个 - 将默认值添加到所需的列

更改表“Table_Name”为“Column_Name”添加默认“值”

我想向 Column IsDeleted 添加默认值,如下所示:

示例:ALTER TABLE [dbo].[Employees] ADD IsDeleted 默认值为 0

Two - 将列值可为空更改为不为空

ALTER TABLE 'table_name' ALTER COLUMN 'column_name' 'data_type' NOT NULL

我想让 IsDeleted 列不为空

ALTER TABLE [dbo].[Employees] 更改列 IsDeleted BIT NOT NULL

you need to execute two queries:

One - to add the default value to the column required

ALTER TABLE 'Table_Name` ADD DEFAULT 'value' FOR 'Column_Name'

i want add default value to Column IsDeleted as below:

Example: ALTER TABLE [dbo].[Employees] ADD Default 0 for IsDeleted

Two - to alter the column value nullable to not null

ALTER TABLE 'table_name' ALTER COLUMN 'column_name' 'data_type' NOT NULL

i want to make the column IsDeleted as not null

ALTER TABLE [dbo].[Employees] Alter Column IsDeleted BIT NOT NULL

纵情客 2024-09-15 12:09:17

如果是 SQL Server,您可以在设计视图中的列属性上执行此操作。

试试这个:

ALTER TABLE dbo.TableName 
  ADD CONSTRAINT DF_TableName_ColumnName
    DEFAULT '01/01/2000' FOR ColumnName

If it's SQL Server, you can do it on the column properties within design view.

Try this:

ALTER TABLE dbo.TableName 
  ADD CONSTRAINT DF_TableName_ColumnName
    DEFAULT '01/01/2000' FOR ColumnName
泪痕残 2024-09-15 12:09:17

试试这个

ALTER TABLE table_name ALTER COLUMN col_name data_type NOT NULL;

Try this

ALTER TABLE table_name ALTER COLUMN col_name data_type NOT NULL;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文