M1G505190 Introduction to Database Development 5. Doing more with queries.

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

XP Chapter 3 Succeeding in Business with Microsoft Office Access 2003: A Problem-Solving Approach 1 Analyzing Data For Effective Decision Making.
SUNY Morrisville-Norwich Campus-Week 10 CITA 130 Advanced Computer Applications II Spring 2005 Prof. Tom Smith.
Introduction to Structured Query Language (SQL)
CSE 190: Internet E-Commerce Lecture 10: Data Tier.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
Database Systems More SQL Database Design -- More SQL1.
Introduction to Structured Query Language (SQL)
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
Define Table Relationships—1 of 3 One of the most powerful features of a relational database management system, such as Access, is its ability to define.
DAY 21: MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Akhila Kondai October 30, 2013.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Session 5: Working with MySQL iNET Academy Open Source Web Development.
ASP.NET Programming with C# and SQL Server First Edition
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
DAY 15: ACCESS CHAPTER 2 Larry Reaves October 7,
Relational Database Concepts. Let’s start with a simple example of a database application Assume that you want to keep track of your clients’ names, addresses,
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
CODD’s 12 RULES OF RELATIONAL DATABASE
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Analyzing Data For Effective Decision Making Chapter 3.
IE 423 – Design of Decision Support Systems Database development – Relationships and Queries.
Programming using C# Joins SQL Injection Stored Procedures
XP New Perspectives on Microsoft Office Access 2003 Tutorial 9 1 Microsoft Office Access 2003 Tutorial 9 – Using Action Queries, and Defining Table Relationships.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
1 What is database 2? What is normalization? What is SQL? What is transaction?
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Chapter 17 Creating a Database.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
M1G Introduction to Database Development 2. Creating a Database.
Nitin Singh/AAO RTI ALLAHABAD 1 SQL Nitin Singh/AAO RTI ALLAHABAD 2 OBJECTIVES §What is SQL? §Types of SQL commands and their function §Query §Index.
ADVANCED SQL SELECT QUERIES CS 260 Database Systems.
Views Lesson 7.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
M1G Introduction to Database Development 4. Improving the database design.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
DAY 9: DATABASES Rohit September 21,
(SQL - Structured Query Language)
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
Databases Flat Files & Relational Databases. Learning Objectives Describe flat files and databases. Explain the advantages that using a relational database.
1 A Very Brief Introduction to Relational Databases.
DAY 14: ACCESS CHAPTER 1 RAHUL KAVI October 8,
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Unit-8 Introduction Of MySql. Types of table in PHP MySQL supports various of table types or storage engines to allow you to optimize your database. The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
DAY 9: DATABASES Rohit February 17,
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
SQL Basics Review Reviewing what we’ve learned so far…….
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Tables and Triggers.
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Database application MySQL Database and PhpMyAdmin
Structured Query Language (SQL) William Klingelsmith
Chapter 8 Working with Databases and MySQL
Flat Files & Relational Databases
M1G Introduction to Database Development
Introduction To Structured Query Language (SQL)
Understanding Core Database Concepts
Manipulating Data Lesson 3.
Presentation transcript:

M1G Introduction to Database Development 5. Doing more with queries

Query performance Queries on the GCUTours database return the results virtually instantaneously With a small database, the performance of a query is not an issue However, when there are many thousands of rows in the database tables, it can take a lot more time to extract the rows you want If query performance is poor, the system can seem unresponsive to the users Introduction to Database Development 5. Doing more with queries 2

Indexes Indexes can enable particular rows of a database table to be found more quickly Database indexes are much like the index you would find at the back of a book Introduction to Database Development 5. Doing more with queries 3

Indexes Introduction to Database Development 5. Doing more with queries 4

Which fields should be indexed? You should index the fields which will be most often used as conditions in queries This will depend on the use cases for the system It’s likely that we’ll want to search for users by username, for example in the Log in use case Therefore the username field should be indexed Introduction to Database Development 5. Doing more with queries 5

Other fields What about the other fields? There might be a need to search for users by name Could create an index on lastname AND firstname a search for David Livingstone will use the first part of the index to find all the Livingstones in the table then the second part to find the Davids among them This is an example of a compound index Introduction to Database Development 5. Doing more with queries 6

Why not index everything? If all the fields are indexed then all queries should run faster, shouldn’t they? Need storage space to hold all the indexes Too many indexes can also cause performance problems when adding new data and deleting data as all the indexes need to be updated One of the decisions in designing a database is how to choose indexes Introduction to Database Development 5. Doing more with queries 7

Creating an index Introduction to Database Development 5. Doing more with queries 8 simple indexcompound index

Creating an index with SQL This seems simpler... Can remove and index using DROP Introduction to Database Development 5. Doing more with queries 9 CREATE INDEX IX_Users_fullname ON Users (lastname, firstname) DROP INDEX Users.IX_Users_fullname

Data in multiple tables What query will the Show tours use case need? We might want to show the name, adult price and date of departure of each tour The problem is that the name and prices are in the Packages table, while the departure date is in the Tours table Introduction to Database Development 5. Doing more with queries 10

Data modelling and tables Data modelling process and/or normalisation encouraged us to treat packages and tours as separate entities Put them in separate tables in the database Database design generally results in data being spread across multiple tables It’s actually fairly uncommon to find all the data need for a particular action stored in a single table Introduction to Database Development 5. Doing more with queries 11

