Chapter 3 The Relational Model & The Relational Constraint. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Similar documents
How To Manage Data In A Database System

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

How To Create A Table In Sql (Ahem)

CSC 443 Data Base Management Systems. Basic SQL

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

The Relational Data Model and Relational Database Constraints

The Structured Query Language. De facto standard used to interact with relational DB management systems Two major branches

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

The Relational Model. Why Study the Relational Model?

Summary on Chapter 4 Basic SQL

Data Modeling. Database Systems: The Complete Book Ch ,

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

EECS 647: Introduction to Database Systems

Lecture 6. SQL, Logical DB Design

SQL NULL s, Constraints, Triggers

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

SQL-99: Schema Definition, Basic Constraints, and Queries

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

Outline. Data Modeling. Conceptual Design. ER Model Basics: Entities. ER Model Basics: Relationships. Ternary Relationships. Yanlei Diao UMass Amherst

Chapter 6: Integrity Constraints

Chapter 8. SQL-99: SchemaDefinition, Constraints, and Queries and Views

New York University Computer Science Department Courant Institute of Mathematical Sciences

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

SQL Server. 1. What is RDBMS?

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

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

5.1 Database Schema Schema Generation in SQL

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

IT2305 Database Systems I (Compulsory)

Oracle 10g PL/SQL Training

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

CHAPTER 8: SQL-99: SCHEMA DEFINITION, BASIC CONSTRAINTS, AND QUERIES

IT2304: Database Systems 1 (DBS 1)

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

Normal Form vs. Non-First Normal Form

SQL Server An Overview

3. Relational Model and Relational Algebra

Normalization in OODB Design

Oracle Database 10g: Introduction to SQL

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

types, but key declarations and constraints Similar CREATE X commands for other schema ëdrop X name" deletes the created element of beer VARCHARè20è,

Oracle Database 10g Express

Programming with SQL

AVOIDANCE OF CYCLICAL REFERENCE OF FOREIGN KEYS IN DATA MODELING USING THE ENTITY-RELATIONSHIP MODEL

Chapter 9, More SQL: Assertions, Views, and Programming Techniques

2. Conceptual Modeling using the Entity-Relationship Model

A. TRUE-FALSE: GROUP 2 PRACTICE EXAMPLES FOR THE REVIEW QUIZ:

More SQL: Assertions, Views, and Programming Techniques

Once the schema has been designed, it can be implemented in the RDBMS.

Section of DBMS Selection & Evaluation Questionnaire

Fundamentals of Database Design

SQL Simple Queries. Chapter 3.1 V3.0. Napier University Dr Gordon Russell

Scheme G. Sample Test Paper-I

Managing Objects with Data Dictionary Views. Copyright 2006, Oracle. All rights reserved.

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

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

Instant SQL Programming

BCA. Database Management System

LiTH, Tekniska högskolan vid Linköpings universitet 1(7) IDA, Institutionen för datavetenskap Juha Takkinen

History of SQL. Relational Database Languages. Tuple relational calculus ALPHA (Codd, 1970s) QUEL (based on ALPHA) Datalog (rule-based, like PROLOG)

Relational Schema. CS 4700/6700 A Sample of Small Database Design Using Microsoft Access

5. CHANGING STRUCTURE AND DATA

Chapter 15 Basics of Functional Dependencies and Normalization for Relational Databases

Databases and BigData

Part 4: Database Language - SQL

Geodatabase Programming with SQL

SQL: Queries, Programming, Triggers

DATABASE MANAGEMENT SYSTEMS. Question Bank:

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

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

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

Oracle Database: SQL and PL/SQL Fundamentals

Maintaining Stored Procedures in Database Application

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

SQL: Queries, Programming, Triggers

Review of Business Information Systems Third Quarter 2013 Volume 17, Number 3

Basic Concepts of Database Systems


2. Basic Relational Data Model

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

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

1 SQL Data Types and Schemas

Review: Participation Constraints

CPS352 Database Systems: Design Project

Chapter 5: Logical Database Design and the Relational Model Part 2: Normalization. Introduction to Normalization. Normal Forms.

Functional Dependency and Normalization for Relational Databases

Procedural Extension to SQL using Triggers. SS Chung

Introduction to SQL ( )

The Relational Data Model: Structure

Intermediate SQL C H A P T E R4. Practice Exercises. 4.1 Write the following queries in SQL:

Oracle Database: SQL and PL/SQL Fundamentals NEW

