Skip to content

Commit f68681d

Browse files
committed
Break up constrainst section to enhance readability
1 parent 08c2243 commit f68681d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

_includes/sqlstyle.guide.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,15 @@ changes to the definitions to keep them up to date.
298298
Once the keys are decided it is possible to define them in the system using
299299
constraints.
300300

301-
* All tables must have at least one key to be useful.
301+
##### General
302+
303+
* Tables must have at least one key to be complete and useful.
304+
* Constraints should be given a custom name excepting `UNIQUE`, `PRIMARY KEY`
305+
and `FOREIGN KEY` where the database vendor will generally supply sufficiently
306+
intelligible names automatically.
307+
308+
##### Layout and order
309+
302310
* Specify the primary key first right after the `CREATE TABLE` statement.
303311
* Constraints should be defined directly beneath the column they correspond to.
304312
Indent the constraint so that it aligns to the right of the column name.
@@ -307,20 +315,22 @@ constraints.
307315
include them at the end of the `CREATE TABLE` definition.
308316
* If it is a table level constraint that applies to the entire table then it
309317
should also appear at the end.
310-
* Use alphabetical order so `ON DELETE` comes before `ON UPDATE`.
311-
* All constraints should be given a custom name except `UNIQUE`, `PRIMARY KEY`
312-
and `FOREIGN KEY` where the database vendor will generally supply sufficiently
313-
intelligible names automatically.
318+
* Use alphabetical order where `ON DELETE` comes before `ON UPDATE`.
319+
* If it make sense to do so align each aspect of the query on the same character
320+
position. For example all `NOT NULL` definitions should start at the same
321+
character position.
322+
323+
##### Validation
324+
314325
* Use `LIKE` and `SIMILAR TO` constraints to ensure the integrity of strings
315326
where the format is known.
316327
* Where the ultimate range of a numerical value is known it must be written as a
317328
range `CHECK()` to prevent incorrect values entering the database or the silent
318329
truncation of data too large to fit the column definition. In the least it
319330
should check that the value is greater than zero in most cases.
320331
* `CHECK()` constraints should be kept in separate clauses to ease debugging.
321-
* If it make sense to do so align each aspect of the query on the same character
322-
position. For example all `NOT NULL` definitions should start at the same
323-
character position.
332+
333+
##### Example
324334

325335
```sql
326336
CREATE TABLE staff (

0 commit comments

Comments
 (0)