Alter is the DDL command of SQL. It is use to update structure on "database object". To modify ,add , drop ,change ,rename etc and use after before clause.
Alter command ADD is use to add column in table with datatype or constraint.
+-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.02 sec) mysql> ALTER Table personal ADD Column fname char(30) DEFAULT NULL; Query OK, 1 row affected (0.17 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> EXPLAIN personal; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql>
Alter command DROP is use to remove column in table with datatype or constraint.
mysql> ALTER Table personal DROP Column fname; Query OK, 1 row affected (0.11 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> EXPLAIN personal; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 1 row in set (0.02 sec) mysql>
Alter command CHANGE is use to change column in table with datatype or constraint.
mysql> ALTER Table personal CHANGE Column lname sir_name char(30); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> EXPLAIN personal; +----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | | sir_name | char(30) | YES | | NULL | | +----------+----------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql>
Alter command MODIFY is use to upgrade column in table with datatype or constraint.
mysql> ALTER Table personal MODIFY sir_name int; Query OK, 1 row affected (0.09 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> EXPLAIN personal; +----------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | | sir_name | int(11) | YES | | NULL | | +----------+----------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql>
AFTER is help to add, column after the first column that we choice.
mysql> ALTER Table personal ADD lname char(30) AFTER fname; Query OK, 1 row affected (0.14 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> DESCRIBE personal; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | | lname | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql>
Rename command use to change the name of table and user. It also use with alter command.
mysql> RENAME Table personal TO mytable; Query OK, 0 rows affected (0.02 sec) mysql> DESCRIBE mytable; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql> DESCRIBE personal; ERROR 1146 (42S02): Table 'test.personal' doesn't exist mysql>
mysql> ALTER Table mytable RENAME TO personal; Query OK, 0 rows affected (0.02 sec) mysql> DESCRIBE personal; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | int(11) | YES | | NULL | | | fname | char(30) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ 2 rows in set (0.01 sec) mysql>