SlideShare a Scribd company logo
1 of 25
Including Constraints  http://ecomputernotes.com
Objectives  After completing this lesson, you should be able to  do the following:  "  Describe constraints  "  Create and maintain constraints  http://ecomputernotes.com
What are Constraints?  "  Constraints enforce rules at the table level.  "  Constraints prevent the deletion of a table if there are  dependencies.  "T he following constraint types are valid:  ±  NOT NULL  ±  UNIQUE  ±  PRIMARY KEY  ±  FOREIGN KEY  ±  CHECK  http://ecomputernotes.com
Constraint Guidelines  "  Name a constraint or the Oracle server generates a  name by using the   SYS_C n   format.  "  Create a constraint either:  At the same time as the table is created, or  After the table has been created  "  Define a constraint at the column or table level.  "  View a constraint in the data dictionary.  http://ecomputernotes.com
Defining Constraints  CREATE TABLE [ schema .] table  ( column datatype  [DEFAULT   expr ]  [ column_constraint ],  [ table_constraint ][,...]);  CREATE TABLE employees( employee_id  NUMBER(6), first_name  VARCHAR2(20),  job_id  VARCHAR2(10) NOT NULL,  CONSTRAINT emp_emp_id_pk  PRIMARY KEY (EMPLOYEE_ID))  http://ecomputernotes.com
Defining Constraints  "  Column constraint level  column  [CONSTRAINT   constraint_name ]  constraint_type ,  "  Table constraint level  column,...  [CONSTRAINT   constraint_name ]  constraint_type  ( column , ...),  http://ecomputernotes.com
The   NOT NULL   Constraint  Ensures that null values are not permitted for the column:  «  NOT NULL   constraint  NOT NULL  Absence of   NOT NULL  (No row can contain  constraint  constraint  a null value for  (Any row can contain  this column.)  null for this column.)  http://ecomputernotes.com
The   NOT NULL   Constraint  Is defined at the column level:  CREATE TABLE employees(  employee_id  NUMBER(6),  System  last_name  VARCHAR2(25) NOT NULL,  named  salary  NUMBER(8,2),  commission_pct NUMBER(2,2), hire_date  DATE  User  CONSTRAINT emp_hire_date_nn  named  NOT NULL,  http://ecomputernotes.com
Th e  UNIQUE   Con st r ain t  «  Allowed  a lready exist  http://ecomputernotes.com
The   UNIQUE   Constraint  Defined at either the table level or the column level:  CREATE TABLE employees(  employee_id  NUMBER(6),  last_name  VARCHAR2(25) NOT NULL,  email  VARCHAR2(25),  salary  NUMBER(8,2),  commission_pct  NUMBER(2,2),  hire_date  DATE NOT NULL,  CONSTRAINT emp_email_uk UNIQUE(email));  http://ecomputernotes.com
The   PRIMARY KEY   Constraint  DEPARTMENTS  PRIMARY KEY  «  Not allowed  INSERT INTO  (Null value)  Not allowed  (50 already exists)  http://ecomputernotes.com
The   PRIMARY KEY   Constraint  Defined at either the table level or the column level:  CREATE TABLE  departments(  department_id  NUMBER(4),  department_name  VARCHAR2(30)  CONSTRAINT dept_name_nn NOT NULL,  manager_id  NUMBER(6),  location_id  NUMBER(4),  CONSTRAINT dept_id_pk PRIMARY KEY(department_id));  http://ecomputernotes.com
The   FOREIGN KEY   Constraint  DEPARTMENTS  IMARY  KEY  «  MPLOYEE  FOREI  KEY  Not allow  INSERT INTO  (9   does n  exist)  Allowed  http://ecomputernotes.com
The   FOREIGN KEY   Constraint  Defined at either the table level or the column level:  CREATE TABLE employees(  employee_id  NUMBER(6),  last_name  VARCHAR2(25) NOT NULL,  email  VARCHAR2(25),  salary  NUMBER(8,2),  commission_pct  NUMBER(2,2),  hire_date  DATE NOT NULL,  department_id  NUMBER(4),  CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id),  CONSTRAINT emp_email_uk UNIQUE(email));
FOREIGN KEY   Constraint Keywords  "  FOREIGN KEY : Defines the column in the child table at the table constraint level  "  REFERENCES : Identifies the table and column in the parent table  "  ON DELETE CASCADE : Deletes the dependent rows in the child table when a row in the parent table is  deleted.  "  ON DELETE SET NULL: Converts dependent foreign key values to null
The   CHECK   Constraint  "  Defines a condition that each row must satisfy  "  The following expressions are not allowed:  References to   CURRVAL ,   NEXTVAL ,   LEVEL , and   ROWNUM  pseudocolumns  Calls to   SYSDATE,   UID,   USER, and   USERENV   functions Queries that refer to other values in other rows  ..., salary  NUMBER(2)  CONSTRAINT emp_salary_min  CHECK (salary > 0),...
Adding a Constraint Syntax  Use the   ALTER TABLE   statement to:  "A dd or drop a constraint, but not modify its  structure  "  Enable or disable constraints  "  Add a   NOT NULL   constraint by using the   MODIFY  clause  ALTER TABLE   table  ADD [CONSTRAINT   constraint ]  type   (( column ););
Adding a Constraint  Add a   FOREIGN KEY   constraint to the   EMPLOYEES  table indicating that a manager must already exist as a valid employee in the   EMPLOYEES   table.  ALTER TABLE  employees  ADD CONSTRAINT  emp_manager_fk FOREIGN KEY(manager_id)  REFERENCES employees(employee_id);  Table altered.
Dropping a Constraint  "R emove the manager constraint from the  EMPLOYEES   table.  ALTER TABLE  employees  DROP CONSTRAINT  emp_manager_fk; Table altered.  "  Remove the   PRIMARY KEY   constraint on the  DEPARTMENTS   table and drop the associated  FOREIGN KEY   constraint on the  EMPLOYEES.DEPARTMENT_ID   column.  ALTER TABLE   departments  DROP PRIMARY KEY CASCADE; Table altered.
Disabling Constraints  "  Execute the   DISABLE   clause of the   ALTER TABLE  statement to deactivate an integrity constraint.  "  Apply the   CASCADE   option to disable dependent  integrity constraints.  ALTER TABLE  employees  DISABLE CONSTRAINT  emp_emp_id_pk CASCADE;  Table altered.
Enabling Constraints  "A ctivate an integrity constraint currently disabled  in the table definition by using the   ENABLE   clause.  ALTER TABLE  employees  ENABLE CONSTRAINT   emp_emp_id_pk; Table altered.  "A   U NIQUE  o r  P RIMARY KEY  i ndex is automatically  created if you enable a   UNIQUE   key or   PRIMARY  KEY   constraint.
Cascading Constraints  "T he  C ASCADE CONSTRAINTS  c lause is used along  with the   DROP COLUMN   clause.  "T he  C ASCADE CONSTRAINTS  c lause drops all  referential integrity constraints that refer to the primary and unique keys defined on the dropped  columns.  "  The   CASCADE CONSTRAINTS   clause also drops all multicolumn constraints defined on the dropped  columns.
Cascading Constraints  Example:  ALTER TABLE test1  DROP (pk) CASCADE CONSTRAINTS;  Table altered.  ALTER TABLE test1  DROP (pk, fk, col1) CASCADE CONSTRAINTS;  Table altered.
Viewing Constraints  Query the   USER_CONSTRAINTS   table to view all constraint definitions and names.  SELECT  constraint_name, constraint_type,  search_condition  FRO M  user_constraints  WHERE  table_name = 'EMPLOYEES';  «
Viewing the Columns Associated with Constraints  View the columns associated with the constraint names in the   USER_CONS_COLUMNS   view.  SELECT  constraint_name, column_name  FRO M  user_cons_columns  WHERE  table_name = 'EMPLOYEES';  «

More Related Content

What's hot

Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Thuan Nguyen
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueriesecomputernotes
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Thuan Nguyen
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statasLouis liu
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Thuan Nguyen
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatementsAnjac
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Thuan Nguyen
 
e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clauseecomputernotes
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Thuan Nguyen
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Thuan Nguyen
 
SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning OverviewKai Liu
 

What's hot (20)

Database
DatabaseDatabase
Database
 
Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06Oracle - Program with PL/SQL - Lession 06
Oracle - Program with PL/SQL - Lession 06
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03Oracle - Program with PL/SQL - Lession 03
Oracle - Program with PL/SQL - Lession 03
 
Contraints
ContraintsContraints
Contraints
 
e computer notes - Advanced subqueries
e computer notes - Advanced subqueriese computer notes - Advanced subqueries
e computer notes - Advanced subqueries
 
Pl sql using_xml
Pl sql using_xmlPl sql using_xml
Pl sql using_xml
 
Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10Oracle - Program with PL/SQL - Lession 10
Oracle - Program with PL/SQL - Lession 10
 
Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08Oracle - Program with PL/SQL - Lession 08
Oracle - Program with PL/SQL - Lession 08
 
Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07Oracle - Program with PL/SQL - Lession 07
Oracle - Program with PL/SQL - Lession 07
 
Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17Oracle - Program with PL/SQL - Lession 17
Oracle - Program with PL/SQL - Lession 17
 
