MySQL DELETE Query
DELETE removes rows from a table.
DELETE FROM students WHERE name = 'Neha';
MySQL says
Query OK, 1 row affected (0.01 sec)
SELECT COUNT(*) AS total FROM students;
Result
| total |
|---|
| 5 |
Note:
Always use WHERE. DELETE FROM students; deletes all the rows. Run a SELECT with the same WHERE first, to see what will be deleted.
Put the row back
INSERT INTO students (id, name, city, marks, course_id)
VALUES (6, 'Neha', 'Mumbai', 45, 2);
DELETE, TRUNCATE and DROP
| Command | What it removes | Can use WHERE? |
|---|---|---|
DELETE FROM t WHERE ... | Some or all rows. Table stays | Yes |
TRUNCATE TABLE t | All rows, fast. Table stays and the counter restarts | No |
DROP TABLE t | The whole table, with its structure | No |
Read about DROP TABLE.
Summary
DELETE FROM table WHERE condition;removes rows.- Without
WHEREevery row is deleted. - Next: DROP TABLE.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |