SlideShare a Scribd company logo
1 of 25
Including Constraints
Objectives After completing this lesson, you should be able to do the following: Describe constraints Create and maintain constraints
What Are Constraints? Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid in Oracle: NOT NULL UNIQUE  PRIMARY KEY FOREIGN KEY CHECK
Constraint Guidelines Name a constraint or the Oracle Server will generate a name by using the SYS_Cn format. Create a constraint: At the same time as the table is created After the table has been created Define a constraint at the column or table level. View a constraint in the data dictionary.
Defining Constraints CREATE TABLE [schema.]table 	    (columndatatype [DEFAULT expr] 		[column_constraint], 		... 		[table_constraint][,...]); CREATE TABLE emp(      empno  NUMBER(4),      ename  VARCHAR2(10),   	     ...      deptno  NUMBER(7,2) NOT NULL,      CONSTRAINT emp_empno_pk  PRIMARY KEY (EMPNO));
Defining Constraints Column constraint level Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type   (column, ...),
The NOT NULL Constraint NOT NULL constraint (no row can containa null value forthis column) Absence of NOT NULL constraint (any row can containnull for this column) NOT NULL constraint Ensures that null values are not permitted for the column EMP  EMPNO ENAME JOB		 ...  COMM  DEPTNO      7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20   ...
The NOT NULL Constraint Defined at the column level SQL> CREATE TABLE emp( 2empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL);
The UNIQUE Key Constraint Insert into Not allowed (DNAME-SALES already exists) 50SALESDETROIT 60BOSTON Allowed UNIQUE key constraint DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
The UNIQUE Key Constraint Defined at either the table level or the column level  SQL> CREATE TABLE   dept( 2deptno   NUMBER(2), 3dname  VARCHAR2(14), 4loc  VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE(dname));
The PRIMARY KEY Constraint Insert into Not allowed (DEPTNO-20 already exists) 20MARKETINGDALLAS FINANCENEW YORK Not allowed(DEPTNO is null) PRIMARY KEY DEPT  DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
The PRIMARY KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE   dept( 2deptno   NUMBER(2), 3dname  VARCHAR2(14), 4loc  VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE (dname), 6CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
The FOREIGN KEY Constraint Not allowed(DEPTNO9 does not exist in the DEPT table) 7571FORDMANAGER	 ...  2009 7571FORDMANAGER	 ...  20020 Insert into Allowed DEPT  PRIMARYKEY DEPTNO DNAME     LOC      ------ ----------	-------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS   ... EMP  EMPNO ENAME JOB		 ...  COMM  DEPTNO      7839KINGPRESIDENT10 7698BLAKEMANAGER30   ... FOREIGNKEY
The FOREIGN KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE emp( 2empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL, 10CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno) 11REFERENCES dept (deptno));
FOREIGN KEY Constraint Keywords FOREIGN KEY Defines the column in the child table atthe table constraint level REFERENCES Identifies the table and column in the parent table ON DELETE CASCADE Allows deletion in the parent table and deletion of the dependent rows in the child table
The CHECK Constraint Defines a condition that each row must satisfy Expressions that 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 ..., deptnoNUMBER(2),       CONSTRAINT emp_deptno_ck               CHECK (DEPTNO BETWEEN 10 AND 99),...
Adding a Constraint   ALTER TABLEtable   ADD [CONSTRAINT constraint] type (column); Add or drop, but not modify, a constraint Enable or disable constraints Add a NOT NULL constraint by using the MODIFY clause
Adding a Constraint Add a FOREIGN KEY constraint to the EMP table indicating that a manager must already exist as a valid employee in the EMP table. SQL> ALTER TABLE     emp 2  ADD CONSTRAINT  emp_mgr_fk  3FOREIGN KEY(mgr) REFERENCES emp(empno); Table altered.
Dropping a Constraint Remove the manager constraint from the EMP table. SQL> ALTER TABLE  emp 2  DROP CONSTRAINT  emp_mgr_fk; Table altered. ,[object Object],SQL> ALTER TABLEdept 2  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. SQL> ALTER TABLEemp 2  DISABLE CONSTRAINTemp_empno_pk CASCADE; Table altered.
Enabling Constraints Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause.  A UNIQUE or PRIMARY KEY index is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint. SQL> ALTER TABLEemp 2  ENABLE CONSTRAINTemp_empno_pk; Table altered.
Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. SQL>  SELECTconstraint_name, constraint_type, 2search_condition 3   FROMuser_constraints 4   WHEREtable_name = 'EMP'; CONSTRAINT_NAME          C SEARCH_CONDITION ------------------------ - -------------------------  SYS_C00674               C EMPNO IS NOT NULL   SYS_C00675               C DEPTNO IS NOT NULL EMP_EMPNO_PK     P ...
Viewing the Columns Associated with Constraints View the columns associated with the constraint names in the USER_CONS_COLUMNS view. SQL> SELECTconstraint_name, column_name 2  FROMuser_cons_columns 3  WHEREtable_name = 'EMP'; CONSTRAINT_NAME           COLUMN_NAME ------------------------- ---------------------- EMP_DEPTNO_FK             DEPTNO EMP_EMPNO_PK              EMPNO EMP_MGR_FK                MGR SYS_C00674                EMPNO SYS_C00675                DEPTNO
Summary Create the following types of constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK Query the USER_CONSTRAINTS table to view all constraint definitions and names.
Practice Overview Adding constraints to existing tables Adding more columns to a table Displaying information in data dictionary views

