ByteBoard Notes Notes

Create a student table with the student id, name and marks as attributes where the student id is the primary key

Informatics Practices XII · Experiment 23 of 38 · SQL

★ CBSE suggested12-IPSQL queries
Expt. No.23
Page No.46

Create the STUDENT table

Aim

To create a STUDENT table with student id, name and marks, where the student id is the primary key.

Concepts used

Queries

USE school;CREATE TABLE student (    sid INT PRIMARY KEY,    name VARCHAR(20) NOT NULL,    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 ( -> sid INT PRIMARY KEY, -> name VARCHAR(20) NOT NULL, -> marks INT -> ); Query OK, 0 rows affected
mysql> DESCRIBE student; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | sid | int | NO | PRI | NULL | | | name | varchar(20) | NO | | NULL | | | marks | int | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set

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

Result

The table was created; DESCRIBE shows sid as the primary key (PRI, not NULL) and name as NOT NULL.

Viva voce: what the examiner may ask

01What does a primary key guarantee?

Every value is unique and none is NULL.

02What does NOT NULL do?

It stops a row being inserted without a value in that column.

03Which command shows the structure of a table?

DESCRIBE (or DESC).

Variations for the lab test

  1. Create a table SUBJECT with code as the primary key.
  2. Add a column class to the STUDENT table.
  3. Try inserting two students with the same sid.
! Common mistake Writing PRIMARY KEY on two columns separately (sid INT PRIMARY KEY, name ... PRIMARY KEY) is an error: a table has one primary key.
Back to the 12-IP index