sql alter table query

sql alter table query

You can modify the mysql table using ALTER TABLE statement, It is used to add, modify or delete columns in an existing table. It is also used to rename a table.
You can also use SQL ALTER TABLE command to add and drop various constraints on an existing table.

SQL ALTER TABLE Add Column

If you want to add columns in SQL table, the SQL alter table syntax is given below for that:

ALTER TABLE table_name ADD column_name column-definition; 
Example of SQL ALTER TABLE Add Column
If you want to add multiple columns in SQL table Employee, the SQL alter table example is given below, in this example we taking a employee table which is exist in database.

Simple SQL ALTER TABLE Add Column Query :
ALTER TABLE `Employee`
ADD (`name` VARCHAR(100) NOT NULL, `age` INT(3) NOT NULL);

SQL ALTER TABLE Add Column Query with PHP Script :

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());
mysql_query(“ALTER TABLE `Employee` ADD (`name` VARCHAR(100) NOT NULL, `age` INT(3) NOT NULL);”);
?>

SQL ALTER TABLE Modify Column

If you want to modify an existing column in SQL table, syntax is given below:

ALTER TABLE table_name MODIFY column_name column_type;  

If you want to modify multiple columns in Employee table, the example is given below
ALTER TABLE table_name.
Simple SQL ALTER TABLE Modify Column Query :

  
MODIFY (`name` VARCHAR(110) NOT NULL, `age` INT(4) NOT NULL,);

SQL ALTER TABLE Modify Column query with PHP Script :

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());
mysql_query(“ALTER TABLE `Employee` MODIFY (`name` VARCHAR(110) NOT NULL, `age` INT(4) NOT NULL)”);
?>

SQL ALTER TABLE RENAME Column

If you want to rename of an existing SQL table, Example is given below:
Simple SQL ALTER TABLE RENAME Column query:

RENAME TABLE `employee` TO `employee1`;

SQL ALTER TABLE RENAME Column with PHP Script

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());
mysql_query(“RENAME TABLE `employee` TO `employee1`”);
?>

SQL ALTER TABLE DROP Column

If you want to delete or drop a column from the exist SQL table, syntax is given below:

ALTER TABLE table_name DROP  column_name;

If you want to delet or drop a column from the employee SQL table, example is given below:
Simple SQL ALTER TABLE DROP Column query :

ALTER TABLE `employee` DROP `name`;

SQL ALTER TABLE DROP Column with PHP script :

<?php
$hostname = 'localhost';
$username = 'root';
$pass = '';
$conn = mysql_connect($hostname, $username, $pass) or die(mysql_error());
mysql_select_db('temp_db') or die(mysql_error());
mysql_query(“ALTER TABLE `employee` DROP `name`”);
?>
About Author

My name is Mukesh Jakhar and I am a Web Application Developer and Software Developer, currently living in Jaipur, India. I have a Master of Computer Application in Computer Science from JNU Jaipur University. I loves to write on technology and programming topics. Apart from this, I love to travel and enjoy the beauty of nature.

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.