Primary key:
Primary key is a minimal set of attributes (columns) in a table that uniquely identifies
tuples (rows) inthat table.
Syntax:
CREATE TABLE <table_name>(<column_name><data_type> primary key,
<column_name2><data_type> CHECK <CONDITION>,..........);
Example:
create table employ(eno number(5) primary key,ename char(30),sal number(10));
Output:
Table created.
Insertion data into table:
insert into employ values(101,'kiran',7000);
Output: 1 row(s) inserted.
insert into employ values(102,'raju',9000);
Output: 1 row(s) inserted.
insert into employ values(101,'raju',9000);
Output:ORA-00001: unique constraint (SYSTEM.SYS_C004047) violated
Displaying data:
Select * from employ;
Output:
FOREIGN KEY:
Foreign keys are the columns of a table that points to the primary key of another table.
They act as a cross-reference between tables.
Syntax:
CREATE TABLE <table_name> (<column_name><data_type>references
tablename(column name),<column_name2><data_type>.........);
Example:
Create table empdept(eno number(5) references employ(eno),dname char(15));
Output: Table created.
Insertion data into table:
insert into empdept values(101,'hr');
Output:1 row(s) inserted.
insert into empdept values(102,'accounts');
Output:1 row(s) inserted.
insert into empdept values(103,'hr');
Output:ORA-02291: integrity constraint (SYSTEM.SYS_C004050) violated - parent key
not found.
Displaying data:
Select * from empdept;
Output: