34

Click here to load reader

oracle Sql constraint

  • Upload
    home

  • View
    2.281

  • Download
    0

Embed Size (px)

Citation preview

Page 1: oracle  Sql constraint

SQL ConstraintMade By:

Israa Al- JarrahCIS Dept-- IT Faculty

Page 2: oracle  Sql constraint

OUT LINE

SQL Constraint Meaning SQL Constraint Meaning

Constraint Type Constraint Type

Write Constraint Write Constraint

Page 3: oracle  Sql constraint

Constraint

Meaning :-

Constraints can be specified when a table is created (with the

CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement)Constraints are used to limit

the type of data that can go into a table.

Page 4: oracle  Sql constraint

Constraint Write:-

Constraint

[Constraint name]

Constraint Type

Page 5: oracle  Sql constraint

Constraint Implementation

Way :--

You can define constraints syntactically in two ways:1. As part of the definition of an individual column or attribute. This

is called inline specification.

2. As part of the table definition. This is called out-of-

line specification.

Page 6: oracle  Sql constraint

Constraint Type:-

Types of constraints: 1.Integrity constraints 2.Value constraintsTable constraint

Restricts data value with respect to all other values in table

Page 7: oracle  Sql constraint

1 .Not Null

2 .Unique Key

3 .Primary Key

4 .Foreign Key

5 .Check Key

6 .Default Key

Constraint Type:-

Page 8: oracle  Sql constraint

Constraint Type:-

Not Null

constraint enforces a column to NOT accept NULL values. constraint

enforces a column to NOT accept NULL values. This means that you cannot

insert a new record, or update a record without adding a value to

this field.Expression of Not Null we use short cut NN

Page 9: oracle  Sql constraint

Constraint Type:-

1 .Not Null Example-:

Create Table Language( Language_Name varchar2(30), Language_Id number(15) Constraint Constraint_Language_language_name_NN Not Null);

Page 10: oracle  Sql constraint

Constraint Implementation

Way :-- NOT NULL Constraints

& Default Constraint Must be declared inline.

Page 11: oracle  Sql constraint

Constraint Implementation

Way :--

All other constraints can be declared either inline or out of line.

Page 12: oracle  Sql constraint

Constraint Type:-

2 .Unique Key

The UNIQUE constraint uniquely identifies each

record in a database table.

Page 13: oracle  Sql constraint

Constraint Type:-

Example 2. Unique Key

Create Table Person( Person_Id number(15) Constraint Person_person_id_UN Unique Person_name varchar2(40), Person_BD Date);

Page 14: oracle  Sql constraint

Constraint Type:-

Example 2. Unique Key

Create Table Person( Person_Id number(15), Person_name varchar2(40), Person_BD Date

Constraint Person_person_id_UN Unique

(Person_Id));

Page 15: oracle  Sql constraint

Constraint Type:-

3 .Primary Key

Primary keys must contain unique values

A primary key column cannot contain NULL values.

Each table should have a primary key, and each table can

have only ONE primary key

Page 16: oracle  Sql constraint

Constraint Type:-

3 .Primary Key Example

CREATE TABLE locations_demo) location_id NUMBER(4) Constraint Constraint_loc_id_pk PRIMARY KEY , street_address VARCHAR2(40) , postal_code VARCHAR2(12) );

Page 17: oracle  Sql constraint

Constraint Type:-

3 .Primary Key Example

CREATE TABLE locations_demo) location_id NUMBER(4) , street_address VARCHAR2(40) , postal_code VARCHAR2(12) , Constraint Constraint_loc_id_pk PRIMARY KEY (Location_id));

Page 18: oracle  Sql constraint

Constraint Type:-

Unique Key Unique Key

Primary Key

Page 19: oracle  Sql constraint

Constraint Type:-

Unique Key Unique Key accept only one null value

unique key use many time in table.

Page 20: oracle  Sql constraint

Constraint Type:-

Unique Key Primary KeyPrimary key does not accept

null value primary key use only

one time in table.

Page 21: oracle  Sql constraint

Constraint Type:-

Unique Key4 .Foreign Key

A FOREIGN KEY in one table points to a PRIMARY KEY in another table

The FOREIGN KEY constraint is used to prevent actions that would destroy links between

tables.

Page 22: oracle  Sql constraint

Constraint Type:-

Unique Key

4 .Foreign Key Example

CIty Address FirstName LastName P-Id

Sandnes Timoteivn10

Ola Hansen 1

Sandnes Borgvn23 Tove Svendson 2

Sandnes Storagt 20 Kari Pettersen 3

The "Persons" table:

Page 23: oracle  Sql constraint

Constraint Type:-

Unique Key

4 .Foreign Key Example

P_id OrderNo O_Id

3 77895 1

3 44678 2

2 22456 3

1 24562 4

The “Order" table:

Page 24: oracle  Sql constraint

Constraint Type:-

4 .Foreign Key Example

Note"P_Id" column in the "Orders"

table points to the "P_Id" column in the "Persons" table.

The "P_Id" column in the "Persons" table is the PRIMARY KEY in the

"Persons" table.The "P_Id" column in the "Orders"

table is a FOREIGN KEY in the "Orders" table.

Page 25: oracle  Sql constraint

Constraint Type:-

4 .Foreign Key Example

First step must create “Person” Table

Create Table Person ( P_Id number Constraint constraint_person_person_Id_PK primary key, LastName varcahr2(45), FirstName varchar2(45), Address varchar2(45), City varchar2(45));

Page 26: oracle  Sql constraint

Constraint Type:-

4 .Foreign Key Example

Create Table Order (Order_id number(15) CONSTRAINT con_Order_id_pk Primary Key,OredrNO number(30) ,P_id number(15) CONSTRINT con_person_order_id_FK refernace Person (p_id));

Page 27: oracle  Sql constraint

Constraint Type:-

5 .Check Key

The CHECK constraint is used to limit the value range that can be

placed in a column. If you define a CHECK constraint

on a single column it allows only certain values for this column

Page 28: oracle  Sql constraint

Constraint Type:-

5 .Check Key Example

CREATE TABLE divisions (div_no NUMBER CONSTRAINT check_divno CHECK (div_no BETWEEN 10 AND 99),

div_name VARCHAR2(9) CONSTRAINT check_divname_cc CHECK (div_name = UPPER(div_name))

);

Page 29: oracle  Sql constraint

Constraint Type:-

5 .Check Key 6 .Default Key

The DEFAULT constraint is used to insert a default value into a

column The default value will be added to all new records

if no other value is specified.

Page 30: oracle  Sql constraint

Constraint Type:-

5 .Check Key 6 .Default Key Example

CREATE TABLE Persons( P_Id Number(15), LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes');

Page 31: oracle  Sql constraint

Composite Key

CONSTRAINT constraint_name PRIMARY KEY (columnname1, columnname2 …)

Page 32: oracle  Sql constraint

Composite Key

Example

Create table person(

Person_ID Number(15),Person_phone number(15),Constraint con_person_un

Unique(Person_ID,Person_Phone)

);

Page 34: oracle  Sql constraint

Thank you for listen

Israa jarrah