Cheatography
https://cheatography.com
DB rules for out projects
General rules
Engels |
Unieke naam zonder gereserveerde MSSQL keywords |
Namen maximaal 30 karakters lang |
snake_casing |
SQL keywoorden altijd met hoofdletters (SELECT, WHERE) |
Altijd AS gebruiken bij aliassen |
Aliassen volledig uitschrijven, niet één letter |
Joins altijd specificeren welke join het is |
Joins sorteren op volgorde: LEFT, RIGHT, INNER |
Where clause altijd op een nieuwe regel |
FROM altijd maar op één tabel |
Select
GOOD:
SELECT
projects.name AS project_name,
COUNT(backings.id) AS backings_count
...
BAD:
SELECT
projects.name project_name,
COUNT(backings.id) backings_count
... |
Where
GOOD:
SELECT
name,
goal
FROM projects AS projects
WHERE
country = 'US'
AND deadline >= '2015-01-01'
...
BAD:
SELECT
name,
goal
FROM projects AS projects
WHERE country = 'US' AND deadline >= '2015-01-01'
... |
|
|
Tables
Namen in meervoud |
Geen prefix |
Koppeltabel niet table1_table2, correcte naam geven |
Achtervoegsels
_id |
unieke identifier |
_total |
totaal of som van andere veden |
_number |
voor numerieke velden |
_name |
voor naamvelden |
_date |
datumvelden |
Join Sorting
GOOD:
INNER JOIN backings AS backings ON ...
INNER JOIN users AS users ON ...
INNER JOIN locations AS locations ON ...
LEFT JOIN backer_rewards AS backer_rewards ON ...
LEFT JOIN ...
BAD:
LEFT JOIN backer_rewards AS backer_rewards ON backings
INNER JOIN users AS users ON ...
LEFT JOIN ...
INNER JOIN locations AS locations ON ... |
From
GOOD:
SELECT
projects.name AS project_name,
COUNT(backings.id) AS backings_count
FROM projects AS projects
INNER JOIN backings AS backings ON backings.project_id = projects.id
...
BAD:
SELECT
projects.name AS project_name,
COUNT(backings.id) AS backings_count
FROM projects AS projects, backings AS backings
WHERE
backings.project_id = projects.id
... |
|
|
Columns
Enkelvoudige naam |
Id velden: tabelnaam_id |
Geen column met de naam van de tabel |
Foreign key velden: tabelnaam_veldnaam |
Stored procedures
Prefix sp_ |
Naam moet een werkwoord/handeling bevatten |
Comments alleen indien echt nodig, deze dan boven de select zetten |
Join Specificeren
GOOD:
SELECT
projects.name AS project_name,
COUNT(backings.id) AS backings_count
FROM projects AS projects
INNER JOIN backings AS backings ON ...
INNER JOIN ...
LEFT JOIN backer_rewards AS backer_rewards ON ...
LEFT JOIN ...
BAD:
SELECT
projects.name AS project_name,
COUNT(backings.id) AS backings_count
FROM projects AS projects
JOIN backings AS backings ON ...
LEFT JOIN backer_rewards AS backer_rewards ON ...
LEFT JOIN ... |
Case
GOOD:
CASE WHEN category = 'Art'
THEN backer_id
ELSE NULL
END
BAD:
CASE
WHEN category = 'Art'
THEN backer_id
ELSE NULL
END |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets