Monday 25 July 2016

ALTER command



ALTER command is used to add/delete columns to given table.

I am going to use following sample data.

CREATE TABLE employee(
  id int PRIMARY KEY,
  name VARCHAR(25),
  age int,
  salary DECIMAL(10, 4),
  mailId VARCHAR(25),
  city VARCHAR(10)
);

INSERT INTO employee VALUES (1, "Hari", 28, 12345.67, "hari@hari.com", "Bangalore");
INSERT INTO employee VALUES (2, "Sandesh", 31, 98345.00, "sandesh@sandesh.com", "Trivendram");
INSERT INTO employee VALUES (3, "Phalgum", 33, 119345.67, "phalgun@hari.com", "Hyderabad");
INSERT INTO employee VALUES (4, "Manju", 36, 87666.87, "manju@sandesh.com", "Bangalore");
INSERT INTO employee VALUES (5, "Rakesh", 26, 38000, "rakesh@hari.com", "Bangalore");
INSERT INTO employee VALUES (6, "Sankalp", 38, 87645.67, "sankalp@sankalp.com", "Chenai");
INSERT INTO employee VALUES (7, "Vadiraj", 40, 12345.67, "vadi@hari.com", "Bangalore");
INSERT INTO employee VALUES (8, "Prasob", 37, 12345.67, "prasob@sandesh.com", "Trivendram");
INSERT INTO employee (id, name, salary) VALUES (9, "Kesav", 123457.89);

mysql> SELECT * FROM employee;
+----+---------+------+-------------+---------------------+------------+
| id | name    | age  | salary      | mailId              | city       |
+----+---------+------+-------------+---------------------+------------+
|  1 | Hari    |   28 |  12345.6700 | hari@hari.com       | Bangalore  |
|  2 | Sandesh |   31 |  98345.0000 | sandesh@sandesh.com | Trivendram |
|  3 | Phalgum |   33 | 119345.6700 | phalgun@hari.com    | Hyderabad  |
|  4 | Manju   |   36 |  87666.8700 | manju@sandesh.com   | Bangalore  |
|  5 | Rakesh  |   26 |  38000.0000 | rakesh@hari.com     | Bangalore  |
|  6 | Sankalp |   38 |  87645.6700 | sankalp@sankalp.com | Chenai     |
|  7 | Vadiraj |   40 |  12345.6700 | vadi@hari.com       | Bangalore  |
|  8 | Prasob  |   37 |  12345.6700 | prasob@sandesh.com  | Trivendram |
|  9 | Kesav   | NULL | 123457.8900 | NULL                | NULL       |
+----+---------+------+-------------+---------------------+------------+
9 rows in set (0.00 sec)



How to add column to table?
ALTER TABLE table_name ADD col1 data_type;

mysql> ALTER TABLE employee ADD pin VARCHAR(25);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0


mysql> SELECT * FROM employee;
+----+---------+------+-------------+---------------------+------------+------+
| id | name    | age  | salary      | mailId              | city       | pin  |
+----+---------+------+-------------+---------------------+------------+------+
|  1 | Hari    |   28 |  12345.6700 | hari@hari.com       | Bangalore  | NULL |
|  2 | Sandesh |   31 |  98345.0000 | sandesh@sandesh.com | Trivendram | NULL |
|  3 | Phalgum |   33 | 119345.6700 | phalgun@hari.com    | Hyderabad  | NULL |
|  4 | Manju   |   36 |  87666.8700 | manju@sandesh.com   | Bangalore  | NULL |
|  5 | Rakesh  |   26 |  38000.0000 | rakesh@hari.com     | Bangalore  | NULL |
|  6 | Sankalp |   38 |  87645.6700 | sankalp@sankalp.com | Chenai     | NULL |
|  7 | Vadiraj |   40 |  12345.6700 | vadi@hari.com       | Bangalore  | NULL |
|  8 | Prasob  |   37 |  12345.6700 | prasob@sandesh.com  | Trivendram | NULL |
|  9 | Kesav   | NULL | 123457.8900 | NULL                | NULL       | NULL |
+----+---------+------+-------------+---------------------+------------+------+
9 rows in set (0.00 sec)



How to remove column from table?
ALTER TABLE table_name DROP column_name;

mysql> DESCRIBE employee;
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id     | int(11)       | NO   | PRI | NULL    |       |
| name   | varchar(25)   | YES  |     | NULL    |       |
| age    | int(11)       | YES  |     | NULL    |       |
| salary | decimal(10,4) | YES  |     | NULL    |       |
| mailId | varchar(25)   | YES  |     | NULL    |       |
| city   | varchar(10)   | YES  |     | NULL    |       |
| pin    | varchar(25)   | YES  |     | NULL    |       |
+--------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql>
mysql> ALTER TABLE employee DROP pin;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> DESCRIBE employee;
+--------+---------------+------+-----+---------+-------+
| Field  | Type          | Null | Key | Default | Extra |
+--------+---------------+------+-----+---------+-------+
| id     | int(11)       | NO   | PRI | NULL    |       |
| name   | varchar(25)   | YES  |     | NULL    |       |
| age    | int(11)       | YES  |     | NULL    |       |
| salary | decimal(10,4) | YES  |     | NULL    |       |
| mailId | varchar(25)   | YES  |     | NULL    |       |
| city   | varchar(10)   | YES  |     | NULL    |       |
+--------+---------------+------+-----+---------+-------+
6 rows in set (0.00 sec)



How to update column?
ALTER TABLE table_name MODIFY COLUMN column_name datatype












Previous                                                 Next                                                 Home

No comments:

Post a Comment