@@ -298,7 +298,15 @@ changes to the definitions to keep them up to date.
298
298
Once the keys are decided it is possible to define them in the system using
299
299
constraints.
300
300
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
+
302
310
* Specify the primary key first right after the ` CREATE TABLE ` statement.
303
311
* Constraints should be defined directly beneath the column they correspond to.
304
312
Indent the constraint so that it aligns to the right of the column name.
@@ -307,20 +315,22 @@ constraints.
307
315
include them at the end of the ` CREATE TABLE ` definition.
308
316
* If it is a table level constraint that applies to the entire table then it
309
317
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
+
314
325
* Use ` LIKE ` and ` SIMILAR TO ` constraints to ensure the integrity of strings
315
326
where the format is known.
316
327
* Where the ultimate range of a numerical value is known it must be written as a
317
328
range ` CHECK() ` to prevent incorrect values entering the database or the silent
318
329
truncation of data too large to fit the column definition. In the least it
319
330
should check that the value is greater than zero in most cases.
320
331
* ` 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
324
334
325
335
``` sql
326
336
CREATE TABLE staff (
0 commit comments