Udostępnij za pośrednictwem


Add Columns to a Table (Database Engine)

This topic describes how to add new columns to a table in SQL Server 2012 by using SQL Server Management Studio or Transact-SQL.

In This Topic

  • Before you begin:

    Limitations and Restrictions

    Security

  • To insert columns, using:

    SQL Server Management Studio

    Transact-SQL

Before You Begin

Limitations and Restrictions

Using the ALTER TABLE statement to add columns to a table automatically adds those columns to the end of the table. If you want the columns in a specific order in the table, use SQL Server Management Studio. However, note that this is not a database design best practice. Best practice is to specify the order in which the columns are returned at the application and query level. You should not rely on the use of SELECT * to return all columns in an expected order based on the order in which they are defined in the table. Always specify the columns by name in your queries and applications in the order in which you would like them to appear.

Security

Permissions

Requires ALTER permission on the table.

Ikona strzałki używana z łączem Powrót na górę strony[Top]

Using SQL Server Management Studio

To insert columns into a table with Table Designer

  1. In Object Explorer, right-click the table to which you want to add columns and choose Design.

  2. Click in the first blank cell in the Column Name column.

  3. Type the column name in the cell. The column name is a required value.

  4. Press the TAB key to go to the Data Type cell and select a data type from the dropdown. This too is a required value, and will be assigned the default value if you don't choose one.

    [!UWAGA]

    You can change the default value in the Options dialog box under Database Tools.

  5. Continue to define any other column properties in the Column Properties tab.

    [!UWAGA]

    The default values for your column properties are added when you create a new column, but you can change them in the Column Properties tab.

  6. When you are finished adding columns, from the File menu, choose Save table name.

Ikona strzałki używana z łączem Powrót na górę strony[Top]

Using Transact-SQL

To insert columns into a table

  1. Connect to the Database Engine.

  2. From the Standard bar, click New Query.

  3. The following example adds two columns to the table dbo.doc_exa. Copy and paste the following example into the query window and click Execute

ALTER TABLE dbo.doc_exa ADD column_b VARCHAR(20) NULL, column_c INT NULL ;

For more information, see ALTER TABLE (Transact-SQL)

Ikona strzałki używana z łączem Powrót na górę strony[Top]