Data Definition and Integrity Constraints

Slides:



Advertisements
Similar presentations
Relational data integrity
Advertisements

Access Control & Views Reading: C&B, Chap 7. Dept of Computing Science, University of Aberdeen2 In this lecture you will learn the principles of object.
Database Design: ER Modelling (Continued)
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Structured Query Language (SQL)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 SQL: Data Definition, Constraints, and Basic Queries and Updates.
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Introduction to Structured Query Language (SQL)
Database Systems: A Practical Approach to Design, Implementation and Management International Computer Science S. Carolyn Begg, Thomas Connolly Lecture.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
1 Pertemuan 11 SQL Data Definition Matakuliah: >/ > Tahun: > Versi: >
Database Design Concepts INFO1408 Term 2 week 1 Data validation and Referential integrity.
Introduction to Structured Query Language (SQL)
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
CSC 240 (Blum)1 Data Definition Language Based on Chapter 6 of Database Systems (Connolly and Begg)
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
CSE314 Database Systems Lecture 4 Basic SQL Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson Ed Slide Set.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Database Technical Session By: Prof. Adarsh Patel.
Chapter 6 SQL Data Definition Language Chapter 7 in Textbook.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
10/25/2012ISC239 Isabelle Bichindaritz1 SQL Commands.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 Chapter 17 Methodology - Local Logical Database Design.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Chapter 7 SQL: Data Definition Pearson Education © 2009.
Visual Programing SQL Overview Section 1.
Chapter Name SQL: Data Definition
Chapter 6 SQL. Agenda Data Definition Language (DDL) Access Control.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Data Definition Language
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Fundamentals of DBMS Notes-1.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
SQL: Data Definition Transparencies
Managing Tables, Data Integrity, Constraints by Adrienne Watt
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Chapter 7 SQL – Data Definition Pearson Education © 2014.
Minggu 5, Pertemuan 9 SQL: Data Definition
STRUCTURED QUERY LANGUAGE
CS4222 Principles of Database System
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Integrity 5/5/2019 See scm-intranet.
Presentation transcript:

Data Definition and Integrity Constraints Reading: C&B, Chap 7

In this lecture you will learn the different SQL data types & related scalar functions how to define new data types with DDL statements some of the integrity constraints used in DBMSs SQL's Integrity Enhancement Features (IEF) how integrity constraints can affect row operations the notion of schemas Dept. of Computing Science, University of Aberdeen

SQL's Integrity Enhancement Features (IEF) So far, we have thought of databases as static repositories. In fact, real databases are often very ‘active’ with 100's of users simultaneously querying and updating the DB. So database integrity is important IEFs allow the DB designer to specify & enforce: domain constraints required data entity integrity referential integrity enterprise constraints (business rules) Dept. of Computing Science, University of Aberdeen

Creating Tables - Data Definition CREATE TABLE is used to define relational tables it defines the data type for each column defines rules for how data may be inserted and deleted CREATE TABLE Staff (StaffNo VARCHAR(5), Lname VARCHAR(20), Salary FLOAT, HireDate DATE); VARCHAR, FLOAT, and DATE are examples of domains Domains specify type & range of allowed data values Dept. of Computing Science, University of Aberdeen

Built-in Data Types (Domains) in ANSI SQL ANSI SQL supports many data types (vendors often also have own dialects): CHARACTER (CHAR), CHARACTER VARYING (VARCHAR) NUMERIC, DECIMAL (DEC), INTEGER (INT), SMALLINT FLOAT, REAL, DOUBLE PRECISION DATE, TIME, TIMESTAMP BOOLEAN, BIT BINARY LARGE OBJECT, etc. Some types have an associated size . e.g. CHAR(5) Dept. of Computing Science, University of Aberdeen

User-Defined Domains in ANSI SQL CREATE DOMAIN SexType AS CHAR(1) DEFAULT 'M' CHECK (VALUE IN ('M', 'F')); CREATE TABLE Staff (StaffNo VARCHAR(5), Lname VARCHAR(20), Salary FLOAT, HireDate DATE, Sex SexType); INSERT INTO Staff VALUES ('S0057', 'Smith', 12075.50, '12-JAN-1990', 'F'); . . OK ('S0023', 'Jones', 14250.50, '14-FEB-1997', 'X'); . . Fails SexType acts as a constraint on allowed range of values Dept. of Computing Science, University of Aberdeen

Required Data & More Domain Constraints Example: CREATE TABLE Staff ( StaffNo VARCHAR(5) NOT NULL, Lname VARCHAR(20) NOT NULL, Salary FLOAT CHECK (Salary BETWEEN 50 and 20000), HireDate DATE, Sex SexType); StaffNo & Lname are required - may not be NULL The CHECK clause gives a domain constraint for Salary Updates & insertions will fail if constraints not satisfied Dept. of Computing Science, University of Aberdeen

