Primary key is an Input/output Data constraint. It serves the purpose of uniquely identifying the rows in a table
Technically Primary key constraint is the combination of NOT NULL and UNIQUE constraints.
There are Two types of Primary key.
Technically Primary key constraint is the combination of NOT NULL and UNIQUE constraints.
There are Two types of Primary key.
1 Simple Primary key and
2 Composite Primary Key.
Primary key which involves only one column of a table is called Simple Primary key whereas the primary key which involvesmore than one columns of a table is called Composite Primary key.
More than one column doesn't mean that you can give any number of columns. This still has some restrictions. A composite key in oracle can have maximum of 32 columns only.
More than one column doesn't mean that you can give any number of columns. This still has some restrictions. A composite key in oracle can have maximum of 32 columns only.
Primary key has a single function of identifying a unique row in the table. In case the simple primary key fails to identify that unique row, the user must then define a composite primary key.
You cannot Delete a Primary key if it is referenced by a foreign key in some other table. There is a unique index created automatically by Oracle so that the requirement of the uniqueness of the PRIMARY KEY constraint is fulfilled.
You cannot Delete a Primary key if it is referenced by a foreign key in some other table. There is a unique index created automatically by Oracle so that the requirement of the uniqueness of the PRIMARY KEY constraint is fulfilled.
There are few things which you should know about primary key. (Features of Primary key)
- A table can have only One Primary key no matter whether its Simple Primary Key Or Composite Primary Key.
- Columns which are participating in Primary Key cannot have NULL values. This means you cannot leave them unattended or you cannot put NULL value into them.
3 A table can have only One Primary key no matter whether its Simple Primary Key Or Composite Primary Key.
4 Columns which are participating in Primary Key cannot have NULL values. This means you cannot leave them unattended or you cannot put NULL value into them.
5 As primary key is all about Row or Record’s uniqueness thus it will not allow duplicate values.
6 When a Primary Key constraint has been defined on multiple columns then its values can be duplicated provided the duplication is happening within one single column. However the combination of values of all the columns defining each primary key constraint should be unique.
7 Data-types such as LOB, LONG, LONG RAW, VARRAY, NESTED TABLE, BFILE, REF, TIMESTAMP WITH TIME ZONE, or user-defined type are not allowed with the columns which are part of Primary key. Any attempt of creating a primary key with the column of these data-types will raise SQL Error: ORA-02269.
8 The size of the primary key cannot exceed approximately one database block.
9 As I have already mentioned above that a composite primary key can have 32 columns maximum.
10 The Primary key and Unique key should never be designated as the same column or combination of columns.
11 You cannot specify a primary key when creating a sub view in an inheritance hierarchy. The primary key can be specified only for the top-level (root) view.
12 Unique cluster Index gets created automatically on the time creating Primary key.
13 Although it is not necessary for you to define a primary key yet it is always recommended to do so.
How to create a primary key or How to Define a Primary key.
You can define a Primary key either while CREATING a table or After Creating a table using ALTER TABLE statement.
Let’s start with Defining a primary key Using CREATE TABLE statement. There are two ways of defining a Primary Key using CREATE TABLE
14 Defining Primary Key at Column Level and
15 Defining Primary Key at Table Level
efining Primary Key using Create table
1. Defining primary Key at Column Level.
This way of defining constraint is called column level because we define constraints with the column definition while creating table, Here we Define a primary key while defining the column during Create table.
For example
Let’s Create a table by the name of “Product_master”
For example
Let’s Create a table by the name of “Product_master”
CREATE TABLE product_master
(
Product_id NUMBER(3) PRIMARY KEY,
Product_name VARCHAR2(30),
Product_price NUMBER(5)
);
(
Product_id NUMBER(3) PRIMARY KEY,
Product_name VARCHAR2(30),
Product_price NUMBER(5)
);
To make a column - a primary key, you just have to put keyword primary key after datatype and size in column definition as we did above with our first column.
But its always a good practice to provide a meaningful name to your constraint while creating it because oracle database saves all the constraints in Data dictionary and constraints with meaningful name are easy to reference.
So Lets modify the above query and provide a meaningful name to our Primary key constraint.
CREATE TABLE product_master
(
Product_id NUMBER(3) CONSTRAINT pmst_pid_pk PRIMARY KEY,
Product_name VARCHAR2(30),
Product_price NUMBER(5)
);
(
Product_id NUMBER(3) CONSTRAINT pmst_pid_pk PRIMARY KEY,
Product_name VARCHAR2(30),
Product_price NUMBER(5)
);
2. Defining Primary Key at Table Level
This way of defining constraint is called table level because we define constraints explicitly in create table statement. Here in this type of primary key creation we first define all our columns and after defining all the columns we define our constraint
for example
Lets again Create a table this time by the name of ORDER_MASTER
for example
Lets again Create a table this time by the name of ORDER_MASTER
CREATE TABLE order_master
(
order_id NUMBER(3),
order_data DATE,
order_qnty NUMBER(5),
CONSTRAINT ordmstr_oid_pk PRIMARY KEY ( order_id )
);
(
order_id NUMBER(3),
order_data DATE,
order_qnty NUMBER(5),
CONSTRAINT ordmstr_oid_pk PRIMARY KEY ( order_id )
);
Defining Primary Key using ALTER TABLE statements.
When we have already created table and then want to add primary key constraint then we use ALTER TABLE statement for adding Primary Key constraint.
Let’s say we have a table “Customer” with 3 columns Cust_id, cust_name, phone_no and we don’t have any Primary Key c onstraint on any column and now we want to add Primary Key constraint on cust_id column.
To do this task we will use ALTER TABLE statement as
To do this task we will use ALTER TABLE statement as
ALTER TABLE customer ADD CONSTRAINT cust_cid_pk PRIMARY KEY (cust_id);
Similarly if you want to define a composite primary key on customer table then
ALTER TABLE customer ADD CONSTRAINT cust_cid_pk PRIMARY KEY (cust_id,phone_number);
COMPOSITE PRIMARY KEY
As we already know that Primary Key which involves more than one column of a table is known as Composite primary Key.
We can only define composite primary key at table level. As composite key requires more than one column thus it becomes mandatory to define column of a table first and then primary key.
Let see how
Let see how
CREATE TABLE customer
(
cust_id NUMBER(3),
cust_name VARCHAR2(3),
phone_no NUMBER(10),
CONSTRAINT cust_cid_pk PRIMARY KEY ( cust_id, phone_no)
);
(
cust_id NUMBER(3),
cust_name VARCHAR2(3),
phone_no NUMBER(10),
CONSTRAINT cust_cid_pk PRIMARY KEY ( cust_id, phone_no)
);
How To Drop Primary Key Constraint
To drop primary key constraint we again use ALTER TABLE statement
Syntax for dropping a constraint is
Syntax for dropping a constraint is
ALTER TABLE table_name DROP CONSTRAINT constraint_name;
For Example.
ALTER TABLE customer DROP CONSTRAINT cust_cid_pk;
ENABLE & DISABLE PRIMARY KEY
You can enable or Disable primary key constraint by using constraint name with ALTER TABLE statement.
For example Let’s say you want to disable cust_cid_pk constraint which we earlier defined on Customer table.
For example Let’s say you want to disable cust_cid_pk constraint which we earlier defined on Customer table.
Disable Primary key constraint
ALTER TABLE customer DISABLE CONSTRAINT cust_cid_pk;
ALTER TABLE customer DISABLE CONSTRAINT cust_cid_pk;
or if you want to enable this constraint then simply write
Enable Primary key constraint
ALTER TABLE customer ENABLE CONSTRAINT cust_cid_pk;
ALTER TABLE customer ENABLE CONSTRAINT cust_cid_pk;
HOW TO CHECK CONSTRAINT ON A TABLE
As we know that DESC statement will show nothing about constraints on a table. But Oracle Database provides us several DATA DICTIONARIES for checking or Describing all the constraint which we have defined on our table.
These Data Dictionaries are
These Data Dictionaries are
16 USER_CONSTRAINTS
17 USER_CONS_COLUMNS
18 .
Where USER_CONSTRAINTS will give a brief about constraint on table. USER_CONS_COLUMNS is a Data Dictionary which holds information about columns of a table by using this Data dictionary you can have detail information about the constraint on a table.
For example
Let’s say you want to see constraint detail on customer table
For example
Let’s say you want to see constraint detail on customer table
USER_CONSTRAINTS
SELECT
CONSTRAINT_NAME,
CONSTRAINT_TYPE,
TABLE_NAME,
STATUS,
INDEX_NAME
FROM user_CONSTRAINTS WHERE table_name = 'CUSTOMER';
SELECT
CONSTRAINT_NAME,
CONSTRAINT_TYPE,
TABLE_NAME,
STATUS,
INDEX_NAME
FROM user_CONSTRAINTS WHERE table_name = 'CUSTOMER';
USER_CONS_COLUMNS
SELECT
CONSTRAINT_NAME,
TABLE_NAME,
COLUMN_NAME,
POSITION
FROM user_cons_columns WHERE table_name = 'CUSTOMER';
SELECT
CONSTRAINT_NAME,
TABLE_NAME,
COLUMN_NAME,
POSITION
FROM user_cons_columns WHERE table_name = 'CUSTOMER';
So that’s all about PRIMARY KEY. Hope it gives you a detailed insight into the concept. You can visit my YouTube channel for tutorials on Primary Key. Do like and Subscribe my videos.
No comments:
Post a Comment