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

Constraints Simple

The document discusses how to define various constraints when creating tables in Oracle including primary keys, unique constraints, not null constraints, check constraints, and foreign keys. It provides examples of creating these constraints both when initially creating the table and by altering existing tables. Useful data dictionary views are also listed that can provide metadata about existing constraints.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Constraints Simple

The document discusses how to define various constraints when creating tables in Oracle including primary keys, unique constraints, not null constraints, check constraints, and foreign keys. It provides examples of creating these constraints both when initially creating the table and by altering existing tables. Useful data dictionary views are also listed that can provide metadata about existing constraints.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

drop table student;

create table student(roll_num number,name varchar2(20),dept_id number,dept


varchar2(20));

PRIMARY KEY:

create table student(roll_num number PRIMARY KEY,name varchar2(20),dept_id


number,dept varchar2(20));
create table student(roll_num number CONSTRAINT PK_ROLL PRIMARY KEY,name
varchar2(20),dept_id number,dept varchar2(20));

create table student(roll_num number,name varchar2(20),dept_id number,dept


varchar2(20),CONSTRAINT PK_ROLL PRIMARY KEY(roll_num,dept_id));

alter table student ADD CONSTRAINT PK_ROLL PRIMARY KEY(roll_num);


alter table student DROP CONSTRAINT SYS_C008327;
alter table student DROP CONSTRAINT PK_ROLL;

UNIQUE:

create table student(roll_num number UNIQUE,name varchar2(20),dept_id number,dept


varchar2(20));
create table student(roll_num number CONSTRAINT UNIQ_ROLL UNIQUE,name
varchar2(20),dept_id number,dept varchar2(20));

create table student(roll_num number,name varchar2(20),dept_id number,dept


varchar2(20),CONSTRAINT UNIQ_ROLL UNIQUE(roll_num,dept_id));

alter table student ADD CONSTRAINT UNIQ_ROLL UNIQUE(roll_num);


alter table student DROP CONSTRAINT SYS_C008326;

NOT NULL:

create table student(roll_num number NOT NULL,name varchar2(20),dept_id number,dept


varchar2(20));
create table student(roll_num number CONSTRAINT NN_ROLL NOT NULL,name
varchar2(20),dept_id number,dept varchar2(20));

create table student(roll_num number ,name varchar2(20),dept_id number,dept


varchar2(20),CONSTRAINT NN_ROLL NOT NULL(roll_num));

alter table student ADD CONSTRAINT NN_ROLL NOT NULL(roll_num);


alter table student DROP CONSTRAINT NN_ROLL;

CHECK:
create table student(roll_num number,name varchar2(20),dept_id number CHECK(dept_id
IN (10,20,30,40,50)),dept varchar2(20));
create table student(roll_num number,name varchar2(20),dept_id number CONSTRAINT
CHK_DEPT CHECK(dept_id IN (10,20,30,40,50)),dept varchar2(20));
create table student(roll_num number,name varchar2(20),dept_id number,dept
varchar2(20),CONSTRAINT CHK_DEPT CHECK(dept_id IN (10,20,30,40,50)));

FOREIGN KEY:

drop table student;


drop table subject;
create table student(roll_num number ,name varchar2(20),subject_id number,dept
varchar2(20));
create table subject(sub_id number,subject varchar2(20));

create table student(roll_num number ,name varchar2(20),subject_id number,dept


varchar2(20),CONSTRAINT PK_SID PRIMARY KEY(subject_id));
create table subject(sub_id number ,subject varchar2(20),CONSTRAINT FK_SID FOREIGN
KEY(sub_id) REFERENCES STUDENT(subject_id));

create table subject(sub_id number ,subject varchar2(20));


--Add foreign key
alter table subject ADD CONSTRAINT FK_SID FOREIGN KEY(sub_id) REFERENCES
STUDENT(subject_id) ON DELETE CASCADE;
alter table student DROP CONSTRAINT PK_SID;
alter table subject DROP CONSTRAINT FK_SID;

Some useful Data Dictionaries:


select * from USER_CONSTRAINTS where table_name='STUDENT';
select * from USER_CONS_COLUMNS where table_name='STUDENT';

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy