To delete the record of a student with a given student id
Expt. No.34
Page No.68
Delete a student (DELETE)
Aim
To delete the record of the student with roll number 7 and display the remaining rows.
Concepts used
Queries
USE school;DELETE FROM student WHERE rno = 7;SELECT rno, name FROM student;
A marked line continues the line above it. In your file, write it on the same line.
Table used
Table: student
+-----+-------+---------+--------+--------+------------+-------+ | rno | class | section | gender | name | dob | marks | +-----+-------+---------+--------+--------+------------+-------+ | 1 | XI | A | F | Aarohi | 2009-03-14 | 78 | | 2 | XI | A | M | Bharat | 2008-11-02 | 45 | | 3 | XI | B | M | Chetan | 2009-07-21 | NULL | | 4 | XI | B | F | Divya | 2009-01-30 | 91 | | 5 | XII | A | M | Eshan | 2008-05-17 | 66 | | 6 | XII | A | F | Fatima | 2008-09-09 | 83 | | 7 | XII | B | M | Gagan | 2007-12-25 | 38 | | 8 | XII | B | F | Hina | 2008-02-11 | NULL | | 9 | XI | A | M | Ishan | 2009-10-05 | 57 | | 10 | XII | A | F | Jaya | 2008-06-28 | 74 | +-----+-------+---------+--------+--------+------------+-------+
Output
mysql> USE school;
Database changed
mysql> DELETE FROM student WHERE rno = 7;
Query OK, 1 row affected
mysql> SELECT rno, name FROM student;
+-----+--------+
| rno | name |
+-----+--------+
| 1 | Aarohi |
| 2 | Bharat |
| 3 | Chetan |
| 4 | Divya |
| 5 | Eshan |
| 6 | Fatima |
| 8 | Hina |
| 9 | Ishan |
| 10 | Jaya |
+-----+--------+
9 rows in set
✓ Printed by running this query set, not typed by hand.
Result
The row of roll number 7 (Gagan) was deleted ("1 row affected"), and 9 students remained.
Viva voce: what the examiner may ask
01What is the difference between DELETE and DROP TABLE?
DELETE removes rows; DROP TABLE removes the whole table with its structure.
02What does DELETE FROM student; do?
It deletes every row, leaving an empty table.
03Why is the primary key used in the WHERE clause?
It identifies exactly one row, so no other student is deleted by mistake.
Variations for the lab test
- Delete all students whose marks are NULL.
- Delete students of class XII section B.
- Insert Gagan's record again.
! Common mistake Writing
DELETE * FROM student WHERE ... is a syntax error; DELETE has no column list.