How to Change Table Structure in MySQL

How to change table structure in mysql - To change the structure of a table, the general form of its SQL command is as follows:
ALTER TABLE table_name alter_options;
Where :
  • ALTER TABLE is the basic command to change the table.
  • table_name is the name of the table to be changed its structure.
  • alter_options is a table change option. Useable options, some of which are as follows:
» ADD new_field_definitions
This option is used to add new fields with " new_field_definitions " (field names, types and other options).
» ADD INDEX index_name
This option is used to add an index with the name "index_name" in the table.
» ADD PRIMARY KEY (field_key)
Option to add a primary key to the table
» CHANGE field_changed new_field_definitions
Option to change field_changed to new_field_definitions
» MODIFY field_definition
The option to change a field into a field_definition
» DROP field_names
Option to delete field field_names
» RENAME TO new_table_name
Option to rename the table
Some examples of ALTER command variations to change the structure of a table include:
Previously read the first tutorial: How to Create Table in MySQL using CMD
1. Add a primary key to a table
ALTER TABLE student ADD PRIMARY KEY(studentnumber);
How to Change Table Structure in MySQL

2. Add the "religion" field to the student table
ALTER TABLE student ADD religion varchar(15) NOT NULL;
How to Change Table Structure in MySQL

3. Change the length of the religion field to 10 characters in the student table
ALTER TABLE student CHANGE religion religion varchar(10);
How to Change Table Structure in MySQL

4. Change the type of the religion field to char(2) in the student table
ALTER TABLE student MODIFY religion char(2) NOT NULL;
How to Change Table Structure in MySQL

5. Delete the religion field from the student table
ALTER TABLE student DROP religion;
How to Change Table Structure in MySQL

That is my explanation of how to change the table structure in mysql. Similarly tutorial from blog php programming language that we can give. Hopefully can add to your knowledge about mysql database.

Comments