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

Constraints

The document discusses Oracle constraints which are used to define conditions that must remain true when inserting, updating or deleting data in a database. Some key points: - Constraints include NOT NULL, unique, primary key, foreign key, check and REF constraints. - Constraints can be defined at the column or table level. - Constraints enforce data integrity by preventing invalid data and maintaining relationships between tables. - If a constraint is violated, the DML statement will be rolled back and an error returned. - Views cannot contain constraints but constraints on base tables can enforce rules on views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Constraints

The document discusses Oracle constraints which are used to define conditions that must remain true when inserting, updating or deleting data in a database. Some key points: - Constraints include NOT NULL, unique, primary key, foreign key, check and REF constraints. - Constraints can be defined at the column or table level. - Constraints enforce data integrity by preventing invalid data and maintaining relationships between tables. - If a constraint is violated, the DML statement will be rolled back and an error returned. - Views cannot contain constraints but constraints on base tables can enforce rules on views.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Data Base Management System & Oracle

Paper V
PGDCS&A

By Rupa Patel
Lecturer, MCA dept.
GHRIIT, Nagpur.
UNIT III -SQL
CODD’s Rules, oracle database objects, Sub Languages of SQL,
data Types, Operators.
DDL Statements : Creating tables, Deriving Table from Existing
table, Altering, Dropping Tables, Integrity Constraints,
Specifying Names for the constraints, Viewing Integrity
constraints ,adding & Dropping Constraints.
DML Statements : SELECT statement, Insert, update, Delete,&
Synonyms.
Functions : Arithmetic, Date, Character, Conversions. Single
row, Aggregate, Decode, Joins, Set operators & Sub queries.
DCL & TCL Statements : Grant, Revoke, Commit, Rollback &
Savepoints.
Constraints

Constraint is a mechanism used by Oracle to


prevent invalid data entry into the table.
Constraints are used for enforcing rules that
one or more columns in a table have to
conform with.
Oracle constraints are means in the process of
defining some conditions about the database
that must remain true while
inputting/modifying/deleting data in the
database.
Data Integrity

 Data Integrity means the condition in which


data are identically maintained during any
operation.
 Data integrity allows to define certain data
quality requirements that the data in the
database needs to meet.
 Data integrity is the assurance that data is
consistent and correct
Data Integrity Constraints
 Oracle uses integrity constraints to prevent
invalid data entry into the base tables of the
database .
 User can define integrity constraints to
enforce the business rules user want to
associate with the information in a database.
 If any of the results of a DML statement
execution violate an integrity constraint, then
Oracle rolls back the statement and returns
an error.
Data Integrity Constraints
 Oracle constraints can be attribute-based
(column), tuple - based (table), key based and
referential integrity based.
 As Oracle views are always dynamically
generated from their base tables, so the view
can not contain constraints.
 If there is a violation of the constraints caused
by some actions performed on the database,
then the Oracle constraints aborts the action
accordingly.
Constraints

 Data integrity allows to define certain data


quality requirements that the data in the
database needs to meet. If a user tries to
insert data that doesn't meet these
requirements, Oracle will not allow so.
Constraints
 Oracle constraints can be attribute-based
(column), tuple - based (table), key based and
referential integrity based.
 As Oracle views are always dynamically
generated from their base tables, so the view
can not contain constraints.
 If there is a violation of the constraints caused
by some actions performed on the database,
then the Oracle constraints aborts the action
accordingly.
 constraint
 Purpose
 Use a constraint to define an integrity constraint--a rule that restricts the values in a
database. Oracle Database lets you create six types of constraints and lets you
declare them in two ways.
 The six types of integrity constraint are described briefly here and more fully in
"Semantics":
 A NOT NULL constraint prohibits a database value from being null.
 A unique constraint prohibits multiple rows from having the same value in the same
column or combination of columns but allows some values to be null.
 A primary key constraint combines a NOT NULL constraint and a unique constraint
in a single declaration. That is, it prohibits multiple rows from having the same value
in the same column or combination of columns and prohibits values from being null.
 A foreign key constraint requires values in one table to match values in another
table.
 A check constraint requires a value in the database to comply with a specified
condition.
 .
 A REF column by definition references an object in another object type or in a relational table. A REF constraint
lets you further describe the relationship between the REF column and the object it references.
 You can define constraints syntactically in two ways:
 As part of the definition of an individual column or attribute. This is called inline specification.
 As part of the table definition. This is called out-of-line specification.
 NOT NULL constraints must be declared inline. All other constraints can be declared either inline or out of line.
 Constraint clauses can appear in the following statements:
 CREATE TABLE (see CREATE TABLE)
 ALTER TABLE (see ALTER TABLE)
 CREATE VIEW (see CREATE VIEW)
 ALTER VIEW (see ALTER VIEW )
 View Constraints Oracle Database does not enforce view constraints. However, you can enforce constraints on
views through constraints on base tables.
 You can specify only unique, primary key, and foreign key constraints on views, and they are supported only in
DISABLE NOVALIDATE mode. You cannot define view constraints on attributes of an object column.
 See Also:
 "View Constraints" for additional information on view constraints and "DISABLE Clause" for information on
DISABLE NOVALIDATE mode
 Prerequisites
 What are Oracle Constraints?
 Oracle constraints are means in the process of defining some conditions
about the database that must remain true while inputting/modifying/deleting
data in the database.
 Constraints are used to enforce table rules and prevent data dependent
deletion (enforce database integrity). You may also use them to enforce
business rules (with some magination).
 These Oracle constraints can be attribute-based (column), tuple-based
(table), key based and referential integrity based.
 As Oracle views are always dynamically generated from their base tables,
so the view can not contain constraints.
 If there is a violation of the constraints caused by some actions performed