More Related Content

What's hot

Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Vidyasagar Mundroy
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptgourav kottawar
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorialGiuseppe Maxia
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commandsBelle Wx
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Abdul Rehman
 
Oracle SQL AND PL/SQL
Oracle SQL AND PL/SQLOracle SQL AND PL/SQL
Oracle SQL AND PL/SQLsuriyae1
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statementsMohd Tousif
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL CommandsShrija Madhu
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statementsVivek Singh
 

What's hot (18)

Ddl commands
Ddl commandsDdl commands
Ddl commands
 
Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)Database Systems - SQL - DDL Statements (Chapter 3/2)
Database Systems - SQL - DDL Statements (Chapter 3/2)
 
SQL(DDL & DML)
SQL(DDL & DML)SQL(DDL & DML)
SQL(DDL & DML)
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Best sql plsql material
Best sql plsql materialBest sql plsql material
Best sql plsql material
 
Les11
Les11Les11
Les11
 
DBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) pptDBMS information in detail || Dbms (lab) ppt
DBMS information in detail || Dbms (lab) ppt
 
My sql.ppt
My sql.pptMy sql.ppt
My sql.ppt
 
MySQL partitions tutorial
MySQL partitions tutorialMySQL partitions tutorial
MySQL partitions tutorial
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL Oracle Database DML DDL and TCL
Oracle Database DML DDL and TCL
 
Oracle SQL AND PL/SQL
Oracle SQL AND PL/SQLOracle SQL AND PL/SQL
Oracle SQL AND PL/SQL
 
Sql basics and DDL statements
Sql basics and DDL statementsSql basics and DDL statements
Sql basics and DDL statements
 
Les10
Les10Les10
Les10
 
dbms lab manual
dbms lab manualdbms lab manual
dbms lab manual
 
SQL - DML and DDL Commands
SQL - DML and DDL CommandsSQL - DML and DDL Commands
SQL - DML and DDL Commands
 
Oracle: PLSQL Commands
Oracle: PLSQL CommandsOracle: PLSQL Commands
Oracle: PLSQL Commands
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 

Viewers also liked

Advanced User Privileges
Advanced User PrivilegesAdvanced User Privileges
Advanced User PrivilegesArena PLM
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slidesmetsarin
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In SqlAnurag
 
Database index
Database indexDatabase index
Database indexRiteshkiit
 
User, roles and privileges
User, roles and privilegesUser, roles and privileges
User, roles and privilegesYogiji Creations
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQLEDB
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep InternalEXEM
 
Sql database object
Sql database objectSql database object
Sql database objectYoung Alista
 
LinkedIn powerpoint
LinkedIn powerpointLinkedIn powerpoint
LinkedIn powerpointguest2137df
 

Viewers also liked (12)

Advanced User Privileges
Advanced User PrivilegesAdvanced User Privileges
Advanced User Privileges
 
PostgreSQL Database Slides
PostgreSQL Database SlidesPostgreSQL Database Slides
PostgreSQL Database Slides
 
Less07 Users
Less07 UsersLess07 Users
Less07 Users
 
Postgre sql unleashed
Postgre sql unleashedPostgre sql unleashed
Postgre sql unleashed
 
Constraints In Sql
Constraints In SqlConstraints In Sql
Constraints In Sql
 
Database index
Database indexDatabase index
Database index
 
PostgreSQL: Advanced indexing
PostgreSQL: Advanced indexingPostgreSQL: Advanced indexing
PostgreSQL: Advanced indexing
 
User, roles and privileges
User, roles and privilegesUser, roles and privileges
User, roles and privileges
 
Data Processing Inside PostgreSQL
Data Processing Inside PostgreSQLData Processing Inside PostgreSQL
Data Processing Inside PostgreSQL
 
PostgreSQL Deep Internal
PostgreSQL Deep InternalPostgreSQL Deep Internal
PostgreSQL Deep Internal
 
Sql database object
Sql database objectSql database object
Sql database object
 
