No duplicates, no double bookings
A table's constraints are rules the database itself enforces: no two records may share a code, and no two bookings of one thing may overlap. They hold for a form, an import, a script and two people saving at the same moment. We imported the Court Bookings sample and tried to break both.

Some mistakes have to be impossible, not just unlikely: two products with the same code, the same room booked twice for the same hour. A check in a form catches the person who's careless. It doesn't catch two people saving at the same moment, because both forms look, find nothing there yet, and save.
A table's constraints close that gap. They're rules the database enforces, so they hold however a record is written.
We used the Court Bookings sample from samples/solutions/mda-no-double-bookings. Import it and
open Court Bookings from the app list to follow along. It has two tables, Courts and
Bookings, and one rule on each.
No two courts with the same code
Under Courts, we created Centre Court with code C1. Then Court Two, also C1:

The save is refused, and it says which field. Change the code to C2 and it saves.
(The first time we tried this, the save was refused correctly but the message was the database's
own: Npgsql.PostgresException 23505: duplicate key value violates unique constraint "uq_par_court_par_code". The platform knows how to say it in words, but it couldn't match the
refusal to the table's rules, because it hadn't loaded them. We fixed that.)
A record that leaves the field empty isn't a duplicate. Any number of courts could have no code. If the code must also be filled in, make the field required as well, which this sample does.
No two bookings of one court at once
Under Bookings, we booked Centre Court from 09:00 to 10:00. Then we tried to book it again from 09:30 to 10:30:

Refused. Then two bookings that are fine:
- Court Two from 09:30 to 10:30. The rule is kept separately for each court.
- Centre Court from 10:00 to 11:00. A booking that starts when another ends doesn't overlap it.

Where the rules live
Open the maker portal, the solution, a table, and select Constraints in its menu. Each rule reads as a sentence. On Court:

And on Court booking:

Two buttons add a rule:
- No duplicates, then the field.
- No overlaps, then the start and end fields of the period, and the field that says what is booked. Leave that first box on the whole table when there's only one thing to book.
Select Save when you're done; the list is saved as a whole. The bin at the end of a line takes a rule off.
Files, images and lookups to several tables can't be part of a rule, since they don't hold a single value that can be compared. Platform fields such as Created On aren't offered either.
When the records already break the rule
We took the no-overlap rule off and saved. Then we booked Centre Court from 09:30 to 10:30 again, and this time it went through:

Then we put the rule back and saved:

