sql-patternslisted
Install: claude install-skill Tibsfox/gsd-skill-creator
# SQL Patterns
Best practices for writing safe, performant, and maintainable SQL. All examples use parameterized queries to prevent SQL injection.
## Security-First Principles
Every query MUST use parameterized inputs. Never concatenate user input into SQL strings.
```sql
-- DANGEROUS: SQL injection vulnerability
SELECT * FROM users WHERE email = '" + email + "';
-- SAFE: Parameterized query
SELECT id, name, email FROM users WHERE email = $1;
```
### Destructive Operation Safety
Never run destructive operations without a WHERE clause and explicit confirmation.
| Operation | Risk Level | Required Safeguards |
|-----------|------------|---------------------|
| `DROP TABLE` | CRITICAL | Backup first, use `IF EXISTS`, require approval |
| `TRUNCATE TABLE` | CRITICAL | Backup first, confirm table name, check foreign keys |
| `DELETE` without `WHERE` | CRITICAL | Always add WHERE clause, run SELECT first to preview |
| `UPDATE` without `WHERE` | CRITICAL | Always add WHERE clause, run SELECT first to preview |
| `ALTER TABLE DROP COLUMN` | HIGH | Backup first, check dependencies, use migration |
| `DROP INDEX` | MEDIUM | Verify query plans won't degrade |
Always preview before modifying:
```sql
-- Step 1: Preview what will be affected
SELECT id, email, status FROM users WHERE status = 'inactive';
-- Step 2: Verify the count
SELECT COUNT(*) FROM users WHERE status = 'inactive';
-- Step 3: Only then modify (parameterized)
DELETE FROM users WHERE status = $1; -- $1 = 'ina