Table Management
13.7 L ISTEN and N OTIFY
POSTGRESQLallows users to send signals to one another usingLISTENandNOTIFY. For example, suppose a user wants to receive notification when a table is updated. He can register the table name using theLISTENcommand. If someone updates the table and then issues aNOTIFYcommand, all registered listeners will be informed of the change. For more information, see theLISTENand
NOTIFYmanual pages.
13.8 Summary
This chapter has covered features that give administrators and users new capabilities in managing database tables. Chapter14turns to restrictions that can be placed on table columns to improve data management.
Chapter 14
Constraints
Constraints keep user data constrained, thereby helping to prevent invalid data from being entered into the database. Defining a data type for a column is a constraint in itself. For example, a column of typeDATEconstrains the column to valid dates.
This chapter covers a variety of constraints. We have already shownDEFAULTcan be specified at table creation. Constraints are defined at table creation in a similar way.
14.1 N
OTN
ULLThe constraintNOT NULLpreventsNULLvalues from appearing in a column. Figure14.1shows the creation of a table with aNOT NULLconstraint. Insertion of aNULLvalue, or anINSERTthat would set col2 toNULL,causes theINSERTto fail. As shown in the figure, anUPDATEof aNULLvalue also fails.
Figure14.2adds aDEFAULTvalue for col2. This addition permitsINSERTs that do not specify a value for col2.
14.2 U
NIQUETheUNIQUEconstraint prevents duplicate values from appearing in the column. It is implemented by creating a unique index on a column. As indicated in Figure14.3,UNIQUEprevents duplicates.
CREATE TABLEdisplays the name of the unique index created. The figure also shows that multiple
NULLvalues can be inserted into aUNIQUEcolumn.
If aUNIQUEconstraint consists of more than one column,UNIQUEcannot be used as a column constraint. Instead, you must use a separateUNIQUEline to specify the columns that make up the constraint. This approach creates aUNIQUEtable constraint.
Figure14.4shows a multicolumnUNIQUEconstraint. While col1 or col2 themselves may not be unique, the constraint requires the combination of col1 and col2 to be unique. For example, in a
155
156 CHAPTER 14. CONSTRAINTS
test=> CREATE TABLE not_null_test (
test(> col1 INTEGER,
test(> col2 INTEGER NOT NULL
test(> );
CREATE
test=> INSERT INTO not_null_test test-> VALUES (1, NULL);
ERROR: ExecAppend: Fail to add null value in not null attribute col2 test=> INSERT INTO not_null_test (col1)
test-> VALUES (1);
ERROR: ExecAppend: Fail to add null value in not null attribute col2 test=> INSERT INTO not_null_test VALUES (1, 1);
INSERT 174368 1
test=> UPDATE not_null_test SET col2 = NULL;
ERROR: ExecReplace: Fail to add null value in not null attribute col2 Figure 14.1: NOT NULLconstraint
test=> CREATE TABLE not_null_with_default_test (
test(> col1 INTEGER,
test(> col2 INTEGER NOT NULL DEFAULT 5
test(> );
CREATE
test=> INSERT INTO not_null_with_default_test (col1) test-> VALUES (1);
INSERT 148520 1 test=> SELECT *
test-> FROM not_null_with_default_test;
col1 | col2
---+---1 | 5
(1 row)
Figure 14.2: NOT NULLwithDEFAULTconstraint
14.2. UNIQUE 157
test=> CREATE TABLE uniquetest (col1 INTEGER UNIQUE);
NOTICE: CREATE TABLE/UNIQUE will create implicit index ’uniquetest_col1_-key’ for table ’uniquetest’
CREATE
test=> \d uniquetest Table "uniquetest"
Attribute | Type | Modifier
---+---+---col1 | integer | Index: uniquetest_col1_key
test=> INSERT INTO uniquetest VALUES (1);
INSERT 148620 1
test=> INSERT INTO uniquetest VALUES (1);
ERROR: Cannot insert a duplicate key into unique index uniquetest_col1_key test=> INSERT INTO uniquetest VALUES (NULL);
INSERT 148622 1
test=> INSERT INTO uniquetest VALUES (NULL);
INSERT
Figure 14.3:UNIQUEcolumn constraint
test=> CREATE TABLE uniquetest2 (
test(> col1 INTEGER,
test(> col2 INTEGER,
test(> UNIQUE (col1, col2)
test(> );
NOTICE: CREATE TABLE/UNIQUE will create implicit index ’uniquetest2_col1_-key’ for table ’uniquetest2’
Figure 14.4: MulticolumnUNIQUEconstraint
158 CHAPTER 14. CONSTRAINTS test=> CREATE TABLE primarytest (col INTEGER PRIMARY KEY);
NOTICE: CREATE TABLE/PRIMARY KEY will create implicit index ’primarytest_-pkey’ for table ’primarytest’
CREATE
test=> \d primarytest Table "primarytest"
Attribute | Type | Modifier
---+---+---col | integer | not null Index: primarytest_pkey
Figure 14.5: Creation of aPRIMARY KEYcolumn
table that contains the driver’s license numbers of people in various states, two people in different states might have the same license number, but the combination of their state and license number should always be unique.
14.3 P
RIMARYK
EYThe PRIMARY KEY constraint, which marks the column that uniquely identifies each row, is a combination ofUNIQUEandNOT NULLconstraints. With this type of constraint,UNIQUEprevents duplicates, andNOT NULLpreventsNULLvalues in the column. Figure14.5shows the creation of aPRIMARY KEYcolumn. Notice that an index is created automatically, and the column is defined as
NOT NULL.
Just as withUNIQUE,a multicolumn PRIMARY KEYconstraint must be specified on a separate line. In Figure14.6, col1 and col2 are combined to form the primary key.
A table cannot have more than one PRIMARY KEY specification. Primary keys have special meaning when using foreign keys, which are covered in the next section.