Primary Key

Primary Key 


Primary Key :- Primary key is a combination of NOT NULL and UNIQUE Key Constraint, Primary key never allow duplicate values and null values in the column.


1) Primary Key (Column Level) :-

a) With System defined name


SQL>create table student(regno number(10) primary key, name varchar2(10),dob date, course varchar2(10));


b) With User defined name


SQL>create table student(regno number(10) constraint pk_regno primary key,name varchar2(10),dob date,course varchar2(10));


2) Primary Key (Table Level) :-


a) With System defined name


SQL>create table student(regno number(10),name varchar2(10),dob date,course varchar2(10), primary key(regno));


b) With User defined name


SQL>create table student(regno number(10),name varchar2(10),dob date,course varchar2(10),constraint pk_regno primary key(regno));


3) Composite Primary Key :- Key, that is created for more that one column is called composite key. If Primary Key is created for more than one column then that Primary Key is called Composite Primary Key.


a) With System defined name


SQL>create table student(regno number(10),name varchar2(10),dob date,course varchar2(10), primary key(regno,name));


b) With User defined name


SQL>create table student(regno number(10),name varchar2(10),dob date,course varchar2(10),constraint pk_regno primary key(regno,name));

                  



--------------------------HAPPY LEARNING -----------------------------


                                                                                                   **THANK YOU**

Primary Key and Foreign Key

 

Primary Key and Foreign Key

Difference between Primary Key and Foreign Key:-

Primary Key :- Primary Key is a column/columns in a table which can uniquely identify the records, it never accept duplicate value and never accept null values.

Foreign Key :- Foreign Key is a column/columns in table which is already defined as a primary/unique key in a another/same table.

1) Primary Key is used for uniquely identified the record where as Foreign key is to ensure the column values referenced from Primary key column

2) Primary Key never accept Null value where as Foreign Key accept Null Value

3) In a table only 1 Primary Key can be defined where as more than 1 foreign key can be defined in a table.

4) Primary never accept duplicate values where as Foreign accept duplicate records