Advanced tips of dbms statas
Advanced tips of dbms statasAdvanced tips of dbms statas
Advanced tips of dbms statas
 
Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13Oracle - Program with PL/SQL - Lession 13
Oracle - Program with PL/SQL - Lession 13
 
Basic sqlstatements
Basic sqlstatementsBasic sqlstatements
Basic sqlstatements
 
Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01Oracle - Program with PL/SQL - Lession 01
Oracle - Program with PL/SQL - Lession 01
 
e computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clausee computer notes - Enhancements to the group by clause
e computer notes - Enhancements to the group by clause
 
Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11Oracle - Program with PL/SQL - Lession 11
Oracle - Program with PL/SQL - Lession 11
 
Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16Oracle - Program with PL/SQL - Lession 16
Oracle - Program with PL/SQL - Lession 16
 
Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14Oracle - Program with PL/SQL - Lession 14
Oracle - Program with PL/SQL - Lession 14
 
SQL Tuning Overview
SQL Tuning OverviewSQL Tuning Overview
SQL Tuning Overview
 

Viewers also liked

Viewers also liked (9)

Teoria general de sistemas presentación
Teoria general de sistemas presentaciónTeoria general de sistemas presentación
Teoria general de sistemas presentación
 
SQL Server Transaction Management
SQL Server Transaction ManagementSQL Server Transaction Management
SQL Server Transaction Management
 
Caracteristicas de los Sistemas Familiares
Caracteristicas de  los Sistemas FamiliaresCaracteristicas de  los Sistemas Familiares
Caracteristicas de los Sistemas Familiares
 
La Familia
La FamiliaLa Familia
La Familia
 
Modelo Sistemico Familiar
Modelo Sistemico FamiliarModelo Sistemico Familiar
Modelo Sistemico Familiar
 
A must Sql notes for beginners
A must Sql notes for beginnersA must Sql notes for beginners
A must Sql notes for beginners
 
La familia
La familiaLa familia
La familia
 
La Familia como sistema
La Familia como sistemaLa Familia como sistema
La Familia como sistema
 
Teoría general de los sistemas
Teoría general de los sistemasTeoría general de los sistemas
Teoría general de los sistemas
 

Similar to Including Constraints in Oracle Database Tables

Similar to Including Constraints in Oracle Database Tables (20)

Les10
Les10Les10
Les10
 
Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
Les11
Les11Les11
Les11
 