Dynamic Domain Constraints Domains may be defined ‘dynamically’ using values that already exist in the database: CREATE DOMAIN StaffNoDomain AS VARCHAR(5) CHECK (VALUE IN (SELECT StaffNo FROM Staff)); CREATE TABLE PropertyForRent (PropertyNo VARCHAR(5) NOT NULL, StaffNo StaffNoDomain); This could be used to ensure every StaffNo in PropertyForRent is valid Domains can be deleted: DROP DOMAIN DomainName [RESTRICT | CASCADE] This is a note Dept. of Computing Science, University of Aberdeen

Dept. of Computing Science, University of Aberdeen Scalar Functions Scalar functions may be used to convert/manipulate data values (remember aggregates: MIN, MAX, etc?). Example: SELECT SUBSTRING(Lname FROM 1 TO 3), CONVERT(INTEGER Salary), EXTRACT(YEAR FROM HireDate) FROM Staff; ANSI SQL supports many scalar functions... See CB, Table 6.2, p163 Result SMI 12075 1990 Dept. of Computing Science, University of Aberdeen

Entity Integrity - Primary Keys Reminder: the primary key of each row in a table must be unique and non-null. Example: The primary key of the Viewing table is composed of two attributes (composite key): CREATE TABLE Viewing ( ClientNo VARCHAR(5) NOT NULL, PropertyNo VARCHAR(5) NOT NULL, PRIMARY KEY (ClientNo, PropertyNo)); SQL will reject operations that would violate primary key uniqueness Can use UNIQUE(Colname) to enforce uniqueness of alternate keys Dept. of Computing Science, University of Aberdeen

Referential Integrity - Foreign Keys Reminders: A foreign key links a child table to its parent table. If a foreign key is non-null, it must match an existing row in the parent table. So... SQL has more keywords for this: CREATE TABLE PropertyForRent (... StaffNo VARCHAR(5) NOT NULL, FOREIGN KEY (StaffNo) REFERENCES Staff); SQL will reject operations that would violate referential integrity Dept. of Computing Science, University of Aberdeen

Referential Integrity and Referential Actions Question: if a key attribute in the parent table is modified, what should happen in the child table ? - SQL provides 4 alternative referential actions: FOREIGN KEY (Key) REFERENCES Table [ON DELETE | UPDATE Action] CASCADE - apply changes to child rows SET NULL - set child keys to NULL SET DEFAULT - set child keys to DEFAULT value NO ACTION - reject the operation (default) Suppose a client is removed from the DreamHome DBMS. What's the most appropriate action to specify for ClientNo in the Viewing table? Dept. of Computing Science, University of Aberdeen

Enterprise Constraints (Business Rules) Sometimes, real-world business rules involve constraints that refer to more than one table. Its useful to define enterprise constraints just once. Example: A member of staff may manage no more than 100 properties: CREATE ASSERTION StaffNotOverLoaded CHECK (NOT EXISTS (SELECT StaffNo FROM PropertyForRent GROUP BY StaffNo HAVING COUNT (*) > 100)); CREATE TABLE PropertyForRent ( ... CONSTRAINT StaffNotOverLoaded); Dept. of Computing Science, University of Aberdeen

Dept. of Computing Science, University of Aberdeen Triggers Often, real-world business rules cannot be implemented using constraints. Example: The branch manager is notified by e-mail if a client views more than 10 properties. Different DBMSs often provide a trigger mechanism Triggers may contain procedural code (if/then/else, function calls) Triggers can implement complex database operations However, triggers can add to database complexity (hidden rules) Triggers are not ANSI standard - should they be? Dept. of Computing Science, University of Aberdeen

Putting It All Together - Schemas A schema is a collection of named DBMS objects: Tables, Domains, Constraints, Views (later), Triggers, and more ... A multi-user DMBS may contain multiple schemas: Each schema is owned by a given user A Database Administrator (DBA) manages schemas (CREATE, DROP) Schemas are maintained in special system tables However, different DBMSs have different ways of managing schemas... Dept. of Computing Science, University of Aberdeen

Simplified Data Model of a DBMS Dept. of Computing Science, University of Aberdeen

Database Schemas Evolve Over Time Ideally, a database is created once and then used for many years ... BUT The data model may be improved (integrity, performance) ... New features may be added in new releases ... Enterprise rules may change ... Therefore, SQL provides many options for changing tables: See ALTER TABLE, CB Ch. 6, p172 Dept. of Computing Science, University of Aberdeen

Dept. of Computing Science, University of Aberdeen Summary So Far... DBs are ‘active’ or ‘alive’ - contents always changing The structure of a DB can also evolve over time... DB contents should always be consistent - integrity ANSI SQL provides several Integrity Enhancement Features (IEFs) IEF => domain constraints, entity/referential integrity, business rules... IEFs imply additional design choices for new DBs One DBMS can manage multiple DBs - notion of schemas & privileges Dept. of Computing Science, University of Aberdeen