1
0
Fork 0
mirror of https://github.com/treffynnon/sqlstyle.guide.git synced 2025-03-09 12:49:51 -05:00

Break up constrainst section to enhance readability

This commit is contained in:
Simon Holywell 2015-07-09 10:10:02 +01:00
parent 08c2243b0e
commit f68681de04

View file

@ -298,7 +298,15 @@ changes to the definitions to keep them up to date.
Once the keys are decided it is possible to define them in the system using Once the keys are decided it is possible to define them in the system using
constraints. constraints.
* All tables must have at least one key to be useful. ##### General
* Tables must have at least one key to be complete and useful.
* Constraints should be given a custom name excepting `UNIQUE`, `PRIMARY KEY`
and `FOREIGN KEY` where the database vendor will generally supply sufficiently
intelligible names automatically.
##### Layout and order
* Specify the primary key first right after the `CREATE TABLE` statement. * Specify the primary key first right after the `CREATE TABLE` statement.
* Constraints should be defined directly beneath the column they correspond to. * Constraints should be defined directly beneath the column they correspond to.
Indent the constraint so that it aligns to the right of the column name. Indent the constraint so that it aligns to the right of the column name.
@ -307,10 +315,13 @@ constraints.
include them at the end of the `CREATE TABLE` definition. include them at the end of the `CREATE TABLE` definition.
* If it is a table level constraint that applies to the entire table then it * If it is a table level constraint that applies to the entire table then it
should also appear at the end. should also appear at the end.
* Use alphabetical order so `ON DELETE` comes before `ON UPDATE`. * Use alphabetical order where `ON DELETE` comes before `ON UPDATE`.
* All constraints should be given a custom name except `UNIQUE`, `PRIMARY KEY` * If it make sense to do so align each aspect of the query on the same character
and `FOREIGN KEY` where the database vendor will generally supply sufficiently position. For example all `NOT NULL` definitions should start at the same
intelligible names automatically. character position.
##### Validation
* Use `LIKE` and `SIMILAR TO` constraints to ensure the integrity of strings * Use `LIKE` and `SIMILAR TO` constraints to ensure the integrity of strings
where the format is known. where the format is known.
* Where the ultimate range of a numerical value is known it must be written as a * Where the ultimate range of a numerical value is known it must be written as a
@ -318,9 +329,8 @@ constraints.
truncation of data too large to fit the column definition. In the least it truncation of data too large to fit the column definition. In the least it
should check that the value is greater than zero in most cases. should check that the value is greater than zero in most cases.
* `CHECK()` constraints should be kept in separate clauses to ease debugging. * `CHECK()` constraints should be kept in separate clauses to ease debugging.
* If it make sense to do so align each aspect of the query on the same character
position. For example all `NOT NULL` definitions should start at the same ##### Example
character position.
```sql ```sql
CREATE TABLE staff ( CREATE TABLE staff (