Как добавить столбец в таблицу sql server
Перейти к содержимому

Как добавить столбец в таблицу sql server

  • автор:

Как добавить столбец в таблицу SQL Server

В SQL Server добавление столбца в существующую таблицу — это распространенная операция, которая позволяет расширить структуру таблицы и добавить новые данные. В этой статье мы рассмотрим различные способы добавления столбца в таблицу SQL Server с примерами кода.

1. Использование команды ALTER TABLE

Самый простой способ добавить столбец в таблицу — использовать команду ALTER TABLE. Эта команда позволяет изменить структуру таблицы добавлением нового столбца.

 ALTER TABLE table_name ADD column_name data_type; 
 ALTER TABLE Customers ADD Age INT; 

В этом примере мы добавляем столбец «Age» с типом данных INT в таблицу «Customers».

2. Использование команды sp_addcolumn

Другой способ добавить столбец в таблицу — использовать хранимую процедуру sp_addcolumn. Эта процедура позволяет добавить новый столбец с различными параметрами, такими как имя столбца, тип данных, длина и т. д.

 EXEC sp_addcolumn @table_name = 'table_name', @column_name = 'column_name', @data_type = 'data_type'; 
 EXEC sp_addcolumn @table_name = 'Customers', @column_name = 'Age', @data_type = 'INT'; 

В этом примере мы используем хранимую процедуру sp_addcolumn для добавления столбца «Age» с типом данных INT в таблицу «Customers».

3. Использование инструкции CREATE TABLE

Если вы хотите создать новую таблицу с добавлением столбца, вы можете использовать инструкцию CREATE TABLE. Это позволит вам определить структуру таблицы и добавить новый столбец вместе с другими столбцами.

 CREATE TABLE table_name ( column1 data_type, column2 data_type, . columnN data_type, new_column data_type ); 
 CREATE TABLE Customers ( CustomerID INT, FirstName VARCHAR(50), LastName VARCHAR(50), Age INT ); 

В этом примере мы создаем таблицу «Customers» с уже существующими столбцами «CustomerID», «FirstName» и «LastName», а также добавляем новый столбец «Age» с типом данных INT.

4. Добавление столбца с указанием положения

Если вам нужно добавить новый столбец в определенное место в таблице, вы можете указать позицию, где вы хотите добавить столбец, с использованием команды ALTER TABLE.

 ALTER TABLE table_name ADD column_name data_type AFTER existing_column; 
 ALTER TABLE Customers ADD Age INT AFTER LastName; 

В этом примере мы добавляем столбец «Age» с типом данных INT после столбца «LastName» в таблице «Customers».

Заключение

Добавление столбца в таблицу SQL Server — это простая операция, которая позволяет расширить структуру таблицы и добавить новые данные. Мы рассмотрели несколько способов добавления столбца в таблицу, используя команду ALTER TABLE, хранимую процедуру sp_addcolumn и инструкцию CREATE TABLE. Вы можете выбрать подходящий для вас способ в зависимости от ваших потребностей.

Как добавить столбец в таблицу sql server

В этом учебном пособии вы узнаете, как использовать оператор ALTER TABLE в SQL Server (Transact-SQL) для добавления столбца, изменения столбца, удаления столбца, переименования столбца или переименования таблицы с синтаксисом и примерами.

Описание

Оператор ALTER TABLE SQL Server (Transact-SQL) используется для добавления, изменения или удаления столбцов в таблице.

Добавить столбец в таблицу.

Вы можете использовать оператор ALTER TABLE в SQL Server, чтобы добавить столбец в таблицу.

Синтаксис

Синтаксис добавления столбца в таблицу в SQL Server (Transact-SQL):

Пример

Рассмотрим пример, который показывает, как добавить столбец в таблицу SQL Server с помощью оператора ALTER TABLE.
Например:

Этот пример SQL Server ALTER TABLE добавит столбец в таблицу employees , с наименованием last_name .

Добавить несколько столбцов в таблицу

Вы можете использовать оператор ALTER TABLE в SQL Server для добавления нескольких столбцов в таблицу.

Синтаксис

Синтаксис добавления нескольких столбцов в существующую таблицу в SQL Server (Transact-SQL):

Пример

Рассмотрим пример, который показывает, как добавить несколько столбцов в таблицу в SQL Server с помощью оператора ALTER TABLE.
Например:

Этот пример SQL Server ALTER TABLE добавит в таблицу employees два столбца, поле last_name как VARCHAR (50) и поле first_name как VARCHAR (40).

Изменить столбец в таблице

Вы можете использовать оператор ALTER TABLE в SQL Server для изменения столбца в таблице.

Синтаксис

Синтаксис изменения столбца в существующей таблице в SQL Server (Transact-SQL):

Пример

Рассмотрим пример, который показывает, как изменить столбец в таблице SQL Server с помощью оператора ALTER TABLE.
Например:

Этот пример SQL Server ALTER TABLE изменит столбец с именем last_name как тип данных VARCHAR (75) и принудит столбец не допускать нулевые значения.

Удалить столбец из таблицы

Вы можете использовать оператор ALTER TABLE в SQL Server для удаления столбца из таблицы.

Синтаксис

Синтаксис удаления столбца в существующей таблице в SQL Server (Transact-SQL):

Пример

Рассмотрим пример, показывающий, как удалить столбец из таблицы на SQL Server с помощью оператора ALTER TABLE.
Например:

Этот пример SQL Server ALTER TABLE удалит столбец с именем last_name из таблицы, называемой employee .

Переименовать столбец в таблице

Вы не можете использовать оператор ALTER TABLE в SQL Server для переименования столбца в таблице. Тем не менее, вы можете использовать sp_rename , хотя Microsoft рекомендует удалять и воссоздавать таблицу, чтобы скрипты и хранимые процедуры не были нарушены.

Синтаксис

Синтаксис переименования столбца в существующей таблице в SQL Server (Transact-SQL):

Пример

Рассмотрим пример, который показывает, как переименовать столбец в таблице на SQL Server, используя sp_rename .
Например:

Изменение таблиц в Microsoft SQL Server или как добавить, удалить, изменить столбец в таблице?

В этом материале я покажу, как вносятся изменения в таблицы в Microsoft SQL Server, под изменениями здесь подразумевается добавление новых столбцов, удаление или изменение характеристик уже существующих столбцов в таблице. По традиции я покажу, как это делается в графическом конструкторе среды SQL Server Management Studio и, конечно же, как это делается на языке T-SQL.

Скриншот 1

Напомню, в прошлых статьях я показывал, как создаются базы данных в Microsoft SQL Server, а также как создаются новые таблицы. Сегодня Вы узнаете, как изменить уже существующие таблицы в Microsoft SQL Server, при этом, как было уже отмечено, будет рассмотрено два способа изменения таблиц: первый – с помощью SQL Server Management Studio (SSMS), и второй – с помощью T-SQL.

Также я расскажу о некоторых нюансах и проблемах, которые могут возникнуть в процессе изменения таблиц, что, на самом деле, характерно для большинства случаев.

Заметка! Для комплексного изучения языка T-SQL рекомендую посмотреть мои видеокурсы по T-SQL, в которых используется последовательная методика обучения и рассматриваются все конструкции языка SQL и T-SQL.

Исходные данные для примеров

Сначала давайте определим исходные данные, а точнее таблицу, которую мы будем изменять. Допустим, это будет точно такая же таблица, которую мы использовали в прошлых статьях, а именно наша тестовая таблица Goods – она содержит информацию о товарах и имеет следующие столбцы:

  • ProductId – идентификатор товара, столбец не может содержать значения NULL, первичный ключ;
  • Category – ссылка на категорию товара, столбец не может содержать значения NULL, но имеет значение по умолчанию, например, для случаев, когда товар еще не распределили в необходимую категорию, в этом случае товару будет присвоена категория по умолчанию;
  • ProductName – наименование товара, столбец не может содержать значения NULL;
  • Price – цена товара, столбец может содержать значения NULL, например, с ценой еще не определились.

Если у Вас нет такой таблицы, то создайте ее и добавьте в нее несколько строк данных, например, следующей инструкцией.

Скриншот 2

Данные мы добавили инструкцией INSERT INTO языка T-SQL.

Примечание! В качестве сервера у меня выступает версия Microsoft SQL Server 2017 Express, как ее установить, можете посмотреть в моей видео-инструкции.

Итак, давайте начнем.

Изменение таблиц в конструкторе SQL Server Management Studio

Сначала я покажу, как изменяются таблицы с помощью графического интерфейса SQL Server Management Studio, а изменяются они точно так же, как и создаются, с помощью того же самого конструктора.

Чтобы открыть конструктор таблиц в среде SQL Server Management Studio, необходимо в обозревателе объектов найти нужную таблицу и щелкнуть по ней правой кнопкой мыши, и выбрать пункт «Проект». Увидеть список таблиц можно в контейнере «Базы данных -> Нужная база данных -> Таблицы».

Скриншот 3

В итоге откроется конструктор таблиц, где Вы можете добавлять, удалять или изменять столбцы таблицы.

Скриншот 4

Важно! При работе в конструкторе с таблицей, в которой есть данные, обязательно стоит учитывать один очень важный момент, большинство изменений внести не получится, например, изменить свойства столбцов. Это связано с тем, что по умолчанию в конструкторе «Запрещено сохранение изменений, требующих повторного создания таблицы», именно так и называется параметр, который по умолчанию включён, за счет чего все соответствующие изменения будут блокироваться и, при попытке сохранить такие изменения, Вы будете получать, например, ошибки следующего характера

Скриншот 5

В случае если Вы работаете исключительно в конструкторе (если делать все то же самое с помощью T-SQL, то такая ошибка возникать не будет) и четко уверены в своих действиях, то этот параметр можно отключить. Для этого зайдите в меню «Сервис -> Параметры» и в разделе «Конструкторы -> Конструкторы таблиц и баз данных» снимите соответствующую галочку.

