Informatics Practices · Ch 7 — Introduction to Structured Query Language (SQL)
DESCRIBE Table
7.4.3
DESCRIBE Table
After a table has been created, you will often need to check exactly how it was defined — which columns it has, their data types, and which constraints apply to them. SQL provides the DESCRIBE statement to view the structure of an already created table:
DESCRIBE tablename;
MySQL also supports the short form DESC of DESCRIBE to get the description of a table — both do the same job. To retrieve details about the structure of the relation STUDENT, write DESC or DESCRIBE followed by the table name:
mysql> DESC STUDENT;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| RollNumber | int | NO | PRI | NULL | |
| SName | varchar(20) | YES | | NULL | |
| SDateofBirth | date | YES | | NULL | |
| GUID | char(12) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
4 rows in set (0.06 sec)
The output rewards a careful reading:
- Field lists the column names, in the order they were declared.
- Type shows the data type of each column — int, varchar(20), date, char(12) — exactly as chosen when the table was created.
- Null tells whether the column may hold NULL. RollNumber shows NO because it is the primary key; every other column shows YES, confirming that by default an attribute can take NULL values.
- Key marks the key columns: PRI against RollNumber identifies it as the primary key.
- Default shows the value a column takes when none is supplied — NULL for all columns here.
Now that STUDENT exists, the SHOW TABLES command no longer reports an empty set; it returns the table:
mysql> SHOW TABLES;
+------------------------------+
| Tables_in_studentattendance |
+------------------------------+
| student | …