3 ms·
An interesting read! I do follow certain rules when writing SQL, and I agree that having them and following them is a good idea. Plenty of what is in the articl
by cema 10y ago
An interesting read! I do follow certain rules when writing SQL, and I agree that having them and following them is a good idea. Plenty of what is in the article looks like good advice.
However, these rules do not always appear to be consistent with how the code in (most) other programming languages is written. Consider indentation, for example. The usual approach is to line up those elements of the code which correspond to the same logical level of the flow, whether it is C++, Javascript, or Lisp. So rather than (quoting from the article)
SELECT file_hash
FROM file_system
WHERE file_name = '.vimrc'
I would rather see either
SELECT file_hash
FROM file_system
WHERE file_name = '.vimrc'
or
SELECT file_hash
FROM file_system
WHERE file_name = '.vimrc'
(The difference here is the same as between indented and non-indented braces in C.) I think this is especially useful when we have multiple joins or inner queries.
Many suggested rules are indeed consistent with what I have seen to be accepted as best practices. Such as naming a table in singular (more precisely, giving it the name of what a single row corresponds to), or avoiding the infamous Hungarian notation (pretty much an accepted best practice in most languages that I have seen).
Overall, I think, a good first step towards building best practices.
- dahdum 10y agoIt's good advice, but the formatting shown is not conducive to quick editing imo. Here's your huckleberry: SELECT fs.id, fs.file_hash FROM file_system fs, other_table ot WHERE file_name = '.vimrc' AND fs.id = ot.file_system_id
- cema 10y agoYes, this is better. BTW, I saw people writing things like fs.id ,fs.file_hash and file_name = '.vimrc' AND fs.id = ot.file_system_id which has subtle advantages (such as easier editing). Also, I cannot bring myself to writing SQL keywords in all caps. It feels ancient. Just a matter of taste.