{"id":3977,"date":"2025-06-07T08:40:43","date_gmt":"2025-06-07T08:40:43","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3977"},"modified":"2025-06-07T08:40:44","modified_gmt":"2025-06-07T08:40:44","slug":"sql-alter-table-add-drop-modify","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-alter-table-add-drop-modify\/","title":{"rendered":"SQL ALTER TABLE (Add, Drop, Modify)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>ALTER TABLE<\/code> lets you <strong>change an existing table\u2019s structure<\/strong> \u2014 add, drop, or modify columns and constraints without losing data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Basic Syntax<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Add a new column\nALTER TABLE table_name\nADD column_name datatype &#91;constraints];\n\n-- Drop a column\nALTER TABLE table_name\nDROP COLUMN column_name;\n\n-- Modify a column (syntax varies by DBMS)\nALTER TABLE table_name\nMODIFY column_name new_datatype &#91;constraints]; -- MySQL, Oracle\n\nALTER TABLE table_name\nALTER COLUMN column_name TYPE new_datatype; -- PostgreSQL, SQL Server<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Examples<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Add a column 'email' to 'employees'\nALTER TABLE employees\nADD email VARCHAR(100);\n\n-- Drop 'salary' column from 'employees'\nALTER TABLE employees\nDROP COLUMN salary;\n\n-- Modify 'email' column to be NOT NULL (MySQL)\nALTER TABLE employees\nMODIFY email VARCHAR(100) NOT NULL;<\/code><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udd39 Notes<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Syntax for modifying columns varies by DBMS \u2014 always check your DBMS docs.<\/li>\n\n\n\n<li>Some DBMS don\u2019t allow dropping columns easily (e.g., older versions of Oracle).<\/li>\n\n\n\n<li>Use <code>ALTER TABLE<\/code> carefully, as structure changes affect existing data.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\udde0 Quick Recap<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Operation<\/th><th>Syntax Example<\/th><\/tr><\/thead><tbody><tr><td>Add column<\/td><td><code>ALTER TABLE table ADD column datatype;<\/code><\/td><\/tr><tr><td>Drop column<\/td><td><code>ALTER TABLE table DROP COLUMN column;<\/code><\/td><\/tr><tr><td>Modify col<\/td><td><code>ALTER TABLE table MODIFY column datatype;<\/code> (MySQL, Oracle)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 <code>ALTER TABLE<\/code> is powerful \u2014 use it to adapt your table as requirements change.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ALTER TABLE lets you change an existing table\u2019s structure \u2014 add, drop, or modify columns and constraints without losing data. \ud83d\udd39 Basic Syntax \ud83d\udd39 Examples \ud83d\udd39 Notes \ud83e\udde0 Quick Recap Operation Syntax Example Add column ALTER TABLE table ADD column datatype; Drop column ALTER TABLE table DROP COLUMN column; Modify col ALTER TABLE table MODIFY [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3977","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}