Beginning SQL

Beginning SQL Beginning SQL

marjan.fesb.hr
from marjan.fesb.hr More from this publisher
20.07.2013 Views

Chapter 4 140 index helps speed up SELECT queries and WHERE clauses, why not always have one? First of all, while it speeds up data retrieval, it slows down data input, with UPDATE and INSERT statements, for example. Additionally, it adds to a database’s size, albeit not massively, though it is a consideration. Using indexes is good practice, but it is best done judiciously — for example, using them to help your most common queries. Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify the table and which column or columns to index, and to indicate whether the index is in ascending or descending order. Indexes can also be unique, similar to the UNIQUE constraint, in that the index prevents duplicate entries in the column or combination of columns on which there’s an index. The basic format of the statement is as follows: CREATE INDEX ON () The following code adds an index called member_name_index on the MemberDetails table, and it indexes the FirstName and LastName columns: CREATE INDEX member_name_index ON MemberDetails (FirstName, LastName); If you execute the following SELECT statement, you notice something interesting: SELECT FirstName, LastName FROM MemberDetails; The results, shown in the following table, appear in ascending order, by first name and then last name, even though you did not add an ORDER BY clause: FirstName LastName Catherine Hawthorn Doris Night Jack Johnson Jamie Hills Jenny Jones John Jackson John Jones Katie Smith Seymour Botts Steve Gee Stuart Dales Susie Simons William Doors

Chapter 4<br />

140<br />

index helps speed up SELECT queries and WHERE clauses, why not always have one? First of all, while it<br />

speeds up data retrieval, it slows down data input, with UPDATE and INSERT statements, for example.<br />

Additionally, it adds to a database’s size, albeit not massively, though it is a consideration. Using indexes<br />

is good practice, but it is best done judiciously — for example, using them to help your most common<br />

queries.<br />

Creating an index involves the CREATE INDEX statement, which allows you to name the index, to specify<br />

the table and which column or columns to index, and to indicate whether the index is in ascending or<br />

descending order. Indexes can also be unique, similar to the UNIQUE constraint, in that the index prevents<br />

duplicate entries in the column or combination of columns on which there’s an index. The basic<br />

format of the statement is as follows:<br />

CREATE INDEX <br />

ON ()<br />

The following code adds an index called member_name_index on the MemberDetails table, and it<br />

indexes the FirstName and LastName columns:<br />

CREATE INDEX member_name_index<br />

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

If you execute the following SELECT statement, you notice something interesting:<br />

SELECT FirstName, LastName<br />

FROM MemberDetails;<br />

The results, shown in the following table, appear in ascending order, by first name and then last name,<br />

even though you did not add an ORDER BY clause:<br />

FirstName LastName<br />

Catherine Hawthorn<br />

Doris Night<br />

Jack Johnson<br />

Jamie Hills<br />

Jenny Jones<br />

John Jackson<br />

John Jones<br />

Katie Smith<br />

Seymour Botts<br />

Steve Gee<br />

Stuart Dales<br />

Susie Simons<br />

William Doors

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

Saved successfully!

Ooh no, something went wrong!