Vous êtes sur la page 1sur 24


Table structure can be changed by using alter command. With this command Field type
or property can be changed or a new field can be added. This sql alter table command is
used like create table command

Please not that while changing the structure we must honor the existing constraints of the
table. For example if you decide to change a field to UNIQUE so it will not accept any
duplicate records then if some records with duplicate value are already there then system
will not allow this and we will get error message.
Same way we have to take care of other constraints

Here is the existing table structure.

Field Type Attributes Null Default Extra

id int(2) No auto_increment
name varchar(50) No
class varchar(10) No
mark int(3) No 0

We will apply our alter table command to this table. We will change field name mark to
student_mark Here is the command

ALTER TABLE `student3` CHANGE `mark` `student_mark` INT( 3 ) DEFAULT '0'


With this alter table command the the field name mark will change to student_mark. This
way we are changing a field name only. Same way field type, default value and other
properties of the field can be changed. The new table structure is listed below.

Field Type Attributes Null Default Extra

id int(2) No auto_increment
name varchar(50) No
class varchar(10) No
student_mark int(3) No 0

Adding / Altering a numeric field to auto increment field

You can read purpose and how to create auto increment field here. We can convert one
existing numeric field to an auto increment field in two steps. First making the field
unique and then altering the field to add auto increment property to it. We will start with
making it unique

Declaring an numeric field to be unique

$q="ALTER TABLE `message_table` ADD UNIQUE (`msg_id`)";

Here in the message_table we already have one numeric field msg_id and we have made
it to UNIQUE field so no duplicate data is allowed, if any duplicate data is there then we
will receive error message.

Now let us change it to add auto increment property to it.

$q="ALTER TABLE `message_table` CHANGE `msg_id` `msg_id` INT( 4 ) NOT


Now our msg_id field became auto increment.

SQL ( Structured Query Language ) is one of the important tool for the programmers.
Sql is a part of the database management tool. There are many databases available from
different venders. For web applications MSSQL, MySQL, MS Access are mostly used.
Oracle is very popular among enterprise users. MySQL can be downloaded from
mysql.net and it is free. Combination of PHP and MySQL is the most popular in the
world today among all the web applications.

What is SQL
Sql is a common language to manage all the operations for a database. There are different
SQL commands available to manage the database. You need not start reading or take
training on all the commands at a time, you can use this site as a referance and read all
our free tutorials on SQL. Learn SQL step by step from here and we will be adding more
resources for you. All our tutorials are free and you can publish our contents in your after
taking our permission. Please use the contact us button of the left menu to write your
comments and suggestions.

Now let us start with simple query to display

all the records of a table. We will create a student table and fill with some data. Don't
worry on how to create the table. Use this SQL to create the table.
CREATE TABLE student ( id int(2) NOT NULL auto_increment, name varchar(50)
NOT NULL default '', class varchar(10) NOT NULL default '', mark int(3) NOT NULL
default '0', UNIQUE KEY id (id) ) TYPE=MyISAM;

This will create the table for us. We can see that there are three fields in the table. One is
id which is an auto increment and will take one unique number each time one record is
added to the table. Next field is name of 50 length, this will store the student name. Next
is class field of 10 length. Let us fill the tables with some data. Here is the SQL to insert
the data to the table. We need not worry at this stage how this SQL works as we will
discuss that in other pages. You can just copy and paste this SQL code in your SQL
window to insert some records to the tables.

INSERT INTO student VALUES (1, 'John Deo', 'Four', 75);

INSERT INTO student VALUES (2, 'Max Ruin', 'Three', 85);
INSERT INTO student VALUES (3, 'Arnold', 'Three', 55);
INSERT INTO student VALUES (4, 'Krish Star', 'Four', 60);
INSERT INTO student VALUES (5, 'John Mike', 'Four', 60);
INSERT INTO student VALUES (6, 'Alex John', 'Five', 55);

This SQL code will insert six records to the table. Now our table with data is ready. We
will start learning one SQL command today. Let us start with SELECT query.
tini haris
havid dias
tito ita
tito ita
tito ita
tini haris
havid dias