Normal Form vs. Non-First Normal Form

Similar documents
Introduction to Triggers using SQL

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

IT2305 Database Systems I (Compulsory)

IT2304: Database Systems 1 (DBS 1)

CS2Bh: Current Technologies. Introduction to XML and Relational Databases. The Relational Model. The relational model

Oracle SQL. Course Summary. Duration. Objectives

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database: SQL and PL/SQL Fundamentals

Instant SQL Programming

Chapter 9: Object-Based Databases

Chapter 5. SQL: Queries, Constraints, Triggers

SQL Server. 1. What is RDBMS?

Oracle Database 10g: Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

Schema Evolution in SQL-99 and Commercial (Object-)Relational DBMS

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

CSC 742 Database Management Systems

Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases

Relational Database Basics Review

SUBQUERIES AND VIEWS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 6

Oracle 10g PL/SQL Training

In This Lecture. SQL Data Definition SQL SQL. Notes. Non-Procedural Programming. Database Systems Lecture 5 Natasha Alechina

Part A: Data Definition Language (DDL) Schema and Catalog CREAT TABLE. Referential Triggered Actions. CSC 742 Database Management Systems

Oracle Database: SQL and PL/SQL Fundamentals

Functional Dependency and Normalization for Relational Databases

Databases and BigData

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Introduction to the Oracle DBMS

Basic Concepts of Database Systems

Database Design. Marta Jakubowska-Sobczak IT/ADC based on slides prepared by Paula Figueiredo, IT/DB

Introduction to Databases

Databases and DBMS. What is a Database?

How To Manage Data In A Database System

Relational model. Relational model - practice. Relational Database Definitions 9/27/11. Relational model. Relational Database: Terminology

BCA. Database Management System

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

The Relational Model. Ramakrishnan&Gehrke, Chapter 3 CS4320 1

In This Lecture. Security and Integrity. Database Security. DBMS Security Support. Privileges in SQL. Permissions and Privilege.

SQL DATA DEFINITION: KEY CONSTRAINTS. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 7

Lecture 6. SQL, Logical DB Design

Chapter 6: Physical Database Design and Performance. Database Development Process. Physical Design Process. Physical Database Design

Database Design Overview. Conceptual Design ER Model. Entities and Entity Sets. Entity Set Representation. Keys

Quiz! Database Indexes. Index. Quiz! Disc and main memory. Quiz! How costly is this operation (naive solution)?

SQL Tables, Keys, Views, Indexes

CS 377 Database Systems. Database Design Theory and Normalization. Li Xiong Department of Mathematics and Computer Science Emory University

COMP 378 Database Systems Notes for Chapter 7 of Database System Concepts Database Design and the Entity-Relationship Model

How To Create A Table In Sql (Ahem)

SQL: Queries, Programming, Triggers


CS2Bh: Current Technologies. Introduction to XML and Relational Databases. Introduction to Databases. Why databases? Why not use XML?

Example Instances. SQL: Queries, Programming, Triggers. Conceptual Evaluation Strategy. Basic SQL Query. A Note on Range Variables

Geodatabase Programming with SQL

Data Modeling. Database Systems: The Complete Book Ch ,

14 Databases. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

MySQL for Beginners Ed 3

Foundations of Information Management

Relational Databases

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

OBJECT ORIENTED EXTENSIONS TO SQL

3. Relational Model and Relational Algebra

COMP 5138 Relational Database Management Systems. Week 5 : Basic SQL. Today s Agenda. Overview. Basic SQL Queries. Joins Queries

OBJECTS AND DATABASES. CS121: Introduction to Relational Database Systems Fall 2015 Lecture 21

SQL: Queries, Programming, Triggers

COSC344 Database Theory and Applications. Lecture 9 Normalisation. COSC344 Lecture 9 1

SQL NULL s, Constraints, Triggers

A basic create statement for a simple student table would look like the following.

Part VI. Object-relational Data Models

CSC 443 Data Base Management Systems. Basic SQL

Triggers & Packages. {INSERT [OR] UPDATE [OR] DELETE}: This specifies the DML operation.

There are five fields or columns, with names and types as shown above.

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

DATABASE INTRODUCTION

More on SQL. Juliana Freire. Some slides adapted from J. Ullman, L. Delcambre, R. Ramakrishnan, G. Lindstrom and Silberschatz, Korth and Sudarshan

David Dye. Extract, Transform, Load

1 File Processing Systems

Oracle 11g PL/SQL training

Databases 2011 The Relational Model and SQL

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Oracle Database: Introduction to SQL

Oracle Database: SQL and PL/SQL Fundamentals NEW

EECS 647: Introduction to Database Systems

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

Oracle Database 11g SQL

Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. COURSE CURRICULUM COURSE TITLE: DATABASE MANAGEMENT (Code: ) Information Technology

Programming with SQL

Database Design. Adrienne Watt. Port Moody

Oracle Database: Introduction to SQL

The Relational Model. Why Study the Relational Model? Relational Database: Definitions. Chapter 3

CSI 2132 Lab 3. Outline 09/02/2012. More on SQL. Destroying and Altering Relations. Exercise: DROP TABLE ALTER TABLE SELECT

Databasesystemer, forår 2005 IT Universitetet i København. Forelæsning 3: Business rules, constraints & triggers. 3. marts 2005

Database Programming with PL/SQL: Learning Objectives

Programming Database lectures for mathema

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Oracle For Beginners Page : 1

SQL SELECT Query: Intermediate

Review: Participation Constraints

Transcription:

Normal Form vs. Non-First Normal Form Kristian Torp Department of Computer Science Aalborg Univeristy www.cs.aau.dk/ torp torp@cs.aau.dk September 1, 2009 daisy.aau.dk Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 1 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 2 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 3 / 96

University ER-Diagram 7 tables flat student, studentemail, takes, course, courseprerequest, lecture, exercise 3 tables nested student, takes, course Taking it to the extreme to explore consequences Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 4 / 96

The Student Table I Create a type for the composite attribute address create or replace type address t as o b j e c t ( s t r e e t varchar2 (255), num varchar2 (255), z i p i n t e g e r, c i t y varchar2 ( 2 5 5 ) ) ; Create a type for the multivalued attribute email create type e m a i l t as t a b l e of varchar2 ( 2 5 5 ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 5 / 96

The Student Table II Create the nested table using the new types create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Note The nested table is stored in a separted table names student email nt Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 6 / 96

Query Nested Tables Find the students that live on Elm streeet s e l e c t from student s where s. address. s t r e e t = Elm Note Use the composite attribute The correlation name s is required The dot notation s.address.street Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 7 / 96

Query Nested Tables, cont. Find where is listed as an email address s e l e c t from student where ann@nice. org member o f student. email Note Use the multivalued attribute The new keyword member The dot notation cannot be used (more email addresses) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 8 / 96

Motivation Supported by Oracle in more than 10 years, little used Background knowledge to understand Oracle s technology, e.g., Spatial Data mining Semantic web Fewer tables (simpler schema) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 9 / 96

Covered in This Lecture Looks at Creating Modifying Querying Single nested Multiple levels of nesting Table() function Value() function Do not look at Advanced OO features Reference types (REF/DEREF) Physical design (a little) VARRAY vs. nested tables Metadata views Implementation details Multiset operations All examples are Oracle specific Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 10 / 96

Terminology Concept Nested column Flat table Nested table Unnest Object table Row object Column object Description A non-atomic column A table with only atomic columns A table with one or more nested columns Convert a nested table to a flat table with same content Table where each row corresponds to an object When a row corresponds to an object A column of an object type in a table with other columns Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 11 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 12 / 96

A More Complicated Nested Table coursename ects prerequest lectures Alg 5 null C 5 null Alg OOP 5 C DBS 5 OOP 1.1 1 Introduction 1.3 1.5 2.2 2 Abstract Data Types 2.4 3.2 3 Searching 3.6 3.8 4.1 4 Graphs 4.2 4.1 5 NP and NP Complete 5.2 5.4 C1.1 1 The C Language C2.1 C2.4 C3.1 2 Control Structures C4.1 5.1 3 Functions and Parameters 6.1 C7.1 4 The Joy of Pointers C8.1 OO Stuff 1 Introduction to OOP On with the OO Stuff Class 2.2 2 Classes and Objects Class 4.2 Inheritance 1.1 3 Inheritance Inheritance 2.2 DBMS in Colors 1 Introduction to DBMSs DBMS in More Colors Class 2.2 Class 2.4 2 ER Modeling Class 2.6 Class 2.8 3.8 3 Query Languages including SQL 3.10 4 Concurrency Control 4.4 Recov 1.1 5 Recovery Recov 2.1 Recov 3.1 Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 13 / 96

Create The Types create or replace type s t r i n g t as t a b l e of varchar2 ( 2 5 5 ) ; This is a collection type Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 14 / 96

Create The Types create or replace type s t r i n g t as t a b l e of varchar2 ( 2 5 5 ) ; This is a collection type create or replace type l e c t u r e t as o b j e c t ( num i n t, s u b j e c t varchar2 (255), exercises s t r i n g t ) ; This is an object type Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 14 / 96

Create The Types create or replace type s t r i n g t as t a b l e of varchar2 ( 2 5 5 ) ; This is a collection type create or replace type l e c t u r e t as o b j e c t ( num i n t, s u b j e c t varchar2 (255), exercises s t r i n g t ) ; This is an object type Create the lecture serie type create or replace type l e c t u r e s e r i e t as t a b l e of l e c t u r e t ; Is this a collection type or and object type? Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 14 / 96

Create The Types create or replace type s t r i n g t as t a b l e of varchar2 ( 2 5 5 ) ; This is a collection type create or replace type l e c t u r e t as o b j e c t ( num i n t, s u b j e c t varchar2 (255), exercises s t r i n g t ) ; This is an object type Create the lecture serie type create or replace type l e c t u r e s e r i e t as t a b l e of l e c t u r e t ; Is this a collection type or and object type? Collection! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 14 / 96

Create the Table create t a b l e course ( course name varchar2 (255) c o n s t r a i n t course pk primary key, ects i n t e g e r check ( ects > 0 ), prerequests s t r i n g t, l e c t u r e s l e c t u r e s e r i e t ) nested t a b l e prerequests s t o r e as c o u r s e p r e r e q u e s t s n t nested t a b l e l e c t u r e s s t o r e as c o u r s e l e c t u r e s n t ( nested t a b l e exercises s t o r e as c o u r s e l e c t u r e e x e r c i s e n t ) ; Have two nested columns The column prerequest is nested one level The column lectures is nested two levels Note the store as clauses Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 15 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 16 / 96

On Types vs. On Tables Overall Design Constraints and defaults are not supported in type specifications Constraints and defaults are supported in create table statements Example The not null is not allowed in the following create or replace type a d d r e s s t n o t a l l o w e d as o b j e c t ( s t r e e t varchar2 (255) not n u l l, not allowed num varchar2 ( 255) not n u l l, not allowed z i p i n t e g e r not n u l l, not allowed c i t y varchar2 (255) not n u l l ) ; not allowed It will be shown in the following how support constraints when creating the tables Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 17 / 96

Not Null on Nested Object Column create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Make sure the street name is not null, note a level lower than above a l t e r t a b l e student add c o n s t r a i n t s t u d e n t a d d r e s s s t r e e t n n check ( address. s t r e e t i s not n u l l ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 18 / 96

Not Null on Collection Type Column create t a b l e c o u r s e n o t n u l l ( course name varchar2 (255) c o n s t r a i n t course nn pk primary key, ects i n t e g e r check ( ects > 0 ), prerequests s t r i n g t not n u l l, l e c t u r e s l e c t u r e s e r i e t not n u l l ) nested t a b l e prerequests s t o r e as course nn prerequests nt nested t a b l e l e c t u r e s s t o r e as c o u r s e n n l e c t u r e s n t ( nested t a b l e exercises s t o r e as c o u r s e n n l e c t u r e e x e r c i s e n t ) ; Results in an ORA-02331 Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 19 / 96

Not Null on Collection Type Column create t a b l e c o u r s e n o t n u l l ( course name varchar2 (255) c o n s t r a i n t course nn pk primary key, ects i n t e g e r check ( ects > 0 ), prerequests s t r i n g t not n u l l, l e c t u r e s l e c t u r e s e r i e t not n u l l ) nested t a b l e prerequests s t o r e as course nn prerequests nt nested t a b l e l e c t u r e s s t o r e as c o u r s e n n l e c t u r e s n t ( nested t a b l e exercises s t o r e as c o u r s e n n l e c t u r e e x e r c i s e n t ) ; Results in an ORA-02331 Must be do at the type level instead, e.g., create or replace type s t r i n g n o t n u l l t as t a b l e of varchar2 (255) not n u l l ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 19 / 96

Primary and Unique Keys on Nested Object Columns create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Make sure that no student live on the same street and number a l t e r t a b l e student add c o n s t r a i n t student address uk unique ( address. s t r e e t, address.num ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 20 / 96

Primary and Unique Keys on Nested Object Columns create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Make sure that no student live on the same street and number a l t e r t a b l e student add c o n s t r a i n t student address uk unique ( address. s t r e e t, address.num ) ; Primary keys are supported in a similar fashion Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 20 / 96

Primary and Unique Keys on Collection Type Columns A nested column that is a collection cannot be part of a primary key A nested column cannot be part of a unique key Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 21 / 96

Foreign Keys and Nested Columns I The prerequests must be known courses The idea in a relational context a l t e r t a b l e c o u r s e p r e r e q u e s t s n t add c o n s t r a i n t course course fk f o r e i g n key ( column value ) references course ( course name ) ; However, this does not work: A nested column cannot be used in a foreign key a l t e r t a b l e c o u r s e p r e r e q u e s t s n t add c o n s t r a i n t course course fk check ( column value i n ( s e l e c t course name from course ) ) ; Not allowed either Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 22 / 96

Foreign Keys and Nested Columns II Must do manual checking, the following must return zero s e l e c t count ( ) from ( s e l e c t cs. column value as course prere from course c, t a b l e ( c. prerequests ) cs minus s e l e c t course name from course ) ) ; Bad It is extremely ugly, and cannot be forgiven, that values in collections cannot be used in foreign key constraints! Fix this Oracle! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 23 / 96

Summary: Constraints Very weak support of constraint on collection type columns Good support of constraints on object type columns Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 24 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 25 / 96

Non-Unique Indexes on a Nested Column create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Create an index on an object type create index s t u d e n t a d d r e s s z i p i d x on student ( address. z i p ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 26 / 96

Non-Unique Indexes on a Nested Column create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; Create an index on an object type create index s t u d e n t a d d r e s s z i p i d x on student ( address. z i p ) ; Create an index on a collection type create index s t u d e n t e m a i l i d x on s t u d e n t e m a i l n t ( column value ) ; Note this special value column value Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 26 / 96

An Index on a Multi-Level Nested Column create t a b l e course ( course name varchar2 (255) c o n s t r a i n t course pk primary key, ects i n t e g e r check ( ects > 0 ), prerequests s t r i n g t, l e c t u r e s l e c t u r e s e r i e t ) nested t a b l e prerequests s t o r e as c o u r s e p r e r e q u e sts n t nested t a b l e l e c t u r e s s t o r e as c o u r s e l e c t u r e s n t ( nested t a b l e exercises s t o r e as c o u r s e l e c t u r e e x e r c i s e n t ) ; Create an index on the lecture subjects Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 27 / 96

An Index on a Multi-Level Nested Column create t a b l e course ( course name varchar2 (255) c o n s t r a i n t course pk primary key, ects i n t e g e r check ( ects > 0 ), prerequests s t r i n g t, l e c t u r e s l e c t u r e s e r i e t ) nested t a b l e prerequests s t o r e as c o u r s e p r e r e q u e sts n t nested t a b l e l e c t u r e s s t o r e as c o u r s e l e c t u r e s n t ( nested t a b l e exercises s t o r e as c o u r s e l e c t u r e e x e r c i s e n t ) ; Create an index on the lecture subjects create index c o u r s e l e c t u r e s u b j e c t i d x on c o u r s e l e c t u r e s n t ( s u b j e c t ) ; Must know that subject is a column in the nested table The name of the nested table is needed, otherwise like other indexes Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 27 / 96

Unique Indexes on a Nested Column Create an unique index create unique index s t u d e n t e m a i l i d x on s t u d e n t e m a i l n t ( column value ) ; Insert with an existing email address i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( curt@cs. com ) ) ; Results in an integrity constraint violation Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 28 / 96

Quiz: Unique Indexes on a Nested Column Will this insert statement with unique index on column value work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org, ann@nice. org ) ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 29 / 96

Quiz: Unique Indexes on a Nested Column Will this insert statement with unique index on column value work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org, ann@nice. org ) ) ; No the uniqueness is specified at the column value! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 29 / 96

Quiz: Unique Indexes on a Nested Column Will this insert statement with unique index on column value work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org, ann@nice. org ) ) ; No the uniqueness is specified at the column value! Will this insert statement work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org ) ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 29 / 96

Quiz: Unique Indexes on a Nested Column Will this insert statement with unique index on column value work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org, ann@nice. org ) ) ; No the uniqueness is specified at the column value! Will this insert statement work? i n s e r t i n t o student values (606, Fred, address t ( Pie, 32, 9000, Aalborg ), e m a i l t ( fred@nice. org ) ) ; Yes! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 29 / 96

Summary: Creating Nested Table Collection type vs. object type The special value column value Good support for primary key and unique key on nested columns No support for foreign keys on nested columns This is clearly a missing feature Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 30 / 96

Summary: Creating Nested Table Collection type vs. object type The special value column value Good support for primary key and unique key on nested columns No support for foreign keys on nested columns This is clearly a missing feature Recommendations Composite attribute from ERD can quite naturally be used in NFNF Multivalue attributes are well-suited for using with NFNF Be careful nesting more than two levels Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 30 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 31 / 96

Introduction Rules of Thumb At the outer-table level insert, update, and delete syntax is as in plain SQL At the inner-table level the syntax has changed Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 32 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 33 / 96

Insert i n s e r t i n t o student values ( 102, Pete, address t ( Pie, 31, 9000, Aalborg ), e m a i l t ( pete@verynice. org, pete@example. com ) ) ; i n s e r t i n t o student values ( 101, Curt, address t ( Elm, 11, 8000, Aarhus ), e m a i l t ( curt@cs. com ) ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 34 / 96

Insert i n s e r t i n t o student values ( 102, Pete, address t ( Pie, 31, 9000, Aalborg ), e m a i l t ( pete@verynice. org, pete@example. com ) ) ; i n s e r t i n t o student values ( 101, Curt, address t ( Elm, 11, 8000, Aarhus ), e m a i l t ( curt@cs. com ) ) ; Even if only a single row in the nested column email t must be used Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 34 / 96

Insert with Null I i n s e r t i n t o student values ( 303, Nick, address t ( Sun, 21, 9000, Aalborg ), n u l l ) ; 303 Nick Sun 21 9000 Aalborg null The address cannot be a null value, decleared as not null Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 35 / 96

Insert with Null II i n s e r t i n t o student values (707, Jim, n u l l, n u l l ) Disallowed because address is declared not null i n s e r t i n t o student s values (707, Jim, address t ( n u l l, n u l l, n u l l, n u l l ), n u l l ) Disallowed, because address.street is declared not null i n s e r t i n t o student s values (707, Jim, address t ( Boulevarden, n u l l, n u l l, n u l l ), n u l l ) Allowed, minimal set of information provided Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 36 / 96

Insert with Column Using Column Names i n s e r t i n t o student ( ssn, student name, address ) values (606, Jim, address t ( Boulevarden, 20, 9000, Aalborg ) ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 37 / 96

Insert with Column Using Column Names i n s e r t i n t o student ( ssn, student name, address ) values (606, Jim, address t ( Boulevarden, 20, 9000, Aalborg ) ) This is the same i n s e r t i n t o student s ( ssn, student name, s. address ) values (606, Jim, address t ( Boulevarden, 20, 9000, Aalborg ) ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 37 / 96

Insert with Column Using Column Names i n s e r t i n t o student ( ssn, student name, address ) values (606, Jim, address t ( Boulevarden, 20, 9000, Aalborg ) ) This is the same i n s e r t i n t o student s ( ssn, student name, s. address ) values (606, Jim, address t ( Boulevarden, 20, 9000, Aalborg ) ) Weird correlation name is optional when inserting Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 37 / 96

Illegale Inserts i n s e r t i n t o student s ( ssn, student name, s. address. s t r e e t, s. address. num, s. address. zip, s. address. c i t y ) values (606, Jim, Boulevarden, 20, 9000, Aalborg ) Does not work: only simple column names are allowed, not that logical Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 38 / 96

Illegale Inserts i n s e r t i n t o student s ( ssn, student name, s. address. s t r e e t, s. address. num, s. address. zip, s. address. c i t y ) values (606, Jim, Boulevarden, 20, 9000, Aalborg ) Does not work: only simple column names are allowed, not that logical i n s e r t i n t o student values (606, Jim, Boulevarden, 20, 9000, Aalborg ) Too many values, this is quite logical! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 38 / 96

Insert Empty Collection i n s e r t i n t o student values ( 404, E r i c, address t ( Blv, 29, 9000, Aalborg ), e m a i l t ( ) ) ; 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> Note the empty cell value The is called a literal invocation of the type constructor. Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 39 / 96

Insert Collection 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> Giv Eric an email address update student set email = e m a i l t ( eric@nice. org ) where ssn = 404; 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg eric@nice.org Updates the entire collection column Multiple email addresses can be provided Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 40 / 96

Insert Collection, Alternative 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> i n s e r t i n t o t a b l e ( s e l e c t s. email from student s where s. ssn = 404) values ( eric@nice. org ) ; 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg eric@nice.org This insert is actually an update! Only a single email address can be provided Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 41 / 96

Insert Multi-Level Collection coursename ects prerequest lectures Alg 5 null 1 Introduction 2 Abstract Data Types 3 Searching 4 Graphs 5 NP and NP Complete 1.1 1.3 1.5 2.2 2.4 3.2 3.6 3.8 4.1 4.2 4.1 5.2 5.4 i n s e r t i n t o course values ( Alg, 5, n u l l, l e c t u r e s e r i e t ( l e c t u r e t ( 1, I n t r o d u c t i o n, s t r i n g t ( 1.1, 1.3, 1.5 ) ), l e c t u r e t ( 2, A b s tract Data Types, s t r i n g t ( 2.2, 2.4 ) ), l e c t u r e t ( 3, Searching, s t r i n g t ( 3.2, 3.6, 3.8 ) ), l e c t u r e t ( 4, Graphs, s t r i n g t ( 4.1, 4.2 ) ), l e c t u r e t ( 5, NP and NP Complete, s t r i n g t ( 4.1, 5.2, 5.4 ) ) ) ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 42 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 43 / 96

Update Part of a Nested Column Ann lives on Sun Boulevard not Elm Street update student s set s. address. s t r e e t = Sun where ssn = 202; Note the correlation name is required, i.e., both below are wrong update student wrong set address. s t r e e t = Sun where ssn = 202; update student wrong set student. address. s t r e e t = Sun where ssn = 202; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 44 / 96

Update Entire Nested Column Move Pete to Aarhus update student set address = address t ( Elm, 13, 8000, Aarhus ) where ssn = 102; Looks like plain SQL update Note the use of the constructor Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 45 / 96

Update a Collection using Table Syntax 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> Changes Curt s cs address to a.edu domain update t a b l e ( s e l e c t s. email from student s where s. ssn = 404) s t set value ( s t ) = curt@cs. edu where value ( s t ) = curt@cs. com Restriction on the Query Must return a single collection Select list must contain this single collection Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 46 / 96

Quiz: Update a Collection using Table Syntax Changes Ann s nice.org address to a.cs.org domain update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) x set value ( x ) = ann@cs. org Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 47 / 96

Quiz: Update a Collection using Table Syntax Changes Ann s nice.org address to a.cs.org domain update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) x set value ( x ) = ann@cs. org ann@cs.org ann@cs.org Both rows are updated! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 47 / 96

Quiz: Update a Collection using Table Syntax, Again update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) x set value ( x ) = ann@cs. org where value ( x ) = ann@nice. org Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 48 / 96

Quiz: Update a Collection using Table Syntax, Again update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) x set value ( x ) = ann@cs. org where value ( x ) = ann@nice. org Alternative notation is update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) set column value = ann@cs. org where column value = ann@nice. org Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 48 / 96

