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`”);
?>

Leave a Reply

Related Posts: You may like

comma separated values in mysql query

SQL statement to retrieve names beginning with A

Php pdo prepared statements select insert update queries

php pdo queries statements select insert update delete

Tools:

Crypto Currency

PHPKIDA provides free online the best real-time crypto market price, news, predictions, index with graphs & historical data for 2000+ coins including bitcoin, ethereum, & altcoins from major cryptocurrency exchanges.

Visite Tool

Icon Generator

PHPKIDA provides free online icon generator. Our icon generator help you to edit, modify, resize and customize icon for your website and applications. Convert to a shap you like add icon border, you can change icon border color, border size, background color, icon shadow, icons color, icon size in your own way and save it as a png image.

Visite Tool

Youtube Video Downloader

PHPKIDA provides Free online YouTube video downloader to download YouTube videos quickly in MP4, 3GP, and more. Search your video and free fast download youtube videos.

Visite Tool

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.