Скриншот 6

После чего данное ограничение будет снято, и Вы сможете вносить изменения в таблицы с помощью конструктора. При сохранении таблицы ошибок возникать уже не будет.

Как работать с конструктором, я думаю, понятно, например, для добавления нового столбца просто пишем название столбца в новую строку, выбираем тип данных и указываем признак, может ли данный столбец хранить значения NULL. Для сохранения изменений нажимаем сочетание клавиш «Ctrl+S» или на панели инструментов нажимаем кнопку «Сохранить» (также кнопка «сохранить» доступна и в меню «Файл», и в контекстном меню самой вкладки конструктора).

Скриншот 7

Для внесения изменений в существующие столбцы точно так же изменяем параметры, и сохраняем изменения.

Важно!

Во всех случаях, т.е. не важно с помощью конструктора или с помощью языка T-SQL, когда Вы будете вносить изменения в таблицы, в которых уже есть данные, важно понимать и знать, как эти изменения отразятся на существующих данных, и можно ли вообще применить эти изменения к данным.

Например, изменить тип данных можно, только если он явно преобразовывается без потери данных или в столбце нет данных вообще. Допустим, если в столбце с типом данных VARCHAR(100) есть данные, при этом максимальная длина фактических данных в столбце, к примеру, 80 символов, то изменить тип данных, без потери данных можно только в сторону увеличения или уменьшения до 80 символов (VARCHAR(80)).

Также если в столбце есть данные, при этом он может принимать значение NULL, а Вы хотите сделать его обязательным, т.е. задать свойство NOT NULL, Вам сначала нужно проставить всем записям, в которых есть NULL, значение, например, то, которое будет использоваться по умолчанию, или уже более детально провести анализ для корректной простановки значений.

Еще стоит отметить, что даже просто добавить новый столбец, который не должен принимать значения NULL, не получится, если в таблице уже есть записи, в таких случаях нужно сначала добавить столбец с возможностью принятия значения NULL, потом заполнить его данными, и уже потом обновить данный параметр, т.е. указать NOT NULL.

Изменение таблиц в Microsoft SQL Server на языке T-SQL (ALTER TABLE)

Теперь давайте я покажу, как изменять таблицы в Microsoft SQL Server на T-SQL. Все изменения в таблицы вносятся с помощью инструкции ALTER TABLE. Для начала давайте рассмотрим упрощённый синтаксис инструкции ALTER TABLE, чтобы Вы лучше понимали структуру тех запросов, которые мы будем рассматривать далее в примерах.

Упрощенный синтаксис инструкции ALTER TABLE
Добавление нового столбца в таблицу на T-SQL

Чтобы добавить новый столбец в таблицу, мы пишем инструкцию ALTER TABLE с параметром ADD, указываем название нового столбца (в нашем случае ProductDescription, т.е. описание товара), его тип данных и возможность принятия значения NULL (как было уже отмечено ранее, если в таблице есть строки, то сначала столбец должен принимать значения NULL).

Скриншот 8

Удаление столбца из таблицы на T-SQL

Если Вам столбец не нужен, то его легко удалить (если он не участвует ни в каких связях) параметром DROP COLUMN, например, мы передумали добавлять новый столбец с описанием товара, и чтобы его удалить, пишем следующую инструкцию.

Скриншот 9

Задаем свойство NOT NULL для столбца на T-SQL

Если у Вас возникла необходимость сделать столбец обязательным, т.е. задать свойство NOT NULL для столбца, то для этого необходимо использовать параметр ALTER COLUMN, но обязательно помним о том, что в столбце уже должны быть заполнены все строки, т.е. отсутствовать значения NULL.

Допустим, в нашем случае цена стала обязательной, чтобы это реализовать в нашей таблице, пишем следующую инструкцию (просто указываем все фактические параметры столбца и изменяем тот, который нужно, в данном конкретном случае возможность принятия значения NULL).

Изменяем тип данных столбца на T-SQL

Для изменения типа данных столбца точно так же перечисляем все параметры столбца с изменением нужного, т.е. указываем новый тип данных.

Допустим, у нас возникла необходимость увеличить длину строки для хранения наименования товара (например, до 200 символов).

Name already in use

sql-docs / docs / t-sql / statements / alter-table-transact-sql.md

  • Go to file T
  • Go to line L
  • Copy path
  • Copy permalink
  • Open with Desktop
  • View raw
  • Copy raw contents Copy raw contents
  • Disk-based tables:

For more information about the syntax conventions, see Transact-SQL syntax conventions.

Syntax for disk-based tables

Syntax for memory-optimized tables

Syntax for Azure Synapse Analytics and Parallel Data Warehouse

Syntax for [!INCLUDE fabricdw] in [!INCLUDE fabric]

The name of the database in which the table was created.

The name of the schema to which the table belongs.

The name of the table to be altered. If the table isn’t in the current database or contained by the schema owned by the current user, you must explicitly specify the database and schema.

Specifies that the named column is to be changed or altered.

The modified column can’t be:

A column with a timestamp data type.

The ROWGUIDCOL for the table.

A computed column or used in a computed column.

Used in statistics generated by the CREATE STATISTICS statement. Users need to run DROP STATISTICS to drop the statistics before ALTER COLUMN can succeed. Run this query to get all the user created statistics and statistics columns for a table.

[!NOTE] Statistics that are automatically generated by the query optimizer are automatically dropped by ALTER COLUMN.

Used in a PRIMARY KEY or [FOREIGN KEY] REFERENCES constraint.

Used in a CHECK or UNIQUE constraint. But, changing the length of a variable-length column used in a CHECK or UNIQUE constraint is allowed.

Associated with a default definition. However, the length, precision, or scale of a column can be changed if the data type isn’t changed.

The data type of text, ntext, and image columns can be changed only in the following ways:

Some data type changes may cause a change in the data. For example, changing a nchar or nvarchar column, to char or varchar, might cause the conversion of extended characters. For more information, see CAST and CONVERT. Reducing the precision or scale of a column can cause data truncation.

[!NOTE] The data type of a column of a partitioned table can’t be changed.

The data type of columns included in an index can’t be changed unless the column is a varchar, nvarchar, or varbinary data type, and the new size is equal to or larger than the old size.

A column included in a primary key constraint, can’t be changed from NOT NULL to NULL.

When using Always Encrypted (without secure enclaves), if the column being modified is encrypted with ‘ENCRYPTED WITH’, you can change the datatype to a compatible datatype (such as INT to BIGINT), but you can’t change any encryption settings.

When using Always Encrypted with secure enclaves, you can change any encryption setting, if the column encryption key protecting the column (and the new column encryption key, if you’re changing the key) support enclave computations (encrypted with enclave-enabled column master keys). For details, see Always Encrypted with secure enclaves.

The name of the column to be altered, added, or dropped. The column_name maximum is 128 characters. For new columns, you can omit column_name for columns created with a timestamp data type. The name timestamp is used if you don’t specify column_name for a timestamp data type column.

[!NOTE] New columns are added after all existing columns in the table being altered.

[ type_schema_name. ] type_name

The new data type for the altered column, or the data type for the added column. You can’t specify type_name for existing columns of partitioned tables. type_name can be any one of the following types:

  • A [!INCLUDEssNoVersion] system data type.
  • An alias data type based on a [!INCLUDEssNoVersion] system data type. You create alias data types with the CREATE TYPE statement before they can be used in a table definition.
  • A [!INCLUDEdnprdnshort] user-defined type, and the schema to which it belongs. You create user-defined types with the CREATE TYPE statement before they can be used in a table definition.

The following are criteria for type_name of an altered column:

  • The previous data type must be implicitly convertible to the new data type.
  • type_name can’t be timestamp.
  • ANSI_NULL defaults are always on for ALTER COLUMN; if not specified, the column is nullable.
  • ANSI_PADDING padding is always ON for ALTER COLUMN.
  • If the modified column is an identity column, new_data_type must be a data type that supports the identity property.
  • The current setting for SET ARITHABORT is ignored. ALTER TABLE operates as if ARITHABORT is set to ON.

[!NOTE] If the COLLATE clause isn’t specified, changing the data type of a column causes a collation change to the default collation of the database.

The precision for the specified data type. For more information about valid precision values, see Precision, Scale, and Length.

The scale for the specified data type. For more information about valid scale values, see Precision, Scale, and Length.

max

Applies only to the varchar, nvarchar, and varbinary data types for storing 2^31-1 bytes of character, binary data, and of Unicode data.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Applies only to the xml data type for associating an XML schema with the type. Before typing an xml column to a schema collection, you first create the schema collection in the database by using CREATE XML SCHEMA COLLECTION.

Specifies the new collation for the altered column. If not specified, the column is assigned the default collation of the database. Collation name can be either a Windows collation name or a SQL collation name. For a list and more information, see Windows Collation Name and SQL Server Collation Name.

The COLLATE clause changes the collations only of columns of the char, varchar, nchar, and nvarchar data types. To change the collation of a user-defined alias data type column, use separate ALTER TABLE statements to change the column to a [!INCLUDEssNoVersion] system data type. Then, change its collation and change the column back to an alias data type.

ALTER COLUMN can’t have a collation change if one or more of the following conditions exist:

  • If a CHECK constraint, FOREIGN KEY constraint, or computed columns reference the column changed.
  • If any index, statistics, or full-text index are created on the column. Statistics created automatically on the column changed are dropped if the column collation is changed.
  • If a schema-bound view or function references the column.

For more information, see COLLATE.

Specifies whether the column can accept null values. Columns that don’t allow null values are added with ALTER TABLE only if they have a default specified or if the table is empty. You can specify NOT NULL for computed columns only if you have also specified PERSISTED. If the new column allows null values and you don’t specify a default, the new column contains a null value for each row in the table. If the new column allows null values and you add a default definition with the new column, you can use WITH VALUES to store the default value in the new column for each existing row in the table.

