Computer Science · Ch 9 — Structured Query Language (SQL)
Describe Table
Describe Table
Why the DESCRIBE Statement Matters
Before you can work with a table — insert data, query it, or modify it — you need to know its structure: what columns exist, what type of data each column holds, whether a column can be empty, and so on. The DESCRIBE statement gives you that blueprint in a single command.
The DESCRIBE (or DESC) Statement
DESCRIBE (or its shorter form DESC) shows the structure of an already created table. You use it when you have forgotten the column names or datatypes, or when you are checking whether a table was created correctly.
Syntax:
DESCRIBE tablename;
or simply:
DESC tablename;
Example: Describing the STUDENT Table
Suppose you have a table named STUDENT. Running:
mysql> DESCRIBE STUDENT;
produces output like this:
| 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)
Each row in the output describes one column of the table. Let's understand what each column in the output means:
- Field: The name of the column (e.g.,
RollNumber,SName). - Type: The datatype assigned to that column (e.g.,
int,varchar(20),date,char(12)). - Null: Shows whether the column can contain NULL (empty) values.
NOmeans it cannot be left empty;YESmeans it can. - Key: Indicates if the column is part of any key.
PRImeans it is the primary key — a unique identifier for each row. A blank means no special key role. - Default: The default value that will be inserted if you do not provide a value for that column. Here, all columns show
NULLas default. - Extra: Any additional information about the column (like
auto_increment). In this example, it is blank for all columns.
The DESCRIBE output tells you that RollNumber is the primary key (PRI) and cannot be NULL. This means every student must have a unique roll number, and it must always be provided.
A Thought Question from the Textbook
The textbook asks: Which datatype out of Char and Varchar will you prefer for storing a contact number (mobile number)?
Think about it: A mobile number is a fixed-length string — typically 10 digits in India. It does not vary in length from one person to another. CHAR is designed for fixed-length data, while VARCHAR is for variable-length data. Since all mobile numbers have the same length, CHAR is the better choice — it is more efficient for storage and retrieval when the length is truly fixed.
Use CHAR for data that always has the same number of characters (like PIN codes, GSTIN, or mobile numbers). Use VARCHAR for data that varies in length (like names or addresses).
Viewing All Tables in a Database: SHOW TABLES
To see which tables exist in your current database, use the SHOW TABLES statement. For example, in the StudentAttendance database:
mysql> SHOW TABLES;
Output:
| Tables_in_studentattendance |
|---|
| student |
1 row in set (0.00 sec) …