oracle primary key | Primary Key - oracle tutorial - sql tutorial
![Oracle create table with only primary key](https://wikitechy.com/tutorials/oracle/img/oracle-images/oracle-create-table-with-only-primary-key.gif)
Learn oracle - oracle tutorial - Oracle create table with only primary key - oracle examples - oracle programs
What is oracle primary key ?
- A primary key is a single field or combination of fields that uniquely defines a record.
- A primary key column cannot contain NULL values.
- Most tables should have a primary key, and each table can have only ONE primary key.
![Oracle primary key](https://wikitechy.com/tutorials/oracle/img\oracle-images\primary-key-in-sql.jpg)
Oracle primary key and foreign key
Syntax:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ... column_n)
);
click below button to copy the code. By - oracle tutorial - team
![Oracle constrains primary key](https://wikitechy.com/tutorials/oracle/img/oracle-images/oracle-constrains-primary-key.gif)
Learn oracle - oracle tutorial - Oracle constrains primary key - oracle examples - oracle programs
oracle tutorial , sql tutorial , sql , pl sql tutorial , oracle , pl sql , plsql
Example:
CREATE TABLE wikitechy_emp(
emp_id number(1) PRIMARY KEY,
emp_name varchar(20),
emp_designation varchar(20))
click below button to copy the code. By - oracle tutorial - team
Creating Primary key in the Table:
![Oracle primary key query](https://wikitechy.com/tutorials/oracle/img\oracle-images\oracle-primary-key-query.png)
- In the create table statement, PRIMARY KEY constraint is applied for emp_id column.
![Oracle primary key insert query](https://wikitechy.com/tutorials/oracle/img\oracle-images\oracle-primary-key-insert-query.png)
- We are inserting id value 1, name value as ARUN and designation value as project manager into a wikitechy_emp table using insert into keyword.
![Oracle primary key insert query with another values](https://wikitechy.com/tutorials/oracle/img\oracle-images\oracle-primary-key-insert-query-with-another-values.png)
- Here, when we insert a same value for emp_id which you have already inserted in your first row. The unique constraint (PRIMARY KEY) reject your insertion of data into the wikitechy_emp table and shows a violated message.