Introduction This document s purpose is to define Microsoft SQL server database design standards.

ATTACHMENT 6 SQL Server 2012 Programming Standards

Database Programming with PL/SQL: Learning Objectives


Working with DB2 UDB objects

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

Using Temporary Tables to Improve Performance for SQL Data Services

COURSE CODE: CIT 844 COURSE TITLE: ADVANCED DATABASE MANAGEMENT SYSTEM

Oracle Database 12c: Introduction to SQL Ed 1.1

Transcription:

Chapter 3 The Relational Model & The Relational Constraint

Relational Model Concepts Represents data as a collection of relations Table of values Row Represents a collection of related data values Fact that typically corresponds to a real-world entity or relationship Tuple Table name and column names Interpret the meaning of the values in each row attribute

Relational Model Concepts

Attributes, Tuples, and Relations Relation schema R Denoted by R(A 1, A 2,...,A n ) Made up of a relation name R and a list of attributes, A 1, A 2,..., A n Attribute A i Name of a role played by some domain D in the relation schema R Degree (or arity) of a relation Number of attributes n of its relation schema

Attributes, Tuples, and Relations Relation (or relation state) Set of n-tuples r = {t 1, t 2,..., t m } Each n-tuple t Ordered list of n values t =<v 1, v 2,..., v n Each value v i, 1 i n, is an element of dom(a i ) or is a special NULL value

Characteristics of Relations Ordering of tuples in a relation Relation defined as a set of tuples Elements have no order among them Ordering of values within a tuple and an alternative definition of a relation Order of attributes and values is not that important As long as correspondence between attributes and values maintained

Characteristics of Relations

Characteristics of Relations Values and NULLs in tuples Each value in a tuple is atomic Flat relational model Composite and multivalued attributes not allowed First normal form assumption Multivalued attributes Must be represented by separate relations Composite attributes Represented only by simple component attributes in basic relational model

Characteristics of Relations NULL values Represent the values of attributes that may be unknown or may not apply to a tuple Meanings for NULL values Value unknown Value exists but is not available Attribute does not apply to this tuple (also known as value undefined)

Relational Model Notation Name of a relation schema: STUDENT Indicates the current set of tuples in that relation Notation: STUDENT(Name, Ssn,...) Refers only to relation schema Attribute A can be qualified with the relation name R to which it belongs Using the dot notation R.A

Relational Model Constraints Constraints Restrictions on the actual values in a database state Derived from the rules in the miniworld that the database represents Inherent model-based constraints or implicit constraints Inherent in the data model

Relational Model Constraints Schema-based constraints or explicit constraints Can be directly expressed in schemas of the data model Application-based or semantic constraints or business rules Cannot be directly expressed in schemas Expressed and enforced by application program

Domain Constraints Typically include: Numeric data types for integers and real numbers Characters Booleans Fixed-length strings Variable-length strings Date, time, timestamp Money Other special data types

Key Constraints and Constraints Candidate key on NULL Values Relation schema may have more than one key Primary key of the relation Designated among candidate keys Underline attribute Other candidate keys are designated as unique keys

Key Constraints and Constraints on NULL Values

Relational Databases and Relational Database Schemas Invalid state Does not obey all the integrity constraints Valid state Satisfies all the constraints in the defined set of integrity constraints IC

Integrity, Referential Integrity, and Foreign Keys Entity integrity constraint No primary key value can be NULL Referential integrity constraint Specified between two relations Maintains consistency among tuples in two relations

Integrity, Referential Integrity, Foreign key rules: and Foreign Keys The attributes in FK have the same domain(s) as the primary key attributes PK All integrity constraints should be specified on relational database schema

Other Types of Constraints Semantic integrity constraints May have to be specified and enforced on a relational database Use triggers and assertions More common to check for these types of constraints within the application programs

Update Operations, Transactions, and Dealing with Constraint Violations Operations of the relational model can be categorized into retrievals and updates Basic operations that change the states of relations in the database: Insert Delete Update (or Modify)

Insert Examples Insert, Update INSERT INTO EMPLOYEE (empno,fname, Lname, Dno) VALUES (1, Robert, Hatcher, 5); Update UPDATE EMPLOYEE SET Salary = Salary * 1.1 WHERE Dno = 5;

The Insert Operation Provides a list of attribute values for a new tuple t that is to be inserted into a relation R Can violate any of the types of constraints If an insertion violates one or more constraints Default option is to reject the insertion

