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

PHPKIDA Offering Free Tools:

INSTA DOWNLOADER

Download Instagram Videos & Photos.

Download Now

CURRENCY CONVERTER

Real Time Currency Converter

Convert Now

ROBOST.TXT GEN.

Robots.txt File Creater

Create Now

HTML MINIFIER

Compress yout HTML Code

Minify Now

CSS MINIFIER

Compress yout CSS Code

Minify Now

JAVASCRIPT MINIFIER

Compress yout JavaScript Code

Minify Now

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.