Quiz: Update a Collection using Table Syntax, Again update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) x set value ( x ) = ann@cs. org where value ( x ) = ann@nice. org Alternative notation is update t a b l e ( s e l e c t s. email from student s where s. ssn = 202) set column value = ann@cs. org where column value = ann@nice. org ann@cs.org This is the result wanted! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 48 / 96

Update a Part of Multiple Collections I ann@cs.org Update all domains to pete@nice.org Assume that email addresses are not unique! update t a b l e ( s e l e c t s. email from student s ) set column value = pete@nice. org where column value = pete@example. com Gives an ORA-01427 error, more than one collection in table() Cannot be done in SQL must be done in PL/SQL Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 49 / 96

Update a Part of Multiple Collections II create or replace procedure update student email ( from value i n varchar2, t o v a l u e i n varchar2 ) i s type t s n n t a b i s t a b l e of student. ssn%type ; v t s n n t a b ; begin s e l e c t s. ssn bulk c o l l e c t i n t o v from student s where from value member o f s. email ; f o r a l l i i n v. f i r s t.. v. l a s t update t a b l e ( s e l e c t s. email from student s where s. ssn = v ( i ) set column value = t o v a l u e where column value = from value ; end ; Find all outer rows to update Update the collections of emails for each outer row Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 50 / 96

Update a Part of Multiple Collections III ann@cs.org execute update student email ( pete@example. com, pete@nice. org ) ; ann@cs.org pete@nice.org pete@nice.org Note Procedural need because table function can use only one collection Ugly Oracle, fix this! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 51 / 96

Update a Collection using Constructor 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> Give Eric an email address update student set email = e m a i l t ( eric@nice. org ) where ssn = 303; 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg eric@nice.org More than one email address allowed in constructor! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 52 / 96

Updating a Multiple-Level Nested Column coursename ects prerequest lectures 1 Introduction 2 Abstract Data Types Alg 5 null 3 Searching 4 Graphs 5 NP and NP Complete 1.1 1.3 1.5 2.2 2.4 3.2 3.6 3.8 4.1 4.2 4.1 5.2 5.4 Course number 2 should be number 7 instead update t a b l e ( s e l e c t l e c t u r e s from course where course name = Alg ) s t set s t. no = 7 where s t. no = 2; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 53 / 96

Updating a Multiple-Level Nested Column, cont. coursename ects prerequest lectures 1 Introduction 2 Abstract Data Types Alg 5 null 3 Searching 4 Graphs 5 NP and NP Complete 1.1 1.3 1.5 2.2 2.4 3.2 3.6 3.8 4.1 4.2 4.1 5.2 5.4 The first exercise for the third lecture should be 3.3 and not 3.2 update t a b l e ( s e l e c t l. exercises from course, t a b l e ( l e c t u r e s ) l where course name = Alg and l. no = 3) e set value ( e ) = 3.3 where value ( e ) = 3.2 Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 54 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 55 / 96

Delete Entire Rows 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg eric@nice.org Delete Eric and Nick d e l e te from student where ssn = 404 or ssn = 303; Plain old SQL delete syntax! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 56 / 96

Delete Entire Rows Based on Nested Column Delete the students that live in Aalborg d e l e te from student s where s. address. c i t y = Aalborg As before the correlation name is needed! Straight-forward compared to plain SQL Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 57 / 96

Delete Entire Rows Based on Collection I Delete student that have the email address d e l e te from student where ann@nice. org member o f student. email This is quite straight forward! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 58 / 96

Delete Entire Rows Based on Collection II Delete students that have an email address like example.com Find the rows that have the email address like example.com s e l e c t from student s, t a b l e ( s. email ) x l where x l. column value l i k e %example. com ; However, more than one table is involved, therefore the following is illegale d e l e te from student s, t a b l e ( s. email ) x l where x l. column value l i k e %example. com ; This is not logical at the conceptual level! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 59 / 96

Delete Entire Rows Based on Collection III Delete students that have an email address like example.com d e l e te from student s where 1 = ( s e l e c t 1 from t a b l e ( s. email ) x l where x l. column value l i k e %example. com ) Note that only one row is returned for each outer row! Sorry, could not find nicer formulation Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 60 / 96

Delete Single Value from a Collection Deletes Ann s email address at nice.org (but not the other addresses) d e l e te from t a b l e ( s e l e c t s. email from student s where s. ssn = 202) s t where value ( s t ) = ann@nice. org Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 61 / 96

Delete a Single Value from a Number of Collections I Deletes.com email addresses d e l e te from t a b l e ( s e l e c t s. email from student s ) s t where value ( s t ) l i k e %.com Results in ORA-01427 error Cannot be done in SQL must be done in PL/SQL Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 62 / 96

Delete a Single Value from a Number of Collections II create or replace procedure d e l e t e s t u d e n t e m a i l l i k e ( p a t t e r n i n varchar2 ) i s type t s n n t a b i s t a b l e of student. ssn%type ; v t s n n t a b ; begin s e l e c t d i s t i n c t s. ssn bulk c o l l e c t i n t o v from student s, t a b l e ( s. email ) where column value l i k e p a t t e r n ; end ; / f o r a l l i i n v. f i r s t.. v. l a s t delete from t a b l e ( s e l e c t s. email from student s where s. ssn = v ( i ) ) where column value l i k e p a t t e r n ; Find the outer rows where collection must be modified Modify the collection for each outer row Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 63 / 96

Delete a Single Value from a Number of Collections III execute d e l e t e s t u d e n t e m a i l l i k e ( %.com ) ; ann@cs.org 101 Curt Elm 11 8000 Aarhus <empty> The Curt tuple has an empty set of emails Recall { } Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 64 / 96

Delete All Values from a Collection coursename ects prerequest lectures OOP 5 Alg C 1 Introduction to OOP 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove all prerequest for the OOP course update course set prerequests = n u l l where course name = OOP Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 65 / 96

Delete All Values from a Collection coursename ects prerequest lectures OOP 5 Alg C 1 Introduction to OOP 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove all prerequest for the OOP course update course set prerequests = n u l l where course name = OOP coursename ects prerequest lectures 1 Introduction to OOP OOP 5 null 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 65 / 96

Delete Some Values from a Collection coursename ects prerequest lectures 1 Introduction to OOP OOP 5 null 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove the first and third lecture d e l e te from t a b l e ( s e l e c t l e c t u r e s from course where course name = OOP ) s t where value ( s t ) = l e c t u r e t ( 1, I n t r o d u c t i o n to OOP, s t r i n g t ( OO S t u f f, On with the OO S t u f f ) ) ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 66 / 96

Delete Some Values from a Collection coursename ects prerequest lectures 1 Introduction to OOP OOP 5 null 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove the first and third lecture d e l e te from t a b l e ( s e l e c t l e c t u r e s from course where course name = OOP ) s t where value ( s t ) = l e c t u r e t ( 1, I n t r o d u c t i o n to OOP, s t r i n g t ( OO S t u f f, On with the OO S t u f f ) ) ; coursename ects prerequest lectures 2 Classes and Objects OOP 5 null 3 Inheritance Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Deletes only the first row Very long (and ugly!) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 66 / 96

Delete Some Values from a Collection, Alternative coursename ects prerequest lectures 1 Introduction to OOP OOP 5 null 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove the first and third lecture d e l e te from t a b l e ( s e l e c t l e c t u r e s from course where course name = OOP ) s t where s t. no = 1 or s t. no = 3; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 67 / 96

Delete Some Values from a Collection, Alternative coursename ects prerequest lectures 1 Introduction to OOP OOP 5 null 2 Classes and Objects 3 Inheritance OO Stuff On with the OO Stuff Class 2.2 Class 4.2 Inheritance 1.1 Inheritance 2.2 Remove the first and third lecture d e l e te from t a b l e ( s e l e c t l e c t u r e s from course where course name = OOP ) s t where s t. no = 1 or s t. no = 3; coursename ects prerequest lectures OOP 5 null 2 Classes and Objects Class 2.2 Class 4.2 Note where the dot is placed Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 67 / 96

Summary: Modifications For nested tables insert statement can become quite complicated Constructors are needed when inserting values in nested columns Default constructor is provided by the system The statements update from table() and delete from table () are new The dot notation can be used for object types, e.g., set address.zip = 9000 For collection types the syntax the value() function must be used, e.g., value(e) = 23 A correlation name is needed to access a nested column Unnesting is necessary to update a nested collection column This is covered in details in the next section Deleting single elements from a collection is quite complicated! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 68 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 69 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 70 / 96

Quiz s e l e c t count ( ) from student What result is returned (3 or 5)? Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 71 / 96

Quiz s e l e c t count ( ) from student What result is returned (3 or 5)? 3! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 71 / 96

Unnesting s e l e c t from student, table ( email ) ssn student name address.street address.num address.zip address.city email column value curt@cs.com This is called unnesting. Much more on this later! Note the dot names, e.g., address.zip Note that the nested column email is there twice Note the special column column value Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 72 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 73 / 96

List all the Students s e l e c t from student Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 74 / 96

List all the Students s e l e c t from student Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 74 / 96

Access a Nested Table Directly create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; s e l e c t from s t u d e n t e m a i l n t Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 75 / 96

Access a Nested Table Directly create t a b l e student ( ssn varchar2 (255) c o n s t r a i n t student pk primary key, student name varchar2 ( 255) not n u l l, address address t not n u l l, email e m a i l t ) nested t a b l e email s t o r e as s t u d e n t e m a i l n t ; s e l e c t from s t u d e n t e m a i l n t Gets an ORA-22812 error Nested tables cannot be accessed directly Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 75 / 96

Get the Value of a Collection s e l e c t value ( e ) as email from student s, t a b l e ( s. email ) e where s. ssn = 202 Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 76 / 96

Get the Value of a Collection s e l e c t value ( e ) as email from student s, t a b l e ( s. email ) e where s. ssn = 202 email Note the value keyword Note the table function Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 76 / 96

The Value Function An object reference function Takes single argument that is correlation name to a collection or an object table Returns object instances stored in the collection or object table The return type is the same type as the collection or object table Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 77 / 96

Using Constructors in the Where Clause s e l e c t from student s where s. email = e m a i l t ( curt@cs. com ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 78 / 96

Using Constructors in the Where Clause s e l e c t from student s where s. email = e m a i l t ( curt@cs. com ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 78 / 96

Using Constructors in the Where Clause s e l e c t from student s where s. email = e m a i l t ( curt@cs. com ) s e l e c t from student s where s. email = e m a i l t ( ann@nice. org ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 78 / 96

Using Constructors in the Where Clause s e l e c t from student s where s. email = e m a i l t ( curt@cs. com ) s e l e c t from student s where s. email = e m a i l t ( ann@nice. org ) Returns no rows, because Ann has two email addresses Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 78 / 96

Order in Collection Types s e l e c t from student s where s. email = e m a i l t ( ann@nice. org, ann@example. com ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 79 / 96

Order in Collection Types s e l e c t from student s where s. email = e m a i l t ( ann@nice. org, ann@example. com ) s e l e c t from student s where s. email = e m a i l t ( ann@example. com, ann@nice. org ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 79 / 96

Order in Collection Types s e l e c t from student s where s. email = e m a i l t ( ann@nice. org, ann@example. com ) s e l e c t from student s where s. email = e m a i l t ( ann@example. com, ann@nice. org ) Both returns one row Nice result! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 79 / 96

The Table Expression Get the names of students with a.org email address s e l e c t s. student name from student s, t a b l e ( s. email ) email where email. column value l i k e %.org Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 80 / 96

The Table Expression Get the names of students with a.org email address s e l e c t s. student name from student s, t a b l e ( s. email ) email where email. column value l i k e %.org student name Ann Pete Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 80 / 96

The Table Expression Get the names of students with a.org email address s e l e c t s. student name from student s, t a b l e ( s. email ) email where email. column value l i k e %.org student name Ann Pete Get the maximum value of an email address s e l e c t max( value ( x ) ) from student s, t a b l e ( s. email ) x ; Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 80 / 96

The Table Expression Get the names of students with a.org email address s e l e c t s. student name from student s, t a b l e ( s. email ) email where email. column value l i k e %.org student name Ann Pete Get the maximum value of an email address s e l e c t max( value ( x ) ) from student s, t a b l e ( s. email ) x ; email Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 80 / 96

The Table Expression Continued States that a value is a collection, i.e., not a scalar value Returns a collection that can be queried as if it was a flat table Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 81 / 96

The Table Expression Continued States that a value is a collection, i.e., not a scalar value Returns a collection that can be queried as if it was a flat table How many rows returned by s e l e c t from student, t a b l e ( email ) e1, t a b l e ( email ) e2 Make simple stuff look complicated s e l e c t column value from t a b l e ( cast ( m u l t i s e t ( s e l e c t table name from cat ) as s t r i n g t ) ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 81 / 96

The Table Expression Continued States that a value is a collection, i.e., not a scalar value Returns a collection that can be queried as if it was a flat table How many rows returned by s e l e c t from student, t a b l e ( email ) e1, t a b l e ( email ) e2 Make simple stuff look complicated s e l e c t column value from t a b l e ( cast ( m u l t i s e t ( s e l e c t table name from cat ) as s t r i n g t ) ) Alternative s e l e c t table name from cat Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 81 / 96

The cardinality Keyword The the SSN of student with more than on email address s e l e c t s. ssn from student s where c a r d i n a l i t y ( s. email ) > 1 Find the number of email addresses for each student s e l e c t s. student name, c a r d i n a l i t y ( s. email ) as no email addresses from student s Find the number rows in the unnested table s e l e c t sum( c a r d i n a l i t y ( s. email ) ) from student s Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 82 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 83 / 96

From Nested to Flat s e l e c t student. ssn, student. student name, column value as email from student, t a b l e ( student. email ) It is necessary to list the columns Weird that implicit natural-join happens, i.e., No where clause, and No join specified in from clause Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 84 / 96

Unnesting With Null Values 303 Nick Sun 21 9000 Aalborg null s e l e c t student. ssn, student. student name, column value as email from student, t a b l e ( student. email ) ( + ) Here the Oracle specific outer-join syntax must be used. This is quite ugly! Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 85 / 96

Looking only at Subtable Data Show me the lecture information for the OOP course s e l e c t l. from course, t a b l e ( course. l e c t u r e s ) l where course. course name = DBS The outer-table course must be listed in the from clause Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 86 / 96

Multilevel Unnest s e l e c t c. course name, c. ects, value ( p ) as prerequest, l. no, l. subject, value ( e ) as exercise from course c, t a b l e ( c. prerequests ) ( + ) p, t a b l e ( c. l e c t u r e s ) ( + ) l, t a b l e ( l. exercises ) ( + ) e order by c. course name, l. no Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 87 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 88 / 96

Is Null vs. Is Empty 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where student. email i s n u l l Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 89 / 96

Is Null vs. Is Empty 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where student. email i s n u l l 303 Nick Sun 21 9000 Aalborg null Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 89 / 96

Is Null vs. Is Empty 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where student. email i s n u l l 303 Nick Sun 21 9000 Aalborg null s e l e c t from student where student. email i s empty Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 89 / 96

Is Null vs. Is Empty 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where student. email i s n u l l 303 Nick Sun 21 9000 Aalborg null s e l e c t from student where student. email i s empty 404 Eric Blv 29 9000 Aalborg <empty> Returns the row with Eric Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 89 / 96

Is Null vs. Is Empty 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where student. email i s n u l l 303 Nick Sun 21 9000 Aalborg null s e l e c t from student where student. email i s empty 404 Eric Blv 29 9000 Aalborg <empty> Returns the row with Eric Insert null vs. email t () Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 89 / 96

Member Of 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where ann@nice. org member o f student. email Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 90 / 96

Member Of 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where ann@nice. org member o f student. email Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 90 / 96

Member Of 303 Nick Sun 21 9000 Aalborg null 404 Eric Blv 29 9000 Aalborg <empty> s e l e c t from student where ann@nice. org member o f student. email Similar to s e l e c t from student s where ann@nice. org i n ( s e l e c t value ( e ) from t a b l e ( s. email ) e ) Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 90 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 91 / 96

Summary on Queries Cannot query nested storage tables directly Correlation name always needed to access nested column Difference between is null and is empty Unnesting: converted a nested table to a flat table The member of predicate similar to in Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 92 / 96

Outline 1 Introduction 2 Creating Nested Tables Constraints Indexing 3 Modifications Insert Update Delete 4 Querying Introduction Simple Queries Unnesting Empty and Member Keywords Summary 5 Summary Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 93 / 96

NF vs NFNF Query language still declarative Collection types very handy NF More tables More rows No user-defined types Excellent support for constraints Uses standard SQL NFNF Fewer table Typically fewer rows Require user-defined types Limited support for constraints Non-standard SQL Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 94 / 96

Recommendations Use collection types for multi-valued attributes Use object type for composite attributes Paticular if composite attribute used in several places Consider using nested table for 1-n relationships Consider if there are places where collection types are useful Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 95 / 96

Readings Object-Relational Developer s Guide: Good overview of the object-relational features in Oracle 11g. Data Cartridge Developer s Guide: Object-relational feature targeted towards domain indexes and operators. Has good examples. Object-Oriented Oracle. Introduction book. Covers several object-oriented topics both in general and with PL/SQL. No advanced topics covered. Oracle PL/SQL Programming. Good PL/SQL in general (packages). Too simple coverage of object-oriented programming in PL/SQL. Kristian Torp (Aalborg University) Normal Form vs. Non-First Normal Form September 1, 2009 96 / 96