SQL Coding Standards: Difference between revisions
From iSchool Reference
(Formatted "SQL Coding Standards" page) |
(Update standards to require underscores instead of camel case) |
||
(5 intermediate revisions by the same user not shown) | |||
Line 6: | Line 6: | ||
=== Relational Notation Standards: === | === Relational Notation Standards: === | ||
* Relation (entity) names must be in UPPERCASE; multi-word relations are separated by an underscore | * Relation (entity) names must be in UPPERCASE; multi-word relations are separated by an underscore.<br>Examples:<br>  <code>STUDENT(…)</code><br>  <code>STUDENT_ADVISOR(…)</code><br><br> | ||
* Relation names are singular | * Relation names are singular.<br>Example:<br>  <code>STUDENT(…)</code>, not <code>STUDENTS(…)</code><br><br> | ||
* Primary keys are underlined | * Primary keys are underlined.<br>Example:<br>  <code>STUDENT(<u>univ_id</u>, …)</code><br><br> | ||
* Foreign key are italicized when typed, dash-underlined when written Example: | * Foreign key are italicized when typed, dash-underlined when written <br>Example:<br>  <code>STUDENT(<u>univ_id</u>, ''college_id'', …)</code><br><br> | ||
* Attribute names that would otherwise include a space | * Attribute names that would otherwise include a space as a separator must include an underscore instead.<br>Example:<br>  <code>STUDENT(univ_id, ''college_id'', first_name, last_name, expected_grad_year, …)</code> | ||
=== Implementation (Coding) Standards: === | === Implementation (Coding) Standards: === | ||
* One clause per line (<code>SELECT</code>, <code>FROM</code>, <code>WHERE</code>, etc.) | * One clause per line (<code>SELECT</code>, <code>FROM</code>, <code>WHERE</code>, etc.)<br>Example: | ||
SELECT | SELECT univ_id, first_name, last_name | ||
FROM student | FROM student | ||
WHERE city = 'Rochester' AND state = 'NY'; | WHERE city = 'Rochester' AND state = 'NY'; | ||
* Keywords and data types must be in UPPERCASE Example: | * Keywords and data types must be in UPPERCASE <br>Example: | ||
CREATE TABLE test ( | CREATE TABLE test ( | ||
test_id CHAR(9), | |||
test_count INT, | |||
test_desc VARCHAR(255), | |||
CONSTRAINT test_pk PRIMARY KEY( | CONSTRAINT test_pk PRIMARY KEY(test_id) | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; | ||
* Table names and attribute names that would otherwise include | * Table names and attribute names that would otherwise include a space as a separator must include an underscore instead. Don't use camel case to separate words, since capital letters in identifiers cause problems with Postgres, SQLite, and Oracle (even though they're fine in MySQL/MariaDB). E.g., in Postgres, if an identifier contains a capital letter, any time that identifier is referenced, it must be written in double quotes. In Oracle, even if identifiers are created with camel case, the database reports them as all caps.<br>Example: | ||
SELECT | SELECT univ_id, college_id, first_name, last_name | ||
* Single quotes for a string literal Double quotes do not work for string literals in all DBMSs | * Single quotes for a string literal. Double quotes do not work for string literals in all DBMSs.<br>Example: | ||
WHERE city = 'Rochester' AND state = 'NY' | WHERE city = 'Rochester' AND state = 'NY' | ||
* Double quotes for any alias that includes a space Example: | * Double quotes for any alias that includes a space <br>Example: | ||
SELECT | SELECT univ_id, first_name "First Name", last_name "Last Name" | ||
* Script submission must include a syntactically correct comment with student’s name and a syntactically correct comment identifying each task number Comment options include: | * Script submission must include a syntactically correct comment with student’s name and a syntactically correct comment identifying each task number. Comment options include: | ||
-- single line comment (a space MUST be included after --) | -- single line comment (a space MUST be included after --) | ||
# single line comment | # single line comment | ||
/* block comment (can span multiple lines) */ | /* block comment (can span multiple lines) */ | ||
* Script submission must be an executable script file (i.e. only comments and SQL statements). Common violations: | * Script submission must be an executable script file (i.e. only comments and SQL statements). Common violations: | ||
Line 44: | Line 42: | ||
=== Suggestions: === | === Suggestions: === | ||
* Include whitespace around operators Example: | * Include whitespace around operators <br>Example:<br>  <code>city = 'Rochester'</code>, not <code>city='Rochester'</code> | ||
* Prefix column names with a table identifier | * Prefix column names with a table identifier<br>Example, in table student:<br>  <code>stu_univ_id</code> <br>  <code>stu_first_name</code> <br>  <code>stu_last_name</code> <br>  <code>stu_maj_id</code> | ||
* Indent each hierarchical level Example: | * Indent each hierarchical level <br>Example: | ||
SELECT | SELECT stu_univ_id, stu_first_name, stu_last_name, stu_maj_id | ||
FROM student | FROM student | ||
JOIN major | JOIN major | ||
ON | ON stu_maj_id = maj_id | ||
AND | AND maj_active = TRUE | ||
WHERE | WHERE stu_city = 'Rochester' | ||
AND | AND stu_state = 'NY' | ||
AND | AND stu_year_lvl >= 3; |
Latest revision as of 11:58, 11 August 2025
Any place that you work in the technology industry typically has standards for how code is written, which you are expected to follow. This course is no different.
We will be requiring (and enforcing) the following standards on homework assignments (HWs), practice exercises (PEs), zyLabs, and examinations. In all cases, these are not part of the total points, but rather result in additional deductions (1 point for any occurrence per each item, with up to 7 points deducted if none are followed).
Relational Notation Standards:
- Relation (entity) names must be in UPPERCASE; multi-word relations are separated by an underscore.
Examples:
STUDENT(…)
STUDENT_ADVISOR(…)
- Relation names are singular.
Example:
STUDENT(…)
, notSTUDENTS(…)
- Primary keys are underlined.
Example:
STUDENT(univ_id, …)
- Foreign key are italicized when typed, dash-underlined when written
Example:
STUDENT(univ_id, college_id, …)
- Attribute names that would otherwise include a space as a separator must include an underscore instead.
Example:
STUDENT(univ_id, college_id, first_name, last_name, expected_grad_year, …)
Implementation (Coding) Standards:
- One clause per line (
SELECT
,FROM
,WHERE
, etc.)
Example:
SELECT univ_id, first_name, last_name FROM student WHERE city = 'Rochester' AND state = 'NY';
- Keywords and data types must be in UPPERCASE
Example:
CREATE TABLE test ( test_id CHAR(9), test_count INT, test_desc VARCHAR(255), CONSTRAINT test_pk PRIMARY KEY(test_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- Table names and attribute names that would otherwise include a space as a separator must include an underscore instead. Don't use camel case to separate words, since capital letters in identifiers cause problems with Postgres, SQLite, and Oracle (even though they're fine in MySQL/MariaDB). E.g., in Postgres, if an identifier contains a capital letter, any time that identifier is referenced, it must be written in double quotes. In Oracle, even if identifiers are created with camel case, the database reports them as all caps.
Example:
SELECT univ_id, college_id, first_name, last_name
- Single quotes for a string literal. Double quotes do not work for string literals in all DBMSs.
Example:
WHERE city = 'Rochester' AND state = 'NY'
- Double quotes for any alias that includes a space
Example:
SELECT univ_id, first_name "First Name", last_name "Last Name"
- Script submission must include a syntactically correct comment with student’s name and a syntactically correct comment identifying each task number. Comment options include:
-- single line comment (a space MUST be included after --) # single line comment /* block comment (can span multiple lines) */
- Script submission must be an executable script file (i.e. only comments and SQL statements). Common violations:
- Submitting a log file
- Including the MySQL prompt along with the statement
- Including an uncommented result set
Suggestions:
- Include whitespace around operators
Example:
city = 'Rochester'
, notcity='Rochester'
- Prefix column names with a table identifier
Example, in table student:
stu_univ_id
stu_first_name
stu_last_name
stu_maj_id
- Indent each hierarchical level
Example:
SELECT stu_univ_id, stu_first_name, stu_last_name, stu_maj_id FROM student JOIN major ON stu_maj_id = maj_id AND maj_active = TRUE WHERE stu_city = 'Rochester' AND stu_state = 'NY' AND stu_year_lvl >= 3;