ByteBoard Notes Notes

Insert the details of a new student in the student table

Informatics Practices XII · Experiment 24 of 38 · SQL

★ CBSE suggested12-IPSQL queries
Expt. No.24
Page No.48

Insert a student

Aim

To insert the details of a new student into the STUDENT table and display the table.

Concepts used

Queries

USE school;INSERT INTO student VALUES (5, 'Diya', 74);INSERT INTO student (sid, name) VALUES (6, 'Neel');SELECT * FROM student;

A marked line continues the line above it. In your file, write it on the same line.

Table used

Table: student
+-----+-------+-------+
| sid | name  | marks |
+-----+-------+-------+
|   1 | Asha  |    82 |
|   2 | Rohan |    67 |
|   3 | Ira   |    88 |
|   4 | Kabir |    91 |
+-----+-------+-------+

Output

mysql> USE school; Database changed
mysql> INSERT INTO student VALUES (5, 'Diya', 74); Query OK, 1 row affected
mysql> INSERT INTO student (sid, name) VALUES (6, 'Neel'); Query OK, 1 row affected
mysql> SELECT * FROM student; +-----+-------+-------+ | sid | name | marks | +-----+-------+-------+ | 1 | Asha | 82 | | 2 | Rohan | 67 | | 3 | Ira | 88 | | 4 | Kabir | 91 | | 5 | Diya | 74 | | 6 | Neel | NULL | +-----+-------+-------+ 6 rows in set

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

Result

Diya was inserted with all three values and Neel with only sid and name, so his marks are NULL. The table now has 6 students.

Viva voce: what the examiner may ask

01What value does a column get if it is left out of INSERT?

Its default, which is NULL unless a DEFAULT is set.

02Why is the column list needed for Neel?

Only two values are given, so MySQL must be told which columns they go into.

03What happens if name is left out?

An error, because name is NOT NULL and has no default.

Variations for the lab test

  1. Insert two students in one INSERT statement.
  2. Insert a student with a duplicate sid and read the error.
  3. Insert a student giving the columns in a different order.
! Common mistake Giving fewer values than columns without a column list (VALUES (6, 'Neel')) raises "Column count doesn't match".
Back to the 12-IP index