on the database, then the Oracle constraints aborts the action accordingly.
The Oracle implementation of constraints differs from the SQL
implementation of these constraints.
 More on Oracle constraints:
 The basic structure of an Oracle constraint is defined as:
 The CONSTRAINT keyword is followed by a unique constraint name and then the constraint definition. The constraint name
is used to manipulate the constraint once the table has been created.
 In Oracle, constraints can be defined at the column or table level. An example of defining constraints at table level may be:
 CREATE TABLE STUDENT (
STUDENT _ID NUMBER(3) CONSTRAINT S_ID
CHECK (STUDENT _ID > 0),
STUDENT _NAME CHAR(30) CONSTRAINT S_NAME NOT NULL,
MARKS_COUNT NUMBER(6),
CONSTRAINT STUDENT _PRIME PRIMARY KEY (STUDENT _ID))
 We have now created our table with constraints. In this table, in the first attribute definition, after the CONSTRAINT
keyword, S_ID is the name of the attribute on which it has to applied and “CHECK” is the type of constraint followed by the
definition of that constraint to be followed for this table.
 Column level constraints go directly after the column definition to which they refer and the table level constraints go after the
last column definition.
 CREATE TABLE CLASS (
ROOM NUMBER(10) CONSTRAINT ID CHECK (ID BETWEEN 1 AND 2000),
SUBJECT VARCHAR2(200) CONSTRAINT S_TITLE NOT NULL,
CODE VARCHAR2(50) CONSTRAINT CODE NOT NULL,
ID NUMBER(8,2) DEFAULT 0.00 DISABLE,
CLASS_DATE DATE,
LAB_DATE DATE,
LECT_TAKEN NUMBER(6),
SUBJECT_ID NUMBER(3),
CONSTRAINT ROOM PRIMARY KEY (ISBN),
CONSTRAINT SUBJECT_SCORE FOREIGN KEY (SECTION_ID) REFERENCES SECTION(SECTION_ID))
 Table level Oracle constraints are used for compound foreign and prime key definitions.
 In the table given above, table level constraints could also have been placed as column definitions.
 In a table constraint, you may omit the CONSTRAINT keyword and constraint name if you wish. But if you
omit the constraint name then you will have no easy way of enabling / disabling the constraint without
deleting the table and rebuilding it.
 Oracle does give default names to constraints not explicitly named. This can be checked and verified by
selecting from the USER_CONSTRAINTS data dictionary view.
 Oracle supports the following constraints on tables and views:
 NOT NULL – This is always inherited directed from the base tables that make-up the view.
 Unique constraints – Oracle9i allows for unique constraints to be defined upon any column of the view.
 Primary key – Today we can get primary key constraints defined directly upon the view.
 Foreign key – Foreign key referential integrity is now directly available whenever a view has foreign key
dependencies against other base tables.
 Our two example tables do have some rules which need enforcing.
 Mainly, both tables have a primary key constraint so that the database doesn't allow replication of data.
 Similarly, the Section ID needs to be linked to each book to identify which library section it belongs to and
this supports foreign key.
 We also want to specify through Oracle constraints which columns must be filled in and which columns
have default values for other attributes.
 If we wish we can introduce cascading validation and some constraint violation logging to our tables.
 CREATE TABLE AUDIT (
ROWID ROWID,
OWNER VARCHAR2,
TABLE_NAME VARCHAR2,
CONSTRAINT VARCHAR2))
 CREATE TABLE SECTION (
SECTION_ID NUMBER(3) CONSTRAINT S_ID CHECK (SECTION_ID > 0),
SECTION_NAME CHAR(30) CONSTRAINT S_NAME NOT NULL,
BOOK_COUNT NUMBER(6),
CONSTRAINT SECT_PRIME PRIMARY KEY (SECTION_ID),
EXCEPTIONS INTO AUDIT)
 CREATE TABLE BOOK (
ISBN NUMBER(10) CONSTRAINT B_ISBN CHECK (ISBN BETWEEN 1 AND 2000),
TITLE VARCHAR2(200) CONSTRAINT B_TITLE NOT NULL,
AUTHOR VARCHAR2(50) CONSTRAINT B_AUTH NOT NULL,
COST NUMBER(8,2) DEFAULT 0.00 DISABLE,
LENT_DATE DATE,
RETURNED_DATE DATE,
TIMES_LENT NUMBER(6),
SECTION_ID NUMBER(3),
CONSTRAINT BOOK_PRIME PRIMARY KEY (ISBN),
CONSTRAINT BOOK_SECT FOREIGN KEY (SECTION_ID) REFERENCES SECTION(SECTION_ID)
ON DELETE CASCADE)
 Oracle does not allow us to delete a section which had books assigned to it as this breaks integrity rules.
 If we wanted to get rid of all the book records assigned to a particular section when that section was deleted we could implement a DELETE
CASCADE.
 The delete cascade operates across a foreign key link and removes all child records associated with a parent record.
 ALTER TABLE BOOK ENABLE CONSTRAINT B_AUTH
 The above statements demonstrate disabling and enabling a constraint: Note that if, between disabling a constraint and re enabling it, data
was entered to the table that included NULL values in the AUTHOR column, then you wouldn't be able to re enable the constraint.
 This is because the existing data would break the constraint integrity. You could update the column to replace NULL values with some
default and then re enable the constraint.
 Return from Oracle constraints to Oracle Database constraints
 Data integrity is a term used in computer science
and telecommunications that can mean ensuring
data is "whole" or complete, the condition in which
data are identically maintained during any operation
(such as transfer, storage or retrieval), the
preservation of data for their intended use, or,
relative to specified operations, the a priori
expectation of data quality. Put simply, data integrity
is the assurance that data is consistent and correct.

You might also like