SQL Set 5: SHOW, DESCRIBE, adding and removing a primary key, DROP TABLE and DROP DATABASE
Expt. No.23
Page No.46
SHOW, DESCRIBE, keys, DROP
Aim
To use SHOW DATABASES, SHOW TABLES and DESCRIBE, add and remove a primary key with ALTER TABLE, and remove a table and a database with DROP.
Concepts used
Queries
CREATE DATABASE library;SHOW DATABASES;USE library;CREATE TABLE book ( bookid INT, title VARCHAR(40), author VARCHAR(25), price INT);SHOW TABLES;DESCRIBE book;-- Add a primary key to an existing tableALTER TABLE book ADD PRIMARY KEY (bookid);DESCRIBE book;-- Remove the primary keyALTER TABLE book DROP PRIMARY KEY;DESCRIBE book;DROP TABLE book;SHOW TABLES;DROP DATABASE library;
A marked line continues the line above it. In your file, write it on the same line.
Output
mysql> CREATE DATABASE library;
Query OK, 1 row affected
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| library |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
6 rows in set
mysql> USE library;
Database changed
mysql> CREATE TABLE book (
-> bookid INT,
-> title VARCHAR(40),
-> author VARCHAR(25),
-> price INT
-> );
Query OK, 0 rows affected
mysql> SHOW TABLES;
+-------------------+
| Tables_in_library |
+-------------------+
| book |
+-------------------+
1 row in set
mysql> DESCRIBE book;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| bookid | int | YES | | NULL | |
| title | varchar(40) | YES | | NULL | |
| author | varchar(25) | YES | | NULL | |
| price | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set
mysql> ALTER TABLE book ADD PRIMARY KEY (bookid);
Query OK, 0 rows affected
mysql> DESCRIBE book;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| bookid | int | NO | PRI | NULL | |
| title | varchar(40) | YES | | NULL | |
| author | varchar(25) | YES | | NULL | |
| price | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set
mysql> ALTER TABLE book DROP PRIMARY KEY;
Query OK, 0 rows affected
mysql> DESCRIBE book;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| bookid | int | NO | | NULL | |
| title | varchar(40) | YES | | NULL | |
| author | varchar(25) | YES | | NULL | |
| price | int | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set
mysql> DROP TABLE book;
Query OK, 0 rows affected
mysql> SHOW TABLES;
Empty set
mysql> DROP DATABASE library;
Query OK, 0 rows affected
✓ Printed by running this query set, not typed by hand.
Result
SHOW DATABASES listed the new library database along with the server's own databases. After
ADD PRIMARY KEY, DESCRIBE showed PRI in the Key column and NO in Null for bookid; after DROP PRIMARY KEY the key mark was gone but the NOT NULL rule stayed. DROP TABLE left an empty database, and DROP DATABASE removed it.Viva voce: what the examiner may ask
01What does DESCRIBE show?
The structure of a table: each column's name, data type, whether it can be NULL, key and default value.
02What two rules does a primary key enforce?
Its values must be unique and cannot be NULL.
03What is the difference between a candidate key and a primary key?
Every column (or set) that can identify rows uniquely is a candidate key; the one chosen to identify rows is the primary key.
04Can a table have two primary keys?
No. It has one primary key, which may be made of more than one column.
Variations for the lab test
- Create the table with the primary key in CREATE TABLE itself.
- Add a UNIQUE constraint on title.
- Try to insert two books with the same bookid after adding the key and note the error.
! Common mistake Running DROP DATABASE on the wrong name deletes every table in it with no undo; check the name with SHOW DATABASES first.