Skip to content

Commit 65b785e

Browse files
authored
Update guide-sql.asciidoc
1 parent ff1382e commit 65b785e

File tree

1 file changed

+36
-48
lines changed

1 file changed

+36
-48
lines changed

documentation/guide-sql.asciidoc

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,20 @@ For general guides on dealing or avoiding SQL, preventing SQL-injection, etc. yo
1010
Here we define naming conventions that you should follow whenever you write SQL files:
1111

1212
* All SQL-Keywords in UPPER CASE
13-
* Table names in upper CamelCase (e.g. `RestaurantOrder`)
14-
* Column names in CamelCase (e.g. `drinkState`)
1513
* Indentation should be 2 spaces as suggested by devonfw for every format.
1614

1715
=== DDL
18-
For DDLs follow these additional guidelines:
16+
The naming conventions for database constructs (tables, columns, triggers, constraints, etc.) should be aligned with your database product and their operators.
17+
However, when you have the freedom of choice and a modern case-sensitive database, you can simply use your code conventions also for database constructs to avoid explicitly mapping each and every property (e.g. `RestaurantTable` vs. `RESTAURANT_TABLE`).
1918

20-
* ID column names without underscore (e.g. `tableId`)
2119
* Define columns and constraints inline in the statement to create the table
2220
* Indent column types so they all start in the same text column
2321
* Constraints should be named explicitly (to get a reasonable hint error messages) with:
2422
** `+PK_{table}+` for primary key (name optional here as PK constraint are fundamental)
2523
** `+FK_{table}_{property}+` for foreign keys (`+{table}+` and `+{property}+` are both on the source where the foreign key is defined)
2624
** `+UC_{table}_{property}[_{propertyN}]*+` for unique constraints
2725
** `+CK_{table}_{check}+` for check constraints (`+{check}+` describes the check, if it is defined on a single property it should start with the property).
28-
* Databases have hard limitations for names (e.g. 30 characters). If you have to shorten names try to define common abbreviations in your project for according (business) terms. Especially do not just truncate the names at the limit.
26+
* Old RDBMS had hard limitations for names (e.g. 30 characters). Please note that recent databases have overcome this very low length limitations. However, keep your names short but precise and try to define common abbreviations in your project for according (business) terms. Especially do not just truncate the names at the limit.
2927
* If possible add comments on table and columns to help DBAs understanding your schema. This is also honored by many tools (not only DBA-tools).
3028

