‹ All guides

Small constraints. Strong foundations.

Use primary keys, uniqueness, nullability, and checks to make your database enforce the rules that matter.

Application validation is useful, but it is not the only place to enforce rules. Database constraints protect your data even when it comes from a script, a background job, or a future version of your application.

A primary key identifies one record

A primary key must be unique and not null. A table can have one primary key constraint, which may include multiple columns. An integer identifier is a common choice, while UUIDs can be useful when identifiers are created independently across systems.

Keep identifiers stable. An email address may be unique today, but users can change it. An independent primary key lets relationships remain intact while business attributes change.

Composite keys express a shared identity

In a post_tags table, a post and tag together can identify one association. Mark both columns as primary keys to generate one composite primary key. Neither column alone identifies the row.

CREATE TABLE post_tags (
  post_id INTEGER NOT NULL,
  tag_id INTEGER NOT NULL,
  PRIMARY KEY (post_id, tag_id)
);

SchemaCraft supports composite primary keys and unique indexes. Its visual relationships currently reference one column at a time, so they cannot target a non-unique component of a composite key.

Unique does not mean required

A unique constraint prevents repeated non-null values in the database’s comparison rules. It does not generally make the column mandatory. Add NOT NULL when every row needs a value. NULL behavior varies by dialect: review it before relying on nullable unique fields.

Use nullability to express absence

NULL represents an absent or unknown value. It is different from an empty string, zero, or false. A returned_at date on an active library loan can reasonably be NULL. An order’s creation time usually should not be.

Marking everything NOT NULL can force artificial values into records. Allowing NULL everywhere can obscure required data. Choose the rule column by column.

Defaults are starting values

A default supplies a value when an insert omits a column. It does not prevent a caller from explicitly supplying another value. A default of zero is appropriate for a counter; it is not a substitute for checking that the counter remains non-negative.

SchemaCraft accepts SQL literals such as 0, TRUE, and 'draft', plus CURRENT_TIMESTAMP and CURRENT_DATE. Text defaults need single quotes. Auto-increment identity columns cannot also have a separate default in the designer.

Check constraints describe a valid row

A check such as price >= 0 rejects negative prices. When NULL is allowed, SQL’s three-valued logic matters: a CHECK alone does not necessarily reject NULL. Pair it with NOT NULL if a value is required.

price DECIMAL(10,2) NOT NULL CHECK (price >= 0)

Check expressions are SQL. Review their identifiers and functions for the dialect you select. SchemaCraft detects some structural issues but does not run arbitrary expressions against a database.

Read more in the PostgreSQL constraint reference.

Put it into practice.

Make a small schema and explore the SQL as you go.

Open the designer