Databases Are Weirder Than I Expected
i started a databases course. turns out storing data is an entire field and it's been around since before i was born.
who knew? (everyone in the industry, apparently.)
sql: first impressions
it's english-ish. like, you can kind of read it and understand what's happening:
SELECT * FROM students WHERE grade > 80
"give me all students where grade is more than 80." makes sense.
but then it gets weird:
SELECT s.name, c.title, e.grade
FROM students s
JOIN enrollments e ON s.id = e.student_id
JOIN courses c ON e.course_id = c.id
WHERE c.department = 'CS'
GROUP BY s.name, c.title
HAVING AVG(e.grade) > 75
ORDER BY s.name
my eyes crossed. multiple times.
joins: the concept
when i first learned about joins i genuinely didn't understand why data would be split across multiple tables. just put it all in one big table, right?
wrong. so wrong.
the short version:
- redundant data is bad (takes space, causes inconsistencies)
- splitting data into related tables is good (normalization)
- joins bring it back together when you need it
- there are like five types of joins and they all do different things
i drew diagrams. so many diagrams.
the beautiful parts
once it clicks, SQL is actually pretty elegant:
- aggregations: count things, sum things, average things, in one line
- subqueries: queries inside queries (query-ception)
- views: save a query as if it's a table
- transactions: do multiple things atomically (either all succeed or all fail)
there's a reason this tech has been around for 50+ years.
the frustrating parts
- debugging is hard (where did the data go? why is it null? why are there duplicates?)
- performance matters and i don't understand performance yet
- every database has slightly different syntax
- accidentally deleting data is terrifyingly easy
i've run DROP TABLE on a test database and still felt a moment of panic.
nosql exists
apparently there's a whole other world of databases that don't use SQL. mongodb, redis, cassandra... they solve different problems.
adding to the "things to learn eventually" pile.
practical realization
every app i want to build needs to store data somewhere. understanding databases isn't optionalβit's fundamental.
the course is hard but i get why it's required.
wrote my first stored procedure today. it works. i don't fully understand why. classic.