Once we have created a table in our database we can use alter statement to modify it.
ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
Syntax :
ALTER TABLE table_name ADD (column name) data type;
--first lets drop the table Test (if she exists, if not skip the drop line).
Drop table test;
create table test (id int,name varchar(20),email varchar(20),salary int ,de char(20));
--insert values into table
insert into test values (1,'Eve','[email protected]',1500,'HR');
insert into test values (2,'Jon','[email protected]',2500,'AD');
insert into test values (3,'Mike','[email protected]',3000,'AD');
insert into test values (4,'Paul','[email protected]',3200,'HR');
insert into test values (5,'Mary','[email protected]',1800,'IT');
insert into test values (6,'Jane','[email protected]',2200,'IT');
SQL
select * from test ;
ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- -- --------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
ALTER table_name add (column name) data type;
SQL
alter table test add AGE int;
SQLselect * from test ;
ID NAME EMAIL SALARY DE AGE
---------- -------------------- -------------------- ---------- -------- ---------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
ALTER table_name drop column (column name);
SQLalter table test drop column age;
SQLselect * from test ; ID NAME EMAIL SALARY DE
---------- -------------------- -------------------- ---------- -- --------
1 Eve [email protected] 1500 HR
2 Jon [email protected] 2500 AD
3 Mike [email protected] 3000 AD
4 Paul [email protected] 3200 HR
5 Mary [email protected] 1800 IT
6 Jane [email protected] 2200 IT
ALTER TABLE table_name MODIFY column (column name) "new data type";
mysqldesc test;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| <a style="color: red;">de | char(20) | YES | | NULL | |</a><a>
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)
</a>
mysql
alter table test modify column de char(30);Query OK, 6 rows affected (0.27 sec)Records: 6 Duplicates: 0 Warnings: 0
mysqldesc test;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
|<a style="color: red;">de | char(30) | YES | | NULL | |</a>
+--------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
alter table "table name" change "old column name"
"new column name" "data type for the new column";
mysqlalter table test change name full_name varchar(20);
Query OK, 6 rows affected (0.16 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysqldesc test;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
|<b style="color: red;">full_name|varchar(20) | YES | | NULL | |</b>
| email | varchar(20) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| de | char(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
alter table "table name" add
index "index name"(column that will represent the index);
mysql
desc test;+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
|<b style="color: red;">id | int(11) | YES | | NULL | |</b>
| full_name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| de | char(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql
alter table test add index id_pk(id);
Query OK, 6 rows affected (0.18 sec)
Records: 6 Duplicates: 0 Warnings: 0
mysqldesc test;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
|<b style="color: red;">id | int(11) | YES | <b style="color: #000000;">MUL</b>| NULL | |</b>| full_name | varchar(20) | YES | | NULL | |
| email | varchar(20) | YES | | NULL | |
| salary | int(11) | YES | | NULL | |
| de | char(30) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
Show index from "table name";
mysql
SHOW INDEX FROM test;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
| test | 1 | id_pk | 1 | id | A | 2 | NULL | NULL | YES | BTREE | NULL |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+
1 row in set (0.00 sec)
alter table "table name" drop index "index name";