{"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 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 Modifying an Existing Column<\/strong>:<\/p>\n\n\n\n To modify an existing column, you can use the Changing a Column Name:<\/strong><\/p>\n\n\n\n To change the name of an existing column, you can use the Deleting a Column:<\/strong><\/p>\n\n\n\n To delete an existing column, you can use the Adding a Primary Key:<\/strong><\/p>\n\n\n\n To add a primary key constraint to an existing table, you can use the Adding a Foreign Key<\/strong>:<\/p>\n\n\n\n To add a foreign key constraint to an existing table, you can use the These are just a few examples of how you can use the 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":"\nALTER 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
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
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
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
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
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
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
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":"