1. Trang chủ
  2. » Công Nghệ Thông Tin

The Language of SQL- P6 pptx

5 324 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 130,23 KB

Nội dung

In the 1970s, advances were made as hierarchical and network databases were invented and utilized. These newer databases, through an elaborate system of internal pointers, made it easier to read through data. For example, a program could read a record for a customer, automatically be pointed to all orders for that customer, and then be pointed to all details for each order. But it was basically still the case that data needed to be processed one record at a time. The main problem with data storage prior to relational databases was not how the data was stored, but how it was accessed. The real breakthrough with rela- tional databases came when the language of SQL was developed, because it allowed for an entirely new method of accessing data. Unlike earlier data retrieval methods, SQL permitted the user to access a large set of data at a time. With one single statement, a SQL command could retrieve or update thousands of records from multiple tables. This eliminated a great deal of complexity. Computer programs no longer needed to read one record at a time in a special sequence, while deciding what to do with each record. What used to require hundreds of lines of programming code could now be accomplished with just a few lines of logic. Looking Ahead This chapter has provided enough background information about relational databases so that you can move on to the main topic, which involves retrieving data from databases. We have discussed a number of important characteristics of relational databases, such as primary keys, foreign keys, and datatypes. We also have talked about the possible existence of NULL values in data. We will add to our discussion of NULL values in Chapter 8 and return to the general topics of database maintenance in Chapter 18 and database design in Chapter 19. Why is the all-important topic of database design held off until much later in the book? In the real world, databases are designed and created before any data retrieval is attempted. Why would I not follow the same sequence of events in this book? In short, I have found that it is much more productive to plunge into using SQL without having to worry about details of database design. In truth, database design is as much an art as it is a science. As such, the principles of database design will be much more meaningful after you’re more aware of the details and nuances of retrieving some data. So we’re going to temporarily ignore the question of how to design a database and jump right into data retrieval in our very next chapter. Looking Ahead 11 This page intentionally left blank chapter 2 Basic Data Retrieval Keywords Introduced: SELECT, FROM In this chapter, we are going to begin our exploration of the most important topic in SQL: how to retrieve data from a database. Whether you’re in a large or small organization, the most common request made of SQL developers is the request for a report. Of course, it’s a nontrivial exercise to get data into a data- base. But once data is in a database, the energies of business analysts turn to the wealth of data at their disposal and the desire to extract useful information from all that data. This is where the fun and usefulness of SQL begins. The emphasis in this book on data retrieval corresponds nicely to the real-world demands that are placed on SQL de velopers. Your typical analysts don’t care about how data gets into a database, but they do care about how to get some- thing out of it. Your knowledge of SQL will go a long way toward helping your organization unlock the secrets of the dat a stored in their databases. A Simple SELECT The ability to retrieve data in SQL is accomplished through something called the SELECT statement. Without a lot of preliminary explanation, here is an example of the simplest possible SELECT statement: SELECT * FROM Customers In the SQL language, as in all computer languages, certain words are keywords. These words have a special meaning and must be used in a particular way. In this 13 statement, the words SELECT and FROM are keywords. The SELECT keyword indicates that you are beginning a SELECT statement. The FROM keyword is used to designate the table from which data is to be retrieved. The name of the table follows the FROM. In this case, the table name is Customers. As is the custom, I will print keywords in all capital letters. This is done to ensure that they are noticeable. The asterisk (*) in this example is a special symbol that means ‘‘all columns.’’ So to sum up, the statement means: Select all columns from the Customers table. If the Customers table looks like this: CustomerID FirstName LastName 1 William Smith 2 Natalie Lopez 3 Brenda Harper then this SELECT will return the following data: CustomerID FirstName LastName 1 William Smith 2 Natalie Lopez 3 Brenda Harper In other words, it brings back everything in the table. In the first chapter, I mentioned that it’s a common practice to specify a primary key for all tables. In the previous example, the CustomerID column is such a column. I also mentioned that primary keys are sometimes set up to generate sequential numbers automatically in a numeric sequence as rows are added to a table. This is the case in the previous example. Most of the sample data I’ll show throughout this book will show a similar column that is both a primary key and defined as auto-increment. By convention, this is generally the first column in a table. Chapter 2 ■ Basic Data Retrieval14 Syntax Notes Two points must be remembered when writing any SQL statement. First, the keywords in SQL are not case sensitive. The word SELECT is treated identically to ‘‘select’’ or ‘‘Select.’’ Second, a SQL statement can be written on any number of lines. For example, the SQL statement: SELECT * FROM Customers is identical to: SELECT * FROM Customers It’s usually a good idea to begin each important keyword on a separate line. When we get to more complex SQL statements, this will make it easier to quickly grasp the meaning of the statement. Finally, as I present different SQL statements in this book, I will often show both a specific example and a more general format. For instance, the general format of the previous statement would be shown as this: SELECT * FROM table Italics are used to indicate a general expression. The italicized word table means that you can substitute any table name of your own in that spot. So when you see italicized words in any SQL statement in this book, that is simply my way of saying that you can put any valid word or phrase in that location. DATABASE DIFFERENCES: MySQL and Oracle Many SQL implementations require a semicolon (;) at the end of every statement. This is true of MySQL and Oracle, but not of Microsoft SQL Server. For simplicity, I will show SQL statements without semicolons in this book. If you’re using MySQL or Oracle, you’ll need to add a semicolon to the end of your statements. Therefore, the previous statement would appear as: SELECT * FROM Customers; Syntax Notes 15 . such, the principles of database design will be much more meaningful after you’re more aware of the details and nuances of retrieving some data. So we’re going to temporarily ignore the question of. report. Of course, it’s a nontrivial exercise to get data into a data- base. But once data is in a database, the energies of business analysts turn to the wealth of data at their disposal and the. database, but they do care about how to get some- thing out of it. Your knowledge of SQL will go a long way toward helping your organization unlock the secrets of the dat a stored in their databases. A

Ngày đăng: 05/07/2014, 05:20

TỪ KHÓA LIÊN QUAN