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.
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.
The Oracle database includes a unique identifier for every row in the database through the use of a
ROWID
.
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.
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.