Including Constraints

Slides:



Advertisements
Similar presentations
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Advertisements

10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
8 Copyright © Oracle Corporation, All rights reserved. Manipulating Data.
Sections 10 – Constraints
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
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.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
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
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
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.
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.
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.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
Defining NOT NULL and UNIQUE Constraints
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;
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Using DDL Statements to Create and Manage Tables
Including Constraints
SQL: Schema Definition and Constraints Chapter 6 week 6
Creating Database Triggers
SQL Creating and Managing Tables
Manipulating Data.
Using DDL Statements to Create and Manage Tables
SQL Creating and Managing Tables
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL Creating and Managing Tables
Restricting and Sorting Data
Chapter 2 Views.
“Manipulating Data” Lecture 6.
SQL DATA CONSTRAINTS.
CS122 Using Relational Databases and SQL
“Manipulating Data” Lecture 6.
Manipulating Data.
Oracle Data Definition Language (DDL)
Using DDL Statements to Create and Manage Tables
Chapter 2 Views.
Using DDL Statements to Create and Manage Tables
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
IST 318 Database Administration
CS122 Using Relational Databases and SQL
Presentation transcript:

Including Constraints lecture5

Outlines What are Constraints ? Constraint Guidelines Defining Constraint NOT NULL constraint Unique constraint Primary key constraint Foreign key constraint Check constraint Adding Constraints

What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: Enforce rules on the data in a table whenever a row is inserted, updated, or deleted from that table. “The constraint must be satisfied for the operation to succeed”. Prevent the deletion of a table if there are dependencies from other tables The following constraint types are valid (Table 1): – NOT NULL – UNIQUE – PRIMARY KEY – FOREIGN KEY – CHECK

What Are Constraints? (Cont.) Table 1: Data Integrity Constraint

Constraint Guidelines Name a constraint or the Oracle server generates a name by using the SYS_Cn format (where n is an integer number so that the constrain names are unique) Constrain names must follow the standard object naming rules Create a constraint either: – At the same time as the table is created: (at the column or table level). – After the table has been created. ”using alter table” All constrains stores in the data dictionary

when creating the table Defining constraint when creating the table At the column level At the table level After creating the table (using Alter table)

Defining Constraints: During Table Creation Syntax: CREATE TABLE table (column datatype [DEFAULT expr][column_constraint], …. [table_constraint]); column_constraint: is an integrity constrain as part of the column definition table_constraint: is an integrity constrain as part of the table definition

Defining Constraints: During Table Creation

Defining Constraints: During Table Creation Example1: 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)); Column level constraint System defined nmae Table level constraint User defined name CONSTRAINT: keyword emp_emp_id_pk : the constraint name PRIMARY KEY, NOT NULL: constrain type EMPLOYEE_ID: column name

The NOT NULL Constraint Ensures that null values are not permitted for the column Columns without the NOT NULL constraint can contain null values by default. The NOT NULL constraint can be specified only at the column level, not at the table level.

The NOT NULL Constraint Example2: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, salary NUMBER(8,2) ); System named Note That: last_name column constraint is unnamed, thus the system will give it a name.

The NOT NULL Constraint Example2 (Cont.): SALARY LAST_NAME EMPLOYEE_ID 15000 Al Ghanim 111111 10258 Al Mane 222222 12800 Bssam 333333 8450 Bin Saleh 444444 allowed Al Ali 555555 X NOT Allowed (Why?) 85642 666666

The NOT NULL Constraint Exercise: rewrite the previews query where the NOT NULL constraint defined on the table level. Is it possible? [Explain] CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25), salary NUMBER(8,2), hire_date DATE ------------------------------------------------------ ); NOT NULL Constraint can’t be defined on the table level

The UNIQUE Constraint A UNIQUE key integrity constraint requires that every value in a column or set of columns (key) to be unique UNIQUE constraints allow the input of nulls unless you also define NOT NULL constraints for the same columns UNIQUE constraints can be defined at the column or table level. A composite unique key is created by using the table level definition

The UNIQUE Constraint Example3: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), ... CONSTRAINT emp_email_uk UNIQUE(email));

The UNIQUE Constraint Example3 (Cont.):

The UNIQUE Constraint Exercise a: rewrite the previews query where the UNIQUE constraint defined on the column level and give it a name. Is it possible? [Explain] CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), );

The UNIQUE Constraint Exercise b: rewrite the previews query where the UNIQUE constraint defined on 2 columns ; employee_id and email. CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, email VARCHAR2(25), );

The PRIMARY KEY Constraint A PRIMARY KEY constraint creates a primary key for the table The PRIMARY KEY constraint is a column or set of columns that uniquely identifies each row in a table This constraint enforces uniqueness of the column or column combination and ensures that no column that is part of the primary key can contain a null value A table can have only one PRIMARY KEY constraint but can have several UNIQUE constraints. defined at either the table level or the column level

The PRIMARY KEY Constraint Example 4: CREATE TABLE departments( department_id NUMBER(4), department_name VARCHAR2(30) CONSTRAINT dept_name_nn NOT NULL, manager_id NUMBER(6), CONSTRAINT dept_id_pk PRIMARY KEY(department_id));

The PRIMARY KEY Constraint Example4 (Cont.):

The PRIMARY KEY Constraint Exercise a: rewrite the previews query where the PRIMRY KEY constraint defined on the column level. CREATE TABLE departments( department_id NUMBER(4), department_name VARCHAR2(30) CONSTRAINT dept_name_nn NOT NULL, manager_id NUMBER(6), ));

The PRIMARY KEY Constraint Exercise b: rewrite the previews query where the PRIMRY KEY constraint defined on 2 columns ; department_id and dempartment_name. CREATE TABLE departments( department_id NUMBER(4), department_name VARCHAR2(30) CONSTRAINT dept_name_nn NOT NULL, manager_id NUMBER(6), ));

The FOREIGN KEY Constraint The FOREIGN KEY, or referential integrity constraint, designates a column or combination of columns as a foreign key and establishes a relationship between tables A foreign key value must match an existing value in the parent table or be NULL Defined at either the table level or the column level Foreign keys are based on values and purely logical ,not physical, pointers

The FOREIGN KEY Constraint Example 5: CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, department_id NUMBER(4), CONSTRAINT emp_dept_fk FOREIGN KEY (department_id) REFERENCES departments(department_id), CONSTRAINT emp_email_uk UNIQUE(email));

The FOREIGN KEY Constraint Example 5 (Cont.):

The FOREIGN KEY Constraint The foreign key can also be defined at the column level, provided the constraint is based on a single column. The syntax differs in that the keywords FOREIGN KEY do not appear. For example: CREATE TABLE employees (... department_id NUMBER(4) CONSTRAINT emp_deptid_fk REFERENCES departments(department_id), ...) OR:

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

The CHECK Constraint Defines a condition that each row must satisfy Example CREATE TABLE employees (... salary NUMBER(8,2) CONSTRAINT emp_salary_min CHECK (salary > 0), ….);

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 constraintname] type (column); Note that: You can define a NOT NULL column only if the table is empty or if the column has a value for every row.

Adding a Constraint (Example) Add a FOREIGN KEY constraint to the EMPLOYEES table to indicate that a department id must already exist as a valid department in the department table. ALTER TABLE employees ADD CONSTRAINT emp_dept_fk FOREIGN KEY(dept_id) REFERENCES department(dept_id);