Beginning SQL
Beginning SQL Beginning SQL
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
- Page 268: Chapter 3 114 FirstName LastName Da
- Page 272: Chapter 3 ❑ The slightly tricky t
- Page 276: Chapter 4 normalization can make da
- Page 280: Chapter 4 120 Field Name Data Type
- Page 284: Chapter 4 122 The Street, City, and
- Page 288: Chapter 4 MySQL versions prior to 5
- Page 292: Chapter 4 126 Except for IBM’s DB
- Page 296: Chapter 4 128 You can add more than
- Page 300: Chapter 4 Try It Out Adding a CHECK
- Page 304: Chapter 4 Obviously, if the table i
- Page 308: Chapter 4 134 Drop the MoreHolidayB
- Page 312: Chapter 4 136 If you want to return
- Page 316: Chapter 4 If you’re using IBM DB2
- Page 322: The results are ordered because, by
- Page 326: The index is no longer needed, so d
- Page 330: WHERE MemberAttended = ‘Y’; DRO
- Page 334: Primary keys must be unique and can
- Page 338: Then add the PRIMARY KEY constraint
- Page 342: ( ); FilmId integer NOT NULL, FilmN
- Page 346: ALTER TABLE Attendance ADD CONSTRAI
- Page 350: ❑ You learned the importance of e
- Page 356: Chapter 5 158 Function Operator Mul
- Page 360: Chapter 5 Consider the following SQ
- Page 364: Chapter 5 The SQRT() Function The S
- Page 368: Chapter 5 164 chairperson now also
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