Les02
Les02Les02
Les02
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
 
zekeLabs sql-slides
zekeLabs sql-slideszekeLabs sql-slides
zekeLabs sql-slides
 
1.Implementing_Data_Integrity.pdf
1.Implementing_Data_Integrity.pdf1.Implementing_Data_Integrity.pdf
1.Implementing_Data_Integrity.pdf
 
SQL
SQLSQL
SQL
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11
 
Data integrity
Data integrityData integrity
Data integrity
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
Check Constraints in MySQL 8.0. Presented at pre-FOSDEM MySQL Day 2020
 
Oracle SQL Fundamentals - Lecture 3
Oracle SQL Fundamentals - Lecture 3Oracle SQL Fundamentals - Lecture 3
Oracle SQL Fundamentals - Lecture 3
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Bn1017 a demo rdbms
Bn1017 a demo  rdbmsBn1017 a demo  rdbms
Bn1017 a demo rdbms
 
e computer notes - From multiple tables
e computer notes - From multiple tablese computer notes - From multiple tables
e computer notes - From multiple tables
 

More from ecomputernotes

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30ecomputernotes
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39ecomputernotes
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11ecomputernotes
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20ecomputernotes
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15ecomputernotes
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraintsecomputernotes
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functionsecomputernotes
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueriesecomputernotes
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objectsecomputernotes
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28ecomputernotes
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19ecomputernotes
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31ecomputernotes
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4ecomputernotes
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13ecomputernotes
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueriesecomputernotes
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functionsecomputernotes
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16ecomputernotes
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22ecomputernotes
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35ecomputernotes
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36ecomputernotes
 

More from ecomputernotes (20)

computer notes - Data Structures - 30
computer notes - Data Structures - 30computer notes - Data Structures - 30
computer notes - Data Structures - 30
 
computer notes - Data Structures - 39
computer notes - Data Structures - 39computer notes - Data Structures - 39
computer notes - Data Structures - 39
 
computer notes - Data Structures - 11
computer notes - Data Structures - 11computer notes - Data Structures - 11
computer notes - Data Structures - 11
 
computer notes - Data Structures - 20
computer notes - Data Structures - 20computer notes - Data Structures - 20
computer notes - Data Structures - 20
 
computer notes - Data Structures - 15
computer notes - Data Structures - 15computer notes - Data Structures - 15
computer notes - Data Structures - 15
 
Computer notes - Including Constraints
Computer notes - Including ConstraintsComputer notes - Including Constraints
Computer notes - Including Constraints
 
Computer notes - Date time Functions
Computer notes - Date time FunctionsComputer notes - Date time Functions
Computer notes - Date time Functions
 
Computer notes - Subqueries
Computer notes - SubqueriesComputer notes - Subqueries
Computer notes - Subqueries
 
Computer notes - Other Database Objects
Computer notes - Other Database ObjectsComputer notes - Other Database Objects
Computer notes - Other Database Objects
 
computer notes - Data Structures - 28
computer notes - Data Structures - 28computer notes - Data Structures - 28
computer notes - Data Structures - 28
 
computer notes - Data Structures - 19
computer notes - Data Structures - 19computer notes - Data Structures - 19
computer notes - Data Structures - 19
 
computer notes - Data Structures - 31
computer notes - Data Structures - 31computer notes - Data Structures - 31
computer notes - Data Structures - 31
 
computer notes - Data Structures - 4
computer notes - Data Structures - 4computer notes - Data Structures - 4
computer notes - Data Structures - 4
 
computer notes - Data Structures - 13
computer notes - Data Structures - 13computer notes - Data Structures - 13
computer notes - Data Structures - 13
 
Computer notes - Advanced Subqueries
Computer notes -   Advanced SubqueriesComputer notes -   Advanced Subqueries
Computer notes - Advanced Subqueries
 
Computer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group FunctionsComputer notes - Aggregating Data Using Group Functions
Computer notes - Aggregating Data Using Group Functions
 