If the new column doesn’t allow null values and the table isn’t empty, you have to add a DEFAULT definition with the new column. And, the new column automatically loads with the default value in the new columns in each existing row.

You can specify NULL in ALTER COLUMN to force a NOT NULL column to allow null values, except for columns in PRIMARY KEY constraints. You can specify NOT NULL in ALTER COLUMN only if the column contains no null values. The null values must be updated to some value before the ALTER COLUMN NOT NULL is allowed, for example:

When you create or alter a table with the CREATE TABLE or ALTER TABLE statements, the database and session settings influence and possibly override the nullability of the data type that’s used in a column definition. Be sure that you always explicitly define a column as NULL or NOT NULL for noncomputed columns.

If you add a column with a user-defined data type, be sure to define the column with the same nullability as the user-defined data type. And, specify a default value for the column. For more information, see CREATE TABLE.

[!NOTE] If NULL or NOT NULL is specified with ALTER COLUMN, new_data_type [(precision [, scale ])] must also be specified. If the data type, precision, and scale are not changed, specify the current column values.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies that the ROWGUIDCOL property is added to or dropped from the specified column. ROWGUIDCOL indicates that the column is a row GUID column. You can set only one uniqueidentifier column per table as the ROWGUIDCOL column. And, you can only assign the ROWGUIDCOL property to a uniqueidentifier column. You can’t assign ROWGUIDCOL to a column of a user-defined data type.

ROWGUIDCOL doesn’t enforce uniqueness of the values stored in the column and doesn’t automatically generate values for new rows that are inserted into the table. To generate unique values for each column, either use the NEWID or NEWSEQUENTIALID function on INSERT statements. Or, specify the NEWID or NEWSEQUENTIALID function as the default for the column.

Specifies that the PERSISTED property is added to or dropped from the specified column. The column must be a computed column that’s defined with a deterministic expression. For columns specified as PERSISTED, the [!INCLUDEssDE] physically stores the computed values in the table and updates the values when any other columns on which the computed column depends are updated. By marking a computed column as PERSISTED, you can create indexes on computed columns defined on expressions that are deterministic, but not precise. For more information, see Indexes on Computed Columns.

SET QUOTED_IDENTIFIER must be ON when you’re creating or changing indexes on computed columns or indexed views. For more information, see SET QUOTED_IDENTIFIER (Transact-SQL).

Any computed column that’s used as a partitioning column of a partitioned table must be explicitly marked PERSISTED.

DROP NOT FOR REPLICATION

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies that values are incremented in identity columns when replication agents carry out insert operations. You can specify this clause only if column_name is an identity column.

Indicates that the column is a sparse column. The storage of sparse columns is optimized for null values. You can’t set sparse columns as NOT NULL. When you convert a column from sparse to nonsparse, or from nonsparse to sparse, locks the table for the duration of the command execution. You may need to use the REBUILD clause to reclaim any space savings. For additional restrictions and more information about sparse columns, see Use Sparse Columns.

ADD MASKED WITH ( FUNCTION = ‘ mask_function ‘)

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsssql16-md] and later) and [!INCLUDEssSDSfull].

Specifies a dynamic data mask. mask_function is the name of the masking function with the appropriate parameters. Three functions are available:

  • default()
  • email()
  • partial()
  • random()

Requires ALTER ANY MASK permission.

To drop a mask, use DROP MASKED . For function parameters, see Dynamic Data Masking.

Add and drop a mask require ALTER ANY MASK permission.

WITH ( ONLINE = ON | OFF)

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsssql16-md] and later) and [!INCLUDEssSDSfull].

Allows many alter column actions to be carried out while the table remains available. Default is OFF. You can run alter column online for column changes related to data type, column length or precision, nullability, sparseness, and collation.

Online alter column allows user created and autostatistics to reference the altered column for the duration of the ALTER COLUMN operation, which allows queries to run as usual. At the end of the operation, autostats that reference the column are dropped and user-created stats are invalidated. The user must manually update user-generated statistics after the operation is completed. If the column is part of a filter expression for any statistics or indexes then you can’t perform an alter column operation.

  • While the online alter column operation is running, all operations that could take a dependency on the column (index, views, and so on.) block or fail with an appropriate error. This behavior guarantees that online alter column won’t fail because of dependencies introduced while the operation was running.
  • Altering a column from NOT NULL to NULL isn’t supported as an online operation when the altered column is referenced by nonclustered indexes.
  • Online ALTER isn’t supported when the column is referenced by a check constraint and the ALTER operation is restricting the precision of the column (numeric or datetime).
  • The WAIT_AT_LOW_PRIORITY option can’t be used with online alter column.
  • ALTER COLUMN . ADD/DROP PERSISTED isn’t supported for online alter column.
  • ALTER COLUMN . ADD/DROP ROWGUIDCOL/NOT FOR REPLICATION isn’t affected by online alter column.
  • Online alter column doesn’t support altering a table where change tracking is enabled or that’s a publisher of merge replication.
  • Online alter column doesn’t support altering from or to CLR data types.
  • Online alter column doesn’t support altering to an XML data type that has a schema collection different than the current schema collection.
  • Online alter column doesn’t reduce the restrictions on when a column can be altered. References by index/stats, and so on, might cause the alter to fail.
  • Online alter column doesn’t support altering more than one column concurrently.
  • Online alter column has no effect in a system-versioned temporal table. ALTER column isn’t run as online regardless of which value was specified for ONLINE option.

Online alter column has similar requirements, restrictions, and functionality as online index rebuild, which includes:

  • Online index rebuild isn’t supported when the table contains legacy LOB or filestream columns or when the table has a columnstore index. The same limitations apply for online alter column.
  • An existing column being altered requires twice the space allocation, for the original column and for the newly created hidden column.
  • The locking strategy during an alter column online operation follows the same locking pattern used for online index build.

WITH CHECK | WITH NOCHECK

Specifies whether the data in the table is or isn’t validated against a newly added or re-enabled FOREIGN KEY or CHECK constraint. If you don’t specify, WITH CHECK is assumed for new constraints, and WITH NOCHECK is assumed for re-enabled constraints.

If you don’t want to verify new CHECK or FOREIGN KEY constraints against existing data, use WITH NOCHECK. We don’t recommend doing this, except in rare cases. The new constraint is evaluated in all later data updates. Any constraint violations that are suppressed by WITH NOCHECK when the constraint is added may cause future updates to fail if they update rows with data that doesn’t follow the constraint. The query optimizer doesn’t consider constraints that are defined WITH NOCHECK. Such constraints are ignored until they are re-enabled by using ALTER TABLE table WITH CHECK CHECK CONSTRAINT ALL . For more information, see Disable Foreign Key Constraints with INSERT and UPDATE Statements.

ALTER INDEX index_name

Specifies that the bucket count for index_name is to be changed or altered.

The syntax ALTER TABLE . ADD/DROP/ALTER INDEX is supported only for memory-optimized tables.

[!IMPORTANT] Without using an ALTER TABLE statement, the statements CREATE INDEX, DROP INDEX, ALTER INDEX, and PAD_INDEX are not supported for indexes on memory-optimized tables.

Specifies that one or more column definitions, computed column definitions, or table constraints are added. Or, the columns that the system uses for system versioning are added. For memory-optimized tables, you can add an index.

[!NOTE] New columns are added after all existing columns in the table being altered.

[!IMPORTANT] Without using an ALTER TABLE statement, the statements CREATE INDEX, DROP INDEX, ALTER INDEX, and PAD_INDEX aren’t supported for indexes on memory-optimized tables.

PERIOD FOR SYSTEM_TIME ( system_start_time_column_name, system_end_time_column_name )

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL17] and later) and [!INCLUDEssSDSfull].

Specifies the names of the columns that the system uses to record the period of time for which a record is valid. You can specify existing columns or create new columns as part of the ADD PERIOD FOR SYSTEM_TIME argument. Set up the columns with the datatype of datetime2 and define them as NOT NULL. If you define a period column as NULL, an error results. You can define a column_constraint and/or Specify Default Values for Columns for the system_start_time and system_end_time columns. See Example A in the following System Versioning examples that demonstrates using a default value for the system_end_time column.

Use this argument with the SET SYSTEM_VERSIONING argument to make an existing table a temporal table. For more information, see Temporal Tables and Getting Started with Temporal Tables in Azure SQL Database.

As of [!INCLUDEssSQL17], users can mark one or both period columns with HIDDEN flag to implicitly hide these columns such that SELECT * FROM doesn’t return a value for the columns. By default, period columns aren’t hidden. In order to be used, hidden columns must be explicitly included in all queries that directly reference the temporal table.

Specifies that one or more column definitions, computed column definitions, or table constraints are dropped, or to drop the specification for the columns that the system uses for system versioning.

[!NOTE] Columns dropped in ledger tables are only soft deleted. A dropped column remains in the ledger table, but it is marked as a dropped column by setting sys.tables.dropped_ledger_table to 1. The ledger view of the dropped ledger table is also marked as dropped by setting sys.tables.dropped_ledger_view_column to 1. A dropped ledger table, its history table, and its ledger view are renamed by adding a prefix (MSSQL_DroppedLedgerTable, MSSQL_DropedLedgerHistory, MSSQL_DroppedLedgerView) and appending a GUID to the original name.ing

Specifies that constraint_name is removed from the table. Multiple constraints can be listed.

You can determine the user-defined or system-supplied name of the constraint by querying the sys.check_constraint , sys.default_constraints , sys.key_constraints , and sys.foreign_keys catalog views.

A PRIMARY KEY constraint can’t be dropped if an XML index exists on the table.

Specifies that index_name is removed from the table.

The syntax ALTER TABLE . ADD/DROP/ALTER INDEX is supported only for memory-optimized tables.

[!IMPORTANT] Without using an ALTER TABLE statement, the statements CREATE INDEX, DROP INDEX, ALTER INDEX, and PAD_INDEX are not supported for indexes on memory-optimized tables.

Specifies that constraint_name or column_name is removed from the table. Multiple columns can be listed.

A column can’t be dropped when it’s:

  • Used in an index, whether as a key column or as an INCLUDE
  • Used in a CHECK, FOREIGN KEY, UNIQUE, or PRIMARY KEY constraint.
  • Associated with a default that’s defined with the DEFAULT keyword, or bound to a default object.
  • Bound to a rule.

[!NOTE] Dropping a column doesn’t reclaim the disk space of the column. You may have to reclaim the disk space of a dropped column when the row size of a table is near, or has exceeded, its limit. Reclaim space by creating a clustered index on the table or rebuilding an existing clustered index by using ALTER INDEX. For information about the impact of dropping LOB data types, see this CSS blog entry.

PERIOD FOR SYSTEM_TIME

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsssql16-md] and later) and [!INCLUDEssSDSfull].

Drops the specification for the columns that the system will use for system versioning.

Specifies that one or more drop clustered constraint options are set.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Overrides the max degree of parallelism configuration option only for the duration of the operation. For more information, see Configure the max degree of parallelism Server Configuration Option.

Use the MAXDOP option to limit the number of processors used in parallel plan execution. The maximum is 64 processors.

max_degree_of_parallelism can be one of the following values:

1
Suppresses parallel plan generation.

>1
Restricts the maximum number of processors used in a parallel index operation to the specified number.

0 (default)
Uses the actual number of processors or fewer based on the current system workload.

ONLINE =

Specifies whether underlying tables and associated indexes are available for queries and data modification during the index operation. The default is OFF. You can run REBUILD as an ONLINE operation.

ON
Long-term table locks aren’t held for the duration of the index operation. During the main phase of the index operation, only an Intent Share (IS) lock is held on the source table. This behavior enables queries or updates to the underlying table and indexes to continue. At the start of the operation, a Shared (S) lock is held on the source object for a short time. At the end of the operation, for a short time, an S (Shared) lock is acquired on the source if a nonclustered index is being created. Or, an SCH-M (Schema Modification) lock is acquired when a clustered index is created or dropped online and when a clustered or nonclustered index is being rebuilt. ONLINE can’t be set to ON when an index is being created on a local temporary table. Only single-threaded heap rebuild operation is allowed.

To run the DDL for SWITCH or online index rebuild, all active blocking transactions running on a particular table must be completed. When executing, the SWITCH or rebuild operation prevents new transactions from starting and might significantly affect the workload throughput and temporarily delay access to the underlying table.

OFF
Table locks apply for the duration of the index operation. An offline index operation that creates, rebuilds, or drops a clustered index, or rebuilds or drops a nonclustered index, acquires a Schema modification (Sch-M) lock on the table. This lock prevents all user access to the underlying table for the duration of the operation. An offline index operation that creates a nonclustered index acquires a Shared (S) lock on the table. This lock prevents updates to the underlying table but allows read operations, such as SELECT statements. Multi-threaded heap rebuild operations are allowed.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies a location to move the data rows currently in the leaf level of the clustered index. The table is moved to the new location. This option applies only to constraints that create a clustered index.

[!NOTE] In this context, default isn’t a keyword. It is an identifier for the default filegroup and must be delimited, as in MOVE TO «default» or MOVE TO [default]. If «default» is specified, the QUOTED_IDENTIFIER option must be ON for the current session. This is the default setting. For more information, see SET QUOTED_IDENTIFIER.

Specifies that constraint_name is enabled or disabled. This option can only be used with FOREIGN KEY and CHECK constraints. When NOCHECK is specified, the constraint is disabled and future inserts or updates to the column are not validated against the constraint conditions. DEFAULT, PRIMARY KEY, and UNIQUE constraints can’t be disabled.

ALL
Specifies that all constraints are either disabled with the NOCHECK option or enabled with the CHECK option.

Specifies that trigger_name is enabled or disabled. When a trigger is disabled, it’s still defined for the table. However, when INSERT, UPDATE, or DELETE statements run against the table, the actions in the trigger aren’t carried out until the trigger is re-enabled.

ALL
Specifies that all triggers in the table are enabled or disabled.

trigger_name
Specifies the name of the trigger to disable or enable.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies whether change tracking is enabled disabled for the table. By default, change tracking is disabled.

This option is available only when change tracking is enabled for the database. For more information, see ALTER DATABASE SET Options.

To enable change tracking, the table must have a primary key.

WITH ( TRACK_COLUMNS_UPDATED = )

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies whether the [!INCLUDEssDE] tracks, which change tracked columns were updated. The default value is OFF.

SWITCH [ PARTITION source_partition_number_expression ] TO [ schema_name. ] target_table [ PARTITION target_partition_number_expression ]

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Switches a block of data in one of the following ways:

  • Reassigns all data of a table as a partition to an already-existing partitioned table.
  • Switches a partition from one partitioned table to another.
  • Reassigns all data in one partition of a partitioned table to an existing non-partitioned table.

If table is a partitioned table, you must specify source_partition_number_expression. If target_table is partitioned, you must specify target_partition_number_expression. When reassigning a table’s data as a partition to an already-existing partitioned table, or switching a partition from one partitioned table to another, the target partition must exist and it must be empty.

When reassigning one partition’s data to form a single table, the target table must already exist and it must be empty. Both the source table or partition, and the target table or partition, must be located in the same filegroup. The corresponding indexes, or index partitions, must also be located in the same filegroup. Many additional restrictions apply to switching partitions. table and target_table can’t be the same. target_table can be a multi-part identifier.

Both source_partition_number_expression and target_partition_number_expression are constant expressions that can reference variables and functions. These include user-defined type variables and user-defined functions. They can’t reference [!INCLUDEtsql] expressions.

A partitioned table with a clustered columnstore index behaves like a partitioned heap:

  • The primary key must include the partition key.
  • A unique index must include the partition key. But, including the partition key with an existing unique index can change the uniqueness.
  • To switch partitions, all nonclustered indexes must include the partition key.

For SWITCH restriction when using replication, see Replicate Partitioned Tables and Indexes.

Nonclustered columnstore indexes were built in a read-only format before [!INCLUDEssNoVersion] 2016 and for SQL Database before version V12. You must rebuild nonclustered columnstore indexes to the current format (which is updatable) before any PARTITION operations can be run.

SET ( FILESTREAM_ON = )

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later). [!INCLUDEssSDSfull] doesn’t support FILESTREAM .

Specifies where FILESTREAM data is stored.

ALTER TABLE with the SET FILESTREAM_ON clause succeeds only if the table has no FILESTREAM columns. You can add FILESTREAM columns by using a second ALTER TABLE statement.

If you specify partition_scheme_name, the rules for CREATE TABLE apply. Be sure the table is already partitioned for row data, and its partition scheme uses the same partition function and columns as the FILESTREAM partition scheme.

filestream_filegroup_name specifies the name of a FILESTREAM filegroup. The filegroup must have one file that’s defined for the filegroup by using a CREATE DATABASE or ALTER DATABASE statement, or an error results.

«default» specifies the FILESTREAM filegroup with the DEFAULT property set. If there’s no FILESTREAM filegroup, an error results.

«NULL» specifies that all references to FILESTREAM filegroups for the table are removed. All FILESTREAM columns must be dropped first. Use SET FILESTREAM_ON to delete all FILESTREAM data that’s associated with a table.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsssql16-md] and later) and [!INCLUDEssSDSfull].

Either disables or enables system versioning of a table. To enable system versioning of a table, the system verifies that the datatype, nullability constraint, and primary key constraint requirements for system versioning are met. The system will record the history of each record in the system-versioned table in a separate history table. If the HISTORY_TABLE argument is not used, the name of this history table will be MSSQL_TemporalHistoryFor . If the history table does not exists, the system generates a new history table matching the schema of the current table, creates a link between the two tables, and enables the system to record the history of each record in the current table in the history table. If you use the HISTORY_TABLE argument to create a link to and use an existing history table, the system creates a link between the current table and the specified table. When creating a link to an existing history table, you can choose to do a data consistency check. This data consistency check ensures that existing records don’t overlap. Running the data consistency check is the default. Use the SYSTEM_VERSIONING = ON argument on a table that is defined with the PERIOD FOR SYSTEM_TIME clause to make the existing table a temporal table. For more information, see Temporal Tables.

Applies to: [!INCLUDEssSQL17] and [!INCLUDEssSDSfull].

Specifies finite or infinite retention for historical data in a temporal table. If omitted, infinite retention is assumed.

Applies to: Azure SQL Edge only

Enables retention policy based cleanup of old or aged data from tables within a database. For more information, see Enable and Disable Data Retention. The following parameters must be specified for data retention to be enabled.

FILTER_COLUMN =
Specifies the column, that should be used to determine if the rows in the table are obsolete or not. The following data types are allowed for the filter column.

  • Date
  • DateTime
  • DateTime2
  • SmallDateTime
  • DateTimeOffset

RETENTION_PERIOD = >
Specifies the retention period policy for the table. The retention period is specified as a combination of a positive integer value and the date part unit.

SET ( LOCK_ESCALATION = )

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies the allowed methods of lock escalation for a table.

AUTO
This option allows [!INCLUDEssDEnoversion] to select the lock escalation granularity that’s appropriate for the table schema.

  • If the table is partitioned, lock escalation will be allowed to the heap or B-tree (HoBT) granularity. In other words, escalation will be allowed to the partition level. After the lock is escalated to the HoBT level, the lock will not be escalated later to TABLE granularity.
  • If the table isn’t partitioned, the lock escalation is done to the TABLE granularity.

TABLE
Lock escalation is done at table-level granularity whether the table is partitioned or not partitioned. TABLE is the default value.

DISABLE
Prevents lock escalation in most cases. Table-level locks aren’t completely disallowed. For example, when you’re scanning a table that has no clustered index under the serializable isolation level, [!INCLUDEssDE] must take a table lock to protect data integrity.

