Part III. Data Modelling. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Similar documents
CS2 Current Technologies Lecture 2: SQL Programming Basics

CS2 Current Technologies Lecture 3: SQL - Joins and Subqueries

King Fahd University of Petroleum and Minerals

CS2 Current Technologies Note 1 CS2Bh

Department of Computer Science and Information Systems, College of Business and Technology, Morehead State University

Real-World Performance Training SQL Introduction

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

Programming Languages

Introduction. Introduction to Oracle: SQL and PL/SQL

1 SQL Structured Query Language

Database Management System. * First install Mysql Database or Wamp Server which contains Mysql Databse.

SQL Structured Query Language Introduction

Database implementation Further SQL

1 SQL Structured Query Language

Informatics Practices (065) Sample Question Paper 1 Section A

CS Reading Packet: "Writing relational operations using SQL"

SQL. Char (30) can store ram, ramji007 or 80- b

Pivot Tables Motivation (1)

Part V. Working with Information Systems. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Databases. Relational Model, Algebra and operations. How do we model and manipulate complex data structures inside a computer system? Until

Introduc.on to Databases

Creating and Managing Tables Schedule: Timing Topic

CS Reading Packet: "Views, and Simple Reports - Part 1"

Objectives. After completing this lesson, you should be able to do the following:

Databases 1. Daniel POP

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Databases. Jörg Endrullis. VU University Amsterdam

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

Programming the Database

RESTRICTING AND SORTING DATA

RDBMS Using Oracle. BIT-4 Lecture Week 3. Lecture Overview

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Databases - 3. Null, Cartesian Product and Join. Null Null is a value that we use when. Something will never have a value

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

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

Objectives. After completing this lesson, you should be able to do the following:

Table : Purchase. Field DataType Size Constraints CustID CHAR 5 Primary key CustName Varchar 30 ItemName Varchar 30 PurchaseDate Date

Q.1 Short Questions Marks 1. New fields can be added to the created table by using command. a) ALTER b) SELECT c) CREATE. D. UPDATE.

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

SQL STRUCTURED QUERY LANGUAGE

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Relational Databases

SQL. Rodrigo García Carmona Universidad San Pablo-CEU Escuela Politécnica Superior

Database Technology Introduction. Heiko Paulheim

Active Databases Part 1: Introduction CS561

Slides by: Ms. Shree Jaswal

CIS Reading Packet: "Views, and Simple Reports - Part 1"

Basant Group of Institution

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

P.G.D.C.M. (Semester I) Examination, : ELEMENTS OF INFORMATION TECHNOLOGY AND OFFICE AUTOMATION (2008 Pattern)

Mahathma Gandhi University

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Relational Database Management Systems Mar/Apr I. Section-A: 5 X 4 =20 Marks

DBMS. Relational Model. Module Title?

COSC 122 Computer Fluency. Databases. Dr. Ramon Lawrence University of British Columbia Okanagan

Database Design. 1-3 History of the Database. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Relational Database Management Systems Oct I. Section-A: 5 X 4 =20 Marks

Overview of PL/SQL. About PL/SQL. PL/SQL Environment. Benefits of PL/SQL. Integration

SYSTEM CODE COURSE NAME DESCRIPTION SEM

8) A top-to-bottom relationship among the items in a database is established by a

5 Integrity Constraints and Triggers

The SQL data-definition language (DDL) allows defining :

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

Relational Algebra and SQL

Chapter 3: Introduction to SQL

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Slide 17-1

Chapter 3: Introduction to SQL

Sample Question Paper

SQL functions fit into two broad categories: Data definition language Data manipulation language

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

QQ Group

The Relational Model

Relational Model: History

Full file at

CSIE30600 Database Systems Basic SQL 2. Outline

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

chapter 2 G ETTING I NFORMATION FROM A TABLE

Database Systems SQL SL03

The Relational Model

Chapter 1: Introduction

Databases IIB: DBMS-Implementation Exercise Sheet 13

CMPT 354: Database System I. Lecture 3. SQL Basics

Database Compatibility for Oracle Developers Tools and Utilities Guide

Database Systems SQL SL03

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Database Management System Dr. S. Srinath Department of Computer Science & Engineering Indian Institute of Technology, Madras Lecture No.

Oracle Database 18c. Gentle introduction to Polymorphic Tables Functions with Common patterns and sample use cases

Overview of Data Management

Chapter 4. The Relational Model

CPS510 Database System Design Primitive SYSTEM STRUCTURE

The Relational Model. Chapter 3. Comp 521 Files and Databases Fall

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

LAB 3 Notes. Codd proposed the relational model in 70 Main advantage of Relational Model : Simple representation (relationstables(row,

MTA Database Administrator Fundamentals Course

Transcription:

Part III Data Modelling Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 1

Outline of this part (I) 1 Introduction to the Relational Model and SQL Relational Tables Simple Constraints Simple SQL Queries Historical Remarks 2 The Entity-Relationship Model Introduction Basic ER Constructs Kinds of Relationships (Cardinalities) Keys & Weak Entities N-Ary Relationships Translation into the Relational Model Extended ER Models: Generalization Hierarchies Extended ER Modelling Mapping Extended ER Models to Relations Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 2

Outline of this part (II) 3 Relational Normal Forms Introduction First Normal Form Functional Dependencies Examples Implication of FDs Determining Keys Anomalies, FD-based Normal Forms Consequences of bad DB design Anomalies Boyce-Codd Normal Form Third Normal Form Second Normal Form Decomposition of Relations Dependency Preservation & 3NF Synthesis Summary Multi-Valued Dependencies (MVDs) and 4NF Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 3

Outline of this part (III) Introduction Multi-Valued Dependencies (MVDs) Fourth Normal Form (4NF) Fifth Normal Form Other Constraints Normal Forms and ER Design Denormalization Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 4

1. Introduction to the Relational Model and SQL 1. Introduction to the Relational Model and SQL This section s goal: After completing this chapter, you should be able to explain basic notions of the relational model: table/relation, row/tuple, column/attribute, column value/attribute value, explain the meaning of keys and foreign keys, write (very) simple SQL queries (queries to one table). Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 5

1. Introduction to the Relational Model and SQL Relational Tables The Relational Model The relational model structures data in table form, i.e., a relational DB is a set of named tables. A relational database schema defines: 1 Names of tables in the database, 2 the columns of each table, i.e., the column name and the data types of the column entries, 3 integrity constraints, i.e., conditions that data entered into the tables is required to satisfy. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 6

1. Introduction to the Relational Model and SQL Relational Tables Example Assume a database maintaining information about a small real-world subset: a company s departments and employees. Two tables: EMP: information about employees. DEPT: information about departments. Table DEPT DEPT DEPTNO DNAME LOC 10 20 ACCOUNTING RESEARCH NEW YORK DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 7

1. Introduction to the Relational Model and SQL Relational Tables Columns types The three columns of table DEPT have the following data types: DEPTNO has data type NUMERIC(2), i.e., the column can hold two-digit integer entries 99... 99. An integrity constraint can be used to exclude negative department numbers. DNAME has type VARCHAR(14), i.e., the entries are character strings of variable length of up to 14 characters. LOC has type VARCHAR(13). Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 8

1. Introduction to the Relational Model and SQL Relational Tables Table rows A relational database state (instance of a given schema) defines for each table a set of rows. In the current state shown on the slide above, table DEPT has four rows. The relational model does not define any particular order of the rows (e.g., first row, second row). 1 Rows can be sorted for output, though. Each row specifies values for each column of the table. 1 formally, a table is a relation, i.e., a set. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 9

1. Introduction to the Relational Model and SQL Relational Tables Summary A relational table Column Row Row Table Name R A 1... A n a 1,1... a 1,n... a m,1... a m,n Column Table Entry Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 10

1. Introduction to the Relational Model and SQL Relational Tables Towards a formalization A more theoretically inclined person would use the following equivalent terminology: Table relation Row tuple Column attribute Data type of a column domain of the attribute Table entry attribute value Definition (Relation) Formally, a table is a subset of the Cartesian product of the domains of the column data types, i.e., a relation in the mathematical sense. In an RDBMS, relations are always finite, may involve any number of columns, and we typically ignore the order of columns. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 11

1. Introduction to the Relational Model and SQL Relational Tables Towards a programmer s view Old-style practitioners might equivalently say Row record A table row (or tuple) is basically the same as a record in Pascal or struct in C: it has several named components. However, the storage structure of a tuple in external memory (disk) does not necessarily match the layout of a record in main memory. Column field Table entry field value Table file Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 12

1. Introduction to the Relational Model and SQL Simple Constraints Keys The column DEPTNO is declared as the key of table DEPT. Relational Keys A key always uniquely identifies a single row in its associated table. Example (A key is a contraint!) Table DEPT, for example, already contains a row with key DEPTNO = 10. If one tries to add another row with the same value 10 for DEPTNO, the DBMS responds with an error. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 13

1. Introduction to the Relational Model and SQL Simple Constraints Constraints Keys are an example of constraints: conditions that the table contents (DB state) must satisfy in addition to the basic structure prescribed by the columns. Constraints are declared as part of the DB schema. More than one key can be declared for a table. Example (More keys?) One could, for example, discuss whether DNAME should also be a key (in addition to DEPTNO already being a key). This would exclude the possibility that there can ever be two or more departments of the same name. Keys and other constraints are treated more fully in the next part. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 14

1. Introduction to the Relational Model and SQL Simple Constraints Another example table (I) Table EMP (data about employees) with the following columns: EMPNO: A unique number for every employee. ENAME: Employee name. JOB: Employee position (e.g., ENGINEER) MGR: Direct supervisor of this employee. HIREDATE: Employee hire date. SAL: Employee salary. COMM: Commission (only for salespeople). DEPTNO: Department where this employee works. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 15

1. Introduction to the Relational Model and SQL Simple Constraints Another example table (II) Table EMP EMP EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 20 7782 CLARK MANAGER 7839 09-JUN-81 2450 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7839 KINGPRESIDENT 17-NOV-81 5000 20 7844 TURNER SALESMAN 7698 09-SEP-81 1500 0 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 20 Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 16

1. Introduction to the Relational Model and SQL Simple Constraints Foreign Keys Physical pointers are unknown to the relational model. However, the relational model provides a kind of logical pointer. For example, the column DEPTNO in table EMP refers to column DEPTNO in table DEPT. Since DEPTNO is declared as a key in table DEPT, a department number uniquely identifies a single row of DEPT. A value for DEPTNO (in table EMP) can be seen as a logical address of, or reference to, a row in table DEPT. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 17

1. Introduction to the Relational Model and SQL Simple Constraints Foreign Keys are constraints, too! By including a department number in table EMP, each row in EMP points to a single row in table DEPT. It is important for the integrity of the database, that the department numbers in EMP in fact occur in DEPT. Example ( Dangling pointers ) If a row in table EMP contains a DEPTNO value of 70, the reference dangles. A DBMS, however, does not crash as would be the case with real physical pointers (memory addresses). In SQL, references are followed by comparing column values. Nevertheless, such a column entry is a kind of error and should be avoided. This is the purpose of foreign keys. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 18

1. Introduction to the Relational Model and SQL Simple Constraints Declaring Foreign Keys The relational model (and thus, any RDBMS) permits to declare column DEPTNO as a foreign key that references table DEPT. Definition (DB modifications in the presence of Foreign Keys) The DBMS will refuse an insertion into table EMP with a value for DEPTNO that does not appear in DEPT, a deletion of a row in DEPT that is still referenced by a row in EMP, a corresponding updates (changes) of DEPTNO values. a It might be reasonable to recursively delete all employees of that department, too (cascading delete). Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 19

1. Introduction to the Relational Model and SQL Simple Constraints More Foreign Keys Table EMP also contains a second foreign key: Column MGR contains the employee number of the employee s direct supervisor. This shows that it is possible that a foreign key refer to another row in the same table (or even the same row), the foreign key column and referenced key may have different names. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 20

1. Introduction to the Relational Model and SQL Simple Constraints Null values The relational model allows column entries to remain empty (i.e., the column/row contains a null value). For example, in table EMP: 1 only salespeople have a commission, 2 the company president has no supervisor. When a table schema is declared, one can specify for each column whether null values are accepted or not (this is another example of a constraint specified for the table). Null values are treated specially in comparisons. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 21

1. Introduction to the Relational Model and SQL Simple SQL Queries Quick intro to SQL Simple SQL queries have the following syntactic form: SQL s basic SFW-block SELECT FROM WHERE After FROM: comma-separated list of table names from which to extract data. After WHERE: specify predicate (Boolean expression) to select a subset of rows (search condition). Note: A missing WHERE-clause is equivalent to WHERE TRUE. After SELECT: define which columns appear in the result table. Note: SELECT * selects all columns. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 22

1. Introduction to the Relational Model and SQL Simple SQL Queries Simple SQL queries (I) Show the entire department table (all rows, all columns) Result: Equivalent formulation: SELECT * FROM DEPT DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON SELECT DEPTNO, DNAME, LOC FROM DEPT WHERE TRUE Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 23

1. Introduction to the Relational Model and SQL Simple SQL Queries Simple SQL queries (II) SQL is not case-sensitive, except inside string constants. The SQL parser converts all characters into upper case (except inside quotes) before further processing continues. To refer to a table/column name containing lower case characters, enclose the name in double quotes ("). The syntax of SQL is format-free (newlines, white-spaces, etc. may be arbitrarily used). Commonly found layout of SELECT-FROM-WHERE blocks select from deptno, dname, loc dept Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 24

1. Introduction to the Relational Model and SQL Simple SQL Queries Queries with conditions (I) To extract all data about the department(s) in DALLAS Result: SELECT * FROM DEPT WHERE LOC = DALLAS DEPTNO DNAME LOC 20 RESEARCH DALLAS String constants are enclosed in single quotes ( ). Inside string constants, SQL is case-sensitive. The following will select 0 rows (empty result): SELECT * FROM DEPT WHERE LOC = Dallas Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 25

1. Introduction to the Relational Model and SQL Simple SQL Queries Queries with conditions (II) Print the name, job, and salary of all employees earning at least $2500 SELECT ENAME, JOB, SAL FROM EMP WHERE SAL >= 2500 Result: ENAME JOB SAL JONES MANAGER 2975 BLAKE MANAGER 2850 SCOTT KING ANALYST PRESIDENT 3000 5000 FORD ANALYST 3000 Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 26

1. Introduction to the Relational Model and SQL Simple SQL Queries Queries with conditions (III) SQL evaluates WHERE before SELECT: The WHERE-clause may refer to columns which do not appear in the result. Print the employee number and name of all managers SELECT FROM WHERE EMPNO, ENAME EMP JOB = MANAGER Result: EMPNO ENAME 7566 JONES 7698 BLAKE 7782 CLARK Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 27

1. Introduction to the Relational Model and SQL Simple SQL Queries Pattern matching (I) SQL also provides an operator for text pattern matching allowing the use of wild-cards: Print the employee number and name of all managers SELECT FROM WHERE EMPNO, ENAME EMP JOB LIKE MANA% % matches any sequence of arbitrary characters, matches any single character. Also see: UNIX shell globbing via * and?. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 28

1. Introduction to the Relational Model and SQL Simple SQL Queries Pattern matching (II) The SQL keyword LIKE needs to be used for pattern matching. The equals sign (=) tests for literal equality. The following is legal SQL but will return the empty result: SELECT FROM WHERE EMPNO, ENAME EMP JOB = MANA% Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 29

1. Introduction to the Relational Model and SQL Simple SQL Queries Arithmetic expressions SQL comes with the standard arithmetic operators. Print employee names earning less than $15,000 per year Result: SELECT ENAME FROM EMP WHERE SAL < 15000 / 12 ENAME SMITH ADAMS JAMES The predicate SAL * 12 < 15000 is equivalent (column names act like variable identifiers in programming languages). Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 30

1. Introduction to the Relational Model and SQL Simple SQL Queries Renaming output columns Print the annual salary of all managers Result: SELECT ENAME, SAL * 12 FROM EMP WHERE JOB = MANAGER ENAME SAL * 12 JONES 35700 BLAKE 34200 CLARK 29400 To rename the second result column: SELECT ENAME, SAL * 12 [AS] YEARLY SALARY Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 31

1. Introduction to the Relational Model and SQL Simple SQL Queries Logical connectives (I) The Boolean operators AND, OR, NOT and parentheses () may be used to construct more complex conditions. Print name and salary of all managers and the president SELECT FROM WHERE ENAME, SAL EMP JOB = MANAGER OR JOB = PRESIDENT N.B. The query would not work as expected with AND instead of OR. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 32

1. Introduction to the Relational Model and SQL Simple SQL Queries Logical connectives (II) Without parentheses, AND binds stronger than OR (and NOT binds stronger than AND). Print name, salary of all well-paid managers and presidents SELECT ENAME, SAL FROM EMP WHERE JOB = MANAGER OR JOB = PRESIDENT AND SAL >= 3000 The system will parse the condition as: WHERE JOB = MANAGER OR (JOB = PRESIDENT AND SAL >= 3000) Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 33

1. Introduction to the Relational Model and SQL Simple SQL Queries Removing duplicate rows In general, queries can produce duplicate rows. List all jobs SELECT JOB FROM EMP The DBMS processes this query by a loop over table EMP and extracts the job of every employee. The same job name will be extracted multiple times (and then printed by the SQL console). Duplicate row elimination may be requested by adding the keyword DISTINCT after SELECT: List all distinct jobs SELECT DISTINCT JOB FROM EMP So..., relations are not really sets, are they? Formally, tables are multisets (or bags), i.e., sets where elements can be contained more than once. (SQL differs from relational theory here.) Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 34

1. Introduction to the Relational Model and SQL Simple SQL Queries Sorting output rows (I) The row ordering emitted by a SQL query is not predictable, unless an explicit request for sorting is added. Print employees names and salary, alphabetically ordered by employee name SELECT FROM ORDER BY ENAME, SAL EMP ENAME Most SQL implementations implement the ORDER BY-clause on the SQL console level only: the row printing order is changed. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 35

1. Introduction to the Relational Model and SQL Simple SQL Queries Sorting output rows (II) The ORDER BY-clause permits multiple sorting criteria (useful only if one or more columns contain duplicate entries). Print employees who earn $2000 at least, ordered by department number and descending salaries SELECT DEPTNO, ENAME, SAL FROM EMP WHERE SAL >= 2000 ORDER BY DEPTNO, SAL DESC SQL orders lexicographically. Sort order is ascending (ASC) by default. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 36

1. Introduction to the Relational Model and SQL Simple SQL Queries Outlook: Joining tables SQL allows to combine data from different tables. Print employees in the RESEARCH department SELECT FROM WHERE ENAME EMP, DEPT EMP.DEPTNO = DEPT.DEPTNO AND DNAME = RESEARCH Conceptually, SQL evaluates the WHERE-predicate for every combination of rows from both tables (Cartesian product). Since column name DEPTNO appears in both tables, disambiguate the column reference by prefixing the table name. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 37

1. Introduction to the Relational Model and SQL Simple SQL Queries SQL quiz (I) Table EMP EMP EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 17-DEC-80 800 20 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 20 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 20 7698 BLAKE MANAGER 7839 01-MAY-81 2850 20 7782 CLARK MANAGER 7839 09-JUN-81 2450 20 7788 SCOTT ANALYST 7566 09-DEC-82 3000 20 7839 KINGPRESIDENT 17-NOV-81 5000 20 7844 TURNER SALESMAN 7698 09-SEP-81 1500 0 20 7876 ADAMS CLERK 7788 12-JAN-83 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 20 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 20 Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 38

1. Introduction to the Relational Model and SQL Simple SQL Queries SQL quiz (II) Who has employee KING as direct supervisor? Who has a salary between $1000 and $2000 (inclusive)? Print name and salary and order the result by names. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 39

1. Introduction to the Relational Model and SQL Simple SQL Queries SQL quiz (III) Which employee names consist of exactly four characters? Print name, salary, department of all employees who work in department 10 or 30 and earn less than $1500? Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 40

1. Introduction to the Relational Model and SQL Simple SQL Queries SQL quiz (IV) Which jobs occur in which departments? Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 41

1. Introduction to the Relational Model and SQL Historical Remarks Relational Model: History The relational model (RM) was proposed by Edgar F. Codd (1970). It was the first data model that was theoretically defined prior to implementation. Codd was awarded the Turing Prize in 1981. First implementations (1967): System R (Codd et al., IBM) Ingres (Mike Stonebraker, UC Berkeley) First commercial systems: Oracle (1979), Ingres (1980?), and IBM SQL/DS (1981). Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 42

1. Introduction to the Relational Model and SQL Historical Remarks Reasons for success (I) Much simpler than earlier data models. One core concept: finite relation (set of tuples). Easily understandable, even for non-specialists: Relations correspond to tables. The theoretical model fits well with common implementation techniques: A relation is an abstraction of a file of records. The relational model features set-oriented operation. In earlier models, record-to-record navigation was explicit. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 43

1. Introduction to the Relational Model and SQL Historical Remarks Reasons for success (II) Declarative query language. A SQL query expresses the format and conditions that resulting tuples need to respect. The RDBMS contains a query optimizer that derives an efficient query evaluation plan (i.e., an imperative program that evaluates the query) from this declarative specification. In earlier models, programmers had to explicitly think about the use of indexes and many more details. The relational model has a solid theoretical foundation. It is tightly connected to first-order logic. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 44

1. Introduction to the Relational Model and SQL Historical Remarks Obstacles In the beginning, there have been religious wars about whether or not this model could possibly be implemented in an efficient way. Early prototypes (and the proponents) have had a real hard time trying to prove this possible. Providing an automatically optimized translation from high-level SQL to efficient query execution plans remains to be a challenge, up to now and likely in the future... Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 45

1. Introduction to the Relational Model and SQL Historical Remarks Standards (I) First standard 1986/87 (ANSI/ISO). This was late as there were already several SQL systems on the market. The standard was the smallest common denominator, containing only the features common to existing implementations. Extension to foreign keys etc. in 1989 (SQL-89). This version is also called SQL-1. All commercial implementations today support SQL-89, but also feature significant extensions. Major extension: SQL-2 (SQL-92) (1992). Standard defines three levels, entry, intermediate, full. Oracle 8.0, SQL Server 7.0 have entry level conformance. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 46

1. Introduction to the Relational Model and SQL Historical Remarks Standards (II) Current standard: SQL-2003. SQL-1999 was a preliminary version of the SQL-3 standard. Until 12/2000, the volumes 1 5 and 10 appeared (2355 pages; the SQL-2 standard, which is not completely implemented yet had 587 pages). SQL-2003 added some more parts. Some features in the (mostly still upcoming) SQL-3: User-defined data type constructors: LIST, SET, ROW to structure column values OO features (e.g., inheritance, sub-tables). Recursive queries. Marc H. Scholl (DBIS, Uni KN) Information Management Winter 2007/08 47