{"id":130,"date":"2023-07-22T12:08:40","date_gmt":"2023-07-22T12:08:40","guid":{"rendered":"http:\/\/local.tutorials\/?post_type=topic&p=130"},"modified":"2023-07-22T12:08:43","modified_gmt":"2023-07-22T12:08:43","slug":"mysql-alter-table","status":"publish","type":"topic","link":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/","title":{"rendered":"MySQL\u00a0– ALTER TABLE"},"content":{"rendered":"\n

The ALTER TABLE<\/code> statement in MySQL is used to modify an existing table’s structure. It allows you to add, modify, or delete columns, change data types, add constraints, rename the table, and more. Here are some examples of how to use the ALTER TABLE<\/code> statement:<\/p>\n\n\n\n

Adding a New Column:<\/strong><\/p>\n\n\n\n

To add a new column to an existing table named “employees,” you can use the ADD<\/code> keyword along with the new column definition. For example, let’s add a “phone” column of type VARCHAR(20):<\/p>\n\n\n\n

ALTER TABLE employees\nADD phone VARCHAR(20);<\/code><\/pre>\n\n\n\n

Modifying an Existing Column<\/strong>:<\/p>\n\n\n\n

To modify an existing column, you can use the MODIFY<\/code> keyword along with the new column definition. Let’s modify the “phone” column’s data type to VARCHAR(15):<\/p>\n\n\n\n

ALTER TABLE employees\nMODIFY phone VARCHAR(15);<\/code><\/pre>\n\n\n\n

Changing a Column Name:<\/strong><\/p>\n\n\n\n

To change the name of an existing column, you can use the CHANGE<\/code> keyword. Let’s change the column name “phone” to “contact_number”:<\/p>\n\n\n\n

ALTER TABLE employees\nCHANGE COLUMN phone contact_number VARCHAR(15);<\/code><\/pre>\n\n\n\n

Deleting a Column:<\/strong><\/p>\n\n\n\n

To delete an existing column, you can use the DROP<\/code> keyword. Let’s remove the “contact_number” column from the “employees” table:<\/p>\n\n\n\n

ALTER TABLE employees\nDROP COLUMN contact_number;<\/code><\/pre>\n\n\n\n

Adding a Primary Key:<\/strong><\/p>\n\n\n\n

To add a primary key constraint to an existing table, you can use the ADD<\/code> keyword along with the PRIMARY KEY<\/code> constraint. Let’s add a primary key on the “id” column:<\/p>\n\n\n\n

ALTER TABLE employees\nADD PRIMARY KEY (id);<\/code><\/pre>\n\n\n\n

Adding a Foreign Key<\/strong>:<\/p>\n\n\n\n

To add a foreign key constraint to an existing table, you can use the ADD<\/code> keyword along with the FOREIGN KEY<\/code> constraint. Let’s add a foreign key on the “department_id” column referencing the “id” column of the “departments” table:<\/p>\n\n\n\n

ALTER TABLE employees\nADD FOREIGN KEY (department_id) REFERENCES departments(id);<\/code><\/pre>\n\n\n\n

These are just a few examples of how you can use the ALTER TABLE<\/code> statement to modify an existing MySQL table. Always exercise caution when making changes to your database schema, especially in production environments. Ensure that you have appropriate privileges to alter the table and that your changes are consistent with the application’s data requirements.<\/p>\n","protected":false},"excerpt":{"rendered":"

The ALTER TABLE statement in MySQL is used to modify an existing table’s structure. It allows you to add, modify, or delete columns, change data types, add constraints, rename the table, and more. Here are some examples of how to use the ALTER TABLE statement: Adding a New Column: To add a new column to […]<\/p>\n","protected":false},"featured_media":45,"comment_status":"open","ping_status":"closed","template":"","meta":[],"categories":[3],"tags":[],"subject":[16,17],"yoast_head":"\nMySQL ALTER TABLE Statement<\/title>\n<meta name=\"description\" content=\"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL ALTER TABLE Statement\" \/>\n<meta property=\"og:description\" content=\"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com Tutorials and Courses\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-22T12:08:43+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.tutorials\/wp-content\/uploads\/2022\/03\/tutorial-mysql.png\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"360\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/\",\"url\":\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/\",\"name\":\"MySQL ALTER TABLE Statement\",\"isPartOf\":{\"@id\":\"http:\/\/local.tutorials\/#website\"},\"datePublished\":\"2023-07-22T12:08:40+00:00\",\"dateModified\":\"2023-07-22T12:08:43+00:00\",\"description\":\"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways\",\"breadcrumb\":{\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.tutorials\/topic\/mysql-alter-table\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.tutorials\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Topics\",\"item\":\"http:\/\/local.tutorials\/topic\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL\u00a0– ALTER TABLE\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.tutorials\/#website\",\"url\":\"http:\/\/local.tutorials\/\",\"name\":\"Brightwhiz.com Tutorials and Courses\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/local.tutorials\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.tutorials\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.tutorials\/#organization\",\"name\":\"Brightwhiz.com\",\"url\":\"http:\/\/local.tutorials\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.tutorials\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz.com\"},\"image\":{\"@id\":\"http:\/\/local.tutorials\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\",\"https:\/\/twitter.com\/brightwhizmag\",\"https:\/\/www.instagram.com\/glamorglaze\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/c\/Sobbayi\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"MySQL ALTER TABLE Statement","description":"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways","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":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/","og_locale":"en_US","og_type":"article","og_title":"MySQL ALTER TABLE Statement","og_description":"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways","og_url":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/","og_site_name":"Brightwhiz.com Tutorials and Courses","article_publisher":"https:\/\/www.facebook.com\/brightwhiz","article_modified_time":"2023-07-22T12:08:43+00:00","og_image":[{"width":640,"height":360,"url":"http:\/\/local.tutorials\/wp-content\/uploads\/2022\/03\/tutorial-mysql.png","type":"image\/png"}],"twitter_card":"summary_large_image","twitter_site":"@brightwhizmag","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/","url":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/","name":"MySQL ALTER TABLE Statement","isPartOf":{"@id":"http:\/\/local.tutorials\/#website"},"datePublished":"2023-07-22T12:08:40+00:00","dateModified":"2023-07-22T12:08:43+00:00","description":"The ALTER TABLE statement in MySQL is used to modify an existing table's structure in various ways","breadcrumb":{"@id":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.tutorials\/topic\/mysql-alter-table\/"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/local.tutorials\/topic\/mysql-alter-table\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.tutorials\/"},{"@type":"ListItem","position":2,"name":"Topics","item":"http:\/\/local.tutorials\/topic\/"},{"@type":"ListItem","position":3,"name":"MySQL\u00a0– ALTER TABLE"}]},{"@type":"WebSite","@id":"http:\/\/local.tutorials\/#website","url":"http:\/\/local.tutorials\/","name":"Brightwhiz.com Tutorials and Courses","description":"","publisher":{"@id":"http:\/\/local.tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.tutorials\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.tutorials\/#organization","name":"Brightwhiz.com","url":"http:\/\/local.tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png","contentUrl":"https:\/\/staging.brightwhiz.com\/wp-content\/uploads\/2022\/03\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz.com"},"image":{"@id":"http:\/\/local.tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz","https:\/\/twitter.com\/brightwhizmag","https:\/\/www.instagram.com\/glamorglaze\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/c\/Sobbayi"]}]}},"_links":{"self":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/topic\/130"}],"collection":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/types\/topic"}],"replies":[{"embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/comments?post=130"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/media\/45"}],"wp:attachment":[{"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/media?parent=130"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/categories?post=130"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/tags?post=130"},{"taxonomy":"subject","embeddable":true,"href":"http:\/\/local.tutorials\/wp-json\/wp\/v2\/subject?post=130"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}