将列添加到 SQL Server [英] Add column to SQL Server

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

问题描述

我需要向我的 SQL Server 表中添加一列.是否有可能在不丢失数据的情况下这样做,我已经有了?

I need to add a column to my SQL Server table. Is it possible to do so without losing the data, I already have?

推荐答案

当然!只需使用 ALTER TABLE...句法.

Of course! Just use the ALTER TABLE... syntax.

示例

ALTER TABLE YourTable
  ADD Foo INT NULL /*Adds a new int column existing rows will be 
                     given a NULL value for the new column*/

ALTER TABLE YourTable
  ADD Bar INT NOT NULL DEFAULT(0) /*Adds a new int column existing rows will
                                    be given the value zero*/

在 SQL Server 2008 中,第一个是仅元数据更改.第二个将更新所有行.

In SQL Server 2008 the first one is a metadata only change. The second will update all rows.

在 SQL Server 2012+ 企业版中,第二个是 元数据也只改变了.

In SQL Server 2012+ Enterprise edition the second one is a metadata only change too.

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

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