Using SQL for Data Manipulation, Queries, and Transactions
💬 SQL Basics in PostgreSQL
SQL is the cornerstone of interacting with PostgreSQL, enabling:
- 📥 Data insertion
- 🔍 Retrieval
- ✏️ Updating
- ❌ Deletion
➕ INSERT Example
INSERT INTO books (title, author_id) VALUES ('Deep Learning', 1);
🔍 SELECT with Conditions
SELECT * FROM books WHERE author_id = 1;
🔄 Transactions for Data Consistency
BEGIN;
UPDATE accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;
✅ Use BEGIN
, COMMIT
, and ROLLBACK
to group operations atomically.
🛡️ This guarantees all operations within a transaction either complete successfully or not at all, maintaining data integrity.