1.3 INSERT, SELECT, ORDER BY, UPDATE, DELETE
After structure exists, daily work is mostly about rows. Common actions are:
INSERT: add rows.SELECT: read rows.ORDER BY: sort query results.UPDATE: modify rows.DELETE: remove rows.
INSERT
INSERT INTO students (student_id, name, country, score)
VALUES (1, 'Ada', 'US', 96);Prefer writing column names explicitly. If the table gains new columns later, the statement is less likely to break because of column-order assumptions.
SELECT and ORDER BY
SELECT student_id, name, score
FROM students
WHERE score >= 85
ORDER BY score DESC;SELECT * is convenient while learning, but real application code should use it carefully. Explicit column names are more stable and make data sources clearer.
ORDER BY controls result order. Without ORDER BY, returned order should not be treated as business logic.
The safety boundary for UPDATE and DELETE
UPDATE students
SET score = 86
WHERE student_id = 2;DELETE FROM students
WHERE student_id = 5;WHERE decides which rows are changed or removed. An UPDATE or DELETE without WHERE may affect the whole table.
A recommended change flow
Query first:
SELECT *
FROM students
WHERE student_id = 2;After confirming the target row, write:
UPDATE students
SET score = 86
WHERE student_id = 2;In real environments, you can also run changes inside a transaction, inspect affected row counts and results, then commit.