Example of required data in two tables Introduction to Database Development 5. Doing more with queries 12

Joining the tables The query now needs to join the tables If you were to get the information just by looking at the tables you would: look at each tour in turn then look up the corresponding package e.g.pick out the value of ‘01/03/2011’ for departure date, and then look for the matching row of Packages (with packageID = 2) pick out the values ‘Roof of the World Explorer’ and £1599 for name and adult price Introduction to Database Development 5. Doing more with queries 13

Joining tables with SQL We can do the same thing with an SQL query Looking up matching rows is done using a join condition: This means “join each row in Tours to the row in Packages where the value of packageID in Packages matches the value of packageID in Tours” Introduction to Database Development 5. Doing more with queries 14

Dot notation The dot notation, for example Packages.packageID, is used to specify a field belonging to a particular table Then we know exactly which field we mean if different tables have fields with identical names Introduction to Database Development 5. Doing more with queries 15

SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages, Tours WHERE Packages.packageID = Tours.packageID SQL join query example Introduction to Database Development 5. Doing more with queries 16 fields to include names of tables join condition Result has one row for each tour There is a lot of repeated data in the query result This is OK because this is just a view of the data – there is still no repetition in the stored data

Combining join and other conditions Can filter query results in the usual way: Introduction to Database Development 5. Doing more with queries 17 SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages, Tours WHERE Packages.packageID = Tours.packageID AND location = 'Asia'

Don’t forget the join condition... A common mistake people make when writing join queries is to miss out the join condition, like this: The database will then join every row of the first table to every row of the second in this database Packages has 12 rows and Tours has 26, so the query gives (12x26) = 312 rows Should be one row per Tour = 26 rows Introduction to Database Development 5. Doing more with queries 18 SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages, Tours

Joining more than two tables Related data can be split between many tables in a database Can join as many tables as you need in a query Include join conditions for each pair of related tables combined with AND, for example: Introduction to Database Development 5. Doing more with queries 19

Inner joins Returns data only from rows where there are matching values in both tables Previous examples were inner joins Can be written as: Introduction to Database Development 5. Doing more with queries 20 SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages INNER JOIN Tours ON Packages.packageID = Tours.packageID

Outer joins A left outer join returns data from all the rows of the first table and only the rows of the second table which have matching values In Tours table, there is no row with packageID = 10 However, there is a package in the Packages table with that ID Inner join would not return any rows with packageID = 10 Introduction to Database Development 5. Doing more with queries 21

Left outer join example Introduction to Database Development 5. Doing more with queries 22 SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages LEFT OUTER JOIN Tours ON Packages.packageID = Tours.packageID

Left outer join example Introduction to Database Development 5. Doing more with queries 23

Other outer joins Can also have Right outer join What effect do you think this will have? Introduction to Database Development 5. Doing more with queries 24

Data definition queries You have already seen several examples of data definition queries. Data definition queries include CREATE TABLE ALTER TABLE DROP TABLE CREATE INDEX This part of SQL is sometimes known as DDL (Data Definition Language). Introduction to Database Development 5. Doing more with queries 25

Insert queries You have also seen an example of an INSERT query A single INSERT query can only apply to one table The same is true for updating and deleting Introduction to Database Development 5. Doing more with queries 26

Update query An UPDATE query allows you to change data that is in a table This example updates the single row with bookingID = 1 Introduction to Database Development 5. Doing more with queries 27

Updating multiple rows You can update many rows, or even all the rows in a table, at the same time Introduction to Database Development 5. Doing more with queries 28

Delete queries A DELETE query allows you remove a row or rows from a table Seems straightforward, but it won’t work! Introduction to Database Development 5. Doing more with queries 29

Delete restrictions There are bookings for this user: If we deleted the user, then those bookings with the value ‘vdagama’ in the username field would no longer match a value in the Users table this is disallowed by foreign key Introduction to Database Development 5. Doing more with queries 30

Deleting rows with relationships Delete all bookings for the user first Run this query before deleting user This is the safer option Introduction to Database Development 5. Doing more with queries 31

Deleting rows with relationships Make deleting a user automatically delete all bookings for the user This is called a cascading delete Option you choose when you create the foreign key relationship between the tables Use with care You should only choose this option if you are sure that it will not result in the loss of important data Introduction to Database Development 5. Doing more with queries 32

Configuring CASCADE-DELETE Introduction to Database Development 5. Doing more with queries 33 CONSTRAINT FK_Bookings_Users FOREIGN KEY(username) REFERENCES Users(username) ON DELETE CASCADE This SQL could be part of a CREATE TABLE or ALTER TABLE statement

Parameterized queries One of the first queries we looked at was this: This query finds the user with username mpolo, and retrieves all his or her details That sounds like something we might want to do again and again, but not always for the same user Introduction to Database Development 5. Doing more with queries 34

Parameterized queries You can make the value of username a parameter for the query Every time the query is executed, a different value can be supplied for the parameter The query needs to be modified slightly: Introduction to Database Development 5. Doing more with queries 35 SELECT * FROM Users WHERE username

Executing a parameterized query You can’t actually execute this query directly in a WebMatrix query window There is no way to supply a parameter value. You will see in the next lecture how parameterized queries help when building a data-driven web application with WebMatrix Introduction to Database Development 5. Doing more with queries 36