LinkedIn powerpoint
LinkedIn powerpointLinkedIn powerpoint
LinkedIn powerpoint
 

Similar to Les11 Including Constraints

Similar to Les11 Including Constraints (20)

Les10
Les10Les10
Les10
 
e computer notes - Including constraints
e computer notes - Including constraintse computer notes - Including constraints
e computer notes - Including constraints
 
SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Les02
Les02Les02
Les02
 
SQL-8 Table Creation.pdf
SQL-8 Table Creation.pdfSQL-8 Table Creation.pdf
SQL-8 Table Creation.pdf
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Les09 Manipulating Data
Les09 Manipulating DataLes09 Manipulating Data
Les09 Manipulating Data
 
Les11[1]Including Constraints
Les11[1]Including ConstraintsLes11[1]Including Constraints
Les11[1]Including Constraints
 
Dms 22319 micro project
Dms 22319 micro projectDms 22319 micro project
Dms 22319 micro project
 
1.Implementing_Data_Integrity.pdf
1.Implementing_Data_Integrity.pdf1.Implementing_Data_Integrity.pdf
1.Implementing_Data_Integrity.pdf
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
Unit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptxUnit 2 Chap 4 SQL DDL.pptx
Unit 2 Chap 4 SQL DDL.pptx
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Entigrity constraint
Entigrity constraintEntigrity constraint
Entigrity constraint
 
Dbmsmanual
DbmsmanualDbmsmanual
Dbmsmanual
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
SQL
SQLSQL
SQL
 
Oracle sql material
Oracle sql materialOracle sql material
Oracle sql material
 

More from NETsolutions Asia: NSA – Thailand, Sripatum University: SPU (11)

Workshop_BDATools-MSAzure.pdf
Workshop_BDATools-MSAzure.pdfWorkshop_BDATools-MSAzure.pdf
Workshop_BDATools-MSAzure.pdf
 
Managing Big Data with Apache Hadoop.pdf
Managing Big Data with Apache Hadoop.pdfManaging Big Data with Apache Hadoop.pdf
Managing Big Data with Apache Hadoop.pdf
 
Introduction-Management NoSQL with MongoDB.pdf
Introduction-Management NoSQL with MongoDB.pdfIntroduction-Management NoSQL with MongoDB.pdf
Introduction-Management NoSQL with MongoDB.pdf
 
Les12 creating views
Les12 creating viewsLes12 creating views
Les12 creating views
 
Les04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple TableLes04 Displaying Data From Multiple Table
Les04 Displaying Data From Multiple Table
 
Les02 Restricting And Sorting Data
Les02 Restricting And Sorting DataLes02 Restricting And Sorting Data
Les02 Restricting And Sorting Data
 
Les00 Intoduction
Les00 IntoductionLes00 Intoduction
Les00 Intoduction
 
Les06 Subqueries
Les06 SubqueriesLes06 Subqueries
Les06 Subqueries
 
Les05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group FunctionLes05 Aggregating Data Using Group Function
Les05 Aggregating Data Using Group Function
 
Les01 Writing Basic Sql Statements
Les01 Writing Basic Sql StatementsLes01 Writing Basic Sql Statements
Les01 Writing Basic Sql Statements
 
Les03 Single Row Function
Les03 Single Row FunctionLes03 Single Row Function
Les03 Single Row Function
 

