"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
About SQLSQL is:
A programming language . SQL is a formal language in which you write programs to create, modify, and query databases. Your database system executes your SQL program, performs the tasks you''ve specified, and displays the results (or an error message). Programming languages differ from natural (spoken) languages in that programming languages are designed for a specific purpose, have a small vocabulary, and are inflexible and utterly unambiguous. Consequently, if you don''t get the results you expect, it''s because your program contains an erroror bugand not because the computer misinterpreted your instructions. (Debugging one''s programs is a cardinal programming task.)
Easy compared with other programming languages, that is. If you''ve never written a program before, you''ll find the transition from natural to formal language frustrating. Still, SQL''s statements read like sentences to make things easy on humans. A novice programmer probably would understand the SQL statement SELECT au_fname, au_lname FROM authors ORDER BY au_lname; to mean "List the authors'' first and last names, sorted by last name," whereas the same person would find the equivalent C or Perl program impenetrable.Nonprocedural . If you''ve never programmed, you can skip this point without loss of continuity. If you''ve programmed in a language such as C or Perl, you''ve used a procedural language, in which you specify the explicit steps to follow to produce a result. SQL is a declarative language, in which you describe what you want and not how to do it; your database system''s optimizer will determine the "how." As such, standard SQL lacks traditional control-flow constructs such as if-then-else, while, for, and goto statements.To demonstrate this difference, I''ve written programs that perform an equivalent task in Microsoft Access Visual Basic (VB; a procedural language) and SQL. Listing i.1 shows a VB program that extracts author names from a table that contains author information. You needn''t understand the entire program, but note that it uses a Do Until loop to define explicitly how to extract data. Listing i.2 shows how to do the same task with a single SQL statement (as opposed to about 20 lines of VB code). With SQL, you specify only what needs to be accomplished; the DBMS determines and performs internally the actual step-by-step operations needed to get the result. Listing i.1. This Microsoft Access Visual Basic routine extracts the first and last names from a table containing author information and places the results in an array.![]() Listing i.2. This single SQL statement performs the query as the Visual Basic routine in Listing i.1. Access''s internal optimizer determines the best way to extract the data.![]() In interactive SQL, you issue SQL commands directly to your DBMS, which displays the results as soon as they''re produced. DBMS servers come with both graphical and command-line tools that accept typed SQL statements or text files that contain SQL programs (scripts).If you''re developing database applications, you can "embed" SQL statements in programs written in a host language, which commonly is a general-purpose language (C++, Java, or COBOL, for example) or a scripting language (Perl, PHP, or Python). A PHP CGI script can use an SQL statement to query a MySQL database, for example; MySQL will pass the query result back to a PHP variable for further analysis or web-page display. Drawing from the preceding examples, I''ve included an SQL statement in an Access Visual Basic program in Listing i.3 . Listing i.3. Here, Visual Basic serves as the host language for embedded SQL.![]() SQL isn''t "owned" by any particular firm. It''s an open standard defined by an international standards working group, under the joint leadership of the International Organization for Standardization (ISO) and the International Engineering Consortium (IEC). The American National Standards Institute (ANSI) participates in the working groups and has ratified the standard. "ISO/IEC SQL" isn''t a commonly used term, so I''ll stick to the better-known "ANSI SQL" name throughout this book. This book is based on the 2003 SQL standard, so you should consider ANSI SQL, SQL:2003, and SQL to be synonymous unless I note otherwise. For more information, see "SQL Standards and Conformance" in Chapter 3.All DBMS vendors add proprietary features to standard SQL to enhance the language. These extensions usually are additional commands, keywords, functions, operators, data types, and control-flow constructs such as if, while, and goto statements. Microsoft, Oracle, and IBM have added so many features to standard SQL that the resulting languagesTransact-SQL, PL/SQL, and SQL PL, respectivelycan be considered to be separate languages in their own right, rather than just supersets of SQL. One vendor''s extensions generally are incompatible with other vendors'' products. I don''t cover proprietary SQL extensions, but I do point out when a vendor''s SQL dialect doesn''t comply with the standard SQL examples in this book; see "Using SQL with a specific DBMS" later in this chapter.Used to change data and database objects. SQL statements are divided into three categories:
Pronounced es-kyoo-el . SQL isn''t pronounced sequel; that pronunciation is a historical artifact. Avoid the error and articulate each letter: S -Q -L. I disagree with people who claim that saying sequel is so common that nothing''s wrong with it. You shouldn''t say sequel for the same reason that you shouldn''t split infinitives in your novel; it generally may be accepted, but it will rasp on the ears of knowledgeable people. Also, pronounce MySQL as my-es-kyoo-el and PostgreSQL as post-gres-kyoo-el (particularly if you''re on a job interview).Not an acronym . It''s a common misconception that SQL stands for structured query language; it stands for SQL and nothing else. Why? Because ANSI says so. The official name is Database Language SQL (see Figure i.1 ). Furthermore, referring to it as a structured query language is a disservice to new SQL programmers. It amuses insiders to point out that "structured query language" is the worst possible description, because SQL:
Figure i.1. This is the cover ofISO /IEC 9075:2003, which defines the SQL:2003 language officially. You can purchase it in electronic format at www.ansi.org or www.iso.org if you like. Its intended audience is not SQL programmers, however, but people who design and program DBMS products themselves.![]() |



