课程: SQL Essential Training

Query composition best practices - SQL教程

课程: SQL Essential Training

Query composition best practices

- [Instructor] Before we add on to what we have just learned with regard to our basic SQL statement, let's point out some things that may not have been that obvious. So in the composition of our SQL statement, we've done a few particular things that I wanted to draw to your attention. So I started with the FROM clause and what this does is point me in the direction of where the data is. So FROM Customer effectively says, go to the Customer table. And by putting SELECT up here and specifying the columns, it's effectively saying, well now that I'm at the Customer table, I'd like to see the first name, last name and email address displayed. And that's exactly what has happened. So let's talk a little bit about how this statement is structured or laid out. We have some indentation taking place and this way of formatting a SQL statement is what's considered part of that best practice. The same best practice that has us putting a comment before we start composing any SQL statement. There is nothing preventing this statement from being written all in one line. This will be perfectly legal. There'll be no syntax errors, if you put SELECT FirstName, LastName, Email FROM Customer all in one line. Nothing stops you from doing this, except a best practice. So why don't we put the sequel statement even though it works in one line? It seems like it might save some space. Well, the reason for this is readability. It's important the way that this syntax is laid out for the human eye. When it's laid out like this, we can clearly tell the distinct clauses, the SELECT clause, the FROM clause. We can clearly tell what's housed within each of those clauses. The SELECT houses three fields, the FROM has one particular table listed and this is very readable for not only ourselves who compose this SQL statement, but for others who's going to actually inherit this SQL statement from us. It also holds true for SQL statements that we'll inherit from others. When it's formatted according to this best practice, it helps us be that much more of a professional practitioner of SQL. Helps us to read what we have composed that much more clearly and helps others who actually are going to inherit our work after us. So these are some important points that's worth pausing to point out and it is something that you should employ early in your learning journey of SQL.

内容