Welcome back to the SQL Refresher series! In this post, we’re diving into the following SQL concepts—how to DELETE and UPDATE records, understand DELETE rules, explore smart table design with normalization (1NF), and learn how to ALTER tables.
DELETE
and UPDATE
StatementsDELETE
StatementThe DELETE
statement removes rows from a table based on a condition.
Example – Delete galaxies that are not observable:
DELETE FROM Galaxies
WHERE is_observable = false;
Be careful! If you omit the
WHERE
clause, all rows will be deleted:
DELETE FROM Galaxies;
You can’t delete specific columns (SQL doesn't support partial deletes).
Cannot delete rows from a view unless it’s updatable.
Cannot delete if there are foreign key constraints blocking the operation unless:
You cascade deletes
Or manually delete related child records first
UPDATE
StatementThe UPDATE
statement modifies existing records in a table.
Example – Update the status of galaxies in the Virgo cluster:
UPDATE Galaxies
SET classification = 'Elliptical'
WHERE cluster_name = 'Virgo';
Always use a
WHERE
clause to avoid unintended changes.
Designing good tables avoids redundancy, improves performance, and ensures consistency.
A table is in 1NF if:
It has a primary key
All columns contain atomic (indivisible) values
No repeating groups or arrays
Must be unique and not null
Only one primary key per table
Can span multiple columns (composite key)
Example:
CREATE TABLE Galaxies (
galaxy_id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
type VARCHAR(50),
age_in_million_years INT
);
ALTER TABLE
– Change Structure on the FlyThe ALTER TABLE
command lets you modify an existing table.
ADD
ColumnALTER TABLE Galaxies
ADD distance_light_years BIGINT;
MODIFY
ColumnALTER TABLE Galaxies
MODIFY distance_light_years INT;
CHANGE
ColumnALTER TABLE Galaxies
CHANGE COLUMN name galaxy_name VARCHAR(100);
DROP
ColumnALTER TABLE Galaxies
DROP COLUMN type;
Be cautious—
DROP
deletes the column and all its data permanently.
In this blog, we covered:
How to update and delete records
Rules and constraints with DELETE
Smart design with 1NF, atomic columns, and proper primary keys
The power of ALTER TABLE
to evolve your schema
Keep your tables clean, atomic, and cosmic!
Join Mutukundu Mahendra on Peerlist!
Join amazing folks like Mutukundu Mahendra and thousands of other people in tech.
Create ProfileJoin with Mutukundu Mahendra’s personal invite link.
0
11
0