Use the REBUILD WITH syntax to rebuild an entire table including all the partitions in a partitioned table. If the table has a clustered index, the REBUILD option rebuilds the clustered index. REBUILD can be run as an ONLINE operation.

Use the REBUILD PARTITION syntax to rebuild a single partition in a partitioned table.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Rebuilds all partitions when changing the partition compression settings.

All options apply to a table with a clustered index. If the table doesn’t have a clustered index, the heap structure is only affected by some of the options.

When a specific compression setting isn’t specified with the REBUILD operation, the current compression setting for the partition is used. To return the current setting, query the data_compression column in the sys.partitions catalog view.

For complete descriptions of the rebuild options, see ALTER TABLE index_option.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

Specifies the data compression option for the specified table, partition number, or range of partitions. The options are as follows:

NONE Table or specified partitions aren’t compressed. This option doesn’t apply to columnstore tables.

ROW Table or specified partitions are compressed by using row compression. This option doesn’t apply to columnstore tables.

PAGE Table or specified partitions are compressed by using page compression. This option doesn’t apply to columnstore tables.

COLUMNSTORE
Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL14] and later) and [!INCLUDEssSDSfull].

Applies only to columnstore tables. COLUMNSTORE specifies to decompress a partition that was compressed with the COLUMNSTORE_ARCHIVE option. When the data is restored, it continues to be compressed with the columnstore compression that’s used for all columnstore tables.

COLUMNSTORE_ARCHIVE
Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL14] and later) and [!INCLUDEssSDSfull].

Applies only to columnstore tables, which are tables stored with a clustered columnstore index. COLUMNSTORE_ARCHIVE will further compress the specified partition to a smaller size. Use this option for archival or other situations that require less storage and can afford more time for storage and retrieval.

To rebuild multiple partitions at the same time, see index_option. If the table doesn’t have a clustered index, changing the data compression rebuilds the heap and the nonclustered indexes. For more information about compression, see Data Compression.

Applies to: [!INCLUDEsssql22-md] and later, and [!INCLUDEssSDSfull] Preview.

Specifies the XML compression option for any xml data type columns in the table. The options are as follows:

ON
Columns using the xml data type are compressed.

OFF
Columns using the xml data type are not compressed.

ONLINE =

Specifies whether a single partition of the underlying tables and associated indexes is available for queries and data modification during the index operation. The default is OFF. You can run REBUILD as an ONLINE operation.

ON
Long-term table locks aren’t held for the duration of the index operation. S-lock on the table is required in the beginning of the index rebuild and a Sch-M lock on the table at the end of the online index rebuild. Although both locks are short metadata locks, the Sch-M lock must wait for all blocking transactions to be completed. During the wait time, the Sch-M lock blocks all other transactions that wait behind this lock when accessing the same table.

[!NOTE] Online index rebuild can set the low_priority_lock_wait options described later in this section.

OFF
Table locks are applied for the duration of the index operation. This prevents all user access to the underlying table for the duration of the operation.

column_set_name XML COLUMN_SET FOR ALL_SPARSE_COLUMNS

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsql2008-md] and later) and [!INCLUDEssSDSfull].

The name of the column set. A column set is an untyped XML representation that combines all of the sparse columns of a table into a structured output. A column set can’t be added to a table that contains sparse columns. For more information about column sets, see Use Column Sets.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL11] and later).

Enables or disables the system-defined constraints on a FileTable. Can only be used with a FileTable.

SET ( FILETABLE_DIRECTORY = directory_name )

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL11] and later). [!INCLUDEssSDSfull] doesn’t support FILETABLE .

Specifies the Windows-compatible FileTable directory name. This name should be unique among all the FileTable directory names in the database. Uniqueness comparison is case-insensitive, despite the SQL collation settings. Can only be used with a FileTable.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL17] and later).

Enables or disables Stretch Database for a table. For more information, see Stretch Database.

Enabling Stretch Database for a table

When you enable Stretch for a table by specifying ON , you also have to specify MIGRATION_STATE = OUTBOUND to begin migrating data immediately, or MIGRATION_STATE = PAUSED to postpone data migration. The default value is MIGRATION_STATE = OUTBOUND . For more information about enabling Stretch for a table, see Enable Stretch Database for a table.

Prerequisites. Before you enable Stretch for a table, you have to enable Stretch on the server and on the database. For more information, see Enable Stretch Database for a database.

Permissions. Enabling Stretch for a database or a table requires db_owner permissions. Enabling Stretch for a table also requires ALTER permissions on the table.

Disabling Stretch Database for a table

When you disable Stretch for a table, you have two options for the remote data that’s already been migrated to Azure. For more information, see Disable Stretch Database and bring back remote data.

To disable Stretch for a table and copy the remote data for the table from Azure back to SQL Server, run the following command. This command can’t be canceled.

This operation incurs data transfer costs, and it can’t be canceled. For more information, see Data Transfers Pricing Details.

After all the remote data has been copied from Azure back to SQL Server, Stretch is disabled for the table.

To disable Stretch for a table and abandon the remote data, run the following command.

After you disable Stretch Database for a table, data migration stops and query results no longer include results from the remote table.

Disabling Stretch doesn’t remove the remote table. If you want to delete the remote table, you drop it by using the Azure portal.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL17] and later).

Optionally specifies a filter predicate to select rows to migrate from a table that contains both historical and current data. The predicate must call a deterministic inline table-valued function. For more information, see Enable Stretch Database for a table and Select rows to migrate by using a filter function — Stretch Database.

[!IMPORTANT] If you provide a filter predicate that performs poorly, data migration also performs poorly. Stretch Database applies the filter predicate to the table by using the CROSS APPLY operator.

If you don’t specify a filter predicate, the entire table is migrated.

When you specify a filter predicate, you also have to specify MIGRATION_STATE.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL17] and later).

Specify OUTBOUND to migrate data from SQL Server to Azure.

Specify INBOUND to copy the remote data for the table from Azure back to SQL Server and to disable Stretch for the table. For more information, see Disable Stretch Database and bring back remote data.

This operation incurs data transfer costs, and it can’t be canceled.

Specify PAUSED to pause or postpone data migration. For more information, see Pause and resume data migration — Stretch Database.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL14] and later) and [!INCLUDEssSDSfull].

An online index rebuild has to wait for blocking operations on this table. WAIT_AT_LOW_PRIORITY indicates that the online index rebuild operation waits for low-priority locks, allowing other operations to carry on while the online index build operation is waiting. Omitting the WAIT AT LOW PRIORITY option is the same as WAIT_AT_LOW_PRIORITY ( MAX_DURATION = 0 minutes, ABORT_AFTER_WAIT = NONE) .

MAX_DURATION = time [MINUTES ]
Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL14] and later) and [!INCLUDEssSDSfull].

The wait time, which is an integer value specified in minutes, that the SWITCH or online index rebuild locks wait with low priority when running the DDL command. If the operation is blocked for the MAX_DURATION time, one of the ABORT_AFTER_WAIT actions will run. MAX_DURATION time is always in minutes, and you can omit the word MINUTES.

ABORT_AFTER_WAIT = [NONE | SELF | BLOCKERS > ]

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEssSQL14] and later) and [!INCLUDEssSDSfull].

NONE
Continue waiting for the lock with normal (regular) priority.

SELF
Exit the SWITCH or online index rebuild DDL operation currently being run without taking any action.

BLOCKERS
Kill all user transactions that currently block the SWITCH or online index rebuild DDL operation so that the operation can continue.

Requires ALTER ANY CONNECTION permission.

Applies to: [!INCLUDEssNoVersion] ([!INCLUDEsssql16-md] and later) and [!INCLUDEssSDSfull].

Conditionally drops the column or constraint only if it already exists.

Applies to: [!INCLUDE sssql22-md] and later.

Specifies whether an ALTER TABLE ADD CONSTRAINT operation is resumable. Add table constraint operation is resumable when ON . Add table constraint operation is not resumable when OFF . Default is OFF . The RESUMABLE option can be used as part of the ALTER TABLE index_option in the ALTER TABLE table_constraint.

MAX_DURATION when used with RESUMABLE = ON (requires ONLINE = ON ) indicates time (an integer value specified in minutes) that a resumable online add constraint operation is executed before being paused. If not specified, the operation continues until completion.

For more information on enabling and using resumable ALTER TABLE ADD CONSTRAINT operations, see Resumable table add constraints.

To add new rows of data, use INSERT. To remove rows of data, use DELETE or TRUNCATE TABLE. To change the values in existing rows, use UPDATE.

If there are any execution plans in the procedure cache that reference the table, ALTER TABLE marks them to be recompiled on their next execution.

Changing the size of a column

You can change the length, precision, or scale of a column by specifying a new size for the column data type. Use the ALTER COLUMN clause. If data exists in the column, the new size can’t be smaller than the maximum size of the data. Also, you can’t define the column in an index, unless the column is a varchar, nvarchar, or varbinary data type and the index isn’t the result of a PRIMARY KEY constraint. See the example in the short section titled Altering a Column Definition.

Locks and ALTER TABLE

Changes you specify in ALTER TABLE implement immediately. If the changes require modifications of the rows in the table, ALTER TABLE updates the rows. ALTER TABLE acquires a schema modify (SCH-M) lock on the table to make sure that no other connections reference even the metadata for the table during the change, except online index operations that require a short SCH-M lock at the end. In an ALTER TABLE. SWITCH operation, the lock is acquired on both the source and target tables. The modifications made to the table are logged and fully recoverable. Changes that affect all the rows in large tables, such as dropping a column or, on some editions of [!INCLUDEssNoVersion], adding a NOT NULL column with a default value, can take a long time to complete and generate many log records. Run these ALTER TABLE statements with the same care as any INSERT, UPDATE, or DELETE statement that affects many rows.

Adding NOT NULL columns as an online operation

