Creating a Non-Clustered Index on a Table
4. Right-click the Indexes subfolder and select New index from the pop-up menu that appears, as shown in Figure 4-16
Figure 4-16 New Index menu
5. You will now see a new dialog box appear, the New Index properties box, for which you can input the desired inputs. This is where you would select whether the index you are creating is to be clustered or non-clustered (as shown in Figure 4-17).
Figure 4-17
New Index property box
PAUSE. Leave the SSMS interface open for the next exercise.
c04UnderstandingDataStorage.indd78 Page 78 5/3/11 12:21:35 PM user-F409
c04UnderstandingDataStorage.indd78 Page 78 5/3/11 12:21:35 PM user-F409 /Users/user-F409/Desktop/Users/user-F409/Desktop
Understanding Data Storage | 79
For an idea of what a clustered index’s Properties dialog box may look like, see Figure 4-18.
Figure 4-18
Clustered index property box
When looking at the example clustered index, note that you cannot add another index.
Herein lies, as mentioned previously, the importance of ensuring you pick the right key to act as your clustered index key: This is your primary sorting index for the table, and you cannot have two clustered indexes per table.
However, on a table with a non-clustered index, you can add multiple table columns to the index key, as shown in Figure 4-19.
Figure 4-19 Non-clustered index property box
c04UnderstandingDataStorage.indd79 Page 79 5/3/11 12:21:35 PM user-F409
c04UnderstandingDataStorage.indd79 Page 79 5/3/11 12:21:35 PM user-F409 /Users/user-F409/Desktop/Users/user-F409/Desktop
80 | Lesson 4
S K I L L S U M M A R Y
INTHISLESSON, YOULEARNED THEFOLLOWING:
• Normalization, in a nutshell, is the elimination of redundant data to save space.
• In the first normalized form (1NF), the data is in an entity format, which basically means that the following three conditions must be met: the table must have no duplicate records, the table must not have multi-valued attributes, and the entries in the column or attribute must be of the same data type.
• The second normal form (2NF) ensures that each attribute does in fact describe the entity.
• The third normal form (3NF) checks for transitive dependencies. A transitive dependency is similar to a partial dependency in that they both refer to attributes that are not fully depen- dent on a primary key.
• The fourth normal form (4NF) involves two independent attributes brought together to form a primary key along with a third attribute.
• The fifth normal form (5NF) provides the method for designing complex relationships involving multiple (usually three or more) entities.
• Three different types of constraints available within SQL Server can help you maintain database integrity: primary keys, foreign keys, and composite (unique) keys.
• A unique key constraint allows you to enforce the uniqueness property of columns, in addition to a primary key within a table.
• Perhaps the most important concept in designing any database table is that it has a primary key—an attribute or set of attributes that can be used to uniquely identify each row.
• Every table must have a primary key; without a primary key, it’s not a valid table. By defini- tion, a primary key must be unique and must have a value that is not null.
• In order to connect two tables, the primary key is replicated from the primary to secondary table, and all the key attributes duplicated from the primary table are known as the foreign key.
• A composite primary key occurs when you define more than one column as your primary key.
• The primary drawbacks to using indexes are the time it takes to build the indexes and the storage space the indexes require.
• When you begin implementing indexes, it is important to remember that each table can have only one clustered index that defines how SQL Server will sort the data stored inside it, because that data can be sorted in only one way.
• A non-clustered index contains the non-clustered index key values, and each of those keys has a pointer to a data row that contains the key value.
True or False
Circle T if the statement is true or F if the statement is false.
T F 1. Creating a primary key satisfi es the fi rst normal form.
T F 2. Tables in a database must satisfy all fi ve normal forms in order to maximize performance.
T F 3. A primary key can contain NULL values.
T F 4. A clustered index usually improves performance when inserting data.
T F 5. A table can contain only one clustered index.
■
Knowledge Assessment
c04UnderstandingDataStorage.indd80 Page 80 5/3/11 12:21:36 PM user-F409
c04UnderstandingDataStorage.indd80 Page 80 5/3/11 12:21:36 PM user-F409 /Users/user-F409/Desktop/Users/user-F409/Desktop
Understanding Data Storage | 81
Fill in the Blank
Complete the following sentences by writing the correct word or words in the blanks provided.
1. Normalization is the elimination of redundant data to save ____________.
2. The value of a primary key must be ____________.
3. A foreign key works in conjunction with primary key or unique constraints to enforce ____________ between tables.
4. Add an index to one or more columns to speed up data ____________.
5. Values in a clustered index are ____________.
Multiple Choice
Circle the letter that corresponds to the best answer.
1. Which of the following is not a constraint?
a. CHECK b. DEFAULT c. UNIQUE d. INDEX
2. Which of the following things can help speed data retrieval?
a. A DEFAULT constraint b. A primary key constraint c. A clustered index d. A foreign key constraint
3. Which of the following statements are true?
a. A greater number of narrow tables (with fewer columns) is a characteristic of a normalized database.
b. A few wide tables (with more columns) are characteristic of a normalized database.
c. Indexes allow faster data retrieval.
d. Optimal database performance can be achieved by indexing every column in a table.
4. Which of the following statements is not true with regard to foreign keys?
a. A foreign key is a combination of one or more columns used to establish and enforce a link between the data in two tables.
b. You can create a foreign key by defining a foreign key constraint when you create or alter a table.
c. A foreign key enforces referential integrity by ensuring only valid data is stored.
d. A table can contain only one foreign key.
5. Consider using a clustered index when:
a. Columns contain a large number of distinct values b. Columns are accessed sequentially
c. Columns undergo frequent changes d. Queries return large result sets
6. Which normal form ensures that each attribute describes the entity?
a. 1NF b. 2NF c. 3NF d. 4NF
c04UnderstandingDataStorage.indd81 Page 81 5/3/11 12:21:36 PM user-F409
c04UnderstandingDataStorage.indd81 Page 81 5/3/11 12:21:36 PM user-F409 /Users/user-F409/Desktop/Users/user-F409/Desktop
82 | Lesson 4
7. Which of the following could not be used as a primary key?
a. A Social Security number b. An address
c. An employee number
d. The serial number of an electronic component 8. How many clustered indexes can you have for a database?
a. 1 b. 2 c. 4 d. 8
9. What is the name for the situation in which more than one columns act as a primary key?
a. Composite primary key b. Escalating key
c. Foreign key d. Constraint key
10. When you define a primary key, you have met the criteria for:
a. 1NF b. 2NF c. 3NF d. 4NF
■