INSERT, DELETE, and UPDATE Statements in SQL
INSERT, DELETE, and UPDATE Statements in SQL
In SQL, database modification is performed using three fundamental commands:
-
INSERT – to add new data
-
DELETE – to remove existing data
-
UPDATE – to modify existing data
Unlike retrieval queries, these commands change the database state and therefore must obey all integrity constraints defined in the schema (domain, key, entity integrity, and referential integrity constraints).
6.4.1 The INSERT Command
The INSERT command is used to add new tuples (rows) into a table.
1. INSERT with Complete Tuple
In its simplest form, INSERT specifies:
-
The table name
-
A complete list of attribute values in the same order as defined in
CREATE TABLE
📌 Important rules:
-
Number of values must match the number of attributes
-
Data types must be compatible
-
Order must match the table schema
-
All integrity constraints are checked
2. INSERT with Selected Attributes
Often, not all attribute values are known. SQL allows insertion by specifying only some attributes.
📌 Behavior:
-
Attributes not listed:
-
Get their DEFAULT value, or
-
Become NULL (if allowed)
-
-
Attributes declared NOT NULL and without DEFAULT must be included
3. Constraint Enforcement during INSERT
A DBMS enforces all constraints automatically.
Example: Referential Integrity Violation
❌ Rejected if no department with Dnumber = 2 exists.
Example: Entity Integrity Violation
❌ Rejected because Ssn is the primary key and cannot be NULL.
4. Inserting Multiple Tuples
SQL allows inserting multiple rows in a single command:
5. INSERT with Query Result (INSERT … SELECT)
INSERT can be combined with a SELECT query to populate a table.
Step 1: Create the Table
Step 2: Insert Query Results
📌 This creates a snapshot table.
-
Data may become outdated
-
To maintain consistency → views are preferred
6. Bulk Loading and Table Copying
Bulk Loading
-
DBMS tools load data from files efficiently
-
Faster than repeated INSERT commands
Creating and Loading a Table Using LIKE
📌 This:
-
Copies table structure
-
Loads selected data immediately
6.4.2 The DELETE Command
The DELETE command removes tuples from a table.
General Syntax
-
Deletes rows, not columns
-
Affects only one table
-
May trigger cascading actions due to referential integrity
Examples
Delete specific employees
➡ Deletes zero or more rows
Delete one specific tuple
➡ Deletes exactly one tuple (primary key match)
Delete multiple tuples
➡ Deletes all employees in department 5
Delete all tuples
⚠️ Table structure remains
To remove the table completely → DROP TABLE
Referential Integrity Effects
If ON DELETE CASCADE is defined:
-
Deleting a tuple may automatically delete related tuples in other tables
If ON DELETE SET NULL:
-
Referencing foreign keys are set to NULL
6.4.3 The UPDATE Command
The UPDATE command modifies attribute values in existing tuples.
General Syntax
-
Modifies rows, not schema
-
WHERE clause selects affected tuples
-
SET clause defines new values
Example 1: Updating Specific Attributes
➡ Updates a single project
Example 2: Updating Multiple Tuples
📌 Important concept:
-
Right-hand
Salary→ old value -
Left-hand
Salary→ new value
Setting NULL or DEFAULT Values
Referential Integrity Effects of UPDATE
If a primary key is updated:
-
Changes may cascade to foreign keys (
ON UPDATE CASCADE) -
Or be rejected (
RESTRICT) -
Or set to NULL / DEFAULT
Summary of SQL Modification Commands
| Command | Purpose |
|---|---|
| INSERT | Adds new tuples |
| DELETE | Removes existing tuples |
| UPDATE | Modifies existing tuples |
Key Observations
-
All commands obey integrity constraints
-
Operations may cascade across tables
-
Each command modifies one table at a time
-
Multiple changes require multiple commands
Comments
Post a Comment