Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "M1G505190 Introduction to Database Development 5. Doing more with queries."— Presentation transcript:

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

2 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

3 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

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

5 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

6 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

7 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

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

9 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

10 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

11 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

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

13 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

14 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

15 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

16 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

17 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'

18 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

19 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

20 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

21 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

22 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

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

24 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

25 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

26 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

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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 = @username

36 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


Download ppt "M1G505190 Introduction to Database Development 5. Doing more with queries."

Similar presentations


Ads by Google