Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMagnus Rydberg
4,683 PointsWhy are so many SQL keywords optional?
Watching the tutorials I have noticed s quite many situations where some SQL keywords are optional. In this particular example it is the COLUMN keyword.
Why is that? No other computer language I have come across so far seem to behave in this way.
My hunch is that is perhaps just for readability alone. SQL was designed to be accessible and close to English I understand.
But perhaps there are situations where having the optional keyword adds a necessary specificity. Lets say I use the ALTER COLUMN followed by a value that is not a column at all. Would that throw an error?
Or are there more complex commands where otherwise optional keywords are required?
I am really curious to know.
2 Answers
Andrew Sheragy
16,379 PointsIt was most likely just a convenience they left off after designing the rules for the syntax because its the most common operation. I don't think there is any more to it than that.
| DROP [COLUMN] col_name
| DROP PRIMARY KEY
| DROP {INDEX|KEY} index_name
| DROP FOREIGN KEY fk_symbol
If you leave out COLUMN its "DROP col_name" which is never ambiguous since all the remaining choices require 3 words so when the parser reaches that section it can assume you mean the first case. It could just as easily be index|key but column is used more often so they chose that. Another simple example is in math expressions, you can say 2*x or just 2x. You know its multiplication and not the others.
Andrew Sheragy
16,379 PointsI don't see any places its used outside of the ATLER TABLE syntax so it looks like it might be redundant in all cases. There may be some other keywords that are optional in some place but required in another. I personally would use them it makes the code more clear what its doing.
Other languages have optional keywords too. For example in Java or C if a function has no return value you can end it with or without "return". In C extern can also be optionally used in some cases
Magnus Rydberg
4,683 PointsI think I will also keep using the optional COLUMN keyword, if only to make the code clearer to my eyes. Thanks again for expanding my knowledge of coding.
Magnus Rydberg
4,683 PointsMagnus Rydberg
4,683 PointsHello Andrew and thanks for the answer.
Your answer clarifies some things but also further increases my curiosity. Which of course is a great thing!
The way I implicitly understand what computer code is, is that it deals with absolutes. Keywords either belong in the code or not. Code can be either efficient or unnecessarily complex depending on the coder, but optionality does not belong to coding at all. I may need to rethink this.
If Column is a convenience that was left off after designing the rules for the syntax, does this mean that the Column keyword is effectively redundant in all possible scenarios, or are there other possible SQL statements where it still would be required?
In your own code experience: Do you make use of optional keywords or do you skip them altogether?