{"id":495,"date":"2016-04-07T05:04:00","date_gmt":"2016-04-07T05:04:00","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=495"},"modified":"2025-02-08T01:22:51","modified_gmt":"2025-02-08T08:22:51","slug":"sql-alter-table","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-alter-table\/","title":{"rendered":"SQL ALTER TABLE Statement"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use the SQL <code>ALTER TABLE<\/code> statement to change the structure of an existing table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-alter-table-statement'>Introduction to SQL ALTER TABLE statement <a href=\"#introduction-to-sql-alter-table-statement\" class=\"anchor\" id=\"introduction-to-sql-alter-table-statement\" title=\"Anchor for Introduction to SQL ALTER TABLE statement\">#<\/a><\/h2>\n\n\n\n<p>In SQL, the <code>ALTER TABLE<\/code> statement changes the structure of an existing table.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code>ALTER TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">action<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, provide the name of the table you want to change in the <code>ALTER TABLE<\/code> clause.<\/li>\n\n\n\n<li>Second, specify an <code>action<\/code> you want to perform.<\/li>\n<\/ul>\n\n\n\n<p>SQL allows you to perform the following actions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Adding a Column<\/li>\n\n\n\n<li>Dropping a Column<\/li>\n\n\n\n<li>Modifying a Column<\/li>\n\n\n\n<li>Renaming a Column<\/li>\n\n\n\n<li>Adding a Constraint<\/li>\n\n\n\n<li>Dropping a Constraint<\/li>\n\n\n\n<li>Renaming the Table<\/li>\n\n\n\n<li>Adding a Primary Key<\/li>\n\n\n\n<li>Dropping a Primary Key<\/li>\n\n\n\n<li>Adding a Foreign Key<\/li>\n\n\n\n<li>Dropping a Foreign Key<\/li>\n<\/ul>\n\n\n\n<p>We&#8217;ll <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">create a table<\/a> called <code>travel_requests<\/code> for practicing with the <code>ALTER TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests (\n  request_id <span class=\"hljs-built_in\">INT<\/span>,\n  employee_id <span class=\"hljs-built_in\">INT<\/span>,\n  destination <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  end_date <span class=\"hljs-built_in\">DATE<\/span>,\n  purpose <span class=\"hljs-built_in\">TEXT<\/span>,\n  <span class=\"hljs-keyword\">status<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">20<\/span>),\n  note <span class=\"hljs-built_in\">TEXT<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIHRyYXZlbF9yZXF1ZXN0cyAoIHJlcXVlc3RfaWQgSU5ULCBlbXBsb3llZV9pZCBJTlQsIGRlc3RpbmF0aW9uIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCwgZW5kX2RhdGUgREFURSwgcHVycG9zZSBURVhULCBzdGF0dXMgVkFSQ0hBUigyMCksIG5vdGUgVEVYVCApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-a-column'>Adding a column <a href=\"#adding-a-column\" class=\"anchor\" id=\"adding-a-column\" title=\"Anchor for Adding a column\">#<\/a><\/h2>\n\n\n\n<p>To <a href=\"https:\/\/www.sqltutorial.org\/sql-add-column\/\">add a new column to a table<\/a>, you use the <code>ADD COLUMN<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> colum_name datatype <span class=\"hljs-keyword\">constraint<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement adds the <code>start_date<\/code> column to the <code>travel_requests<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> start_date <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIEFERCBDT0xVTU4gc3RhcnRfZGF0ZSBEQVRFIE5PVCBOVUxMOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='dropping-a-column'>Dropping a column <a href=\"#dropping-a-column\" class=\"anchor\" id=\"dropping-a-column\" title=\"Anchor for Dropping a column\">#<\/a><\/h2>\n\n\n\n<p>To <a href=\"https:\/\/www.sqltutorial.org\/sql-drop-column\/\">remove a column from a table<\/a>, you use the <code>DROP COLUMN<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> column_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following query drops the <code>note<\/code> column from the <code>travel_requests<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> note;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIERST1AgQ09MVU1OIG5vdGU7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='modifying-a-column'>Modifying a column <a href=\"#modifying-a-column\" class=\"anchor\" id=\"modifying-a-column\" title=\"Anchor for Modifying a column\">#<\/a><\/h2>\n\n\n\n<p>To change the attribute of an existing column, you use the <code>ALTER COLUMN<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> column_name <span class=\"hljs-keyword\">SET<\/span> datatype;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement changes the data type of the <code>purpose<\/code> column to <code>VARCHAR(255)<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> purpose\n<span class=\"hljs-keyword\">SET<\/span> <span class=\"hljs-keyword\">DATA<\/span> <span class=\"hljs-keyword\">TYPE<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIEFMVEVSIENPTFVNTiBwdXJwb3NlIFNFVCBEQVRBIFRZUEUgVkFSQ0hBUigyNTUpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='renaming-a-column'>Renaming a column <a href=\"#renaming-a-column\" class=\"anchor\" id=\"renaming-a-column\" title=\"Anchor for Renaming a column\">#<\/a><\/h2>\n\n\n\n<p>To rename a column, you use the <code>RENAME COLUMN<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">RENAME<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> column_name <span class=\"hljs-keyword\">TO<\/span> new_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement changes the column <code>status<\/code> to <code>approval_status<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">RENAME<\/span> <span class=\"hljs-keyword\">COLUMN<\/span> <span class=\"hljs-keyword\">status<\/span> <span class=\"hljs-keyword\">TO<\/span> approval_status;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIFJFTkFNRSBDT0xVTU4gc3RhdHVzIFRPIGFwcHJvdmFsX3N0YXR1czs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-a-constraint'>Adding a constraint <a href=\"#adding-a-constraint\" class=\"anchor\" id=\"adding-a-constraint\" title=\"Anchor for Adding a constraint\">#<\/a><\/h2>\n\n\n\n<p>To add a new constraint to a table, you use the <code>ADD CONSTRAINT<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> constraint_name <span class=\"hljs-keyword\">constraint<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement adds a <code>CHECK<\/code> constraint to the <code>travel_requests<\/code> table to ensure that the end date is on or after the start date:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> check_dates \n<span class=\"hljs-keyword\">CHECK<\/span>(end_date &gt;= start_date);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIEFERCBDT05TVFJBSU5UIGNoZWNrX2RhdGVzIENIRUNLKGVuZF9kYXRlID49IHN0YXJ0X2RhdGUpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='dropping-a-constraint'>Dropping a constraint <a href=\"#dropping-a-constraint\" class=\"anchor\" id=\"dropping-a-constraint\" title=\"Anchor for Dropping a constraint\">#<\/a><\/h2>\n\n\n\n<p>To delete a constraint from a table, you use the <code>DROP CONSTRAINT<\/code>&nbsp;clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> constraint_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement drops the constraint <code>check_dates<\/code> from the <code>travel_requests<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> check_dates;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIERST1AgQ09OU1RSQUlOVCBjaGVja19kYXRlczs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-a-primary-key'>Adding a primary key <a href=\"#adding-a-primary-key\" class=\"anchor\" id=\"adding-a-primary-key\" title=\"Anchor for Adding a primary key\">#<\/a><\/h2>\n\n\n\n<p>If a table does not have a <a href=\"https:\/\/www.sqltutorial.org\/sql-primary-key\/\">primary key<\/a>, you can add one using the <code>ADD PRIMARY KEY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (column_name);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement makes the <code>request_id<\/code> as the primary key of the <code>requests<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">ADD<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span> (request_id);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIEFERCBQUklNQVJZIEtFWSAocmVxdWVzdF9pZCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='dropping-a-primary-key'>Dropping a primary key <a href=\"#dropping-a-primary-key\" class=\"anchor\" id=\"dropping-a-primary-key\" title=\"Anchor for Dropping a primary key\">#<\/a><\/h2>\n\n\n\n<p>To drop a primary key from a table, you use the <code>DROP PRIMARY KEY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE table_name\nDROP PRIMARY KEY;<\/code><\/span><\/pre>\n\n\n<p>The following example removes the primary key from the <code>travel_requests<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE travel_requests\nDROP PRIMARY KEY;<\/code><\/span><\/pre>\n\n\n<p>Note that the Playground does not support this syntax. Instead, you need to specify the primary key name explicitly in the <code>DROP CONSTRAINT<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE travel_requests\nDROP CONSTRAINT travel_requests_pkey;<\/code><\/span><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgdHJhdmVsX3JlcXVlc3RzIERST1AgQ09OU1RSQUlOVCB0cmF2ZWxfcmVxdWVzdHNfcGtleTs%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-a-foreign-key'>Adding a foreign key <a href=\"#adding-a-foreign-key\" class=\"anchor\" id=\"adding-a-foreign-key\" title=\"Anchor for Adding a foreign key\">#<\/a><\/h2>\n\n\n\n<p>To add a <a href=\"https:\/\/www.sqltutorial.org\/sql-foreign-key\/\">foreign key constraint<\/a> to a table, you use the <code>ADD FOREGIN KEY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table1\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> constraint_name\n<span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (column1) <span class=\"hljs-keyword\">REFERENCES<\/span> table2(column2);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement adds a foreign key constraint that includes the <code>employee_id<\/code> column as a foreign key column and references to the <code>employee_id<\/code> column of the <code>employees<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> fk_employee \n<span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (employee_id) \n<span class=\"hljs-keyword\">REFERENCES<\/span> employees (employee_id);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=QUxURVIgVEFCTEUgcmVxdWVzdHMgQUREIENPTlNUUkFJTlQgZmtfZW1wbG95ZWUgRk9SRUlHTiBLRVkgKGVtcGxveWVlX2lkKSBSRUZFUkVOQ0VTIGVtcGxveWVlcyAoZW1wbG95ZWVfaWQpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='dropping-a-foreign-key'>Dropping a foreign key <a href=\"#dropping-a-foreign-key\" class=\"anchor\" id=\"dropping-a-foreign-key\" title=\"Anchor for Dropping a foreign key\">#<\/a><\/h2>\n\n\n\n<p>To delete a foreign key, you use the <code>DROP FOREIGN KEY<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE table_name\nDROP CONSTRAINT fk_name;<\/code><\/span><\/pre>\n\n\n<p>For example, the following statement removes the foreign key constraint <code>fk_employee<\/code> from the travel_requests table:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">ALTER TABLE travel_requests\nDROP CONSTRAINT fk_employee;<\/code><\/span><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='renaming-a-table'>Renaming a table <a href=\"#renaming-a-table\" class=\"anchor\" id=\"renaming-a-table\" title=\"Anchor for Renaming a table\">#<\/a><\/h2>\n\n\n\n<p>To change the name of a table, you use the <code>RENAME TO<\/code> clause:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">RENAME<\/span> <span class=\"hljs-keyword\">TO<\/span> new_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement changes the <code>travel_requests<\/code> table to <code>requests<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> travel_requests\n<span class=\"hljs-keyword\">RENAME<\/span> <span class=\"hljs-keyword\">TO<\/span> requests;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the <code>ALTER TABLE<\/code> statement to modify the structure of an existing table.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=alter-table\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"495\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\"\n\t\t\t\tdata-post-title=\"SQL ALTER TABLE Statement\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"495\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\"\n\t\t\t\tdata-post-title=\"SQL ALTER TABLE Statement\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the SQL ALTER TABLE to change the structure of existing tables in the database.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":46,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-495","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL ALTER TABLE Statement<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL ALTER TABLE Statement\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T08:22:51+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\",\"name\":\"SQL ALTER TABLE Statement\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"datePublished\":\"2016-04-07T05:04:00+00:00\",\"dateModified\":\"2025-02-08T08:22:51+00:00\",\"description\":\"In this tutorial, you'll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alter-table\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-alter-table\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-alter-table\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL ALTER TABLE Statement\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL ALTER TABLE Statement","description":"In this tutorial, you'll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqltutorial.org\/sql-alter-table\/","og_locale":"en_US","og_type":"article","og_title":"SQL ALTER TABLE Statement","og_description":"In this tutorial, you'll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.","og_url":"https:\/\/www.sqltutorial.org\/sql-alter-table\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T08:22:51+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqltutorial.org\/sql-alter-table\/","url":"https:\/\/www.sqltutorial.org\/sql-alter-table\/","name":"SQL ALTER TABLE Statement","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"datePublished":"2016-04-07T05:04:00+00:00","dateModified":"2025-02-08T08:22:51+00:00","description":"In this tutorial, you'll learn how to use the SQL ALTER TABLE statement to change the structure of an existing table.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-alter-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-alter-table\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-alter-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL ALTER TABLE Statement"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/495\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}