Recently uploaded

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Recently uploaded (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 

Les11 Including Constraints

  • 2. Objectives After completing this lesson, you should be able to do the following: Describe constraints Create and maintain constraints
  • 3. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent the deletion of a table if there are dependencies. The following constraint types are valid in Oracle: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK
  • 4. Constraint Guidelines Name a constraint or the Oracle Server will generate a name by using the SYS_Cn format. Create a constraint: At the same time as the table is created After the table has been created Define a constraint at the column or table level. View a constraint in the data dictionary.
  • 5. Defining Constraints CREATE TABLE [schema.]table (columndatatype [DEFAULT expr] [column_constraint], ... [table_constraint][,...]); CREATE TABLE emp( empno NUMBER(4), ename VARCHAR2(10), ... deptno NUMBER(7,2) NOT NULL, CONSTRAINT emp_empno_pk PRIMARY KEY (EMPNO));
  • 6. Defining Constraints Column constraint level Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column, ...),
  • 7. The NOT NULL Constraint NOT NULL constraint (no row can containa null value forthis column) Absence of NOT NULL constraint (any row can containnull for this column) NOT NULL constraint Ensures that null values are not permitted for the column EMP EMPNO ENAME JOB ... COMM DEPTNO 7839KINGPRESIDENT10 7698BLAKEMANAGER30 7782CLARKMANAGER10 7566JONESMANAGER20 ...
  • 8. The NOT NULL Constraint Defined at the column level SQL> CREATE TABLE emp( 2empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL);
  • 9. The UNIQUE Key Constraint Insert into Not allowed (DNAME-SALES already exists) 50SALESDETROIT 60BOSTON Allowed UNIQUE key constraint DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
  • 10. The UNIQUE Key Constraint Defined at either the table level or the column level SQL> CREATE TABLE dept( 2deptno NUMBER(2), 3dname VARCHAR2(14), 4loc VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE(dname));
  • 11. The PRIMARY KEY Constraint Insert into Not allowed (DEPTNO-20 already exists) 20MARKETINGDALLAS FINANCENEW YORK Not allowed(DEPTNO is null) PRIMARY KEY DEPT DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON
  • 12. The PRIMARY KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE dept( 2deptno NUMBER(2), 3dname VARCHAR2(14), 4loc VARCHAR2(13), 5CONSTRAINT dept_dname_uk UNIQUE (dname), 6CONSTRAINT dept_deptno_pk PRIMARY KEY(deptno));
  • 13. The FOREIGN KEY Constraint Not allowed(DEPTNO9 does not exist in the DEPT table) 7571FORDMANAGER ... 2009 7571FORDMANAGER ... 20020 Insert into Allowed DEPT PRIMARYKEY DEPTNO DNAME LOC ------ ---------- -------- 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS ... EMP EMPNO ENAME JOB ... COMM DEPTNO 7839KINGPRESIDENT10 7698BLAKEMANAGER30 ... FOREIGNKEY
  • 14. The FOREIGN KEY Constraint Defined at either the table level or the column level SQL> CREATE TABLE emp( 2empno NUMBER(4), 3enameVARCHAR2(10) NOT NULL, 4jobVARCHAR2(9), 5mgrNUMBER(4), 6hiredateDATE, 7salNUMBER(7,2), 8commNUMBER(7,2), 9deptnoNUMBER(7,2) NOT NULL, 10CONSTRAINT emp_deptno_fk FOREIGN KEY (deptno) 11REFERENCES dept (deptno));
  • 15. FOREIGN KEY Constraint Keywords FOREIGN KEY Defines the column in the child table atthe table constraint level REFERENCES Identifies the table and column in the parent table ON DELETE CASCADE Allows deletion in the parent table and deletion of the dependent rows in the child table
  • 16. The CHECK Constraint Defines a condition that each row must satisfy Expressions that 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 ..., deptnoNUMBER(2), CONSTRAINT emp_deptno_ck CHECK (DEPTNO BETWEEN 10 AND 99),...
  • 17. Adding a Constraint ALTER TABLEtable ADD [CONSTRAINT constraint] type (column); Add or drop, but not modify, a constraint Enable or disable constraints Add a NOT NULL constraint by using the MODIFY clause
  • 18. Adding a Constraint Add a FOREIGN KEY constraint to the EMP table indicating that a manager must already exist as a valid employee in the EMP table. SQL> ALTER TABLE emp 2 ADD CONSTRAINT emp_mgr_fk 3FOREIGN KEY(mgr) REFERENCES emp(empno); Table altered.
  • 19.
  • 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. SQL> ALTER TABLEemp 2 DISABLE CONSTRAINTemp_empno_pk CASCADE; Table altered.
  • 21. Enabling Constraints Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. A UNIQUE or PRIMARY KEY index is automatically created if you enable a UNIQUE key or PRIMARY KEY constraint. SQL> ALTER TABLEemp 2 ENABLE CONSTRAINTemp_empno_pk; Table altered.
  • 22. Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. SQL> SELECTconstraint_name, constraint_type, 2search_condition 3 FROMuser_constraints 4 WHEREtable_name = 'EMP'; CONSTRAINT_NAME C SEARCH_CONDITION ------------------------ - ------------------------- SYS_C00674 C EMPNO IS NOT NULL SYS_C00675 C DEPTNO IS NOT NULL EMP_EMPNO_PK P ...
  • 23. Viewing the Columns Associated with Constraints View the columns associated with the constraint names in the USER_CONS_COLUMNS view. SQL> SELECTconstraint_name, column_name 2 FROMuser_cons_columns 3 WHEREtable_name = 'EMP'; CONSTRAINT_NAME COLUMN_NAME ------------------------- ---------------------- EMP_DEPTNO_FK DEPTNO EMP_EMPNO_PK EMPNO EMP_MGR_FK MGR SYS_C00674 EMPNO SYS_C00675 DEPTNO
  • 24. Summary Create the following types of constraints: NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY CHECK Query the USER_CONSTRAINTS table to view all constraint definitions and names.
  • 25. Practice Overview Adding constraints to existing tables Adding more columns to a table Displaying information in data dictionary views