Summary: in this tutorial, you will learn how to use Oracle PRIMARY KEY
constraint to manage the primary key of a table.
APPLIES TO: SQL Server Azure SQL Database Azure Synapse Analytics (SQL DW) Parallel Data Warehouse. Creates a sequence object and specifies its properties. A sequence is a user-defined schema bound object that generates a sequence of numeric values according to the specification with which the sequence was created. Surrogate keys are keys that have no “business” meaning and are solely used to identify a record in the table. Such keys are either database generated (example: Identity in SQL Server, Sequence in Oracle, Sequence/Identity in DB2 UDB etc.) or system generated values (like generated via a table in the schema).
A primary key is a column of a combination of columns in a table that uniquely identifies a row in the table.
A sequence is an object in SQL Server (Transact-SQL) that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key. Create Sequence.
The following are rules that make a column a primary key:
According to these rules, the following are the recommendations for the primary keys:
To create a primary key in a table, you use the PRIMARY KEY
constraint.
PRIMARY KEY
constraint examplesTypically, you create a primary key for a table when you create that table. In addition, you can add a primary key to a table after the fact by using the ALTER TABLE
statement.
The following CREATE TABLE
statement creates the purchase_orders
table:
The purchase_orders
table has four columns purchase order number (po_nr
), vendor id (vendor_id
), purchase order status (po_status
), and the timestamp (created_at
) of which the purchase order is created.
In this table, defined the po_nr
column as the primary key by using the PRIMARY KEY
clause.
Note that the PRIMARY KEY
clause implicitly makes the po_nr
column NOT NULL
so you don’t have to define the column like:
The PRIMARY KEY
constraint in this example is an inline constraint because it is on the same line as the po_nr
column.
Consider the following statement.
This example used the PRIMARY KEY
constraint as the table constraint. Notice the following clause:
In addition, we explicitly assigned the PRIMARY KEY
constraint a name pk_purchase_orders
.
The following statement creates the purchase order line items table:
In this example, the primary key of the purchase_order_items
table consists of two columns: po_nr
and item_nr
. It means that the combination of values of these columns uniquely identifies a purchase order line item.
This example did not use the CONSTRAINT
clause to explicitly assign the PRIMARY KEY
constraint a name. Therefore, Oracle implicitly assigned the primary key constraint a system-generated name such as SYS_C0010617
.
Sometimes, you may want to add a primary key constraint to an existing table. To do it, you use the ALTER TABLE
statement as follows:
The following example creates the vendors
table first and then adds a primary key constraint to it:
PRIMARY KEY
constraintYou will rarely drop a PRIMARY KEY
constraint from a table. If you have to do so, you use the following ALTER TABLE
statement.
For example, you can drop the primary key constraint of the vendors
table as follows:
It is possible to use the following statement to drop the primary key of a table:
For example:
PRIMARY KEY
constraintTo improve the performance when loading a large amount of data into a table or updating mass data, you can temporarily disable the PRIMARY KEY
constraint.
To disable a PRIMARY KEY
constraint of a table, you use the ALTER TABLE
statement:
or
For example, to disable the primary key constraint of the purchase_orders
table, you use the following statement:
or
To enable a primary key constraint, you use the following ALTER TABLE
statement:
or
The following example enables the PRIMARY KEY
constraint of the purchase_orders
table:
or
In this tutorial, you have learned how to use Oracle PRIMARY KEY
constraint to create, add, disable, enable, and drop a primary key of a table.
Summary: in this tutorial, you will learn how to use the Db2 PRIMARY KEY
constraint to define a primary key for a table.
A primary key of a table is a column or group of columns whose values uniquely identify every row in the table.
Each table has one and only one primary key. A primary key is optional. However, it’s a good practice to have a primary key in every table.
To define a primary key for a table, you use the PRIMARY KEY
constraint.
The following statement creates a table with a primary key:
If the primary key consists of two or more columns, you use the following syntax:
When you create a table with the primary key constraint, Db2 automatically creates a unique index on the primary key columns. This unique index is also known as the primary index.
To add a primary key to a table, you use the following form of ALTER TABLE
statement:
Note that the ADD CONSTRAINT
clause is optional.
To remove a primary key from a table, you use the following form of the ALTER TABLE
statement:
NOT NULL
constraintThe primary key of a table cannot contain NULL values. In other database systems such as SQL Server, Oracle, and MySQL, the columns included in the primary key columns will implicitly receive the NOT NULL
constraint. However, in DB2, you must explicitly declare all primary key columns as NOT NULL
columns.
Because the primary key must contain unique values that uniquely identify each row of a table, the identity column is an ideal choice for the primary key.
The following syntax shows how to create a table with a primary key as an identity column:
Let’s take some examples of creating new tables with primary keys.
Suppose SSN uniquely identifies a person. The following statement creates the persons
table whose primary key is the SSN column:
This statement inserts a new person into the persons
table:
The following statement attempts to insert a new row into the persons
table with an SSN that already exists:
Db2 issued the following error:
The following example creates a table named members
with a primary key column as an identity column:
Each time you insert a new row into the members
table, Db2 automatically generates a sequential number for the member_id
column.
Here are the contents of the members
table:
Notice that the member_id
has the sequential integer 1 and 2.
The following example creates a new table whose primary key contains two columns:
Windows 7 product key generator. This example creates a new table without a primary key:
Before promoting the membership_id
as the primary key, you first need to make sure that this column does not have duplicate values. Then, you use the following statement to add a primary key to the memberships
table:
In this tutorial, you have learned about Db2 primary key and how to create a new table with a primary key.