Constraints are rules that apply to data tables and make up part of the data integrity rules
of the database. The database takes care of its own integrity and makes sure these rules aren??™t
broken. If, for example, you try to add two identical values for a column that has a PRIMARY KEY
constraint, the database refuses the operation and generates an error. We??™ll do some experiments
later in this chapter to show this.
?– Note A primary key is not a column but a constraint that applies to that column; however, from now on
and for convenience, when referring to the primary key, we??™ll be talking about the column that has the
PRIMARY KEY constraint applied to it.
Back to the example, setting the name column as the primary key of the department table
would solve the problem because two departments would not be allowed to have the same
name. If name is the primary key of the department table, searching for a product with a specific
name will always produce exactly one result if the name exists, or no results if no records have
the specified name.
CHAPTER 4 ?– CREATING THE PRODUCT CATALOG: PART 1 71
?– Tip This is common sense, but it has to be said: a primary key column will never allow NULL values.
An alternative solution, and usually the preferred one, is to have an additional column in
the table, called an ID column, to act as its primary key.
Pages:
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144