Bn1017 a demo rdbms

13
Introduction to Oracle Constraints Concepts BN1017 A Demo PPT Demo Oracle Constraints

Transcript of Bn1017 a demo rdbms

Page 1: Bn1017 a demo  rdbms

Introduction to Oracle Constraints Concepts

BN1017 A – Demo PPT

Demo Oracle Constraints

Page 2: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Oracle Constraints Types

Constraints: -- Constraints apply specific rules to data, ensuring the data conforms to the

requirements defined. There are a number of different kinds of constraints.

There are two ways to apply constraint into Database.

1)Column level Constraint.

2)Table Level Constraint.

Page 3: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Types Of Constraints

• Not NULL

• Primary key

• Unique

• Foreign Key

• Check

• Let's look at each of these in a little more detail.

Page 4: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Referential Integrity

Page 5: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

NOT NULL ConstraintsNOT NULL constraints are in-line constraints that indicate that a column can not contain NULL

values. For example, the PERSON_ID column is defined as NOT NULL in that example.

If you need to add a NOT NULL constraint to a table after the fact, simply use the alter table co

mmand as in this example:

Syntax:-

create table

customer

(status char(3) not null,

val number not null);

Alter Table Table Name Modify( person_id NOT NULL);

When copying tables with CTAS, beware that NULL values many not copy properly.

Page 6: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Primary Key Constraints

Primary key constraints define a column or series of columns that uniquely

identify a given row in a table. Defining a primary key on a table is optional and

you can only define a single primary key on a table. A primary key constraint can

consist of one or many columns (up to 32). Any column that is defined as a

primary key column is automatically set with a NOT NULL status.

Syntax:-create table

customer

(status_id Number CONSTRAINT CONSTRAINT _NAME

);

If you need to primary key constraint to a table after the fact, simply use the alter t

able command.

ALTER TABLE customer ADD CONSTRAINT pk_my_status

PRIMARY KEY (status_id);

Page 7: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Unique Constraints

Unique constraints are like alternative primary key constraints.

A unique constraint defines a column, or series of columns, that must be unique in value.

You can have a number of unique constraints defined and the columns can have NULL values

in them, unlike a column that belongs to a primary key constraint.

If you need to add unique key constraints to a table after the fact, simply use the alter table

command.

ALTER TABLE

my_status

ADD CONSTRAINT

uk_my_status

UNIQUE

(status_id,

person_id);

Page 8: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Foreign Key Constraints

A foreign key constraint is used to enforce a relationship between two tables. As an example,

take the case of two tables, ITEM and PART. These tables have a relationship (an item can have

none, one or many parts). Foreign key constraints help to enforce that relationship

Well, foreign key constraints help to enforce the types of relationships between tables we have

just demonstrated. In this example we will create the ITEM and PART table. In the process of

doing so, we will create a foreign key relationship between the two:

CREATE TABLE part

( Part_no NUMBER PRIMARY KEY,

Part_desc VARCHAR2(200) NOT NULL );

Page 9: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Cont..

CREATE TABLE item (Item_no NUMBER,Part_no NUMBER,Item_desc varchar2(200) NOT

NULL, CONSTRAINT fk_item_part FOREIGN KEY (part_no) REFERENCES PART (part_no),

CONSTRAINT pk_item PRIMARY KEY (item_no, part_no) );

In this example, what we are really interested in is the creation of the ITEM table. First, note

that we defined the primary key as an out of line primary key. This is because it is a composite

primary key and composite primary keys have to be defined out of line.

Now, we are interested in the foreign key definition. You must define foreign key constraints as

out of line constraints, as we have done in our example. Here is a snippet of the command that

we used:

CONSTRAINT fk_item_part FOREIGN KEY (part_no) REFERENCES part (part_no);

Page 10: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Constraints

If you need to add foreign key constraints to a table after the fact, simply use the alter table

command as seen here:

ALTER TABLE my_status ADD CONSTRAINT fk_my_status

FOREIGN KEY (part_no) REFERENCES part (part_no);

Page 11: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Check Constraints

Check constraints validate that values in a given column meet a specific criteria.

For example, you could create a check constraint on a varchar2 column so it

only can contain the values T or F as in this example:

Create table my_status

( status_id NUMBER PRIMARY KEY,

person_id NUMBER NOT NULL,

active_record VARCHAR2(1) NOT NULL

CHECK (UPPER(active_record)='T' or

UPPER(active_record)='F'),

person_ssn VARCHAR2(20) CONSTRAINT un_person_ssn UNIQUE

);

Page 12: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Questions ???

Page 13: Bn1017 a demo  rdbms

http://www.conlinetraining.com/courses/rdbms-online-training/

Email us : [email protected]

Visit : www.conlinetraining.com