0% found this document useful (0 votes)
34 views13 pages

MySQL Alter

The ALTER TABLE command is used to change the structure of an existing table. It can add or delete columns, create or drop indexes, modify column types or sizes, rename columns or the entire table. The document provides examples of using ALTER TABLE to rename a table, add a column, drop a column, modify a column's data type, and rename a column.

Uploaded by

NandaShivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views13 pages

MySQL Alter

The ALTER TABLE command is used to change the structure of an existing table. It can add or delete columns, create or drop indexes, modify column types or sizes, rename columns or the entire table. The document provides examples of using ALTER TABLE to rename a table, add a column, drop a column, modify a column's data type, and rename a column.

Uploaded by

NandaShivani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

MySQL Alter

Command
Alter Command
The SQL ALTER TABLE command is used
to change the structure of an existing
table.
It helps to add or delete columns, create
or destroy indexes, change the type of
existing columns, or rename columns or
the table itself.
It can also be used to change the
comment for the table and type of the
table.
Alter Command - Syntax
alter command is used for alteration of
table structures and various uses
of alter command are
to add a column to existing table
to rename any existing column
to change data type of any column or
to modify its size
alter is also used to drop a column
Alter Clause Query Renaming a Table
show tables;

Write a SQL statement to rename the table tutorial


to tutorial1

ALTER table tutorial


RENAME tutorial1
Alter Clause Query Adding a Column to an
Existing Table
Select * from tutorial;

Show columns
from tutorial;
Alter Clause Query Adding a Column to an Existing
Table
Write a SQL statement to add a column tutReview
after tutorial_title to the table tutorial
ALTER TABLE tutorial
ADD tutReview VARCHAR(30) AFTER tutorial_title
Alter Clause Query Drop a Column from an Existing
Table
Write a SQL statement to drop the column
tutReview from the table tutorial
ALTER TABLE tutorial
DROP tutReview
Alter Clause Query Modify the Data Type of a
Column from an Existing Table
Write a SQL statement to change the data type of
the column tutorial_id to varchar in the table
tutorial
ALTER TABLE tutorial
MODIFY tutorial_id VARCHAR(4)
<?php
$db="TUTORIALS";
$link = mysqli_connect('localhost', 'root', '');
if (! $link)
die(mysqli_error($link));
mysqli_select_db( $link,$db) or die("Select Error:
".mysqli_error($link));

$result=mysqli_query($link,"ALTER TABLE tutorial


RENAME tutorial1
") or die("Alter Error: ".mysqli_error($link));
mysqli_close($link);
print "Table tutorial renamed to tutorial1";
?>
<?php
$db="TUTORIALS";
$link = mysqli_connect('localhost', 'root', '');
if (! $link)
die(mysqli_error($link));
mysqli_select_db( $link,$db) or die("Select Error:
".mysqli_error($link));
$result=mysqli_query($link,"ALTER TABLE tutorial
ADD tutReview VARCHAR(30) AFTER tutorial_title
") or die("Alter Error: ".mysqli_error($link));
mysqli_close($link);
print "Column tutReview has been added after tutorial_title";
?>
<?php
$db="TUTORIALS";
$link = mysqli_connect('localhost', 'root', '');
if (! $link)
die(mysqli_error($link));
mysqli_select_db( $link,$db) or die("Select Error:
".mysqli_error($link));
$result=mysqli_query($link,"ALTER TABLE tutorial
DROP tutReview
") or die("Alter Error: ".mysqli_error($link));
mysqli_close($link);
print "Column tutReview has been dropped from tutorial";
?>
<?php
$db="TUTORIALS";
$link = mysqli_connect('localhost', 'root', '');
if (! $link)
die(mysqli_error($link));
mysqli_select_db( $link,$db) or die("Select Error:
".mysqli_error($link));
$result=mysqli_query($link,"ALTER TABLE tutorial
MODIFY tutorial_id VARCHAR(4)
") or die("Alter Error: ".mysqli_error($link));
mysqli_close($link);
print "Column tutorial_id data type has been MODIFIED to
VARCHAR(4)";
?>
<?php
$db="TUTORIALS";
$link = mysqli_connect('localhost', 'root', '');
if (! $link)
die(mysqli_error($link));
mysqli_select_db( $link,$db) or die("Select Error:
".mysqli_error($link));
$result=mysqli_query($link,"ALTER TABLE tutorial
DROP tutorial_author,
ADD tut_author varchar(25)
AFTER tutorial_title;
") or die("Alter Error: ".mysqli_error($link));
mysqli_close($link);
print "Column tutorial_author has been RENAMED to tut_author";
?>

You might also like