Managing Constraints   «Prev  Next»

Lesson 3 PRIMARY KEY constraints
Objective Create PRIMARY KEY constraints on database tables

Create PRIMARY KEY constraints on Tables

A primary key is a column or series of columns that uniquely identifies each individual row in a table. The PRIMARY KEY constraint is used to denote which columns in a table act as the primary key for the table. You can have only one primary key for each table. When you choose a single column as a primary key, you must have a non-NULL value for the column that is unique for the row. If you choose a series of columns as the primary key for a table, you must have at least one of the columns contain a non-NULL unique value. Although you are not required to have a primary key for every table, it is good design practice to include a primary key in your table design. This is so you can uniquely identify an individual row based on data values. In addition, you must have a PRIMARY KEY constraint on a parent table to allow a FOREIGN KEY constraint on a child table, as described later in this module.

PseudoColumn ROWID

The Oracle database includes a psuedo-column for every table called the ROWID. The ROWID is a pseudo-column because it is outside the normal range of awareness for columns in a table. If you query the data dictionary tables or do a SELECT* on the table, the ROWID will not appear.
You can specifically query a table for the ROWID column in the table.
The ROWID column uniquely identifies every row in an Oracle database. The ROWID never changes for a row, unless you export the entire database and import the data. The ROWID is the fastest way to access any particular row in an Oracle database.
A pseudocolumn behaves like a table column, but is not actually stored in the table. You can select from pseudocolumns, but you cannot insert, update, or delete their values. A pseudocolumn is also similar to a function without arguments. However, functions without arguments typically return the same value for every row in the result set, whereas pseudocolumns typically return a different value for each row.
  • ROWID Pseudocolumn
    For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row:
    1. The data object number of the object
    2. The data block in the data file in which the row resides
    3. The position of the row in the data block (first row is 0)
    4. The data file in which the row resides (first file is 1). The file number is relative to the tablespace.

    Usually, a rowid value uniquely identifies a row in the database. However, rows in different tables that are stored together in the same cluster can have the same rowid. Values of the ROWID pseudocolumn have the data type ROWID or UROWID.
    Rowid values have several important uses:
    1. They are the fastest way to access a single row.
    2. They can show you how the rows in a table are stored.
    3. They are unique identifiers for rows in a table.
    You should not use ROWID as the primary key of a table. If you delete and reinsert a row with the Import and Export utilities, for example, then its rowid may change. If you delete a row, then Oracle may reassign its rowid to a new row inserted later. Although you can use the ROWID pseudocolumn in the SELECT and WHERE clause of a query, these pseudocolumn values are not actually stored in the database.
    You cannot insert, update, or delete a value of the ROWID pseudocolumn.
  • Example: This statement selects the address of all rows that contain data for employees in department 20:
    SELECT ROWID, last_name
    FROM employees
    WHERE department_id = 20;
    

The Oracle database includes a unique identifier for every row in the database through the use of a ROWID.

Create Table Example

In the COIN database, the CREATE TABLE command for the AUCTION table, including a PRIMARY KEY constraint, would be:
CREATE TABLE AUCTION ( auction_id NUMBER, 
start_time DATETIME, stop_time DATETIME,
CONSTRAINT pk_auction PRIMARY KEY (auction_id))

  • The operation of a PRIMARY KEY constraint:
    To see how a PRIMARY KEY constraint operates, use the following series of images to view the various ways that users can interact with a PRIMARY KEY constraint.

How a PRIMARY KEY constraint operates
1) This short version of the auction table contains five rows, each with its own unique, non-NULL value for the auction_id table
1) This short version of the auction table contains five rows, each with its own unique, non-NULL value for the auction_id table

2) A user adds a new row to the auction table that has a value for the auction_id column unique to the table.
2) A user adds a new row to the auction table that has a value for the auction_id column unique to the table.

3) The row is successfully inserted into the table.
3) The row is successfully inserted into the table.

4) A user attempts to insert a row into the auction table that has a non-unique value for the auction_id column.
4) A user attempts to insert a row into the auction table that has a non-unique value for the auction_id column.

5) The INSERT operation fails with an error, because a unique constraint has been violated.
5) The INSERT operation fails with an error, because a unique constraint has been violated.

6) A user tries to update a row, but makes the value of the auction_id NULL.
6) A user tries to update a row, but makes the value of the auction_id NULL.

7) The UPDATE operaiton fails with an error, because the non-NULL constraint has been violated
7) The UPDATE operaiton fails with an error, because the non-NULL constraint has been violated.

Oracle Error 01407

Oracle Error 01407 occurs when you attempt to update a column with a `NULL` value, but the column has been defined as `NOT NULL`. This error indicates that you are trying to assign a `NULL` to a field that cannot accept `NULL` values due to its constraint settings.
Error Message
ORA-01407: cannot update ("TABLE_NAME"."COLUMN_NAME") to NULL

Cause This error is raised when an `UPDATE` statement attempts to set a `NOT NULL` column to `NULL`. The `NOT NULL` constraint enforces that the column must always have a non-null value.
Solution
  • Ensure that your UPDATE statement provides a valid, non-null value for the column with the NOT NULL constraint.
  • If NULL values need to be handled, you may consider modifying the table to remove the NOT NULL constraint, though this should be done carefully to maintain data integrity.

The next lesson explains the UNIQUE constraint.

Primary Key constraints - Exercise

Click the Exercise link below to practice creating primary key constraints for the COIN database.
Primary Key constraints - Exercise

SEMrush Software 3 SEMrush Banner 3