0% found this document useful (0 votes)
40 views

Primary Key Constraint:: Output

This document discusses different types of constraints in Oracle: primary key, unique, check, and foreign key. It provides examples of creating tables with these constraints and sample insert statements that violate the constraints, resulting in error messages. Primary key constraints require column values to be unique and not null. Unique constraints require column values to be unique but can be null. Check constraints validate column values meet certain conditions. Foreign key constraints require values in one table match values in a referenced column in another table.

Uploaded by

rishikamate
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Primary Key Constraint:: Output

This document discusses different types of constraints in Oracle: primary key, unique, check, and foreign key. It provides examples of creating tables with these constraints and sample insert statements that violate the constraints, resulting in error messages. Primary key constraints require column values to be unique and not null. Unique constraints require column values to be unique but can be null. Check constraints validate column values meet certain conditions. Foreign key constraints require values in one table match values in a referenced column in another table.

Uploaded by

rishikamate
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Primary key constraint:

create table employee(id varchar(20),Name varchar(50),comp_nm varchar(20),constraint


pk_employee primary key(id)) ;

Output:
Table created

ID NAME COMP_NM
1 Rishi IBM
2 Rohit Intel
3 Mahesh Oracle
4 Sagar Persistence
5 Suraj Infosys

insert into employee (id,name,comp_nm) values ('1','Rishi','IBM');

insert into employee (id,name,comp_nm) values ('1','Rishi','IBM')


*
ERROR at line 1:
ORA-00001: unique constraint (TE30.PK_EMPLOYEE) violated

Unique key constraint:


create table company(id varchar(20),Name varchar(50),place varchar(20),constraint
uk_company unique(id),constraint pk_company primary key(name)) ;

Output:

Table created.

ID NAME PLACE
1 IBM Bangalore
2 Intel Bangalore
3 Infosys hinjawadi
4 Wipro hinjawadi
5 Oracle Hyderabad
insert into company (id, name,place) values ('1','Quickheal','pune');

insert into company (id,name,place) values ('1','Quickheal','pune')


*
ERROR at line 1:
ORA-00001: unique constraint (TE30.UK_COMPANY) violated

Check key constraint:


create table college(name char(20),id number(10),constraint ck_college check(id<10));

Output:

Table created.

NAME ID
DYPIET 1
DYCOE 2
JSPM 4

insert into college(name,id) values ('Shahu','11');

insert into college(name,id) values ('Shahu','11')


*
ERROR at line 1:
ORA-02290: check constraint (TE30.CK_COLLEGE) violated

Foreign key constraint:


CREATE TABLE DEPT1(DEPT_ID NUMBER(6),DEPT_NM
VARCHAR(20),CONSTRAINT UK_DEPT1 UNIQUE(DEPT_ID));

Table created.

SQL> SELECT * FROM DEPT1;

DEPT_ID DEPT_NM
---------- --------------------
1 IT
2 COMP
3 ENTC
SQL> CREATE TABLE EMP1 (EMP_ID NUMBER (10), LAST_NM
VARCHAR (20), EMAIL VARCHAR (20), DEPT_ID NUMBER (20),
2 CONSTRAINT EMP1_DEPT1_FK FOREIGN KEY (DEPT_ID) REFERENCES
DEPT1 (DEPT_ID),
3 CONSTRAINT EMP1_EMAIL_UK UNIQUE (EMAIL));

Table created.

SQL> INSERT INTO EMP1 VALUES(1,'RISHI','[email protected]',21);


INSERT INTO EMP1 VALUES(1,'RISHI','[email protected]',21)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.EMP1_DEPT1_FK) violated - parent key not
found

SQL> INSERT INTO EMP1 VALUES(1,'RISHI','[email protected]',1);

1 row created.

SQL> SELECT * FROM EMP1;

EMP_ID LAST_NM EMAIL DEPT_ID


---------- ------------------- -------------------- ----------
1 RISHI [email protected] 1
2 MAHADEV [email protected] 2
3 VIKAS [email protected] 3

You might also like