How to create a table and set a primary key in PLSQL?
To create a table and set a primary key in PL/SQL, you can use the CREATE TABLE statement and the ALTER TABLE statement. Here is an example:
Create a table and define the primary key.
CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
first_name VARCHAR2(50),
last_name VARCHAR2(50),
hire_date DATE
);
In the example above, we created a table named employees with the employee_id column set as the primary key.
Setting a primary key using the ALTER TABLE statement.
ALTER TABLE employees
ADD CONSTRAINT pk_employee_id PRIMARY KEY (employee_id);
In the example above, we set the employee_id column as the primary key of the employees table using the ALTER TABLE statement.
Please note that the primary key must be unique and not empty. When creating or setting a primary key, ensure the uniqueness and integrity of the data.