computer notes - Data Structures - 16
computer notes - Data Structures - 16computer notes - Data Structures - 16
computer notes - Data Structures - 16
 
computer notes - Data Structures - 22
computer notes - Data Structures - 22computer notes - Data Structures - 22
computer notes - Data Structures - 22
 
computer notes - Data Structures - 35
computer notes - Data Structures - 35computer notes - Data Structures - 35
computer notes - Data Structures - 35
 
computer notes - Data Structures - 36
computer notes - Data Structures - 36computer notes - Data Structures - 36
computer notes - Data Structures - 36
 

Recently uploaded

Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfVanessa Camilleri
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfPatidar M
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...JojoEDelaCruz
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptxmary850239
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 

Recently uploaded (20)

FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdfICS2208 Lecture6 Notes for SL spaces.pdf
ICS2208 Lecture6 Notes for SL spaces.pdf
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Active Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdfActive Learning Strategies (in short ALS).pdf
Active Learning Strategies (in short ALS).pdf
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
ENG 5 Q4 WEEk 1 DAY 1 Restate sentences heard in one’s own words. Use appropr...
 
4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx4.16.24 Poverty and Precarity--Desmond.pptx
4.16.24 Poverty and Precarity--Desmond.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 

Including Constraints in Oracle Database Tables

  • 1. Including Constraints http://ecomputernotes.com
  • 2. Objectives After completing this lesson, you should be able to do the following: " Describe constraints " Create and maintain constraints http://ecomputernotes.com
  • 3. What are Constraints? " Constraints enforce rules at the table level. " Constraints prevent the deletion of a table if there are dependencies. "T he following constraint types are valid: ± NOT NULL ± UNIQUE ± PRIMARY KEY ± FOREIGN KEY ± CHECK http://ecomputernotes.com
  • 4. Constraint Guidelines " Name a constraint or the Oracle server generates a name by using the SYS_C n format. " Create a constraint either: At the same time as the table is created, or After the table has been created " Define a constraint at the column or table level. " View a constraint in the data dictionary. http://ecomputernotes.com
  • 5. Defining Constraints CREATE TABLE [ schema .] table ( column datatype [DEFAULT expr ] [ column_constraint ], [ table_constraint ][,...]); CREATE TABLE employees( employee_id NUMBER(6), first_name VARCHAR2(20), job_id VARCHAR2(10) NOT NULL, CONSTRAINT emp_emp_id_pk PRIMARY KEY (EMPLOYEE_ID)) http://ecomputernotes.com
  • 6. Defining Constraints " Column constraint level column [CONSTRAINT constraint_name ] constraint_type , " Table constraint level column,... [CONSTRAINT constraint_name ] constraint_type ( column , ...), http://ecomputernotes.com
  • 7. The NOT NULL Constraint Ensures that null values are not permitted for the column: « NOT NULL constraint NOT NULL Absence of NOT NULL (No row can contain constraint constraint a null value for (Any row can contain this column.) null for this column.) http://ecomputernotes.com
  • 8. The NOT NULL Constraint Is defined at the column level: CREATE TABLE employees( employee_id NUMBER(6), System last_name VARCHAR2(25) NOT NULL, named salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE User CONSTRAINT emp_hire_date_nn named NOT NULL, http://ecomputernotes.com
  • 9. Th e UNIQUE Con st r ain t « Allowed a lready exist http://ecomputernotes.com
  • 10. The UNIQUE Constraint Defined at either the table level or the column level: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, CONSTRAINT emp_email_uk UNIQUE(email)); http://ecomputernotes.com
  • 11. The PRIMARY KEY Constraint DEPARTMENTS PRIMARY KEY « Not allowed INSERT INTO (Null value) Not allowed (50 already exists) http://ecomputernotes.com
  • 12. The PRIMARY KEY Constraint Defined at either the table level or the column level: CREATE TABLE departments( department_id NUMBER(4), department_name VARCHAR2(30) CONSTRAINT dept_name_nn NOT NULL, manager_id NUMBER(6), location_id NUMBER(4), CONSTRAINT dept_id_pk PRIMARY KEY(department_id)); http://ecomputernotes.com
  • 13. The FOREIGN KEY Constraint DEPARTMENTS IMARY KEY « MPLOYEE FOREI KEY Not allow INSERT INTO (9 does n exist) Allowed http://ecomputernotes.com
  • 14. The FOREIGN KEY Constraint Defined at either the table level or the column level: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL, department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email));
  • 15. FOREIGN KEY Constraint Keywords " FOREIGN KEY : Defines the column in the child table at the table constraint level " REFERENCES : Identifies the table and column in the parent table " ON DELETE CASCADE : Deletes the dependent rows in the child table when a row in the parent table is deleted. " ON DELETE SET NULL: Converts dependent foreign key values to null
  • 16. The CHECK Constraint " Defines a condition that each row must satisfy " The following expressions are not allowed: References to CURRVAL , NEXTVAL , LEVEL , and ROWNUM pseudocolumns Calls to SYSDATE, UID, USER, and USERENV functions Queries that refer to other values in other rows ..., salary NUMBER(2) CONSTRAINT emp_salary_min CHECK (salary > 0),...
  • 17. Adding a Constraint Syntax Use the ALTER TABLE statement to: "A dd or drop a constraint, but not modify its structure " Enable or disable constraints " Add a NOT NULL constraint by using the MODIFY clause ALTER TABLE table ADD [CONSTRAINT constraint ] type (( column ););
  • 18. Adding a Constraint Add a FOREIGN KEY constraint to the EMPLOYEES table indicating that a manager must already exist as a valid employee in the EMPLOYEES table. ALTER TABLE employees ADD CONSTRAINT emp_manager_fk FOREIGN KEY(manager_id) REFERENCES employees(employee_id); Table altered.
  • 19. Dropping a Constraint "R emove the manager constraint from the EMPLOYEES table. ALTER TABLE employees DROP CONSTRAINT emp_manager_fk; Table altered. " Remove the PRIMARY KEY constraint on the DEPARTMENTS table and drop the associated FOREIGN KEY constraint on the EMPLOYEES.DEPARTMENT_ID column. ALTER TABLE departments DROP PRIMARY KEY CASCADE; Table altered.
  • 20. Disabling Constraints " Execute the DISABLE clause of the ALTER TABLE statement to deactivate an integrity constraint. " Apply the CASCADE option to disable dependent integrity constraints. ALTER TABLE employees DISABLE CONSTRAINT emp_emp_id_pk CASCADE; Table altered.
  • 21. Enabling Constraints "A ctivate an integrity constraint currently disabled in the table definition by using the ENABLE clause. ALTER TABLE employees ENABLE CONSTRAINT emp_emp_id_pk; Table altered. "A U NIQUE o r P RIMARY KEY i ndex is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint.
  • 22. Cascading Constraints "T he C ASCADE CONSTRAINTS c lause is used along with the DROP COLUMN clause. "T he C ASCADE CONSTRAINTS c lause drops all referential integrity constraints that refer to the primary and unique keys defined on the dropped columns. " The CASCADE CONSTRAINTS clause also drops all multicolumn constraints defined on the dropped columns.
  • 23. Cascading Constraints Example: ALTER TABLE test1 DROP (pk) CASCADE CONSTRAINTS; Table altered. ALTER TABLE test1 DROP (pk, fk, col1) CASCADE CONSTRAINTS; Table altered.
  • 24. Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. SELECT constraint_name, constraint_type, search_condition FRO M user_constraints WHERE table_name = 'EMPLOYEES'; «
  • 25. Viewing the Columns Associated with Constraints View the columns associated with the constraint names in the USER_CONS_COLUMNS view. SELECT constraint_name, column_name FRO M user_cons_columns WHERE table_name = 'EMPLOYEES'; «