What SQL Is and Isn’t ?
SQL is a flexible language that you can use in a variety of ways. It’s the most widely used tool for communicating with a relational database.
In this chapter, I explain what SQL is and isn’t — specifically, what distinguishes SQL from other types of computer languages. Then I introduce the commands and data types that standard SQL supports and explain key concepts: null values and constraints. Finally, I give an overview of how SQL fits into the client/server environment, as well as the Internet and organizational intranets.
What SQL Is and Isn’t
The first thing to understand about SQL is that SQL isn’t a procedural language, as are BASIC, C, C++, C#, and Java. To solve a problem in one of those procedural languages, you write a procedure that performs one specific operation after another until the task is complete. The procedure may be a linear sequence or may loop back on itself, but in either case, the programmer specifies the order of execution.
SQL, on the other hand, is nonprocedural. To solve a problem using SQL, simply tell SQL what you want (as if you were talking to Aladdin’s genie) instead of telling the system how to get you what you want. The database management system (DBMS) decides the best way to get you what you request.
All right. I just told you that SQL is not a procedural language. This is essentially true. However, millions of programmers out there (and you are probably one of them) are accustomed to solving problems in a procedural manner. So, in recent years, there has been a lot of pressure to add some procedural functionality to SQL. Thus, SQL now incorporates procedural language facilities,
such as BEGIN blocks, IF statements, functions, and procedures. These facilities have been added so you can store programs at the server, where multiple clients can use these programs repeatedly.
To illustrate what I mean by “tell the system what you want,” suppose that you have an EMPLOYEE table and you want to retrieve from that table the rows that correspond to all your senior people. You want to define a senior person as anyone older than age 40 or anyone earning more than $60,000 per year. You can make the desired retrieval by using the following query:
SELECT * FROM EMPLOYEE WHERE Age>40 OR Salary>60000 ; This statement retrieves all rows from the EMPLOYEE table where either the value in the Age column is greater than 40 or the value in the Salary column is greater than 60,000. In SQL, you don’t need to specify how the information is retrieved. The database engine examines the database and decides for
itself how to fulfill your request. You need only to specify what data you want to retrieve.
A query is a question you ask the database. If any of the data in the database satisfies the conditions of your query, SQL retrieves that data. Current SQL implementations lack many of the basic programming constructs fundamental to most other languages. Real-world applications usually require at least some of these programming constructs, which is why SQL is actually a
data sublanguage. Even with the extensions that were added in 1999, 2003, and 2005, you still need to use SQL in combination with a procedural language, such as C, to create a complete application.
You can extract information from a database in one of two ways:
Make an ad hoc query from a computer console by just typing an SQL statement and reading the results from the screen. Console is the traditional term for the computer hardware that does the job of the keyboard and screen used in current PC-based systems. Queries from the console
are appropriate when you want a quick answer to a specific question. To meet an immediate need, you may require information that you never needed before from a database. You’re likely never to need that information again either, but you need it now. Enter the appropriate SQL query statement from the keyboard, and in due time, the result appears on your screen.
Execute a program that collects information from the database and then reports on the information, either on-screen or in a printed report. Incorporating an SQL query directly into a program is a good way to run a complex query that you’re likely to run again in the future.
That way, you can formulate a query just once for use as often as you want. Chapter 15 explains how to incorporate SQL code into programs written in another language.

0 comments:
Post a Comment