SQL Basics: Statements

The main body of the SQL language consists of about 40 statements. The most important and frequently used statements are summarized in Table 5-1. Each statement requests a specific action from the DBMS, such as creating a new table, retrieving data, or inserting new data into the database. All SQL statements have the same basic form, illustrated in Figure 5-1.

Every SQL statement begins with a verb, a keyword that describes what the statement does. CREATE, INSERT, DELETE, and COMMIT are typical verbs. The statement continues with one or more clauses. A clause may specify the data to be acted on by the statement or provide more detail about what the statement is supposed to do. Every clause also begins with a keyword, such as WHERE, FROM, INTO, and HAVING. Some clauses are optional; others are required. The specific structure and content vary from one clause to another. Many clauses contain table or column names; some may contain additional keywords, constants, or expressions.

The ANSI/ISO SQL standard specifies the SQL keywords that are used as verbs and in statement clauses. According to the standard, these keywords cannot be used to name database objects, such as tables, columns, and users. Many SQL implementations relax this restriction, but it’s generally a good idea to avoid the keywords when you name your tables and columns. Table 5-2 lists the keywords included in the ANSI/ISO SQL2 standard, which roughly tripled the number of keywords reserved by the earlier SQL1 standard.

The SQL2 standard also includes a list of potential keywords that are candidates for future revisions of the standard. These keywords are listed in Table 5-3.

Throughout this book, the acceptable forms of a SQL statement are illustrated by a syntax diagram, such as the one shown in Figure 5-2. A valid SQL statement or clause is constructed by “following the line” through the syntax diagram to the dot that marks the end of the diagram. Keywords in the syntax diagram and in the examples (such as DELETE and FROM in Figure 5-2) are always shown in UPPERCASE, but almost all SQL implementations accept both uppercase and lowercase keywords, and it’s often more convenient to actually type them in lowercase.

Variable items in a SQL statement (such as the table name and search condition in Figure 5-2) are shown in lowercase italics. It’s up to you to specify the appropriate item each time the statement is used. Optional clauses and keywords, such as the WHERE clause in Figure 5-2, are indicated by alternate paths through the syntax diagram. When a choice of optional keywords is offered, the default choice (that is, the behavior of the statement if no keyword is specified) is UNDERLINED.

Source: Liang Y. Daniel (2013), Introduction to programming with SQL, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *