To create a student table with student id, class, section, gender, name, dob and marks
Expt. No.22
Page No.44
Create the STUDENT table
Aim
To create a STUDENT table with the attributes student id (rno), class, section, gender, name, dob and marks, with the student id as the primary key.
Concepts used
Queries
USE school;CREATE TABLE student ( rno INT PRIMARY KEY, class VARCHAR(3), section CHAR(1), gender CHAR(1), name VARCHAR(20), dob DATE, marks INT);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> CREATE TABLE student (
-> rno INT PRIMARY KEY,
-> class VARCHAR(3),
-> section CHAR(1),
-> gender CHAR(1),
-> name VARCHAR(20),
-> dob DATE,
-> marks INT
-> );
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
The STUDENT table was created with 7 columns. DESCRIBE shows rno as the primary key (PRI, cannot be NULL) and the data type of every column.
Viva voce: what the examiner may ask
01Why is rno the primary key?
Each student has a different roll number, so it identifies every row uniquely.
02What is the difference between CHAR and VARCHAR?
CHAR(n) always uses n characters; VARCHAR(n) uses only as many as the value needs, up to n.
03In what format is a DATE written in MySQL?
'YYYY-MM-DD', e.g. '2009-03-14'.
Variations for the lab test
- Create a TEACHER table with id, name, subject and salary.
- Add NOT NULL to the name column.
- Use DESCRIBE to check the TEACHER table.
! Common mistake Putting a comma after the last column (
marks INT,) before the closing bracket gives a syntax error.