10 Copyright © Oracle Corporation, 2001. All rights reserved. Including Constraints.

Slides:



Advertisements
Similar presentations
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
Advertisements

Database Programming Sections 13. Marge Hohly  1. Which statements are True about the following sequence? The sequence was used to generate numbers.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
10 Copyright © 2004, Oracle. All rights reserved. Creating Other Schema Objects.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Sections 10 – Constraints
4 Copyright © Oracle Corporation, All rights reserved. Displaying Data from Multiple Tables.
Objectives After completing this lesson, you should be able to do the following: Categorize the main database objects Review the table structure List.
11 Copyright © 2007, Oracle. All rights reserved. Creating Other Schema Objects.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes
11 Copyright © Oracle Corporation, All rights reserved. Creating Views.
Objectives After completing this lesson, you should be able to do the following: Describe each data manipulation language (DML) statement Insert rows.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Database Programming Sections 9 – Constraints. Marge Hohly2 CONSTRAINT TYPES  NOT NULL Constraints  UNIQUE Constraints  PRIMARY KEY Constraints  FOREIGN.
11-1 Copyright  Oracle Corporation, All rights reserved. What Are Constraints? Constraints enforce rules at the table level. Constraints prevent.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Copyright  Oracle Corporation, All rights reserved. 11 Including Constraints.
Copyright  Oracle Corporation, All rights reserved. Introduction.
Copyright  Oracle Corporation, All rights reserved. 4 Introduction.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
11 Including Constraints Objectives At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints At the.
8 Copyright © 2007, Oracle. All rights reserved. Managing Schema Objects.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
13 Copyright © Oracle Corporation, All rights reserved. Maintaining Data Integrity.
2 Copyright © 2006, Oracle. All rights reserved. Managing Schema Objects.
Copyright © 2004, Oracle. All rights reserved. Lecture 2: Using DDL Statements to Create and Manage Tables & Indexes ORACLE.
DML Part 1 Yogiek Indra Kurniawan. All Files Can Be Downloaded at : Menu : “Perkuliahan”
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
DDL and Views. Database Objects Logically represents subsets of data from one or more tables View Generates numeric valuesSequence Basic unit of storage;
At the end of this lesson, you will be able to: Describe constraints Create and maintain constraints.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Database Programming Sections 9 & 10 – DDL Data Definition Language,
9 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables (DDL 구문을 이용한 테이블의 생성과 관리 )
Installation Oracle 11g Express 2 double click the "setup" button to install the Oracle.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Insert, update, delete TCL. Data Manipulation Language – A DML statement is executed when you: Add new rows to a table Modify existing rows in a table.
Using DDL Statements to Create and Manage Tables
Including Constraints
SQL Creating and Managing Tables
Manipulating Data.
Using DDL Statements to Create and Manage Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
Managing Schema Objects
“Manipulating Data” Lecture 6.
“Manipulating Data” Lecture 6.
Manipulating Data.
Using DDL Statements to Create and Manage Tables
Chapter 2 Views.
1 Manipulating Data. 2 Objectives After completing this lesson, you should be able to do the following:  Describe each data manipulation language (DML)
Using DDL Statements to Create and Manage Tables
IST 318 Database Administration
Including Constraints
Presentation transcript:

10 Copyright © Oracle Corporation, All rights reserved. Including Constraints

10-2 Copyright © Oracle Corporation, All rights reserved. Objectives After completing this lesson, you should be able to do the following: Describe constraints Create and maintain constraints

10-3 Copyright © Oracle Corporation, All rights reserved. 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: –NOT NULL –UNIQUE –PRIMARY KEY –FOREIGN KEY –CHECK

10-4 Copyright © Oracle Corporation, All rights reserved. Constraint Guidelines Name a constraint or the Oracle server generates a name by using the SYS_Cn 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.

10-5 Copyright © Oracle Corporation, All rights reserved. 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));

10-6 Copyright © Oracle Corporation, All rights reserved. Defining Constraints Column constraint level Table constraint level column [CONSTRAINT constraint_name] constraint_type, column,... [CONSTRAINT constraint_name] constraint_type (column,...), column,... [CONSTRAINT constraint_name] constraint_type (column,...),

