CRUD Operations in SQL
basic · SQL
Data Manipulation Language (DML) & CRUD Matrix Once database containers and structural tables are provisioned via DDL, applications interact with the data using Data Manipulation Language (DML) . DML maps directly to the standard engineering acronym CRUD (Create, Read, Update, Delete), forming the operational foundation for transactional data handling. 1. Create Operations: INSERT The INSERT statement commits new records to a table's data blocks, populating the column schema definitions. A. Explicit Single Row Insertion Specifying column targets explicitly is a production standard. It insulates the query block from crashing if a database migration introduces new optional columns to the table schema later on. SQL INSERT INTO infrastructure_nodes (node_name, assigned_ip, system_weight) VALUES ('Node-Edge-01', '192.168.1.10', 250); B. Bulk Insertion (Multiple Rows) Inserting multiple rows inside a single SQL query block drastically cuts down network latency overhead and transaction log writing stress compared to firing separate, individual insert queries. SQL INSERT INTO infrastructure_nodes (node_name, assigned_ip, system_weight) VALUES ('Node-Edge-02', '192.168.1.11', 200), ('Node-Edge-03', '192.168.1.12', 150), ('Node-Edge-04', '192.168.1.13', 300); 2. Read Operations: SELECT The SELECT statement queries data blocks, fetching matching record pipelines from the disk layout into the active application framework. A. Projections and De-Duplication SQL -- Explicit projection: Query only the specific columns needed to save network memory bandwidth SELECT node_name, assigned_ip FROM infrastructure_nodes; -- DISTINCT: Filters out duplicate entries across the selected columns, returning unique combinations SELECT DISTINCT system_weight FROM infrastructure_nodes; B. The WHERE Clause Filter Execution Matrix The WHERE clause applies boolean evaluation filters, forcing the database engine to return only the rows that match your exact conditions. SQL SELECT node_name, assigned_ip, system_weight FROM infrastructure_nodes WHERE is_active = TRUE AND system_weight >= 200 AND node_name LIKE 'Node-Edge%'; -- '%' matches any trailing characters C. Ordering and Pagination Engine ( ORDER BY , LIMIT , OFFSET ) When presenting records to user interfaces, you must sort and paginate datasets to prevent overwhelming the application layer. SQL SELECT node_id, node_name FROM infrastructure_nodes WHERE is_active = TRUE ORDER BY system_weight DESC, node_name ASC -- Sorts by weight first, then breaks ties by name LIMIT 10 -- Restricts the maximum returned payload to exactly 10 row records OFFSET 20; -- Skips the first 20 matching records (Retrieves Page 3 of the dataset) Engine Differentiation Note: The LIMIT keyword is a native standard in engines like PostgreSQL , MySQL , and SQLite . Conversely, Microsoft SQL Server (T-SQL) handles this boundary using the TOP keyword injected directly at the start of the query projection block: SELECT TOP 10 node_name FROM infrastructure_nodes; . 3. Update Operations: UPDATE The UPDATE statement alters active in-memory record data points and flushes those modified states back down to persistent disk storage blocks. Conditional State Mutations SQL -- Targeted conditional update UPDATE infrastructure_nodes SET system_weight = 500, is_active = TRUE WHERE assigned_ip = '192.168.2'; ⚠️ Critical Production Guardrail If you execute an UPDATE statement without adding a protective WHERE clause, the database engine will apply those updates to every single row in the entire table. Always verify your query logic inside a sandbox environment before executing updates on production systems. 4. Delete Operations: DELETE The DELETE statement permanently removes specific data rows from a table structure while keeping the table layout and structural schemas completely untouched. Targeted Row Removal SQL DELETE FROM infrastructure_nodes WHERE is_active = FALSE AND system_weight < 100; Safe Delete Engineering Practices Because accidentally purging records from a database can take down an application, production systems protect data integrity using two primary strategies: DATA RETENTION STRATEGIES │ ┌────────────────────────┴────────────────────────┐ ▼ ▼ Hard Delete Soft Delete • Physical row destruction • Virtual exclusion toggle • Irreversible data loss • Reversible (Data stays on disk) • Fast storage recovery • Preserves historical logs Soft Deletes: Avoid using the physical DELETE command entirely for user-facing data. Instead, add an is_deleted boolean flag or a deleted_at timestamp column to your table schema. When a user requests a deletion, update that flag to TRUE . Then, update your application queries to filter those rows out automatically using WHERE is_deleted = FALSE; Transactional Sandboxing: When forced to run manual, raw data updates or hard deletions directly on a live production terminal, wrap your queries inside a controlled TRANSACTION block. This allows you to audit the results safely before locking the modifications into disk storage. SQL (postgres) BEGIN TRANSACTION; -- Execute the hazardous data operation DELETE FROM infrastructure_nodes WHERE system_weight < 100; -- Safety Check step: Run a selective select query inside the uncommitted transaction block SELECT COUNT(*) FROM infrastructure_nodes; -- If the row count looks correct, execute: COMMIT; -- If you notice an error or a missing condition, undo everything safely by executing: ROLLBACK; CRUD Operations Reference Matrix CRUD Vector Pattern SQL Command Context Index Scan Profile Impact Critical Production Guardrail Create INSERT INTO Evaluates table constraints and updates all attached index tables. Specify columns explicitly. Avoid implicit row injections to protect your application from crashing if table columns shift during migrations. Read SELECT Leverages index scanning paths to avoid slow, full-table disk reads. Never use open-ended projections like SELECT * in production code. Requesting columns you don't need creates massive network memory overhead. Update UPDATE Locks target data blocks temporarily until the transaction commits. Never omit the WHERE clause unless you intend to overwrite that specific variable across the entire table. Delete DELETE Scans rows individually, writing transaction log records and triggering deletion audit hooks. Hard deletions are irreversible. Prefer a Soft Delete flag architecture to preserve historical logs and avoid accidental data loss.