如何为现有表格添加列 [英] how to add column for existing table

查看:125
本文介绍了如何为现有表格添加列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个表,现在我需要向其中添加一列.

I have created a table, and now I need to add a column to it. How would I go about this?

推荐答案

使用
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


这篇关于如何为现有表格添加列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