Data Constraints Final
Data Constraints Final
Data Constraints Final
Constraint : Rules which are enforced on data being entered, and prevents the user from entering invalid data into tables are called constraints. Thus, constraints super control data being entered in tables for permanent storage. Types of data constraints: Input/Output constraint (primary key /foreign key): this constraint determines the speed at which data can be inserted or extracted from oracle table Business Rule constraints: These rules are applied to data prior the data being inserted into the table columns.
Defining a constraint :
Oracle allows programmers to define constraints at: Column level Table level
Column Level: A column level constraint references a single column/attribute and is defined along with the definition of column/attribute. General Syntax:
Column datatype [CONSTRAINT constraint_name] constraint_type,
Table Level Constraints: If data constraints are defined after defining all the table columns when creating or altering a table structure , it is a table level constraint. General Syntax
[CONSTRAINT constraint_name] constraint_type(Column,),
Naming a constraint: <Table name>_<attribute name>_<constraint type> Ex: emp_deptno_fk refers to a constraint in table EMP on attribute DeptNo of type Foreign Key.
Oracle Server will automatically generate a name for constraint by using a format SYS_Cn, where n is unique number. NOT NULL Constraint: Principles of NULL value: A NULL values is different from a blank or zero. A NULL value can be inserted into the columns of any data type. (Read other detailed principles from book.) Not NULL Constraint can only be applied at column level. When a column is defined as a NOT NULL, then that column becomes mandatory column. It implies that a value must be entered into the column if the record is to be accepted for storage in the table. Syntax: columnname datatype(size) NOT NULL Example: CREATE TABLE client_master ( client_no varchar2(6) NOT NULL, Name varchar2(20) NOT NULL, Address varchar2(25)); It shows an error like: SQL> insert into client_master values(null,'palak','ahmd'); insert into client_master values(null,'palak','ahmd') * ERROR at line 1: ORA-01400: cannot insert NULL into ("SCOTT"."CLIENT_MASTER"."CLIENT_NO") The UNIQUE Constraint: UNIQUE Key Concepts: The purpose of the unique key is to ensure that information in the column is unique,i.e. a value entered in column defined in the unique constraint must not be repeated across the column. A table may have many unique keys. UNIQUE constraint defined at the column level:
Syntax: Columnname datatype (size) UNIQUE Example: CREATE TABLE client_master ( client_no varchar2(6) UNIQUE, Name varchar2(20)); ORA-00001: unique constraint (SCOTT.SYS_C001265) violated UNIQUE constraint defined at the table level: Syntax: UNIQUE (columnname [,columnname,] ) Example: CREATE TABLE client_master1 (client_no varchar2(6), Name varchar2(20), Address varchar2(20). UNIQUE(client_no)); Main difference between primary key and unique constraint is: unique constraint allows NULL values. Primary Key Constraint : Primary Key Concepts: A primary key is one or more column(s) in a table used to uniquely identify each row in the table. A primary key in a table has special attributes: o It defines a column as a mandatory column i.e. the column can not be left blank. o The NOT NULL attribute is active. o The data held across the column must be unique.
Simple key: A single column primary key is known as simple key. Composite key: A multicolumn primary key is called a composite primary key. PRIMARY KEY constraint defined at the column level: Syntax: columnname datatype(size) PRIMARY KEY Example: CREATE TABLE sales_order ( order_no varchar2(6) PRIMARY KEY, Order_date date, client_no varchar2(6)); PRIMARY KEY constraint defined at the table level: Syntax: PRIMARY KEY (columnname [,columnname,]) Example: (simple key) CREATE TABLE sales_order_details ( order_no varchar2(6), Order_date date, client_no varchar2(6), PRIMARY KEY (order_no)); (composite primary key) CREATE TABLE sales_order_details ( order_no varchar2(6), Order_date date, client_no varchar2(6), PRIMARY KEY (order_no,client_no)); FOREIGN KEY Constraints: Foreign Key Concepts: A foreign key is a column (or a group of columns) whose values are derived from the primary key or unique key of some other table.
Foreign keys represent relationships between tables. FOREIGN KEY constraint defined at the column level: Syntax: Columnname datatype (size) REFERENCES tablename [(columnname)] [ON DELETE CASCADE] Example: CREATE TABLE sales_order_details (detlorder_no varchar2(6) REFERENCES sales_order, Product_no varchar2(6), Qty_ordered number(8)); . FOREIGN KEY Constraint defined at the table level: Syntax: FOREIGN KEY(columnname [,columnname]) REFERENCES tablename [(columnname [,columnname]) Example: CREATE TABLE sales_order_details (detlorder_no varchar2(6), Product_no varchar2(6), Qty_ordered number(8), FOREIGN KEY(detlorder_no) REFERENCES sales_order ); CHECK Constraint: Business rule validations can be applied to a table column by using CHECK constraint . CHECK constraint must be specified as a logical expression theat evaluated either to TRUE or FALSE.
CHECK Constraint defined at the column level: Syntax: Example: Create a table client_master with following check constraints: o Data values being inserted into the column client_no must start with the capital letter C. o Data values being inserted into the column name should be in upper case only. o Only aloe Bombay,Delhi,Madras as legitimate values for the column city. CREATE TABLE client_master ( client_no varchar2(6) CHECK (client_no like C%), Name varchar2(20) CHECK (name=upper(name)), City varchar2(15) CHECK (city IN (Delhi,Bombay,Madras)), Remarks varchar2(50)); columnname datatype (size) CHECK (logical expression)
CHECK Constraint defined at the table level: Syntax: CHECK (logical expression) Example: CREATE TABLE client_master (client_no varchar2(6), Name varchar2(20), City varchar2(15), State varchar2(15), CHECK (client_no like C%), CHECK (name=upper(name)), CHECK (city IN (Delhi,Bombay,Madras)));