The Delete Operation Can violate only referential integrity If tuple being deleted is referenced by foreign keys from other tuples Restrict Reject the deletion Cascade Propagate the deletion by deleting tuples that reference the tuple that is being deleted Set null or set default Modify the referencing attribute values that cause the violation

The Update Operation Necessary to specify a condition on attributes of relation Select the tuple (or tuples) to be modified If attribute not part of a primary key nor of a foreign key Usually causes no problems Updating a primary/foreign key Similar issues as with Insert/Delete

SPECIFYING BASIC CONSTRAINTS IN SQL

Specifying Constraints and Default Values Because SQL allows NULLs as attribute values, a constraint NOT NULL may be specified if NULL is not permitted for a particular attribute.

Specifying Constraints and Default Values It is also possible to define a default value for an attribute by appending the clause DEFAULT <value> to an attribute definition. The default value is included in any new tuple if an explicit value is not provided for that attribute. If no default clause is specified, the default default value is NULL.

Schema and Catalog Concepts in SQL A schema is created via the CREATE SCHEMA statement, which can include all the schema elements definitions. Alternatively, the schema can be assigned a name and authorization identifier, and the elements can be defined later. CREATE DATABASE COMPANY AUTHORIZATION JSMITH;

The CREATE TABLE Command The CREATE TABLE command is used to specify a new relation by giving it a name and specifying its attributes and constraints.

Specifying Constraints and Default Values In SQL2, the schema designer can specify the action to be taken if a referential integrity constraint is violated upon deletion of a referenced tuple or upon modification of a referenced primary key value, by attaching a referential triggered action clause to any foreign key constraint. The options include SET NULL, CASCADE, and SET DEFAULT. An option must be qualified with either ON DELETE or ON UPDATE.

SCHEMA CHANGE STATEMENTS IN SQL

The DROP DATABASE and DROP TABLE Commands If a whole schema is not needed any more, the DROP DATABASE command can be used. There are two drop behavior options: CASCADE and RESTRICT. For example, to remove the COMPANY database schema and all its tables, domains, and other elements, the CASCADE option is used as follows: DROP DATABASE COMPANY CASCADE; If the RESTRICT option is chosen in place of CASCADE, the schema is dropped only if it has no elements in it; otherwise, the DROP command will not be executed.

The DROP DATABASE and DROP TABLE Commands If a base relation within a schema is not needed any longer, the relation and its definition can be deleted by using the DROP TABLE command. For example, we can get rid of the DEPENDENT relation by issuing the command: DROP TABLE DEPENDENT CASCADE; If the RESTRICT option is chosen instead of CASCADE, a table is dropped only if it is not referenced in any constraints or views. With the CASCADE option, all such constraints and views that reference the table are dropped automatically from the schema, along with the table itself.

The ALTER TABLE Command The definition of a base table can be changed by using the ALTER TABLE command, which is a schema evolution command. The possible alter table actions include adding or dropping a column (attribute), changing a column definition, and adding or dropping table constraints.

The ALTER TABLE Command For example, to add an attribute for keeping track of jobs of employees to the EMPLOYEE base relations in the COMPANY schema, we can use the command: ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12); We must still enter a value for the new attribute JOB for each individual EMPLOYEE tuple. This can be done either by specifying a default clause or by using the UPDATE command. If no default clause is specified, the new attribute will have NULLs in all the tuples of the relation immediately after the command is executed; hence, the NOT NULL constraint is not allowed in this case.

The ALTER TABLE Command To drop a column, we must choose either CASCADE or RESTRICT for drop behavior. If CASCADE is chosen, all constraints and views that reference the column are dropped automatically from the schema, along with the column. If RESTRICT is chosen, the command is successful only if no views or constraints reference the column. For example, the following command removes the attribute ADDRESS from the EMPLOYEE base table: ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS CASCADE;

The ALTER TABLE Command It is also possible to alter a column definition by dropping an existing default clause or by defining a new default clause. The following examples illustrate this clause: ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN DROP DEFAULT; ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN SET DEFAULT "333445555";

The ALTER TABLE Command Finally, one can change the constraints specified on a table by adding or dropping a constraint. To be dropped, a constraint must have been given a name when it was specified. ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINT EMPSUPERFK CASCADE; Adding a new constraint is specified by using the ADD keyword followed by the new constraint.

The Transaction Concept Transaction Executing program Includes some database operations Must leave the database in a valid or consistent state Online transaction processing (OLTP) systems Execute transactions at rates that reach several hundred per second