Starting with [!INCLUDEssSQL11] Enterprise Edition, adding a NOT NULL column with a default value is an online operation when the default value is a runtime constant. This means that the operation is completed almost instantaneously despite the number of rows in the table, because the existing rows in the table aren’t updated during the operation. Instead, the default value is stored only in the metadata of the table and the value is looked up, as needed, in queries that access these rows. This behavior is automatic. No additional syntax is required to implement the online operation beyond the ADD COLUMN syntax. A runtime constant is an expression that produces the same value at runtime for each row in the table despite its determinism. For example, the constant expression «My temporary data», or the system function GETUTCDATETIME() are runtime constants. In contrast, the functions NEWID() or NEWSEQUENTIALID() aren’t runtime constants, because a unique value is produced for each row in the table. Adding a NOT NULL column with a default value that’s not a runtime constant is always run offline and an exclusive (SCH-M) lock is acquired for the duration of the operation.

While the existing rows reference the value stored in metadata, the default value is stored on the row for any new rows that are inserted and don’t specify another value for the column. The default value stored in metadata moves to an existing row when the row is updated (even if the actual column isn’t specified in the UPDATE statement), or if the table or clustered index is rebuilt.

Columns of type varchar(max), nvarchar(max), varbinary(max), xml, text, ntext, image, hierarchyid, geometry, geography, or CLR UDTS, can’t be added in an online operation. A column can’t be added online if doing so causes the maximum possible row size to exceed the 8,060-byte limit. The column is added as an offline operation in this case.

Parallel plan execution

In [!INCLUDEssEnterpriseEd11] and higher, the number of processors employed to run a single ALTER TABLE ADD (index-based) CONSTRAINT or DROP (clustered index) CONSTRAINT statement is determined by the max degree of parallelism configuration option and the current workload. If the [!INCLUDEssDE] detects that the system is busy, the degree of parallelism of the operation is automatically reduced before statement execution starts. You can manually configure the number of processors that are used to run the statement by specifying the MAXDOP option. For more information, see Configure the max degree of parallelism Server Configuration Option.

In addition to performing SWITCH operations that involve partitioned tables, use ALTER TABLE to change the state of the columns, constraints, and triggers of a partitioned table just like it’s used for nonpartitioned tables. However, this statement can’t be used to change the way the table itself is partitioned. To repartition a partitioned table, use ALTER PARTITION SCHEME and ALTER PARTITION FUNCTION. Additionally, you can’t change the data type of a column of a partitioned table.

Restrictions on tables with schema-bound views

The restrictions that apply to ALTER TABLE statements on tables with schema-bound views are the same as the restrictions currently applied when modifying tables with a simple index. Adding a column is allowed. However, removing or changing a column that participates in any schema-bound view isn’t allowed. If the ALTER TABLE statement requires changing a column used in a schema-bound view, ALTER TABLE fails and the [!INCLUDEssDE] raises an error message. For more information about schema binding and indexed views, see CREATE VIEW.

Adding or removing triggers on base tables isn’t affected by creating a schema-bound view that references the tables.

Indexes and ALTER TABLE

Indexes created as part of a constraint are dropped when the constraint is dropped. Indexes that were created with CREATE INDEX must be dropped with DROP INDEX. Use The ALTER INDEX statement to rebuild an index part of a constraint definition; the constraint doesn’t have to be dropped and added again with ALTER TABLE.

All indexes and constraints based on a column must be removed before the column can be removed.

When you delete a constraint that created a clustered index, the data rows that were stored in the leaf level of the clustered index are stored in a nonclustered table. You can drop the clustered index and move the resulting table to another filegroup or partition scheme in a single transaction by specifying the MOVE TO option. The MOVE TO option has the following restrictions:

  • MOVE TO isn’t valid for indexed views or nonclustered indexes.
  • The partition scheme or filegroup must already exist.
  • If MOVE TO isn’t specified, the table is located in the same partition scheme or filegroup as was defined for the clustered index.

When you drop a clustered index, specify the ONLINE **=** ON option so the DROP INDEX transaction doesn’t block queries and modifications to the underlying data and associated nonclustered indexes.

ONLINE = ON has the following restrictions:

  • ONLINE = ON isn’t valid for clustered indexes that are also disabled. Disabled indexes must be dropped by using ONLINE = OFF.
  • Only one index at a time can be dropped.
  • ONLINE = ON isn’t valid for indexed views, nonclustered indexes, or indexes on local temp tables.
  • ONLINE = ON isn’t valid for columnstore indexes.

Temporary disk space equal to the size of the existing clustered index is required to drop a clustered index. This additional space is released as soon as the operation is completed.

[!NOTE] The options listed under apply to clustered indexes on tables and can’t be applied to clustered indexes on views or nonclustered indexes.

Replicating schema changes

When you run ALTER TABLE on a published table at a [!INCLUDEssNoVersion] Publisher, by default, that change propagates to all [!INCLUDEssNoVersion] Subscribers. This functionality has some restrictions. You can disable it. For more information, see Make Schema Changes on Publication Databases.

System tables can’t be enabled for compression. If the table is a heap, the rebuild operation for ONLINE mode will be single threaded. Use OFFLINE mode for a multi-threaded heap rebuild operation. For a more information about data compression, see Data Compression.

To evaluate how changing the compression state will affect a table, an index, or a partition, use the sp_estimate_data_compression_savings system stored procedure.

The following restrictions apply to partitioned tables:

Dropping NTEXT columns

When dropping columns using the deprecated NTEXT data type, the cleanup of the deleted data occurs as a serialized operation on all rows. The cleanup can require a large amount of time. When dropping an NTEXT column in a table with lots of rows, update the NTEXT column to NULL value first, then drop the column. You can run this option with parallel operations and make it much faster.

Online index REBUILD

To run the DDL statement for an online index rebuild, all active blocking transactions running on a particular table must be completed. When the online index rebuild launches, it blocks all new transactions that are ready to start running on this table. Although the duration of the lock for online index rebuild is short, waiting for all open transactions on a given table to complete and blocking the new transactions to start, might significantly affect the throughput. This can cause a workload slow-down or timeout and significantly limit access to the underlying table. The WAIT_AT_LOW_PRIORITY option allows DBAs to manage the S-lock and Sch-M locks required for online index rebuilds. In all three cases: NONE, SELF, and BLOCKERS, if during the wait time ( (MAX_DURATION =n [minutes]) ) there are no blocking activities, the online index rebuild is run immediately without waiting and the DDL statement is completed.

The ALTER TABLE statement supports only two-part ( schema.object ) table names. In [!INCLUDEssnoversion], specifying a table name using the following formats fails at compile time with error 117.

  • server.database.schema.table
  • .database.schema.table
  • ..schema.table

In earlier versions, specifying the format server.database.schema.table returned error 4902. Specifying the format .database.schema.table or the format ..schema.table succeeded.

To resolve the problem, remove the use of a four-part prefix.

Requires ALTER permission on the table.

ALTER TABLE permissions apply to both tables involved in an ALTER TABLE SWITCH statement. Any data that’s switched inherits the security of the target table.

If you’ve defined any columns in the ALTER TABLE statement to be of a common language runtime (CLR) user-defined type or alias data type, REFERENCES permission on the type is required.

Adding or altering a column that updates the rows of the table requires UPDATE permission on the table. For example, adding a NOT NULL column with a default value or adding an identity column when the table isn’t empty.

Category Featured syntax elements
Adding columns and constraints ADD * PRIMARY KEY with index options * sparse columns and column sets *
Dropping columns and constraints DROP
Altering a column definition change data type * change column size * collation
Altering a table definition DATA_COMPRESSION * SWITCH PARTITION * LOCK ESCALATION * change tracking
Disabling and enabling constraints and triggers CHECK * NO CHECK * ENABLE TRIGGER * DISABLE TRIGGER
Online operations ONLINE
System versioning SYSTEM_VERSIONING

Adding columns and constraints

Examples in this section demonstrate adding columns and constraints to a table.

A. Adding a new column

The following example adds a column that allows null values and has no values provided through a DEFAULT definition. In the new column, each row will have NULL .

B. Adding a column with a constraint

The following example adds a new column with a UNIQUE constraint.

C. Adding an unverified CHECK constraint to an existing column

The following example adds a constraint to an existing column in the table. The column has a value that violates the constraint. Therefore, WITH NOCHECK is used to prevent the constraint from being validated against existing rows, and to allow for the constraint to be added.

D. Adding a DEFAULT constraint to an existing column

The following example creates a table with two columns and inserts a value into the first column, and the other column remains NULL. A DEFAULT constraint is then added to the second column. To verify that the default is applied, another value is inserted into the first column, and the table is queried.

E. Adding several columns with constraints

The following example adds several columns with constraints defined with the new column. The first new column has an IDENTITY property. Each row in the table has new incremental values in the identity column.

F. Adding a nullable column with default values

The following example adds a nullable column with a DEFAULT definition, and uses WITH VALUES to provide values for each existing row in the table. If WITH VALUES isn’t used, each row has the value NULL in the new column.

G. Creating a PRIMARY KEY constraint with index or data compression options

The following example creates the PRIMARY KEY constraint PK_TransactionHistoryArchive_TransactionID and sets the options FILLFACTOR , ONLINE , and PAD_INDEX . The resulting clustered index will have the same name as the constraint.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

This similar example applies page compression while applying the clustered primary key.

H. Adding a sparse column

The following examples show adding and modifying sparse columns in table T1. The code to create table T1 is as follows.

To add an additional sparse column C5 , execute the following statement.

To convert the C4 non-sparse column to a sparse column, execute the following statement.

To convert the C4 sparse column to a nonsparse column, execute the following statement.

I. Adding a column set

The following examples show adding a column to table T2 . A column set can’t be added to a table that already contains sparse columns. The code to create table T2 is as follows.

The following three statements add a column set named CS , and then modify columns C2 and C3 to SPARSE.

J. Adding an encrypted column

The following statement adds an encrypted column named PromotionCode .