10-7 Copyright © Oracle Corporation, All rights reserved. The NOT NULL Constraint Ensures that null values are not permitted for the column: 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 …

10-8 Copyright © Oracle Corporation, All rights reserved. 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,... The NOT NULL Constraint Is defined at the column level: System named User named

10-9 Copyright © Oracle Corporation, All rights reserved. The UNIQUE Constraint EMPLOYEES UNIQUE constraint INSERT INTO Not allowed: already exists Allowed …

10-10 Copyright © Oracle Corporation, All rights reserved. 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, VARCHAR2(25), salary NUMBER(8,2), commission_pct NUMBER(2,2), hire_date DATE NOT NULL,... CONSTRAINT emp_ _uk UNIQUE( ));

10-11 Copyright © Oracle Corporation, All rights reserved. The PRIMARY KEY Constraint DEPARTMENTS PRIMARY KEY INSERT INTO Not allowed (Null value) Not allowed (50 already exists) …

10-12 Copyright © Oracle Corporation, All rights reserved. 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 PRIMARY KEY Constraint Defined at either the table level or the column level:

10-13 Copyright © Oracle Corporation, All rights reserved. The FOREIGN KEY Constraint DEPARTMENTS EMPLOYEES FOREIGN KEY INSERT INTO Not allowed (9 does not exist) Allowed PRIMARY KEY … …

10-14 Copyright © Oracle Corporation, All rights reserved. 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, 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_ _uk UNIQUE( ));

10-15 Copyright © Oracle Corporation, All rights reserved. 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

10-16 Copyright © Oracle Corporation, All rights reserved. The CHECK Constraint Defines a condition that each row must satisfy..., salaryNUMBER(2) CONSTRAINT emp_salary_min CHECK (salary > 0),...

10-17 Copyright © Oracle Corporation, All rights reserved. Adding a Constraint Syntax Use the ALTER TABLE statement to: Add 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); ALTER TABLE table ADD [CONSTRAINT constraint] type (column);

10-18 Copyright © Oracle Corporation, All rights reserved. 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.

10-19 Copyright © Oracle Corporation, All rights reserved. Dropping a Constraint Remove the manager constraint from the EMPLOYEES table. Remove the PRIMARY KEY constraint on the DEPARTMENTS table and drop the associated FOREIGN KEY constraint on the EMPLOYEES.DEPARTMENT_ID column. ALTER TABLE employees DROP CONSTRAINT emp_manager_fk; Table altered. ALTER TABLE employees DROP CONSTRAINT emp_manager_fk; Table altered. ALTER TABLEdepartments DROP PRIMARY KEY CASCADE; Table altered. ALTER TABLEdepartments DROP PRIMARY KEY CASCADE; Table altered.

10-20 Copyright © Oracle Corporation, All rights reserved. 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 TABLEemployees DISABLE CONSTRAINTemp_emp_id_pk CASCADE; Table altered. ALTER TABLEemployees DISABLE CONSTRAINTemp_emp_id_pk CASCADE; Table altered.

10-21 Copyright © Oracle Corporation, All rights reserved. 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. ALTER TABLEemployees ENABLE CONSTRAINTemp_emp_id_pk; Table altered. ALTER TABLEemployees ENABLE CONSTRAINTemp_emp_id_pk; Table altered.

10-22 Copyright © Oracle Corporation, All rights reserved. SELECTconstraint_name, constraint_type, search_condition FROMuser_constraints WHEREtable_name = 'EMPLOYEES'; Viewing Constraints Query the USER_CONSTRAINTS table to view all constraint definitions and names. …

10-23 Copyright © Oracle Corporation, All rights reserved. SELECTconstraint_name, column_name FROMuser_cons_columns WHEREtable_name = 'EMPLOYEES'; Viewing the Columns Associated with Constraints View the columns associated with the constraint names in the USER_CONS_COLUMNS view. …

10-24 Copyright © Oracle Corporation, All rights reserved. Summary In this lesson, you should have learned how to create constraints. Types of constraints: –NOT NULL –UNIQUE –PRIMARY KEY –FOREIGN KEY –CHECK You can query the USER_CONSTRAINTS table to view all constraint definitions and names.