20.07.2013 Views

Beginning SQL

Beginning SQL

Beginning SQL

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 4<br />

142<br />

The results you get may be in a slightly different order, depending on your DBMS. When results are not<br />

ordered, there’s no real guarantee of what the order might be.<br />

You can set two other options when creating an index. The first, the UNIQUE option, prevents duplicate<br />

values from being entered and works very much like the UNIQUE constraint. The second option determines<br />

the column order. Recall that the default results order is ascending, but you can also order results<br />

in descending order.<br />

The following <strong>SQL</strong> creates a unique index that orders results by last name in descending order and then<br />

by first name. Using the DESC keyword, execute the following <strong>SQL</strong>:<br />

CREATE UNIQUE INDEX member_name_indx<br />

ON MemberDetails (LastName DESC, FirstName);<br />

After executing the preceding code, execute this query:<br />

SELECT LastName, FirstName<br />

FROM MemberDetails;<br />

Query results are provided in the following table:<br />

LastName FirstName<br />

Botts Seymour<br />

Dales Stuart<br />

Doors William<br />

Gee Steve<br />

Hawthorn Catherine<br />

Hills Jamie<br />

Jackson John<br />

Johnson Jack<br />

Jones Jenny<br />

Jones John<br />

Night Doris<br />

Simons Susie<br />

Smith Katie<br />

You can see that LastName now goes in order from the lowest to the highest — from a to z in the alphabet.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!