K. Adding a primary key with resumable operation

Resumable ALTER TABLE operation for adding a primary key clustered on column (a) with MAX_DURATION of 240 minutes.

Dropping columns and constraints

The examples in this section demonstrate dropping columns and constraints.

A. Dropping a column or columns

The first example modifies a table to remove a column. The second example removes multiple columns.

B. Dropping constraints and columns

The first example removes a UNIQUE constraint from a table. The second example removes two constraints and a single column.

C. Dropping a PRIMARY KEY constraint in the ONLINE mode

The following example deletes a PRIMARY KEY constraint with the ONLINE option set to ON .

D. Adding and dropping a FOREIGN KEY constraint

The following example creates the table ContactBackup , and then alters the table, first by adding a FOREIGN KEY constraint that references the table Person.Person , then by dropping the FOREIGN KEY constraint.

Altering a column definition

A. Changing the data type of a column

The following example changes a column of a table from INT to DECIMAL .

B. Changing the size of a column

The following example increases the size of a varchar column and the precision and scale of a decimal column. Because the columns contain data, the column size can only be increased. Also notice that col_a is defined in a unique index. The size of col_a can still be increased because the data type is a varchar and the index isn’t the result of a PRIMARY KEY constraint.

C. Changing column collation

The following example shows how to change the collation of a column. First, a table is created table with the default user collation.

Next, column C2 collation is changed to Latin1_General_BIN. The data type is required, even though it isn’t changed.

D. Encrypting a column

The following example shows how to encrypt a column using Always Encrypted with secure enclaves.

First, a table is created without any encrypted columns.

Next, column ‘C2’ is encrypted with a column encryption key, named CEK1 , and randomized encryption. For the following statement to succeed:

  • The column encryption key must be enclave-enabled. Meaning, it must be encrypted with a column master key that allows enclave computations.
  • The target SQL Server instance must support Always Encrypted with secure enclaves.
  • The statement must be issued over a connection set up for Always Encrypted with secure enclaves, and using a supported client driver.
  • The calling application must have access to the column master key, protecting CEK1 .

Altering a table definition

The examples in this section demonstrate how to alter the definition of a table.

A. Modifying a table to change the compression

The following example changes the compression of a nonpartitioned table. The heap or clustered index will be rebuilt. If the table is a heap, all nonclustered indexes will be rebuilt.

The following example changes the compression of a partitioned table. The REBUILD PARTITION = 1 syntax causes only partition number 1 to be rebuilt.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

The same operation using the following alternate syntax causes all partitions in the table to be rebuilt.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

For additional data compression examples, see Data Compression.

B. Modifying a columnstore table to change archival compression

The following example further compresses a columnstore table partition by applying an additional compression algorithm. This compression reduces the table to a smaller size, but also increases the time required for storage and retrieval. This is useful for archiving or for situations that require less space and can afford more time for storage and retrieval.

Applies to: [!INCLUDEssSQL14] and later and [!INCLUDEssSDSfull].

The following example decompresses a columnstore table partition that was compressed with COLUMNSTORE_ARCHIVE option. When the data is restored, it will continue to be compressed with the columnstore compression that’s used for all columnstore tables.

Applies to: [!INCLUDEssSQL14] and later and [!INCLUDEssSDSfull].

C. Switching partitions between tables

The following example creates a partitioned table, assuming that partition scheme myRangePS1 is already created in the database. Next, a non-partitioned table is created with the same structure as the partitioned table and on the same filegroup as PARTITION 2 of table PartitionTable . The data of PARTITION 2 of table PartitionTable is then switched into table NonPartitionTable .

D. Allowing lock escalation on partitioned tables

The following example enables lock escalation to the partition level on a partitioned table. If the table isn’t partitioned, lock escalation is set at the TABLE level.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

E. Configuring change tracking on a table

The following example enables change tracking on the Person.Person table.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

The following example enables change tracking and enables the tracking of the columns that are updated during a change.

Applies to: [!INCLUDEsql2008-md] and later.

The following example disables change tracking on the Person.Person table.

Applies to: [!INCLUDEsql2008-md] and later and [!INCLUDEssSDSfull].

Disabling and enabling constraints and triggers

A. Disabling and re-enabling a constraint

The following example disables a constraint that limits the salaries accepted in the data. NOCHECK CONSTRAINT is used with ALTER TABLE to disable the constraint and allow for an insert that would typically violate the constraint. CHECK CONSTRAINT re-enables the constraint.

B. Disabling and re-enabling a trigger

The following example uses the DISABLE TRIGGER option of ALTER TABLE to disable the trigger and allow for an insert that would typically violate the trigger. ENABLE TRIGGER is then used to re-enable the trigger.

A. Online index rebuild using low-priority wait options

The following example shows how to perform an online index rebuild specifying the low-priority wait options.

Applies to: [!INCLUDEssSQL14] and later and [!INCLUDEssSDSfull].

B. Online alter column

The following example shows how to run an alter column operation with the ONLINE option.

Applies to: [!INCLUDEsssql16-md] and later and [!INCLUDEssSDSfull].

The following four examples will help you become familiar with the syntax for using system versioning. For additional assistance, see Getting Started with System-Versioned Temporal Tables.

Applies to: [!INCLUDEsssql16-md] and later and [!INCLUDEssSDSfull].

A. Add system versioning to existing tables

The following example shows how to add system versioning to an existing table and create a future history table. This example assumes that there’s an existing table called InsurancePolicy with a primary key defined. This example populates the newly created period columns for system versioning using default values for the start and end times because these values can’t be null. This example uses the HIDDEN clause to ensure no impact on existing applications interacting with the current table. It also uses HISTORY_RETENTION_PERIOD that’s available on [!INCLUDEsqldbesa] only.

B. Migrate an existing solution to use system versioning

The following example shows how to migrate to system versioning from a solution that uses triggers to mimic temporal support. The example assumes there’s an existing solution that uses a ProjectTask table and a ProjectTaskHistory table for its existing solution, that’s uses the Changed Date and Revised Date columns for its periods, that these period columns don’t use the datetime2 datatype and that the ProjectTask table has a primary key defined.

C. Disabling and re-enabling system versioning to change table schema

This example shows how to disable system versioning on the Department table, add a column, and re-enable system versioning. Disabling system versioning is required to modify the table schema. Do these steps within a transaction to prevent updates to both tables while updating the table schema, which enables the DBA to skip the data consistency check when re-enabling system versioning and gain a performance benefit. Tasks such as creating statistics, switching partitions, or applying compression to one or both tables doesn’t require disabling system versioning.

D. Removing system versioning

This example shows how to completely remove system versioning from the Department table and drop the DepartmentHistory table. Optionally, you might also want to drop the period columns used by the system to record system versioning information. You can’t drop either the Department or the DepartmentHistory tables while system versioning is enabled.

The following examples A through C use the FactResellerSales table in the [!INCLUDEssawPDW] database.

A. Determining if a table is partitioned

The following query returns one or more rows if the table FactResellerSales is partitioned. If the table isn’t partitioned, no rows are returned.

B. Determining boundary values for a partitioned table

The following query returns the boundary values for each partition in the FactResellerSales table.

C. Determining the partition column for a partitioned table

The following query returns the name of the partitioning column for table. FactResellerSales .

D. Merging two partitions

The following example merges two partitions on a table.

The Customer table has the following definition:

The following command combines the 10 and 25 partition boundaries.

The new DDL for the table is:

E. Splitting a partition

The following example splits a partition on a table.

The Customer table has the following DDL:

The following command creates a new partition bound by the value 75, between 50 and 100.

The new DDL for the table is:

F. Using SWITCH to move a partition to a history table

The following example moves the data in a partition of the Orders table to a partition in the OrdersHistory table.

The Orders table has the following DDL:

In this example, the Orders table has the following partitions. Each partition contains data.

  • Partition 1 (has data): OrderDate < ‘2004-01-01’
  • Partition 2 (has data): ‘2004-01-01’
  • Partition 3 (has data): ‘2005-01-01’
  • Partition 4 (has data): ‘2006-01-01′
  • Partition 5 (has data): ‘2007-01-01’

The OrdersHistory table has the following DDL, which has identical columns and column names as the Orders table. Both are hash-distributed on the id column.

Although the columns and column names must be the same, the partition boundaries don’t need to be the same. In this example, the OrdersHistory table has the following two partitions and both partitions are empty:

  • Partition 1 (no data): OrderDate < ‘2004-01-01’
  • Partition 2 (empty): ‘2004-01-01’

For the previous two tables, the following command moves all rows with OrderDate < ‘2004-01-01’ from the Orders table to the OrdersHistory table.

As a result, the first partition in Orders is empty and the first partition in OrdersHistory contains data. The tables now appear as follows:

  • Partition 1 (empty): OrderDate < ‘2004-01-01’
  • Partition 2 (has data): ‘2004-01-01’
  • Partition 3 (has data): ‘2005-01-01’
  • Partition 4 (has data): ‘2006-01-01′
  • Partition 5 (has data): ‘2007-01-01’
  • Partition 1 (has data): OrderDate < ‘2004-01-01’
  • Partition 2 (empty): ‘2004-01-01’

To clean up the Orders table, you can remove the empty partition by merging partitions 1 and 2 as follows:

After the merge, the Orders table has the following partitions:

  • Partition 1 (has data): OrderDate < ‘2005-01-01’
  • Partition 2 (has data): ‘2005-01-01’
  • Partition 3 (has data): ‘2006-01-01′
  • Partition 4 (has data): ‘2007-01-01’

Suppose another year passes and you’re ready to archive the year 2005. You can allocate an empty partition for the year 2005 in the OrdersHistory table by splitting the empty partition as follows:

Add a column with a default value to an existing table in SQL Server

How can I add a column with a default value to an existing table in SQL Server 2000 / SQL Server 2005?

44 Answers 44

Syntax:

Example:

Notes:

Optional Constraint Name:
If you leave out CONSTRAINT D_SomeTable_SomeCol then SQL Server will autogenerate
a Default-Contraint with a funny Name like: DF__SomeTa__SomeC__4FB7FEF6

Optional With-Values Statement:
The WITH VALUES is only needed when your Column is Nullable
and you want the Default Value used for Existing Records.
If your Column is NOT NULL , then it will automatically use the Default Value
for all Existing Records, whether you specify WITH VALUES or not.

How Inserts work with a Default-Constraint:
If you insert a Record into SomeTable and do not Specify SomeCol ‘s value, then it will Default to 0 .
If you insert a Record and Specify SomeCol ‘s value as NULL (and your column allows nulls),
then the Default-Constraint will not be used and NULL will be inserted as the Value.

Notes were based on everyone’s great feedback below.
Special Thanks to:
@Yatrix, @WalterStabosz, @YahooSerious, and @StackMan for their Comments.

The inclusion of the DEFAULT fills the column in existing rows with the default value, so the NOT NULL constraint is not violated.

When adding a nullable column, WITH VALUES will ensure that the specific DEFAULT value is applied to existing rows:

The most basic version with two lines only

Beware when the column you are adding has a NOT NULL constraint, yet does not have a DEFAULT constraint (value). The ALTER TABLE statement will fail in that case if the table has any rows in it. The solution is to either remove the NOT NULL constraint from the new column, or provide a DEFAULT constraint for it.

If you want to add multiple columns you can do it this way for example:

To add a column to an existing database table with a default value, we can use:

Here is another way to add a column to an existing database table with a default value.

A much more thorough SQL script to add a column with a default value is below including checking if the column exists before adding it also checkin the constraint and dropping it if there is one. This script also names the constraint so we can have a nice naming convention (I like DF_) and if not SQL will give us a constraint with a name which has a randomly generated number; so it’s nice to be able to name the constraint too.

These are two ways to add a column to an existing database table with a default value.

You can do the thing with T-SQL in the following way.

As well as you can use SQL Server Management Studio also by right clicking table in the Design menu, setting the default value to table.

And furthermore, if you want to add the same column (if it does not exists) to all tables in database, then use:

In SQL Server 2008-R2, I go to the design mode — in a test database — and add my two columns using the designer and made the settings with the GUI, and then the infamous Right-Click gives the option «Generate Change Script«!

Bang up pops a little window with, you guessed it, the properly formatted guaranteed-to-work change script. Hit the easy button.

Alternatively, you can add a default without having to explicitly name the constraint:

If you have an issue with existing default constraints when creating this constraint then they can be removed by:

This can be done in the SSMS GUI as well. I show a default date below but the default value can be whatever, of course.

  1. Put your table in design view (Right click on the table in object explorer->Design)
  2. Add a column to the table (or click on the column you want to update if it already exists)
  3. In Column Properties below, enter (getdate()) or ‘abc’ or 0 or whatever value you want in Default Value or Binding field as pictured below:

enter image description here

The MSDN article ALTER TABLE (Transact-SQL) has all of the alter table syntax.

First create a table with name student:

Add one column to it:

The table is created and a column is added to an existing table with a default value.

Image 1

This is for SQL Server:

If you want to add constraints then:

This has a lot of answers, but I feel the need to add this extended method. This seems a lot longer, but it is extremely useful if you’re adding a NOT NULL field to a table with millions of rows in an active database.

What this will do is add the column as a nullable field and with the default value, update all fields to the default value (or you can assign more meaningful values), and finally it will change the column to be NOT NULL.

The reason for this is if you update a large scale table and add a new not null field it has to write to every single row and hereby will lock out the entire table as it adds the column and then writes all the values.

This method will add the nullable column which operates a lot faster by itself, then fills the data before setting the not null status.

I’ve found that doing the entire thing in one statement will lock out one of our more active tables for 4-8 minutes and quite often I have killed the process. This method each part usually takes only a few seconds and causes minimal locking.

Additionally, if you have a table in the area of billions of rows it may be worth batching the update like so:

Похожие публикации:

  1. Как сделать выборку данных по условию из таблицы sql запросом
  2. Как сделать резервную копию бд в sql server
  3. Как складывать строки в sql mysql
  4. Как создать sql server в visual studio

Как добавить столбец в MS SQL

В MS SQL Server столбцы представляют собой основные элементы для организации и хранения данных в таблицах. Вам может понадобиться добавить новый столбец в существующую таблицу для хранения дополнительной информации или для изменения структуры таблицы. В этой статье мы рассмотрим различные способы добавления столбца в MS SQL.

1. Использование оператора ALTER TABLE

ALTER TABLE — это оператор, который позволяет изменять структуру существующей таблицы. Чтобы добавить столбец с использованием оператора ALTER TABLE, вам понадобится следующий синтаксис:

 ALTER TABLE table_name ADD column_name datatype [NULL | NOT NULL] [DEFAULT default_value]; 

Пример:

Предположим, у нас есть таблица «Employees» с уже существующими столбцами «EmployeeID», «FirstName» и «LastName». Чтобы добавить столбец «Email» типа VARCHAR(50), вы можете выполнить следующий запрос:

 ALTER TABLE Employees ADD Email VARCHAR(50); 

2. Использование инструмента SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) — это графический инструмент, предоставляемый Microsoft для управления базами данных SQL Server. SSMS предоставляет простой способ добавления столбцов в таблицы без необходимости написания SQL-запросов.

Чтобы добавить столбец с использованием SSMS, выполните следующие шаги:

  1. Откройте SSMS и подключитесь к SQL Server.
  2. Раскройте дерево «Объекты базы данных» и найдите нужную таблицу.
  3. Щелкните правой кнопкой мыши на таблице и выберите «Дизайн».
  4. В разделе «Столбцы» находится сетка, где вы можете добавить новый столбец, выбрав пустую ячейку в последней строке и вводя имя столбца и его тип.
  5. Сохраните изменения, нажав кнопку «Сохранить» или используя сочетание клавиш Ctrl+S.

3. Использование CREATE TABLE

Если вы планируете создать новую таблицу и добавить столбец в нее одновременно, вы можете использовать оператор CREATE TABLE. В этом случае вам потребуется следующий синтаксис:

 CREATE TABLE table_name ( column1 datatype1 [NULL | NOT NULL] [DEFAULT default_value1], column2 datatype2 [NULL | NOT NULL] [DEFAULT default_value2], . columnn datatypen [NULL | NOT NULL] [DEFAULT default_valuen] ); 

Пример:

Предположим, вы хотите создать новую таблицу «Customers» с двумя столбцами «CustomerID» и «CustomerName». Чтобы добавить столбец «Email» типа VARCHAR(50) при создании таблицы, вы можете выполнить следующий запрос:

 CREATE TABLE Customers ( CustomerID INT PRIMARY KEY, CustomerName VARCHAR(50), Email VARCHAR(50) ); 

Заключение

Добавление столбца в MS SQL может быть выполнено с использованием оператора ALTER TABLE, инструмента SQL Server Management Studio или оператора CREATE TABLE. Каждый из этих методов предоставляет удобные способы изменения структуры существующих таблиц или создания новых таблиц с необходимыми столбцами. Выберите подходящий метод в зависимости от ваших потребностей и предпочтений.

Как добавить столбец в SQL Server: пошаговое руководство

Чтобы добавить столбец в таблицу SQL Server, вы можете использовать оператор ALTER TABLE. Вот простой пример:

 ALTER TABLE Название_таблицы ADD Название_столбца Тип_данных; 

Замените «Название_таблицы» на имя вашей таблицы и «Название_столбца» на имя нового столбца, который вы хотите добавить. «Тип_данных» должен быть соответствующим типом данных для нового столбца (например, VARCHAR для текстового столбца или INT для целочисленного столбца). Например, если вы хотите добавить столбец «age» с типом данных INT в таблицу «users», используйте следующий код:

 ALTER TABLE users ADD age INT; 

Детальный ответ

Привет! Сегодня я хотел бы поделиться с тобой некоторыми знаниями о том, как добавить столбец в SQL Server. Добавление столбца — одна из основных операций, которые необходимо выполнить при работе с базами данных. Это может быть полезно, когда вы хотите добавить новое поле или атрибут к существующей таблице. Существует несколько способов добавить столбец в SQL Server. Давайте рассмотрим два наиболее популярных способа — использование оператора ALTER TABLE и конструктора CREATE TABLE .

Использование оператора ALTER TABLE

Первый способ — использовать оператор ALTER TABLE для изменения существующей таблицы.

 ALTER TABLE table_name ADD column_name datatype; 

Пример:

 ALTER TABLE employees ADD age INT; 

В этом примере мы добавляем столбец «age» в таблицу «employees» с типом данных INT.

Использование конструктора CREATE TABLE

Второй способ — использовать конструктор CREATE TABLE для создания новой таблицы с добавляемым столбцом. Вы можете указать все существующие столбцы, а затем добавить новый столбец.

 CREATE TABLE new_table_name ( existing_column1 datatype, existing_column2 datatype, new_column datatype ); 

Пример:

 CREATE TABLE employees_new ( id INT, name VARCHAR(50), age INT ); 

В этом примере мы создаем новую таблицу «employees_new» с тремя столбцами — «id», «name» и «age».

Обратите внимание на существующие данные

При добавлении столбца в существующую таблицу очень важно обратить внимание на существующие данные. Возможно, вам придется предоставить значения по умолчанию или обновить существующие данные.

Итоги

Добавление столбца в SQL Server может быть легкой задачей, если вы знаете правильный синтаксис. В этой статье мы рассмотрели два основных способа добавления столбца — использование оператора ALTER TABLE и конструктора CREATE TABLE . Вам также следует учесть существующие данные и решить, как будете обрабатывать их при добавлении нового столбца. Теперь, когда вы знаете, как добавить столбец в SQL Server, вы можете продолжать разрабатывать и улучшать свои базы данных. Удачи!

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *