Dr Gordon Russell, Napier University Unit 1.2 - SQL 1 1 SQL 1 Unit 1.2.

Slides:



Advertisements
Similar presentations
SQL – Simple Queries Chapter 3.1 V3.0 Napier University Dr Gordon Russell.
Advertisements

The Relational Model and Relational Algebra Nothing is so practical as a good theory Kurt Lewin, 1945.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Accounting System Design
Relational Algebra 1 Chapter 5.1 V3.0 Napier University Dr Gordon Russell.
Monday, 08 June 2015Dr. Mohamed Osman1 What is Database Administration A high level function (technical Function) that is responsible for ► physical DB.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
CS 380 Introduction to Database Systems (Chapter 5: The Relational Data Model and Relational Database Constraints)
Chapter 3 Relational Model Chapter 4 in Textbook.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
Relational Model Session 6 Course Name: Database System Year : 2012.
Chapter 4 The Relational Model.
Content Resource- Elamsari and Navathe, Fundamentals of Database Management systems.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Relational Algebra 2 Chapter 5.2 V3.0 Napier University Dr Gordon Russell.
Introduction to SQL Steve Perry
Chapter 9 Designing Databases Modern Systems Analysis and Design Sixth Edition Jeffrey A. Hoffer Joey F. George Joseph S. Valacich.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
Instructor: Churee Techawut Basic Concepts of Relational Database Chapter 5 CS (204)321 Database System I.
Module 3: The Relational Model.  Overview Terminology Relational Data Structure Mathematical Relations Database Relations Relational Keys Relational.
Chapter 3 The Relational Model. 2 Chapter 3 - Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
1 The Relational Database Model. 2 Learning Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical.
Dr Gordon Russell, Napier University Unit SQL 2 - V2.0 1 SQL 2 Unit 1.3.
9/7/2012ISC329 Isabelle Bichindaritz1 The Relational Database Model.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
M1G Introduction to Database Development 2. Creating a Database.
Relational Database. Database Management System (DBMS)
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
Databases MIS 21. Some database terminology  Database: integrated collection of data  Database Management System (DBMS): environment that provides mechanisms.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
1 © Prentice Hall, 2002 Chapter 5: Logical Database Design and the Relational Model Modern Database Management 6 th Edition Jeffrey A. Hoffer, Mary B.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
1 CS 430 Database Theory Winter 2005 Lecture 4: Relational Model.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
The relational model A data model (in general) : Integrated collection of concepts for describing data (data requirements). Relational model was introduced.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences The Relational.
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Dr Gordon Russell, Napier University Unit SQL 3 - V2.0 1 SQL 3 Unit 1.4.
Database Systems Logical Data Modelling Tutor:Ian Perry Tel: Web:
CHAPTER 2 : RELATIONAL DATA MODEL Prepared by : nbs.
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
Chapter 3 The Relational Model. Objectives u Terminology of relational model. u How tables are used to represent data. u Connection between mathematical.
Chapter 4 The Relational Model Pearson Education © 2009.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Teacher Workshop Database Design Pearson Education © 2014.
Fundamentals of DBMS Notes-1.
Fundamental of Database Systems
Chapter 6: Integrity (and Security)
COP Introduction to Database Structures
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
CIS 207 The Relational Database Model
INSTRUCTOR: MRS T.G. ZHOU
Integrity 5/5/2019 See scm-intranet.
Unit Relational Algebra 1
Presentation transcript:

Dr Gordon Russell, Napier University Unit SQL 1 1 SQL 1 Unit 1.2

Dr Gordon Russell, Napier University Unit SQL 12 Database Models A data model comprises a data structure a set of integrity constraints operations associated with the data structure Examples of data models include: hierarchic network relational

Dr Gordon Russell, Napier University Unit SQL 13 Relational Databases The relational data model comprises: relational data structure relational integrity constraints relational algebra or equivalent (SQL) – –SQL is an ISO language based on relational algebra – –relational algebra is a mathematical formulation

Dr Gordon Russell, Napier University Unit SQL 14 Relational Data Structure A relational data structure is a collection of tables or relations. A relation is a collection of rows or tuples A tuple is a collection of columns or attributes A domain is a pool of values from which the actual attribute values are taken.

Dr Gordon Russell, Napier University Unit SQL 15 Relational Structure cont

Dr Gordon Russell, Napier University Unit SQL 16 Domain and Integrity Constraints Domain Constraints – –limit the range of domain values of an attribute – –specify uniqueness and ‘nullness’ of an attribute – –specify a default value for an attribute when no value is provided. Entity Integrity – –every tuple is uniquely identified by a unique non-null attribute, the primary key. Referential Integrity – –rows in different tables are correctly related by valid key values (‘foreign’ keys refer to primary keys).

Dr Gordon Russell, Napier University Unit SQL 17 Menu DescriptionPrice Large Cola £0.99 Cheeseburger£1.99 Burger Royalé £3.49

Dr Gordon Russell, Napier University Unit SQL 18 External vs Logical The way a menu is to be shown to a customer may not be the way in which the data is held in a logical model. The menu model could hold tables about ingredients, individual ingredient costs, overheads, and tax. The menu provided to a customer is derived from these tables. It provides a customer-oriented view of the base data.

Dr Gordon Russell, Napier University Unit SQL 19 Columns or Attributes Each column is given a name which is unique within a table Each column holds data of one specified type. E.g. integer decimal character text data -- the range of values can also be constrained Some row-column instances may contain no data but instead hold a special null value to indicate that this value is unavailable or inappropriate.

