ByteBoard Notes Notes

To add a column to the table and then drop it

Informatics Practices XI · Experiment 35 of 36 · SQL

11-IPSQL queries
Expt. No.35
Page No.70

Add and drop a column (ALTER)

Aim

To add a column phone to the STUDENT table, check the structure, and then remove the column.

Concepts used

Queries

USE school;ALTER TABLE student ADD phone CHAR(10);DESCRIBE student;SELECT * FROM student;ALTER TABLE student DROP phone;DESCRIBE student;

A marked line continues the line above it. In your file, write it on the same line.

Output

mysql> USE school; Database changed
mysql> ALTER TABLE student ADD phone CHAR(10); Query OK, 0 rows affected
mysql> DESCRIBE student; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | rno | int | NO | PRI | NULL | | | class | varchar(3) | YES | | NULL | | | section | char(1) | YES | | NULL | | | gender | char(1) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | dob | date | YES | | NULL | | | marks | int | YES | | NULL | | | phone | char(10) | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 8 rows in set
mysql> SELECT * FROM student; +-----+-------+---------+--------+--------+------------+-------+-------+ | rno | class | section | gender | name | dob | marks | phone | +-----+-------+---------+--------+--------+------------+-------+-------+ | 1 | XI | A | F | Aarohi | 2009-03-14 | 78 | NULL | | 2 | XI | A | M | Bharat | 2008-11-02 | 45 | NULL | +-----+-------+---------+--------+--------+------------+-------+-------+ 2 rows in set
mysql> ALTER TABLE student DROP phone; Query OK, 0 rows affected
mysql> DESCRIBE student; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | rno | int | NO | PRI | NULL | | | class | varchar(3) | YES | | NULL | | | section | char(1) | YES | | NULL | | | gender | char(1) | YES | | NULL | | | name | varchar(20) | YES | | NULL | | | dob | date | YES | | NULL | | | marks | int | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 7 rows in set

✓ Printed by running this query set, not typed by hand.

Result

ALTER TABLE … ADD put a phone column at the end of the table; the existing rows got NULL in it. ALTER TABLE … DROP removed the column again, as the second DESCRIBE shows.

Viva voce: what the examiner may ask

01Is ALTER a DDL or a DML command?

DDL: it changes the structure of the table, not the data.

02What value does a new column have in existing rows?

NULL, unless a DEFAULT is given.

03Why store a phone number as CHAR and not INT?

Phone numbers are not used in arithmetic and may start with 0 or +.

Variations for the lab test

  1. Add a column email VARCHAR(30).
  2. Add a column fee INT with DEFAULT 5000 and check the existing rows.
  3. Change the size of the name column to VARCHAR(30) with MODIFY.
! Common mistake Writing ALTER TABLE student ADD COLUMN phone without a data type gives a syntax error.
Back to the 11-IP index