* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download slides
Serializability wikipedia , lookup
Oracle Database wikipedia , lookup
Microsoft Access wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Relational algebra wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Concurrency control wikipedia , lookup
Ingres (database) wikipedia , lookup
Functional Database Model wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Versant Object Database wikipedia , lookup
Clusterpoint wikipedia , lookup
ContactPoint wikipedia , lookup
M1G505190 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 simple index Introduction to Database Development compound index 5. Doing more with queries 8 Creating an index with SQL This seems simpler... CREATE INDEX IX_Users_fullname ON Users (lastname, firstname) Can remove and index using DROP DROP INDEX Users.IX_Users_fullname Introduction to Database Development 5. Doing more with queries 9 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 SQL join query example fields to include SELECT Packages.packageID, packagename, adultprice, departuredate names of tables FROM Packages, Tours WHERE Packages.packageID = Tours.packageID 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 Introduction to Database Development 5. Doing more with queries 16 Combining join and other conditions SELECT Packages.packageID, Can filter query results in packagename, the usual way: adultprice, departuredate FROM Packages, Tours WHERE Packages.packageID = Tours.packageID AND location = 'Asia' Introduction to Database Development 5. Doing more with queries 17 Don’t forget the join condition... A common mistake people make when writing join queries is to miss out the join condition, like this: SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages, Tours 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 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: SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages INNER JOIN Tours ON Packages.packageID = Tours.packageID Introduction to Database Development 5. Doing more with queries 20 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 SELECT Packages.packageID, packagename, adultprice, departuredate FROM Packages LEFT OUTER JOIN Tours ON Packages.packageID = Tours.packageID Introduction to Database Development 5. Doing more with queries 22 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 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 Introduction to Database Development 5. Doing more with queries 33 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: SELECT * FROM Users WHERE username = @username Introduction to Database Development 5. Doing more with queries 35 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