Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed rivers and added more complex cases #5

Open
wants to merge 1 commit into
base: gh-pages
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions _includes/sqlstyle.guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ and `ORDER BY`should be fully left justified.
For single `SELECT`s, you can use the single line form:

```sql
SELECT first_name
SELECT first_name AS name
FROM rappers
```

Expand All @@ -177,7 +177,7 @@ spaces in a block after the `SELECT` keyword.
SELECT
first_name,
last_name,
is_still_tippin_on_four_fours,
is_still_tippin_on_four_fours AS tippin,
is_still_wrapped_in_four_vogues
FROM rappers
WHERE first_name = 'Mike'
Expand All @@ -202,7 +202,7 @@ SELECT
a.recording_date
FROM albums AS a
WHERE a.title = 'Charcoal Lane'
OR a.title = 'The New Danger';
OR a.title = 'The New Danger';
```

#### Line spacing
Expand All @@ -218,18 +218,48 @@ Always include newlines/vertical space:
large chunks of code.

Keeping all the dependent keywords (e.g. `ON` depends on `JOIN`, `AND` depends on `WHERE`,
`SET` depends on `UPDATE` right aligned with the top level keywords helps make it clear
`SET` depends on `UPDATE` indented after the top level keywords helps make it clear
all the lines are part of the same clause.

```sql
INSERT INTO albums (title, release_date, recording_date)
VALUES ('Charcoal Lane', '1990-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000'),
('The New Danger', '2008-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000');
VALUES
('Charcoal Lane', '1990-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000'),
('The New Danger', '2008-01-01 01:01:01.00000', '1990-01-01 01:01:01.00000');
```

```sql
INSERT INTO albums
(
title,
release_date,
recording_date
)
VALUES
(
'Charcoal Lane',
'1990-01-01 01:01:01.00000',
'1990-01-01 01:01:01.00000'
),
(
'The New Danger',
'2008-01-01 01:01:01.00000',
'1990-01-01 01:01:01.00000'
);
```

```sql
UPDATE albums
SET release_date = '1990-01-01 01:01:01.00000'
WHERE title = 'The New Danger';
```

```sql
UPDATE albums
SET release_date = '1990-01-01 01:01:01.00000'
SET
release_date = '1990-01-01 01:01:01.00000',
author = 'John Smith',
price = 123.45
WHERE title = 'The New Danger';
```

Expand All @@ -241,7 +271,7 @@ SELECT
a.production_date
FROM albums AS a
WHERE a.title = 'Charcoal Lane'
OR a.title = 'The New Danger';
OR a.title = 'The New Danger';
```

#### JOINs
Expand All @@ -253,7 +283,7 @@ Single line `JOIN`s are fine for simple situations
```sql
SELECT r.last_name
FROM riders AS r
INNER JOIN bikes AS b ON r.bike_vin_num = b.vin_num
JOIN bikes AS b ON r.bike_vin_num = b.vin_num
INNER JOIN crew AS c ON r.crew_chief_last_name = c.last_name
```

Expand All @@ -263,8 +293,8 @@ Multi line JOINs should be indented as per the dependent keywords rule:
SELECT r.last_name
FROM riders AS r
INNER JOIN bikes AS b
ON r.bike_vin_num = b.vin_num
AND r.bike_lane = r.lane
ON r.bike_vin_num = b.vin_num
AND r.bike_lane = r.lane
INNER JOIN crew c ON r.crew_chief_last_name = c.last_name
WHERE id = 5
```
Expand Down Expand Up @@ -513,12 +543,12 @@ constraints along with field value validation.

```sql
CREATE TABLE staff (
PRIMARY KEY (staff_num),
staff_num INT(5) NOT NULL,
first_name VARCHAR(100) NOT NULL,
pens_in_drawer INT(2) NOT NULL,
CONSTRAINT pens_in_drawer_range
CHECK(pens_in_drawer >= 1 AND pens_in_drawer < 100)
PRIMARY KEY (staff_num),
staff_num INT(5) NOT NULL,
first_name VARCHAR(100) NOT NULL,
pens_in_drawer INT(2) NOT NULL,
CONSTRAINT pens_in_drawer_range
CHECK(pens_in_drawer >= 1 AND pens_in_drawer < 100)
);
```

Expand Down