Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1
you would then use the first name to determine in what order the names should come. This is exactly
how the ORDER BY clause treats multiple column names. It will sort by the first column and, if that
column has identical values it, will then shift to the second column and compare those values to
determine placement in the resultset.

Summary


SQL is a powerful tool that was originally developed by IBM for the average person to access relational
databases. SQL is the language that databases understand. It has a syntax and a vocabulary that it can use
to communicate. Each reserved word in SQL has a certain meaning and performs a particular function.
You have covered a lot of ground today. Remember that this is just a basic overview of the SQL
language. It is not complete, nor was it intended to be. It is in this book to provide you with a means of
manipulating your data and building a base on which you can build. Later lessons will explore the
interface APIs that will use what you have learned today to build reports and support applications. The
hardest part of SQL is not the language or syntax but the logic that is used to gather the data from the
database. Take the time to learn how to create good WHERE clauses and how to use JOINs. It will help
you in the future.


Q&A


Q:

In today's lesson LEFT JOINs were discussed. Is there such a thing as
a RIGHT JOIN?
A:
There is such a thing as a RIGHT JOIN. Most major RDBMs support
RIGHT JOINs. However, MySQL does not, for good reason. If you take a
minute and think what a RIGHT JOIN is—the reverse of a LEFT JOIN—
then there is a simple workaround. Simply switch the table names in your
query. For example, if the following was your LEFT JOIN:
SELECT C.First_Name
FROM Customer as C
LEFT JOIN Orders as O ON C.Customer_ID = O.Customer_ID
Your RIGHT JOIN would look like the following:
SELECT C.First_Name
FROM Orders AS O
LEFT JOIN Customers as C ON C.Customer_ID = O.Customer_ID
As you can see, there really is no reason to code and implement a
RIGHT JOIN when simply reversing the order of your tables will
accomplish the same thing.
Q:
I was looking through the MySQL manual and saw something about a
REPLACE statement. What is it, and how do I use it?
A:
The REPLACE statement is the same as using an INSERT INTO command.
The syntax is pretty much the same. The difference between an INSERT
statement and a REPLACE statement is that MySQL will delete the old
record and replace it with the new values in a REPLACE statement, hence
the name REPLACE. The syntax for a REPLACE statement looks like the
following:
REPLACE INTO Customers
(Customer_ID, First_Name, Last_Name)
VALUES(NULL, "Dina", "Newton")
The REPLACE statement shares the same rules and parameters as the INSERT statement to include
the REPLACE INTO...SELECT statements, as well as the LOW_PRIORITY modifier.

Exercises



  1. Translate the following into SQL statements:
    ƒ View all the records from the Customers table of the customers who
    have placed orders.
    ƒ View all shippers that have been used in the past month.
    ƒ View all customers.

  2. Create an SQL statement that accomplishes the following:
    ƒ Add the following information to the Customers table:

Free download pdf