Home
A primary key should be specified during table creation
Description
When creating a table in PL/SQL, it is important to specify a primary key. A primary key is a column or set of columns that uniquely identifies each row in a table. By specifying a primary key, it ensures that each row in the table is uniquely identified and can be easily referenced. This also helps to ensure data integrity, as it prevents duplicate entries from being created in the table. Additionally, it can help to improve query performance, as the primary key can be used to quickly locate a specific row in the table. Therefore, it is best practice to always specify a primary key when creating a table in PL/SQL.
Key Benefits
- Uniqueness: A primary key should be specified during table creation rule to ensure that each record is uniquely identified.
- Data Integrity: A primary key helps to maintain data integrity by ensuring that no duplicate records are created.
- Data Consistency: A primary key helps to maintain data consistency by ensuring that all records are consistent with each other.
Non-compliant Code Example
Create TABLE EMPLOYEE --Non compliant code (Table definition without primary key)
(
ID number(10) NOT NULL,
FIRSTNAME NVARCHAR2(75),
LASTNAME NVARCHAR2(75),
DEPT_ID int,
ADDRESS NVARCHAR2(250) NOT NULL
);
Compliant Code Example
Create TABLE EMPLOYEE --Compliant code (Table definition with primary key)
(
EMP_ID number(10) NOT NULL,
FIRSTNAME NVARCHAR2(75),
LASTNAME NVARCHAR2(75),
DEPT_ID int,
ADDRESS NVARCHAR2(250) NOT NULL,
CONSTRAINT employee_pk PRIMARY KEY (EMP_ID)
);