3129
Here is a brief example of a DDL:
@@ -34,62 +32,52 @@ Here is a brief example of a DDL:
3432
CREATE SEQUENCE HIBERNATE_SEQUENCE START WITH 1000000;
3533
3634
-- *** Table ***
37-
CREATE TABLE Table (
38-
id BIGINT NOT NULL AUTO_INCREMENT,
39-
modificationCounter INTEGER NOT NULL,
40-
seatsNumber INTEGER NOT NULL,
41-
CONSTRAINT PK_Table PRIMARY KEY(id)
35+
CREATE TABLE RESTAURANT_TABLE (
36+
ID NUMBER(19) NOT NULL,
37+
MODIFICATION_COUNTER INTEGER NOT NULL,
38+
SEATS INTEGER NOT NULL,
39+
CONSTRAINT PK_TABLE PRIMARY KEY(ID)
4240
);
43-
44-
-- *** UserRole ***
45-
CREATE TABLE UserRole (
46-
id BIGINT NOT NULL AUTO_INCREMENT,
47-
modificationCounter INTEGER NOT NULL,
48-
name VARCHAR (255),
49-
active BOOLEAN,
50-
CONSTRAINT PK_UserRole PRIMARY KEY(id)
51-
-- *** User ***
52-
CREATE TABLE User (
53-
id BIGINT NOT NULL AUTO_INCREMENT,
54-
modificationCounter INTEGER NOT NULL,
55-
username VARCHAR (255) NULL,
56-
password VARCHAR (255) NULL,
57-
email VARCHAR (120) NULL,
58-
idRole BIGINT NOT NULL,
59-
CONSTRAINT PK_User PRIMARY KEY(id),
60-
CONSTRAINT PK_User_idRole FOREIGN KEY(idRole) REFERENCES UserRole(id) NOCHECK
41+
COMMENT ON TABLE RESTAURANT_TABLE IS 'The physical tables inside the restaurant.';
42+
-- *** Order ***
43+
CREATE TABLE RESTAURANT_ORDER (
44+
ID NUMBER(19) NOT NULL,
45+
MODIFICATION_COUNTER INTEGER NOT NULL,
46+
TABLE_ID NUMBER(19) NOT NULL,
47+
TOTAL DECIMAL(5, 2) NOT NULL,
48+
CREATION_DATE TIMESTAMP NOT NULL,
49+
PAYMENT_DATE TIMESTAMP,
50+
STATUS VARCHAR2(10 CHAR) NOT NULL,
51+
CONSTRAINT PK_ORDER PRIMARY KEY(ID),
52+
CONSTRAINT FK_ORDER_TABLE_ID FOREIGN KEY(TABLE_ID) REFERENCES RESTAURANT_TABLE(ID)
6153
);
62-
COMMENT ON TABLE User is 'The users of the restaurant site';
54+
COMMENT ON TABLE RESTAURANT_ORDER IS 'An order and bill at the restaurant.';
6355
...
6456
--------
6557

58+
ATTENTION: Please note that `TABLE` and `ORDER` are reserved keywords in SQL and you should avoid using such keywords to avoid problems.
59+
6660
=== Data
6761
For insert, update, delete, etc. of data SQL scripts should additionally follow these guidelines:
6862

6963
* Inserts always with the same order of columns in blocks for each table.
70-
* Insert column values always starting with id, modificationCounter, [dtype, ] ...
64+
* Insert column values always starting with ID, MODIFICATION_COUNTER, [DTYPE, ] ...
7165
* List columns with fixed length values (boolean, number, enums, etc.) before columns with free text to support alignment of multiple insert statements
72-
* Pro Tip: Get familiar with column mode of `+notepad+++` when editing large blocks of similar insert statements.
73-
//Updated with current example from the application
74-
//MyThaiStar.java.mtsj.core.src.main.resources.db.migration.V0005__R001_Master_data.sql
66+
* Pro Tip: Get familiar with column mode of advanced editors such as `+notepad+++` when editing large blocks of similar insert statements.
7567

68+
[source,sql]
7669
--------
77-
INSERT INTO UserRole(id, modificationCounter, name, active) VALUES (0, 1, 'Customer', true);
78-
INSERT INTO UserRole(id, modificationCounter, name, active) VALUES (1, 1, 'Waiter', true);
79-
INSERT INTO User(id, modificationCounter, username, password, email, idRole) VALUES (0, 1, 'user0', 'password', '[email protected]', 0);
80-
INSERT INTO User(id, modificationCounter, username, password, email, idRole) VALUES (1, 1, 'waiter', 'waiter', '[email protected]', 1);
81-
82-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (0, 1, 4);
83-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (1, 1, 4);
84-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (2, 1, 4);
85-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (3, 1, 4);
86-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (4, 1, 6);
87-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (5, 1, 6);
88-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (6, 1, 6);
89-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (7, 1, 8);
90-
INSERT INTO Table(id, modificationCounter, seatsNumber) VALUES (8, 1, 8);
70+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (0, 1, 4);
71+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (1, 1, 4);
72+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (2, 1, 4);
73+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (3, 1, 4);
74+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (4, 1, 6);
75+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (5, 1, 6);
76+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (6, 1, 6);
77+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (7, 1, 8);
78+
INSERT INTO RESTAURANT_TABLE(ID, MODIFICATION_COUNTER, SEATS) VALUES (8, 1, 8);
9179
...
9280
--------
9381

9482

95-
See also link:guide-database-migration.asciidoc[Database Migrations].
83+
See also link:guide-database-migration.asciidoc[Database Migrations].

0 commit comments

Comments
 (0)