Knowledgebase

Understanding Database Constraints Print

  • 0

Rules the database enforces.

WHAT TYPES EXIST

Not null Unique Primary key Foreign key Check constraints Default values

WHAT NOT NULL ENFORCES

That a value must be present.

WHY IT MATTERS

Absent values propagate through calculations and comparisons unexpectedly.

WHAT UNIQUE ENFORCES

That no two rows share a value.

WHAT TO BE CAREFUL WITH

Unique constraints and absent values, which are frequently permitted more than once.

WHAT A CHECK CONSTRAINT ENFORCES

An arbitrary condition on the row.

WHERE IT HELPS

Amounts that cannot be negative Dates that must follow other dates Status values from a permitted set

WHAT VERSIONS SUPPORT THEM

Recent MySQL, and PostgreSQL long before.

WHY THAT MATTERS

Older MySQL accepted the syntax and ignored it entirely.

WHAT TO VERIFY

That your version actually enforces them.

WHAT FOREIGN KEYS ENFORCE

That referenced rows exist.

WHAT THEIR ACTIONS CONTROL

What happens on delete or update of the parent.

WHAT CASCADING DELETE DOES

Removes children automatically.

WHY IT DESERVES CAUTION

A single delete can remove far more than intended.

WHAT TO PREFER USUALLY

Restricting deletion, and handling it explicitly.

WHAT TO NEVER DO

Disable constraints permanently to make an import work.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot