SlideShare una empresa de Scribd logo
1 de 30
Including Constraints
Objectives ,[object Object],[object Object],[object Object],[object Object]
What are Constraints? ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Constraint Guidelines ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
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));
Defining Constraints ,[object Object],[object Object],column  [CONSTRAINT  constraint_name ]  constraint_type , column,... [CONSTRAINT  constraint_name ]  constraint_type ( column , ...),
The  NOT   NULL  Constraint ,[object Object],[object Object],… NOT NULL  constraint (No row can contain a null value for this column.) Absence of  NOT NULL  constraint (Any row can contain null for this column.) NOT NULL   constraint
The  NOT   NULL  Constraint ,[object Object],CREATE TABLE employees( employee_id  NUMBER(6), last_name  VARCHAR2(25) NOT NULL, salary  NUMBER(8,2), commission_pct NUMBER(2,2), hire_date  DATE  CONSTRAINT emp_hire_date_nn NOT NULL, ...  System named User named
The  UNIQUE  Constraint EMPLOYEES  UNIQUE  constraint INSERT INTO … Not allowed: already exists Allowed
The  UNIQUE  Constraint ,[object Object],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));
The  PRIMARY   KEY  Constraint DEPARTMENTS   PRIMARY KEY … INSERT INTO Not allowed (Null value) Not allowed  (50 already exists)
The  PRIMARY   KEY  Constraint ,[object Object],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));
The  FOREIGN   KEY  Constraint DEPARTMENTS   EMPLOYEES … … FOREIGN KEY INSERT INTO Not allowed ( 9  does not exist) Allowed PRIMARY KEY
The  FOREIGN   KEY  Constraint ,[object Object],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 ,[object Object],[object Object],[object Object],[object Object]
The  CHECK  Constraint ,[object Object],[object Object],[object Object],[object Object],[object Object],..., salary NUMBER(2) CONSTRAINT emp_salary_min  CHECK (salary > 0),...
Adding a Constraint Syntax ,[object Object],[object Object],[object Object],[object Object],ALTER TABLE   table ADD [CONSTRAINT  constraint ]  type  ( column );
Adding a Constraint ,[object Object],[object Object],[object Object],ALTER TABLE  employees ADD CONSTRAINT  emp_manager_fk  FOREIGN KEY(manager_id)  REFERENCES employees(employee_id); Table altered.
Dropping a Constraint ,[object Object],[object Object],ALTER TABLE  employees DROP CONSTRAINT  emp_manager_fk; Table altered. ALTER TABLE departments DROP PRIMARY KEY CASCADE; Table altered.
Disabling Constraints ,[object Object],[object Object],ALTER TABLE employees DISABLE CONSTRAINT emp_emp_id_pk CASCADE; Table altered.
Enabling Constraints ,[object Object],[object Object],ALTER TABLE employees ENABLE CONSTRAINT emp_emp_id_pk; Table altered.
Cascading Constraints ,[object Object],[object Object],[object Object]
Cascading Constraints ,[object Object],ALTER TABLE test1  DROP (pk) CASCADE CONSTRAINTS; Table altered. ALTER TABLE test1  DROP (pk, fk, col1) CASCADE CONSTRAINTS; Table altered.
Viewing Constraints ,[object Object],[object Object],SELECT constraint_name, constraint_type, search_condition FROM user_constraints WHERE table_name = 'EMPLOYEES'; …
Viewing the Columns Associated with Constraints ,[object Object],[object Object],SELECT constraint_name, column_name FROM user_cons_columns WHERE table_name = 'EMPLOYEES'; …
Summary ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Practice 10 Overview ,[object Object],[object Object],[object Object],[object Object]
 
 
 

Más contenido relacionado

La actualidad más candente (20)

Sql commands
Sql commandsSql commands
Sql commands
 
Sql
SqlSql
Sql
 
SQL Sort Notes
SQL Sort NotesSQL Sort Notes
SQL Sort Notes
 
Sql dml & tcl 2
Sql   dml & tcl 2Sql   dml & tcl 2
Sql dml & tcl 2
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and deleteDatabase Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
 
Lecture 4 sql {basics keys and constraints}
Lecture 4 sql {basics  keys and constraints}Lecture 4 sql {basics  keys and constraints}
Lecture 4 sql {basics keys and constraints}
 
Sql Tags
Sql TagsSql Tags
Sql Tags
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
DDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and JoinsDDL,DML,SQL Functions and Joins
DDL,DML,SQL Functions and Joins
 
Oracle: DDL
Oracle: DDLOracle: DDL
Oracle: DDL
 
Data Manipulation(DML) and Transaction Control (TCL)
Data Manipulation(DML) and Transaction Control (TCL)  Data Manipulation(DML) and Transaction Control (TCL)
Data Manipulation(DML) and Transaction Control (TCL)
 
RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)RDBMS Lab03 applying constraints (UIU)
RDBMS Lab03 applying constraints (UIU)
 
Views and functions
Views and functionsViews and functions
Views and functions
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
Introduction sql
Introduction sqlIntroduction sql
Introduction sql
 
SQL Views
SQL ViewsSQL Views
SQL Views
 
MULTIPLE TABLES
MULTIPLE TABLES MULTIPLE TABLES
MULTIPLE TABLES
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
 
SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables  SQL Introduction to displaying data from multiple tables
SQL Introduction to displaying data from multiple tables
 

Destacado (20)

Les05
Les05Les05
Les05
 
Les09
Les09Les09
Les09
 
Les07
Les07Les07
Les07
 
Les06
Les06Les06
Les06
 
Les02
Les02Les02
Les02
 
Xml1111
Xml1111Xml1111
Xml1111
 
Les06
Les06Les06
Les06
 
Introduction to j_query
Introduction to j_queryIntroduction to j_query
Introduction to j_query
 
J query introduction
J query introductionJ query introduction
J query introduction
 
Sql xp 03
Sql xp 03Sql xp 03
Sql xp 03
 
Introduction to queries
Introduction to queriesIntroduction to queries
Introduction to queries
 
Sql xp 04
Sql xp 04Sql xp 04
Sql xp 04
 
Sql xp 01
Sql xp 01Sql xp 01
Sql xp 01
 
Les16
Les16Les16
Les16
 
Les17
Les17Les17
Les17
 
Les15
Les15Les15
Les15
 
Les12
Les12Les12
Les12
 
Les20
Les20Les20
Les20
 
Les13
Les13Les13
Les13
 
Les11
Les11Les11
Les11
 

Similar a Les10 (20)

Les11 Including Constraints
Les11 Including ConstraintsLes11 Including Constraints
Les11 Including Constraints
 
Les11
Les11Les11
Les11
 
Sql integrity constraints
Sql integrity constraintsSql integrity constraints
Sql integrity constraints
 
Oraclesql
OraclesqlOraclesql
Oraclesql
 
SQL
SQLSQL
SQL
 
Les09
Les09Les09
Les09
 
Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)Les09 (using ddl statements to create and manage tables)
Les09 (using ddl statements to create and manage tables)
 
zekeLabs sql-slides
zekeLabs sql-slideszekeLabs sql-slides
zekeLabs sql-slides
 
SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11SQL WORKSHOP::Lecture 11
SQL WORKSHOP::Lecture 11
 
Les08 (manipulating data)
Les08 (manipulating data)Les08 (manipulating data)
Les08 (manipulating data)
 
Les08
Les08Les08
Les08
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
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
 
Db1 lecture4
Db1 lecture4Db1 lecture4
Db1 lecture4
 
DBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptxDBMS and SQL(structured query language) .pptx
DBMS and SQL(structured query language) .pptx
 
Query
QueryQuery
Query
 
sql language
sql languagesql language
sql language
 
SQL
SQLSQL
SQL
 
Sql commands
Sql commandsSql commands
Sql commands
 

Más de Vijay Kumar (6)

Les19
Les19Les19
Les19
 
Les18
Les18Les18
Les18
 
Les14
Les14Les14
Les14
 
Les08
Les08Les08
Les08
 
Les04
Les04Les04
Les04
 
Les03
Les03Les03
Les03
 

Último

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Les10

  • 2.
  • 3.
  • 4.
  • 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));
  • 6.
  • 7.
  • 8.
  • 9. The UNIQUE Constraint EMPLOYEES UNIQUE constraint INSERT INTO … Not allowed: already exists Allowed
  • 10.
  • 11. The PRIMARY KEY Constraint DEPARTMENTS PRIMARY KEY … INSERT INTO Not allowed (Null value) Not allowed (50 already exists)
  • 12.
  • 13. The FOREIGN KEY Constraint DEPARTMENTS EMPLOYEES … … FOREIGN KEY INSERT INTO Not allowed ( 9 does not exist) Allowed PRIMARY KEY
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.  
  • 29.  
  • 30.  

Notas del editor

  1. Schedule: Timing Topic 45 minutes Lecture 25 minutes Practice 70 minutes Total