Download presentation
Presentation is loading. Please wait.
1
SQL DATA CONSTRAINTS
2
DATA CONSTRAINTS Business rules, which are enforced on data being stored in a table, are called constraints. Business rules that are applied to data are completely System dependent.
3
APPLYING DATA CONSTRAINTS
Oracle permits data constraints to be attached to table column via SQL syntax. It checks data for integrity prior storage. Both the Create Table/ Alter Table SQL verb can be used to write sentences that attach constraints to a table column.
4
TYPES OF CONSTRAINTS Two types of constraints
(1).I/O constraint (input/output) This data constraint determines the speed at which data can be inserted or extracted from a oracle table (2). Business rule constraint.
5
I/0 CONSTRAINTS PRIMARY KEY constraints FOREIGN KEY (SELF REFERENCE)
UNIQUE KEY constraints
6
PRIMARY KEY A primary key is one or more column(s) in a table used to uniquely identify each row in the table. Null value can not be allowed. A table can have only one PRIMARY KEY. The data held across the column MUST be UNIQUE.
7
PRIMARY KEY A single column primary key is called Simple key.
A multicolumn primary key is called a Composite key. The only function of primary key in a table is to uniquely identify a row. Defined primary key in either a CREATE TABLE statement or an ALTER TABLE statement.
8
FEATURES OF PRIMARY KEY
Record Uniqueness Not allow duplicate values Not allow null values It is not compulsory but it is recommended PRIMARY KEY can not be LONG and LONG RAW data type. Only one key per table. Unique index is created automatically Combine up to 16 columns in composite primary key
9
PRIMARY KEY DEFINED AT COLUMN LEVEL
Syntax: <ColumnName> <Datatype>(<Size>) PRIMARY KEY Example: CREATE TABLE Persons (P_Id Number(10) PRIMARY KEY, LastName varchar2(255), FirstName varchar2(255), Address varchar2(255), City varchar2(255)) ;
10
PRIMARY KEY Insert into persons( 1,’CHINTAN’,’SHAH’,’SATELLITE’,’AHMEDABAD’); Row created Insert into persons(1,’SANJAY’,’PATEL’,’GANDHINAGAR’,’GANDHINAGAR’); ?
11
PRIMARY KEY DEFINED AT TABLE LEVEL
Syntax: PRIMARY KEY (<ColumnName>,<ColumnName>) Example: CREATE TABLE student (Id Number(10), Eno Number(10), LastName varchar2(255), FirstName varchar2(255), Address varchar2(255), City varchar2(255),PRIMARY KEY(Id,Eno)) ;
12
PRIMARY KEY Insert into persons( 1,100,’CHINTAN’,’SHAH’,’SATELLITE’,’AHMEDABAD’); Row created Insert into persons(1,200,’SANJAY’,’PATEL’,’GANDHINAGAR’,’GANDHINAGAR’); ?
13
FOREIGN KEY Foreign keys represent relationships between tables.
A foreign key is a column whose values are derived from the primary key or unique key of some other table. Foreign Table or Detail Table Primary Table or Master Table CREATE and ALTER
14
FEATURES OF FOREIGN KEY
Foreign key is a columns(s) that references a colum(s) of a table and it can be the same table also. Parent that is being referenced has to be unique or Primary key. Child may have duplicates and nulls but unless it specified. Foreign key constraint can be specified on child but not on parent.
15
Parent record can be delete provided no child record exist.
Master table can not be updated if child record exist. Record can not be inserted into a detail table if corresponding records in the master table do not exist. Records of the master table can not be deleted if corresponding records in the detail table actually exist.
16
Principles of Foreign key constraint
Rejects an INSERT or UPDATE of a value, if a corresponding value does not currently exist in the master key table. Rejects a DELETE from the master table if corresponding records in the DETAIL table exist. Must reference PRIMARY KEY or UNIQUE column(s) have matching data types.
17
FOREIGN KEY AT COLUMN LEVEL
Syntax <columnName><DataType(DataSize)> REFERENCES <TableName>[(<Columnname>)];. Let’s take an example for better understanding
18
Example Persons Table Order Table O_ID ORDERNO P-ID 1 12357 2 52863 3
77809 4 45824
19
FOREIGN KEY 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. The FOREIGN KEY constraint is used to prevent actions that would destroy links between tables.
20
FOREIGN KEY CONSTRAINT DEFINED AT COLUMN LEVEL
CREATE TABLE Orders ( O_ID NUMBER(5) PRIMARY KEY, ORDERNO NUMBER(5), P_ID NUMBER(6) REFERENCES Persons(P_ID) );
21
FOREIGN KEY CONSTRAINT DEFINED AT TABLE LEVEL
CREATE TABLE Orders ( O_ID NUMBER(5) PRIMARY KEY, ORDERNO NUMBER(5), P_ID NUMBER(6) FOREIGN KEY REFERENCES Persons(P_ID) )
22
UNIQUE KEY The Unique column constraint permits multiple entries of NULL into the column. The NULL values are clubbed at the top of the column in the order in which they were entered into the table.
23
UNIQUE KEY CONSTRAINTS
Unique key will not allowed duplicate values Unique index is created automatically. More than one unique key in a table. Unique key can combine upto 16 columns in a Composite unique key. Unique key can not be LONG or LONG RAW data type.
24
UNIQUE KEY DEFINED AT COLUMN LEVEL
Syntax: <ColumnName> <Datatype>(<Size>) UNIQUE Example: CREATE TABLE Persons (P_Id Number(10) UNIQUE, LastName varchar2(255), FirstName varchar2(255), Address varchar2(255), City varchar2(255)) ;
25
BUSINESS RULE CONSTRAINTS
ORACLE allows the application of business rules to table column. Example. Business rules can be implemented in ORACLE by using CHECK constraints. CHECK constraints bound to be COLUMN or TABLE using the CREATE TABLE OR ALTER TABLE command.
26
BUSINESS RULE CONSTRAINTS
ORACLE allows programmer to define constraints at: COLUMN LEVEL TABLE LEVEL
27
NULL VALUE Concepts A NULL value is different from a blank or a zero. A NULL value can be inserted in to columns or any data type.
28
Principles of NULL Values
Setting a NULL value is appropriate when the actual value is unknown or not a meaningful value. NULL value can be inserted into columns of any data type. If column has a NULL value oracle ignores any UNIQUE ,FOREIGN KEY, CHECK constraints that may be attach to the column.
29
Difference between Empty String and NULL values
Empty string is treated as a null value in oracle. Ex: CREATE TABLE PERSONS (ID VARCHAR2(10), NAME VARCHAR2(20)); INSERT VALUES: INSERT INTO PERSONS VALUES(‘A’,NULL); INSERT INTO PERSONS VALUES(‘B’,’’);
30
NOT NULL Constraint Defined At the Column Level
Oracle has NOT NULL as a column constraint The NOT NULL column constraint ensures that a table column cannot be left empty. Syntax : <ColumnName> <Datasize>(<size>) NOT NULL
31
NOT NULL Constraint Defined At the Column Level
EX : CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) );
32
The CHECK Constraint Business rule validations can be applied to a table column by using CHECK constraint. CHECK constraints must be specified as logical expression that a evaluates either to TRUE or FALSE
33
CHECK constraint defined at the column level
Syntax: <ColumnName> <Datatype>(<size>) CHECK (<Logical Expression>)
34
CHECK constraint defined at the column level
EX: CREATE TABLE Persons ( P_Id number(10) CHECK(P_Id>0), LastName varchar(255) CHECK(LastName=UPPER(LastName), FirstName varchar2(200) CHECK(FirstName=UPPER(FirstName) );
35
Restrictions on CHECK Constraints
The condition must be a Boolean expression that can be evaluated using the values in the row being inserted or updated. The condition cannot contain subqueries or sequences. The condition cannot include the SYSDATE , UID , USER OR USERENV SQL functions.
36
DEFAULT VALUE CONCEPTS
At the time of table creation a Default Value can be assigned to a column. Oracle engine will automatically load this column with the default value specified. The data type of the default value should match the data type of the column. The DEFAULT clause can be used to specify a default value fro a column.
37
DEFAULT VALUE CONCEPTS
Syntax : <ColumnName> <DataType>(<Size>) DEFAULT <Value>; EX: CREATE TABLE Persons ( P_Id number(10) DEFAULT 0, LastName varchar(255) DEFAULT ‘A’);
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.