Muokkaa

Jaa


Delete columns from a table

Applies to: SQL Server 2016 (13.x) and later Azure SQL Database Azure SQL Managed Instance Azure Synapse Analytics Analytics Platform System (PDW)

This article describes how to delete table columns in SQL Server using SQL Server Management Studio (SSMS) or Transact-SQL.

Caution

When you delete a column from a table, the column and all the data it contains are deleted.

Limitations

You can't delete a column that has a CHECK constraint. You must first delete the constraint.

You can't delete a column that has PRIMARY KEY or FOREIGN KEY constraints or other dependencies except when using the Table Designer in SSMS. When using Object Explorer in SSMS or Transact-SQL, you must first remove all dependencies on the column.

Permissions

Requires ALTER permission on the table.

Delete columns using SQL Server Management Studio

You can delete columns in SSMS using Object Explorer or Table Designer.

Delete columns using Object Explorer

The following steps explain how to delete columns with Object Explorer in SSMS:

  1. Connect to an instance of Database Engine.

  2. In Object Explorer, locate the table from which you want to delete columns, and expand the table to expose the column names.

  3. Right-click the column that you want to delete, and choose Delete.

  4. In the Delete Object dialog box, select OK.

If the column contains constraints or other dependencies, an error message displays in the Delete Object dialog box. Resolve the error by deleting the referenced constraints.

Delete columns using Table Designer

The following steps explain how to delete columns with Table Designer in SSMS:

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

  2. Right-click the column you want to delete and choose Delete Column from the shortcut menu.

  3. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.

Delete columns using Transact-SQL

You can delete columns using Transact-SQL in SSMS, Azure Data Studio, or command-line tools such as the sqlcmd utility.

The following example shows you how to delete a column column_b from table dbo.doc_exb. The table and column must already exist.

ALTER TABLE dbo.doc_exb DROP COLUMN column_b;
GO

If the column contains constraints or other dependencies, an error message is returned. Resolve the error by deleting the referenced constraints.

For more examples, see ALTER TABLE.