Primary Key in SQL describes a column that individually identifies values in a Table. The Primary key cannot be null values and we can say that the Primary Key is the unique value.
For example, we have Name, Roll No., Section, subject, and House of the students.
So the question comes, what can be made as a primary key out of these attributes?
Well, as we all know Name, Section, House, Subject of an individual can be repeated. Two different Students can be present with the same name, same Section, same Subjects, and same Houses. However, the Roll No of a Student can not be the same for multiple students. Therefore, we can say that Roll No can be enough to identify the student. So, it can act as a Primary Key.
To create a Primary key follow the syntax below.
Primary Key Syntax in Oracle
column_name Datatype(size) PRIMARY KEY
Oracle Primary Key Example
We need to create a table Stuu with Attributes as:
- Name
- Section
- Roll No
- House
Wherein we want Roll No to be the Primary key of the table. Follow the below code for the same:
create table stuu(Name varchar(20),
Section Varchar(10),
Roll_no Number (20) PRIMARY KEY,
Subject Varchar(20));
Output
- Log in to post comments