Lecture 14. Indexes and Constraints - MSTeams
Lecture 14. Indexes and Constraints - MSTeams
Lecture 14. Indexes and Constraints - MSTeams
This lecture focuses on two database features that indirectly affect the code you
write: indexes and constraints.
Indexes
In the same way that a person uses an index to find words within a publication,
a database server uses indexes to locate rows in a table. Indexes are special tables
that, unlike normal data tables, are kept in a specific order. Instead of containing all
of the data about an entity, however, an index contains only the column (or columns)
used to locate rows in the data table, along with information describing where the
rows are physically located. Therefore, the role of indexes is to facilitate the retrieval
of a subset of a table’s rows and columns without the need to inspect every row in
the table.
Index Creation
Consider the department table. You might decide to add an index on the
name column to speed up any queries that specify a full or partial department name,
as well as any update or delete operations that specify a department name. Here is
how you can add such an index to a MySQL database:
All database servers allow you to look at the available indexes. MySQL users
can use the show command to see all of the indexes on a specific table:
If, after creating an index, you decide that the index is not proving useful, you
can remove it via the following:
SQL Server and Oracle Database users must use the drop index command to
remove an index:
Unique Indexes
SQL Server and Oracle Database users need only add the unique keyword
when creating an index:
You should not build unique indexes on your primary key column(s), since
the server already checks uniqueness for primary key values.
Multicolumn Indexes
Along with the single-column indexes demonstrated thus far, you may build
indexes that span multiple columns. If, for example, you find yourself searching for
employees by first and last names, you can build an index on both columns together:
This index will be useful for queries that specify the first and last names or
just the last name, but you cannot use it for queries that specify only the employee’s
first name. To understand why, consider how you would find a person’s phone
number; if you know the person’s first and last names, you can use a phone book to
find the number quickly, since a phone book is organized by last name and then by
first name. If you know only the person’s first name, you would need to scan every
entry in the phone book to find all the entries with the specified first name.
Constraints
Constraint Creation
Constraints are generally created at the same time as the associated table via
the create table statement. To illustrate, here is an example of creating the product
table:
The product table includes two constraints: one to specify that the product_cd
column serves as the primary key for the table, and another to specify that the
product_type_cd column serves as a foreign key to the product_type table.
Alternatively, you can create the product table without constraints, and add the
primary and foreign key constraints later via alter table statements:
If you want to remove a primary or foreign key constraint, you can use the
alter table statement again, except that you specify drop instead of add:
While it is unusual to drop a primary key constraint, foreign key constraints are
sometimes dropped during certain maintenance operations and then reestablished.
Cascading Constraints
With foreign key constraints in place, if a user attempts to insert a new row or
change an existing row such that a foreign key column does not have a matching
value in the parent table, the server raises an error. However, you can instruct the
server to propagate the change to all child rows for you, thus preserving the integrity
of the data. Known as a cascading update, this variation of the foreign key constraint
can be installed by removing the existing foreign key and adding a new one that
includes the on update cascade clause:
Along with cascading updates, you can specify cascading deletes as well. A
cascading delete removes rows from the child table when a row is deleted from the
parent table. To specify cascading deletes, use the on delete cascade clause:
Cascading constraints are one case in which constraints do directly affect the
code that you write. You need to know which constraints in your database specify
cascading updates and/or deletes so that you know the full effect of your update and
delete statements.
Literature
1. Alan Beaulieu. Learning SQL. 2nd Edition. – O’Reilly Media, 2009. – 337 p.