Dr Gordon Russell, Napier University Unit SQL 110 Rows or Tuples Each row must be uniquely identifiable with a table one row records a food item in the menu example Columns in a specified row may contain no value a transaction cannot have credit and debit values simultaneously. Some columns must contain values for all rows date and source, which make the row unique, in the bank account case. CARDINALITY – the number of ROWS ARITY – the number of COLUMNS

Dr Gordon Russell, Napier University Unit SQL 111 Primary Keys A table requires a key which uniquely identifies each row in the table-entity integrity. The key could comprise more than one column A table may have several possible keys, the candidate keys, from which one is chosen as the primary key. Primary key implies ‘UNIQUE NOT NULL’. It may be necessary to generate an artificial primary key if no other unique attribute combination is available within the data.

Dr Gordon Russell, Napier University Unit SQL 112 Employee Table - Columns What is a suitable primary key? What is a suitable primary key? A suitable key (empno) must be generated since no othe rattributes or combination of attributes uniquely identifies each row. A suitable key (empno) must be generated since no othe rattributes or combination of attributes uniquely identifies each row. Full Name Col name Employee Number empno Forenamesforenames Addressaddress Department No depno Surnamesurname Date of Birth dob Telephone No telno

Dr Gordon Russell, Napier University Unit SQL 113 Jobhistory Table - Columns The primary key combines empno + position to uniquely identify each row. The primary key combines empno + position to uniquely identify each row. empno relates a Jobhistory row to the corresponding Employee row - it is theit is the primary key in the Employee table and a foreign key in the Jobhistory table. empno relates a Jobhistory row to the corresponding Employee row - it is theit is the primary key in the Employee table and a foreign key in the Jobhistory table. Full Name Col name Employee Number empno Start Date startdate Salarysalary Positionposition End Date enddare

Dr Gordon Russell, Napier University Unit SQL 114 Foreign Keys A foreign key is a value held in a table that has exactly the same value as the primary key column of a row in another table. A foreign key references the primary key of another table and maintains a relationship between the tables. The column empno (foreign key) in the Jobhistory table must have the same value as one of the empno (primary key) values in the Employee table. This relationship controls referential integrity.

Dr Gordon Russell, Napier University Unit SQL 115 SQL An international Standard Language for manipulating relational databases. It is based on an IBM product called the Structured Query Language. SQL creates and manipulates tables of data (relations) - it is a data handling language, not a programming language. A table is a collection of rows (tuples or records). A row is a collection of columns (attributes).

Dr Gordon Russell, Napier University Unit SQL 116 SQL Basics Basic SQL Statements include: CREATE - a data structure SELECT - read one or more rows from a table INSERT - one or more rows into a table DELETE - one or more rows from a table UPDATE - change the column values in a row DROP - a data structure

Dr Gordon Russell, Napier University Unit SQL 117 CREATE table employee CREATE TABLE employee ( empno INTEGER PRIMARY KEY, surname VARCHAR(15), forenames VARCHAR(30), dob date, address VARCHAR(50), telno VARCHAR(50), depno INTEGER REFERENCES department(depno), CHECK(dob IS NULL OR (dob > ’1-jan-1950’ AND dob < ’31-dec-1980’) ) );

Dr Gordon Russell, Napier University Unit SQL 118 CREATE Table Jobhistory CREATE TABLE jobhistory ( empno INTEGER REFERENCES employee(empno) position VARCHAR(30), startdate date, enddate date, salary DECIMAL(8,2), PRIMARY KEY(empno,position) );

Dr Gordon Russell, Napier University Unit SQL 119 SQL SELECT SELECT column-list -- the simplest SQL SELECT FROM table_list; SELECT * -- list ALL employee data FROM employee -- for each employee ; SELECT depno,forenames,surname -- list SOME employee FROM employee -- data for each employee;

Dr Gordon Russell, Napier University Unit SQL 120 Comparison SELECT column-list -- FROM table_list [WHERE condition]; -- Comparison Operators: =,!=,<>,,>= SELECT empno,surname FROM employee WHERE depno = 3; SELECT forenames,surname FROM employee WHERE dob > ’2-jan-1958’;

Dr Gordon Russell, Napier University Unit SQL 121 Comparison cont... SELECT empno -- who is or have been FROM jobhistory -- a programmer? WHERE position = ’Programmer’; SELECT empno,position -- what are employee’s FROM jobhistory -- current positions? WHERE enddate IS NULL; Note that NULL indicates a value which is missing, not known, inappropriate, etc. NULL is not a blank or zero. NULL cannot be tested for equality with other NULL values.

Dr Gordon Russell, Napier University Unit SQL 122 SELECT with BETWEEN SELECTempno,surname,forenames,dob FROM employee WHERE dob BETWEEN ’30-jun-1954’ AND ’1-jan-1959’; Note that the BETWEEN predicate is inclusive. The above condition is equivalent to : WHERE dob >= ’30-jun-1954’ AND <=’1-jan-1959’;

Dr Gordon Russell, Napier University Unit SQL 123 Pattern Matching Simple pattern matching is carried out using LIKE: LIKE ’pattern-to-match’ Where the pattern can include special wildcard characters: % 0 or more arbitrary characters _ any one character SELECT forenames, surname, address FROM employee WHERE address LIKE ’%Edinburgh%’;

Dr Gordon Russell, Napier University Unit SQL 124 ORDER and DISTINCT SELECT [DISTINCT] column_list FROM table_list [WHERE condition] [ORDER BY attribute[ DESC/ASC] [,attribute [DESC,ASC]]...]; Note that ASCending order is the default SELECT DISTINCT empno FROM jobhistory WHERE starddate < ’1-jan-1980’ ORDER BY empno DESC;