Add column to SQL Server

前端 未结 5 1867
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-13 05:27

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

5条回答
  •  一整个雨季
    2020-12-13 06:02

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

    Example

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

    Or

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

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

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

提交回复
热议问题