SwadhyAI Notes

All SQL notes

➕INSERT INTO

Add rows; name your columns

INSERT extends a table with new tuples. Listing target columns explicitly keeps application SQL stable when nullable columns gain defaults, when column order changes in migrations, or when new columns appear mid-sprint. Multi-row VALUES batches reduce round-trips; very large loads often move to INSERT … SELECT or engine-specific bulk tools.

INSERT INTO t VALUES (…)

Positional inserts break silently when someone adds a column in the middle of the table or reorders DDL in a dump, staging looks fine, production misaligns types or semantics.

INSERT INTO t (col_a, col_b) VALUES (…)

Always project the insert shape in the header. Pair with code review rules that reject bare VALUES lists without a column list.

SQL

Explicit column list

INSERT INTO instruments (instrument_id, model, installed_on)
VALUES ('SEQ-14', 'NovaSeq 6000', DATE '2026-02-10');
✓Illustrative result set
status
1 row inserted
SQL

Multi-row insert

INSERT INTO qc_thresholds (assay_code, metric, min_value, max_value)
VALUES
  ('ELISA-IGG', 'OD450', 0.05, 3.0),
  ('ELISA-IGG', 'CV_PERCENT', 0.0, 15.0),
  ('PCR-Q', 'CT', 10, 38);
✓Illustrative result set
rows_inserted
3
SQL

RETURNING generated keys (PostgreSQL-style)

INSERT INTO sample_events (sample_id, event_type)
VALUES ('S-901', 'RECEIVED')
RETURNING event_id, recorded_at;
✓Illustrative result set
event_idrecorded_at
440212026-04-12 09:14:02+00

🧭Pick the insert shape

Same logical row, different operational trade-offs.

PatternWhen to use
INSERT … VALUESSmall static or app-generated batches; OLTP hot paths
INSERT … SELECTETL, staging promotion, repartitioning, same transaction as validation
RETURNING / OUTPUTFetch surrogate keys and defaults in one round-trip for APIs

Triggers and FK checks

Bulk paths sometimes use session flags or minimal logging, verify that triggers, row-level security, and foreign keys still behave the way compliance expects before turning them on in production loads.

TL;DR

INSERT adds new rows to a table; column lists and VALUES tuples must satisfy NOT NULL, FK, CHECK, and UNIQUE constraints or the statement fails (unless deferred). Multi-row inserts amortize round trips; returning clauses fetch generated keys. Bulk loaders bypass some checks unless configured, validate the path you actually use in production.