I’ll also talk about the features of this book, which will allow you to determine readily the SQL syntax for the specific database you’re using.. In common usage, the databases in these
Trang 1Databases and SQL
In this first chapter, I’m going to provide a bit of background that will allow you to get started quickly with the writing of SQL statements in subsequent chapters There are two general topics The first will be an overview of the data-bases covered in this book and some basic information on how those datadata-bases relate to the language of SQL I’ll also talk about the features of this book, which will allow you to determine readily the SQL syntax for the specific database you’re using
Second, I’m going to cover some of the key design features of relational data-bases We’ll talk about tables, rows, columns, keys, and datatypes After you have the basic information, you’ll be up and running in no time So, without further ado, let us begin
Language and Logic
I must begin with an admission that the title of this book is not entirely
appro-priate Although the book is entitled The Language of SQL, a more apt title might have been The Logic of SQL This is because, like all computer languages, the
language of SQL has much more to do with cold, hard logic than with English vocabulary
Nevertheless, there is a unique language-based syntax present in SQL that dis-tinguishes it from many other computer languages Unlike many programming tools, SQL employs ordinary English words such asWHERE, FROM, and HAVING
1
Trang 2as keywords in its syntax As a result, SQL is much less cryptic than other languages you might have seen
As such, after you become familiar with the language of SQL, you might find yourself thinking of SQL commands as being analogous to English sentences and having a certain expressive meaning
For example, compare this sentence:
I would like a hamburger and fries from your value menu,
and make it to go.
with this SQL statement:
Select city, state
from customers
order by state
I’ll get into the details later, but this SQL statement means that you want the city and state fields from a customer’s table in your database, and you want the results sorted by state
In both cases, you’re specifying which items you want (hamburger/fries or city/ state), where you want it from (value menu or customer’s table), and some extra instructions (make it to go or sort the results by state)
So an important goal of this book is to allow you to learn SQL as you would learn English, in a simple, intuitive way My approach will be to introduce one word at
a time, while building on the logical intent and meaning of the language
There is a second and perhaps less obvious meaning to the title of this book There
is often some confusion between the language of SQL and SQL databases There
are many software companies selling database management systems (DBMS) software In common usage, the databases in these types of software packages are
often referred to as SQL databases, since the SQL language is the primary means
of managing and accessing data in these databases Some vendors even use the
word SQL as part of the database name For example, Microsoft calls its latest DBMS SQL Server 2008.
But, in point of fact, SQL is more properly a language It is not really a database
My focus in this book will be on the language of SQL rather than on any parti-cular database
Chapter 1 ■ Relational Databases and SQL
2
Trang 3SQL Defined
So what is SQL? In a nutshell, SQL is a standard computer language for
main-taining and utilizing data in relational databases Put simply, SQL is a language
that lets users interact with relational databases It has a long history of
devel-opment by various organizations going back to the 1970s In 1986, the American
National Standards Institute (ANSI) published its first set of standards regarding
the language, and it has gone through several revisions since that time
Generally speaking, there are three major components of the SQL language The
first is called DML, or Data Manipulation Language This module of the language
allows you to retrieve, update, add, or delete data in a database The second
component is called DDL, or Data Definition Language DDL enables you to
create and modify the database itself For example, DDL providesALTER
state-ments, which let you modify the design of tables in a database Finally, the third
component, DCL, or Data Control Language, maintains proper security for the
database
Many of the major software vendors, such as Microsoft and Oracle, have adapted
the standard for their own purposes and have added numerous extensions and
modifications to the language But although each vendor has its own unique
interpretation of SQL, there is still an underlying base language, which is pretty
much the same for all vendors That is what we will be covering in this book
As a computer language, SQL is different than other languages you may be
familiar with, such as Visual Basic or C++ Other languages tend to be procedural
in nature That means that they allow you to specify particular procedures to
accomplish a desired task SQL is more of a declarative language In SQL, the
desired objective is often declared with a single statement The simpler structure
of SQL is possible since it is only concerned with relational databases rather than
the entirety of computer systems
Microsoft SQL Server, Oracle, and MySQL
Although my aim is to cover the core language of SQL as it applies to all
implementations, I will also provide specific examples of SQL syntax And since
syntax does vary somewhat among vendors, I’ve decided to focus on the SQL
syntax utilized by these three databases:
■ Microsoft SQL Server
Trang 4■ Oracle Database
■ MySQL
If there are any differences among these three databases, I’ll present the syntax for Microsoft SQL Server in the text of the book I’ll then indicate any differences for MySQL or Oracle in a box such as the following:
D A T A B A S E D I F F E R E N C E S
A box such as this one will appear whenever I present syntax differences for Oracle Database or MySQL The syntax for Microsoft SQL Server will appear in the main text.
The heading of the box will indicate if the note is for MySQL, Oracle, or both.
Microsoft SQL Server is available in several versions and editions The most recent
version is called Microsoft SQL Server 2008 Available editions run from a basic
Express edition to a fully featured Enterprise edition The Express edition is free but still has an abundance of features that allow you to get started with full-fledged database development The Enterprise edition includes many sophisticated data-base management features, plus sophisticated business intelligence components Oracle is also available in a number of editions The most recent version is called
Oracle Database 11g Like Microsoft, Oracle also offers a free Express edition of
its database
MySQL is an open source database, which means that no one single organization owns or controls its development Although MySQL was purchased by Sun Microsystems in 2008, it remains one of the top choices for open source software Sun Microsystems was later purchased by Oracle As an open source database, MySQL is available on numerous platforms other than Windows, such as Mac OS X and Linux MySQL offers its Community Edition as a free download
When starting out, it is sometimes useful to download the database of your choice, so you have something to experiment with However, this book does not require you to do that This book has been written in such a way as to allow you
to learn SQL simply by reading through the text I’ll provide enough data in the text that you can understand the results of various SQL statements without having to download software and type in statements yourself
Nevertheless, if you would like to download the free versions of any of these databases, the first three appendices at the back of this book have some useful
Chapter 1 ■ Relational Databases and SQL
4
Trang 5instructions and tips on how to do that Appendix A has complete information
on how to get started with Microsoft SQL Server The instructions include details
on how to install the software and execute SQL commands Similarly, Appendix
B covers MySQL, and Appendix C explains Oracle
In addition, Appendix D points to supplemental material that lists all the SQL
statements shown in this book in all three databases As mentioned, we show all
SQL statements in this book in the syntax of Microsoft SQL Server In most
cases, these statements also work in MySQL and Oracle, but there are occasional
differences
Most readers will find it completely unnecessary to download the software or view
the additional material shown in Appendix D The examples shown throughout
this book are self-explanatory and don’t require you to do anything else in order to
understand the material However, if you are so inclined, then feel free to take
advantage of these extra features
Other Databases
In addition to Microsoft SQL Server, Oracle, and MySQL, there are many other
SQL database software implementations Some of the more popular packages are
the following:
■ Microsoft Access from Microsoft
■ DB2 from IBM
■ Informix from IBM
■ SQL Anywhere from Sybase
■ PostgreSQL, which is an open source database
Of these listed databases, Microsoft Access is somewhat unique, and it’s
parti-cularly useful for SQL novices who want to learn the language In essence, Access
is a graphical interface for relational databases In other words, Access allows you
to create a query against a relational database entirely through graphical means
The useful aspect of Access for beginners is that you can easily create a query in a
visual way and then switch to a SQL view to see the SQL statement you just
created So you can try different things and quickly see what the corresponding
SQL syntax looks like