10.7 ALTER TABLE: Adding, Dropping, and Modifying Columns

Right, so you’ve built your table. It’s a thing of beauty. It holds your data with the tender embrace of a perfectly normalized schema. But time passes, requirements change, and that perfect table now looks… less perfect. Maybe marketing needs to track a new user preference, or you need to purge a column that was a bad idea from the start (we’ve all been there). This is where ALTER TABLE comes in—your digital wrench for performing table surgery. It’s powerful, but like any surgery, you don’t want to do it blindfolded.

10.6 DEFAULT Values: Literals, Functions, and Sequences

Right, let’s talk about DEFAULT values. This is where you stop treating your database like a passive spreadsheet and start making it do the work for you. The DEFAULT clause is your way of telling the database: “Look, if I don’t bother to specify a value for this column when I insert a row, don’t just yell at me with a NOT NULL error. Use this instead.” It’s the single best way to keep your application code from being cluttered with boilerplate logic for setting trivial, predictable values.

10.5 CHECK Constraints: Expressing Business Rules

Right, so you’ve got your columns defined and your primary key set. Good. But a column’s data type is a pretty blunt instrument. INTEGER can hold the value -999999 just as happily as 42. For your actual data, that’s probably nonsense. This is where CHECK constraints come in. They’re your first and best line of defense against data that, while technically the right type, is complete garbage from a business logic perspective. Think of them as bouncers for your rows; if the data doesn’t meet the criteria, it doesn’t get in. Period.

10.4 PRIMARY KEY: Identity, Implicit Unique Index, and NOT NULL

Right, let’s talk about the PRIMARY KEY. You’ve probably heard it’s important. That’s an understatement. It’s the single most important column (or set of columns) in your entire table. It’s the one thing that absolutely, positively must be unique for every single row. Think of it as your table’s social security number, its fingerprint, its “this one is mine” identifier. And because it’s so important, SQL gives it a whole bundle of superpowers automatically. Let’s break down what you’re really getting when you declare one.

10.3 UNIQUE Constraints: Single-Column and Multi-Column

Right, so you’ve told your data it has to be there (NOT NULL) and what it has to look like (CHECK). Now let’s talk about telling it to be, well, unique. A UNIQUE constraint is how you tell the database, “Look, I don’t care what value you put in this column, but it had darn well better be different from every other value in this column for all the other rows.” It’s the database’s way of enforcing that one-of-a-kind snowflake status, but for your data.

10.2 NOT NULL: Enforcing Mandatory Values

Right, let’s talk about NOT NULL. It’s the simplest constraint in the book, but also one of the most important. It doesn’t add fancy logic or relationships; it just does one job: it stops NULL from getting into a column. And that, my friend, is 90% of data sanity. Think of NULL as the database’s way of saying “I have no idea.” It’s not zero, it’s not an empty string, it’s not false. It’s the absence of a value. This is useful sometimes (like when you genuinely don’t have the data), but letting NULL run rampant through your tables is like inviting a poltergeist into your application logic. Your code will spend half its time checking IS NOT NULL before it can do anything useful. NOT NULL is your first and best line of defense against this chaos. It forces you, the human, to make a decision: “What must I have for this record to be valid?”

10.1 Column Definitions: Names, Types, and Defaults

Alright, let’s get our hands dirty and build a table. Think of a CREATE TABLE statement as the architectural blueprint for your data. It’s where you lay down the law: what data you’ll store, what it’s allowed to look like, and what happens when someone tries to break the rules. Screw this up, and you’re building on a foundation of digital quicksand. We’ll start with the core of any table: the column definitions. This is where you name your data, give it a type, and tell it how to behave when it’s feeling indecisive.

— joke —

...