{"id":139,"date":"2018-12-06T09:04:48","date_gmt":"2018-12-06T09:04:48","guid":{"rendered":"https:\/\/barkhane.com\/sql\/2018\/12\/06\/sql-alter-table\/"},"modified":"2024-03-03T18:02:00","modified_gmt":"2024-03-03T18:02:00","slug":"sql-alter-table","status":"publish","type":"post","link":"https:\/\/barkhane.com\/sql\/sql-alter-table\/","title":{"rendered":"SQL ALTER TABLE"},"content":{"rendered":"<h2>SQL ALTER TABLE Statement<\/h2>\n<p>The ALTER TABLE statement is used to add, delete, or modify columns in an existing <a href=\"https:\/\/barkhane.com\/sql\/sql-table\/\">table<\/a>.<\/p>\n<p>The ALTER TABLE statement is also used to add and drop various constraints on an existing table.<\/p>\n<hr \/>\n<h2>ALTER TABLE &#8211; ADD Column<\/h2>\n<p>To add a column in a table, use the following syntax:<\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0<em>table_name<\/em>\nADD\u00a0<em>column_name datatype<\/em>;<\/pre>\n<p><\/code>\n<\/div>\n<p>The following SQL adds an \\&#8221;Email\\&#8221; column to the \\&#8221;Customers\\&#8221; table:<\/p>\n<div>\n<h3>Example<\/h3>\n<p><code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0Customers\nADD\u00a0Email varchar(255);<\/pre>\n<p><\/code>\n<\/div>\n<hr \/>\n<h2>ALTER TABLE &#8211; DROP COLUMN<\/h2>\n<p>To delete a column in a table, use the following syntax (notice that some database systems don\\&#8217;t allow deleting a column):<\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0<em>table_name<\/em>\nDROP\u00a0COLUMN\u00a0<em>column_name<\/em>;<\/pre>\n<p><\/code>\n<\/div>\n<p>The following SQL deletes the \\&#8221;Email\\&#8221; column from the \\&#8221;Customers\\&#8221; table:<\/p>\n<div>\n<h3>Example<\/h3>\n<p><code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0Customers\nDROP\u00a0COLUMN\u00a0Email;<\/pre>\n<p><\/code>\n<\/div>\n<hr \/>\n<h2>ALTER TABLE &#8211; ALTER\/MODIFY COLUMN<\/h2>\n<p>To change the data type of a column in a table, use the following syntax:<\/p>\n<p><strong>SQL Server \/ MS Access:<\/strong><\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0<em>table_name<\/em>\nALTER\u00a0COLUMN\u00a0<em>column_name datatype<\/em>;<\/pre>\n<p><\/code>\n<\/div>\n<p><strong>My SQL \/ Oracle (prior version 10G):<\/strong><\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0<em>table_name<\/em>\nMODIFY\u00a0COLUMN\u00a0<em>column_name datatype<\/em>;<\/pre>\n<p><\/code>\n<\/div>\n<p><strong>Oracle 10G and later:<\/strong><\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0<em>table_name<\/em>\nMODIFY\u00a0<em>column_name datatype<\/em>;<\/pre>\n<p><\/code>\n<\/div>\n<hr \/>\n<h2>SQL ALTER TABLE Example<\/h2>\n<p>Look at the \\&#8221;Persons\\&#8221; table:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<th>ID<\/th>\n<th>LastName<\/th>\n<th>FirstName<\/th>\n<th>Address<\/th>\n<th>City<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Hansen<\/td>\n<td>Ola<\/td>\n<td>Timoteivn 10<\/td>\n<td>Sandnes<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Svendson<\/td>\n<td>Tove<\/td>\n<td>Borgvn 23<\/td>\n<td>Sandnes<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Pettersen<\/td>\n<td>Kari<\/td>\n<td>Storgt 20<\/td>\n<td>Stavanger<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>Now we want to add a column named \\&#8221;DateOfBirth\\&#8221; in the \\&#8221;Persons\\&#8221; table.<\/p>\n<p>We use the following SQL statement:<\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0Persons\nADD\u00a0DateOfBirth date;<\/pre>\n<p><\/code>\n<\/div>\n<p>Notice that the new column, \\&#8221;DateOfBirth\\&#8221;, is of type date and is going to hold a date. The data type specifies what type of data the column can hold. For a complete reference of all the data types available in MS Access, MySQL, and SQL Server, go to our complete\u00a0Data Types reference.<\/p>\n<p>The \\&#8221;Persons\\&#8221; table will now look like this:<\/p>\n<div>\n<table>\n<tbody>\n<tr>\n<th>ID<\/th>\n<th>LastName<\/th>\n<th>FirstName<\/th>\n<th>Address<\/th>\n<th>City<\/th>\n<th>DateOfBirth<\/th>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>Hansen<\/td>\n<td>Ola<\/td>\n<td>Timoteivn 10<\/td>\n<td>Sandnes<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Svendson<\/td>\n<td>Tove<\/td>\n<td>Borgvn 23<\/td>\n<td>Sandnes<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>Pettersen<\/td>\n<td>Kari<\/td>\n<td>Storgt 20<\/td>\n<td>Stavanger<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<hr \/>\n<h2>Change Data Type Example<\/h2>\n<p>Now we want to change the data type of the column named \\&#8221;DateOfBirth\\&#8221; in the \\&#8221;Persons\\&#8221; table.<\/p>\n<p>We use the following SQL statement:<\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0Persons\nALTER\u00a0COLUMN\u00a0DateOfBirth year;<\/pre>\n<p><\/code>\n<\/div>\n<p>Notice that the \\&#8221;DateOfBirth\\&#8221; column is now of type year and is going to hold a year in a two- or four-digit format.<\/p>\n<hr \/>\n<h2>DROP COLUMN Example<\/h2>\n<p>Next, we want to delete the column named \\&#8221;DateOfBirth\\&#8221; in the \\&#8221;Persons\\&#8221; table.<\/p>\n<p>We use the following SQL statement:<\/p>\n<div>\n<code><\/p>\n<pre>ALTER\u00a0TABLE\u00a0Persons\nDROP\u00a0COLUMN\u00a0DateOfBirth;<\/pre>\n<p><\/code>\n<\/div>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SQL ALTER TABLE Statement The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[2],"tags":[],"class_list":["post-139","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/139","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/comments?post=139"}],"version-history":[{"count":0,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/posts\/139\/revisions"}],"wp:attachment":[{"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/media?parent=139"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/categories?post=139"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/barkhane.com\/sql\/wp-json\/wp\/v2\/tags?post=139"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}