{"id":251,"date":"2019-07-26T14:52:07","date_gmt":"2019-07-26T09:22:07","guid":{"rendered":"https:\/\/techieshouts.com\/?p=251"},"modified":"2022-08-09T19:07:34","modified_gmt":"2022-08-09T13:37:34","slug":"alter-table-in-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/alter-table-in-sql-server\/","title":{"rendered":"ALTER TABLE usages in MS SQL Server"},"content":{"rendered":"\n<p>In SQL server, ALTER TABLE statement is used for the following purposes\u00a0<\/p>\n\n\n\n<ol><li>ADD columns\/ constraints in the existing table.\u00a0<\/li><li>DROP columns\/ constraints in the existing table.\u00a0<\/li><li>ALTER columns in the existing table.\u00a0<\/li><li>ENABLE\/ DISABLE triggers.<\/li><\/ol>\n\n\n\n<div class=\"wp-block-ub-table-of-contents ub_table-of-contents\" data-showtext=\"show\" data-hidetext=\"hide\"><div class=\"ub_table-of-contents-header\"><div class=\"ub_table-of-contents-title\">Table of contents<\/div><\/div><div style=\"display:block\" class=\"ub_table-of-contents-container ub_table-of-contents-1-column\"><ul><li><a href=\"#0--add-columnsconstraints--\">ADD columns\/constraints <\/a><\/li><li><a href=\"#4--drop-columnsconstraints-in-the-existing-table--\">DROP columns\/constraints<\/a><\/li><li><a href=\"#8--alter-columns-in-the-existing-table-\">ALTER columns<\/a><\/li><li><a href=\"#10--enable-disable-triggers-\">ENABLE\/ DISABLE triggers<\/a><\/li><\/ul><\/div><\/div>\n\n\n\n<h2 id=\"0--add-columnsconstraints--\"><strong>ADD columns\/constraints<\/strong> <\/h2>\n\n\n\n<p>Syntax to add columns and constraints while creating column<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nADD columnName1 datatype,  \ncolumnName2 datatype CONSTRAINT constraintName constraintType<\/pre>\n\n\n\n<p>Syntax to add column(s)<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nADD columnName1 datatype, columnName2 datatype<\/pre>\n\n\n\n<p>One or more columns can be added at once using the above syntax.&nbsp; <\/p>\n\n\n\n<p>Syntax to add constraint(s) to the existing column<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nADD CONSTRAINT constraintName1 constraintType, CONSTRAINT constraintName2 constraintType<\/pre>\n\n\n\n<p>constraintType &#8211; Type of constraints like Primary Key, Default, etc.<\/p>\n\n\n\n<p>To add constraint(s) to the existing column, use the ADD CONSTRAINT keyword in the ALTER TABLE statement.<\/p>\n\n\n\n<p><strong>Points to note when adding columns and constraints:<\/strong><\/p>\n\n\n\n<ul><li>non-nullable columns can be added only when the table is empty.&nbsp;&nbsp;<\/li><li>To add a non-nullable column when the table isn&#8217;t empty, the column must be added with a default constraint like below<\/li><\/ul>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nADD columnName DATETIME NOT NULL  \nCONSTRAINT DF_tableName_columnName DEFAULT GETDATE()<\/pre>\n\n\n\n<ul><li>When adding a nullable column with a default constraint and the table isn&#8217;t empty, define default constraint WITH VALUES to update the column in the existing rows in the table with default values.<\/li><\/ul>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nADD columnName DATETIME NOT NULL  \nCONSTRAINT DF_tableName_columnName DEFAULT GETDATE() WITH VALUES<\/pre>\n\n\n\n<p>Let us see some of the examples to understand this better<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nADD battingStyle VARCHAR(10), modifiedDate DATETIME<\/pre>\n\n\n\n<p>The above query adds the &#8216;battingStyle&#8217; and &#8216;modifiedDate&#8217; columns to the table &#8216;tblPlayer&#8217;. Both the columns are NULL by default. <\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nADD CONSTRAINT DF_tblPlayer_modifiedDate DEFAULT GETDATE() FOR modifiedDate<\/pre>\n\n\n\n<p>The above query adds the default constraint for the column &#8216;modifiedDate&#8217;. <\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nADD battingStyle VARCHAR(10), modifiedDate DATETIME CONSTRAINT DF_tblPlayer_modifiedDate DEFAULT GETDATE() WITH VALUES<\/pre>\n\n\n\n<p>The above query adds the columns &#8216;battingStyle&#8217; and &#8216;modifiedDate&#8217; to the table &#8216;tblPlayer&#8217;. It also adds the default constraint to the column &#8216;modifiedDate&#8217;.&nbsp; <\/p>\n\n\n\n<h2 id=\"4--drop-columnsconstraints-in-the-existing-table--\"><strong>DROP columns\/constraints<\/strong><\/h2>\n\n\n\n<p>Similar to adding a new column, the ALTER TABLE statement is also used to DROP the columns and constraints. A column can&#8217;t be dropped when it has any constraints or indexes. Before dropping the column, drop all the constraints first.<\/p>\n\n\n\n<p>Syntax to drop constraints and columns<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nDROP CONSTRAINT constraintName, COLUMN columnName<\/pre>\n\n\n\n<p>Syntax to drop column(s) and constraint(s) in a separate queries<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nDROP COLUMN columnName<\/pre>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nDROP CONSTRAINT constraintName<\/pre>\n\n\n\n<p>Now, let us see some examples for the above syntaxes to understand.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nDROP CONSTRAINT DF_tblPlayer_modifiedDate<\/pre>\n\n\n\n<p>The above query drops the default constraint &#8216;DF_tblPlayer_modifiedDate&#8217; on the column &#8216;modifiedDate&#8217;. <\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nDROP COLUMN modifiedDate <\/pre>\n\n\n\n<p>The above query drops the column &#8216;modifiedDate&#8217; from the table &#8216;tblPlayer&#8217;. <\/p>\n\n\n\n<h2 id=\"8--alter-columns-in-the-existing-table-\"><strong>ALTER columns<\/strong><\/h2>\n\n\n\n<p>In ALTER TABLE statement, ALTER COLUMN keyword is mainly used to,<\/p>\n\n\n\n<ul><li>Modify the data type of a column.\u00a0<\/li><li>Change the size of a column.\u00a0\u00a0<\/li><li>Change NULL to NOT NULL or vice versa.\u00a0<\/li><\/ul>\n\n\n\n<div class=\"wp-block-ub-notification-box\"><div class=\"ub_notify_info\"><p class=\"ub_notify_text\" style=\"text-align:left\">It is <strong>not possible<\/strong> to change the column name using ALTER TABLE statement.<\/p><\/div><\/div>\n\n\n\n<p>Syntax to alter column in a table<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tableName \nALTER COLUMN columnName datatype <\/pre>\n\n\n\n<p>Here are some different cases of using Alter column statement<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nALTER COLUMN battingStyle VARCHAR(15) <\/pre>\n\n\n\n<p>The above query increases the size of &#8216;battingStyle&#8217; column from 10 to 15. <\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nALTER COLUMN battingStyle VARCHAR(15) NOT NULL<\/pre>\n\n\n\n<p>The query alters the &#8216;battingStyle&#8217; column from NULL to NOT NULL.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">ALTER TABLE tblPlayer \nALTER COLUMN battingStyle NVARCHAR(15)<\/pre>\n\n\n\n<p>Above query alters the datatype of &#8216;battingStyle&#8217; column from VARCHAR to NVARCHAR. <\/p>\n\n\n\n<p><strong>Here are some important points to consider while using ALTER statement<\/strong><\/p>\n\n\n\n<ul><li>A column can&#8217;t be dropped if it has constraints or indexes.&nbsp;&nbsp;<\/li><li>Primary key or foreign key column can&#8217;t be altered.&nbsp;<\/li><li>Changing the primary key column from NOT NULL to NULL is not allowed.&nbsp;<\/li><li>The previous data type must be implicitly convertible to the new data type.&nbsp;<\/li><li>Altering the size of a column when the table isn&#8217;t empty, the new size should be greater than the current size.&nbsp;<\/li><li> The current data type of the column must be implicitly convertible to the new data type.&nbsp;<\/li><li>A column with a timestamp data type can&#8217;t be altered. <\/li><\/ul>\n\n\n\n<div class=\"wp-block-ub-notification-box\"><div class=\"ub_notify_info\"><p class=\"ub_notify_text\" style=\"text-align:left\">To alter a NULL column to NOT NULL when the table isn&#8217;t empty, update the null values with some values and alter the column as NOT NULL.  <\/p><\/div><\/div>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">UPDATE tableName SET columnName = ' ' WHERE columnName IS NULL; \nALTER TABLE tableName ALTER COLUMN columnName VARCHAR(20) NOT NULL;<\/pre>\n\n\n\n<h2 id=\"10--enable-disable-triggers-\"><strong>ENABLE\/ DISABLE triggers<\/strong><\/h2>\n\n\n\n<p>ALTER TABLE statement can also be used to enable and disable a trigger or all triggers in a table. <\/p>\n\n\n\n<p>Syntax to enable a trigger(s) in a table<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">--Enable a trigger\r\nALTER TABLE tableName\r\nENABLE TRIGGER triggerName;\r\n\r\n--Enable All triggers\r\nALTER TABLE tableName\r\nENABLE TRIGGER ALL;\r\n<\/pre>\n\n\n\n<p>Syntax to disable a trigger(s) in a table<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">--Disable a trigger\r\nALTER TABLE tableName\r\nDISABLE TRIGGER triggerName\r\n\r\n--Disable All triggers\r\nALTER TABLE tableName\r\nDISABLE TRIGGER ALL<\/pre>\n\n\n\n<p>Also, Check the article on <a href=\"https:\/\/techieshouts.com\/create-table-in-sql-server\/\">CREATE TABLE<\/a><\/p>\n\n\n\n<p>References: <a href=\"https:\/\/docs.microsoft.com\/en-us\/sql\/t-sql\/statements\/alter-table-transact-sql?view=sql-server-2017\" target=\"_blank\" rel=\"noopener\">ALTER TABLE (Transact-SQL)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL server, ALTER TABLE statement is used for the following purposes\u00a0 ADD columns\/ constraints in the existing table.\u00a0 DROP columns\/ constraints in the existing table.\u00a0 ALTER columns in the existing table.\u00a0 ENABLE\/ DISABLE triggers. ADD columns\/constraints Syntax to add columns and constraints while creating column Syntax to add column(s) One or more columns can\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/alter-table-in-sql-server\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,15],"tags":[],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/251"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":13,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/251\/revisions\/601"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}