且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为现有表格添加列

更新时间:2022-04-05 10:09:24

使用
ALTER TABLE ADD COLUMN

语句.

请参考 [ ^ ]链接

statement.

Refer this[^] link


像这样尝试

Try like this

CREATE TABLE dbo.Employees
(
  EmployeeID int IDENTITY (1,1) NOT NULL PRIMARY KEY NONCLUSTERED
)
GO

ALTER TABLE dbo.Employees
ADD
    FirstName varchar(50) NULL
    ,LastName varchar(50) NULL


如果您已经将数据添加到表中,则如果要添加的列不可为空,则需要确保添加的列已将此考虑在内.正如其他人所说,您需要使用ALTER TABLE命令,但是正如我已经说过的,如果它不能为NULL,则在创建它时需要应用默认值.一个实际的示例如下所示:
If you have already added data into the table, you need to ensure that the column you add takes this into account if the column you want to add is none nullable. As others have said, you need to use the ALTER TABLE command, but as I''ve said if it can''t be NULL, you need to apply a default when you create it. A practical example would look like this:
ALTER TABLE MyTable
ADD MyColumn DATETIME DEFAULT GETDATE() WITH VALUES NOT NULL