A rule can only be added if the records already obey it. The rule stays on the screen, so you don't have to fill it in again. We deleted the double booking, selected Save again, and it was accepted.
Why the database and not a script
A business rule or a server script could look for a clashing booking before saving. But two saves a millisecond apart both look, both find nothing, and both write. A constraint is checked as the row is written, so one of them succeeds and the other is refused, every time. It holds the same for a form, a spreadsheet import, a script and a process.
The rules travel with the solution, and they're enforced in every environment it's imported into.
The rules as the tables store them
Each rule is a line in its table's document, under constraints. The two in the Court Bookings
sample:
"constraints": [
{ "kind": "Unique", "field": "par_code" }
]
"constraints": [
{ "kind": "NoOverlap", "field": "par_court", "startField": "par_starts", "endField": "par_ends" }
]
Read the second one as the sentence the Constraints page shows: no two records may overlap in
time, for each court, from Starts to Ends. Leave field out and the rule covers the whole table,
which is right when there is only one thing to book, such as a single meeting room or a single
vehicle.
What the database actually does
You never write SQL for a constraint, but knowing what it becomes explains every behaviour on this page.
No duplicates becomes a unique index on the column, for rows where the column has a value:
CREATE UNIQUE INDEX "uq_par_court_par_code"
ON par_court (par_code)
WHERE par_code IS NOT NULL;
The WHERE is why an empty code is never a duplicate. Without it, the first court with no code would
be fine and the second refused, which would quietly turn codes must be unique into a code is
required. Whether a code is required is a separate decision, made on the field.
No overlaps becomes an exclusion constraint. For each court, it treats every booking as a period from its start up to its end and refuses a row whose period overlaps another's. The period includes its start and excludes its end, which is why a booking that ends at 10:00 and one that starts at 10:00 sit side by side. That is what you want at every handover, every shift change and every change of price list.
Both are checked by PostgreSQL itself as the row is written. That is what makes them hold for two saves in the same millisecond: the database lets one through and refuses the other, and the platform turns the refusal into the sentence you saw on the form.
Good uses for each
No duplicates fits anything people type that must identify one record:
- product codes, SKUs and part numbers
- an employee number, a membership number or a tax reference
- an email address on a table of portal sign-ups
- the code of a cost centre or a budget line
No overlaps fits anything that occupies time:
- rooms, courts, desks and vehicles, scoped by the thing booked
- a person's shifts or leave, scoped by the person, so nobody is on two shifts at once
- price lists or tax rates with valid from and valid to, scoped by the product, so exactly one price applies on any date
- a contract's periods, scoped by the contract, so two terms never cover the same day
The last two are worth noticing. A rule against overlaps is as useful for what applied when as for bookings: it guarantees that any date has at most one answer.
Details that catch people out
- Make the start and the end required. A booking with no end is a period with no end, so it would overlap everything after it on that court. The sample makes both fields required, and you should too.
- Uniqueness is exact.
C1andc1are different values to the database. If people type codes, decide on capitals and say so in the field's description, or tidy the value with a server rule before it is written. - One field per uniqueness rule. A rule says that one field's values are unique across the table. If the combination of two fields must be unique, such as one enrolment per person per course, make a field that holds both, filled in by a server rule, and put the rule on that.
- Scope by a lookup, not by a name. The court in the booking rule is a lookup, so renaming Centre Court changes nothing. A rule scoped by a typed name would let Centre Court and Centre court be booked at the same time.
Telling people before they press Save
A constraint is the last line of defence, and it speaks only when somebody saves. On a busy form it helps to show the free slots before then. Two things work well alongside the rule:
- A calendar view of bookings, so people pick a gap rather than guess one. Bookings on a calendar puts the bookings of each court side by side.
- A server rule with a friendlier message, for when a clash should say who has the slot. The rule can query the overlapping booking and name it, while the constraint still guarantees the result if two people save at once. Query records from a script shows how a rule reads other records.
The rule and the constraint don't compete: the script explains, the database guarantees.
Rules in other environments
Constraints are part of the table, so they travel in the solution and are created in each environment it is imported into. The same condition applies there as on this page: the database can only create a rule that the records already obey. In a fresh environment that is never a question. In one that has been in use, check for duplicates or overlaps before importing the version that adds the rule, and fix them first. Moving work from Dev to Test to Prod covers imports, and Seed data that travels with a solution the rows a solution brings with it, which have to obey the rules too.
Questions that come up
Can I see which records break a rule before I add it? Make a view sorted by the field, or by the court and then the start, and the duplicates or overlaps sit next to each other. For a large table, a saved query grouped by the code with a count shows every code used more than once.
Does a refused save lose what I typed? No. The form keeps every value, shows the message at the top, and waits for you to change the clashing field and save again.
What does an import do with a row that breaks a rule? It refuses that row and carries on with the others. The import's results list each refused row with the reason, and you can download them, fix them and import them again. Import records from a spreadsheet shows the results.
Does a cancelled booking still block the slot? Yes, as long as the record exists, because the rule looks at every row of the table. Delete a cancelled booking to free its slot, or, if you need to keep cancellations, keep them in a separate table.
Try it
Book Court Two from 10:15 to 10:45. It overlaps Tom Reed's 09:30 to 10:30 on the same court, so it's refused. Move it to 10:30 and it saves.