Presentation is loading. Please wait.

Presentation is loading. Please wait.

10/05/2015 Introduction to Databases 1 F27DB Introduction to Database Systems More SQL Helen Hastie Room: EMB244 Material available on.

Similar presentations


Presentation on theme: "10/05/2015 Introduction to Databases 1 F27DB Introduction to Database Systems More SQL Helen Hastie Room: EMB244 Material available on."— Presentation transcript:

1 10/05/2015 Introduction to Databases 1 F27DB Introduction to Database Systems More SQL Helen Hastie Room: EMB244 Material available on Vision (modified from slides by Monica Farrow)

2 10/05/2015Introduction to Databases 2 Recap SQL already introduced in Helen’s lectures 1 & 2, and Brian’s lecture 5 & 6 Here are some more examples on Views, Aggregate functions and updates Using the Spy tables With additional interest rate table for different types of account

3 10/05/2015Introduction to Databases 3 The tables now Spy codeName firstName dateOfBirth gender mark amountDue spyMaster bankAccount SpyMaster mCodeName bankAccount contact SpyAccount accountNumber Income Expenditure atype SpySkillList skillCode skillName 1..* skilled at has manages 1 1 SpyWithSkill spyCode skillCode 1 0..* practised by Bold : primary key Italic : foreign key Both : both has Now the relationships are also shown by foreign keys NB Many to many relationships need an extra ‘linking’ table SpyAccRates accType rate 1 1..*

4 10/05/2015Introduction to Databases 4 Spy code Name first Name lastNamedateOfBirth gen der mark amount Due spy Master bank Account 007JamesBond12 December 1972 MMole on chin5050Q23456 budFannyCharleston31 July 1983Fscar on cheek25.67Q34567 freddieJohnSmith05 September 1954 Mone finger missing 312.5M45678 Spy DB SpyMaster mCode Name bankAccountcontact M12345Drop 5 P56789PO Box 23 Q13579Jimmie's SpyAccount account Number in come expend iture atype savings current savings savings current current SpySkillList skillCodeskillName 1top shot 2skilled with a knife 3fast runner 4quick thinker 5can pilot a helicopter SpyWithSkill spy Code skill Code bud2 3 4 freddie4 5 SpyAccRates accTyperate current0.015 savings0.02

5 10/05/2015Introduction to Databases 5 Extra SQL needed Create new table CREATE TABLE SpyAccRates (accType VARCHAR(10) PRIMARY KEY, rate DECIMAL( 6,3)); Insert 2 records INSERT INTO SpyAccRates VALUES ('current', 0.015); INSERT INTO SpyAccRates VALUES ('savings', 0.02); Add new column to SpyAccount table ALTER TABLE SpyAccount add atype VARCHAR (10); (ought to add foreign key constraint too) Rather randomly allocate types to accounts UPDATE SpyAccount SET atype = 'current'; UPDATE SpyAccount SET atype = 'savings' WHERE income < 400;

6 10/05/2015Introduction to Databases 6 Reminder – SQL query joining 2 tables Find the code name and contact point of SpyMasters with 0 income SELECT mCodeName, bankAccount, income FROM SpyMaster, SpyAccount WHERE bankAccount = accountNumber AND income = 0; mCode name bank Account In come M123450

7 10/05/2015Introduction to Databases 7 Calculations Find the balance of each bank account, renaming the result of the calculation SELECT accountNumber, income – expenditure AS ‘balance’ FROM SpyAccount; | accountNumber | balance | | | 0.00 | | | | | | | | | | | | | | | |

8 10/05/2015Introduction to Databases 8 Count Count the male spies SELECT count(*) FROM Spy WHERE gender = ‘M’; Count the spies who are quick thinkers SELECT count(*) FROM SpyWithSkill W, SpySkills L WHERE W.skillCode = L.skillCode AND L.skillName = ‘quick thinker’; | COUNT(*) | | 4 | | COUNT(*) | | 2 |

9 10/05/2015Introduction to Databases 9 Sum Find the total amount due to all spies SELECT sum(amountDue) FROM Spy; | sum(amountDue) | | |

10 10/05/2015Introduction to Databases 10 Sum and grouping Display the amounts due to male and female spies SELECT gender, sum(amountDue) FROM Spy GROUP BY gender ; Whatever you are grouping by, must appear in the SELECT line as well as the GROUP BY line | gender | sum(amountDue) | | F | | | M | |

11 10/05/2015Introduction to Databases 11 Income by type Display the total income of all account holders, grouped according to their type of account SELECT atype, sum(income) FROM SpyAccount GROUP BY atype | atype | sum(income) | | current | | | savings | |

12 10/05/2015Introduction to Databases 12 Low income by type Limit the enquiry to those accounts with under 4000 in income SELECT atype, sum(income) FROM SpyAccount WHERE income < 4000 GROUP BY atype | atype | sum(income) | | current | | | savings | |

13 10/05/2015Introduction to Databases 13 Calculations with update Increase the income column for each account by 10 UPDATE SpyAccount SET income = income + 10; | accountNumber | income | expenditure | atype | | | | 0.00 | savings | | | | | current | | | | | savings | | | | | savings | | | | | current | | | | | current |

14 10/05/2015Introduction to Databases 14 Calculations Calculate the interest due to each spy, by multiplying the income in the SpyAccount table by the rate in the SpyAccRate table. Also rename output column for nice output SELECT A.accountNumber, A.income * R.rate as interest FROM SpyAccount A, SpyAccRates R WHERE A.atype = R.accType; | accountNumber | interest | | | | | | | | | | | | | | | | | | |

15 10/05/2015Introduction to Databases 15 Calculate increased income Calculate each spy’s new income, by adding in the interest SELECT A.accountNumber, A.income + A.income * R.rate as newIncome FROM SpyAccount A, SpyAccRates R WHERE A.atype = R.accType; | accountNumber | newIncome | | | | | | | | | | | | | | | | | | |

16 10/05/2015Introduction to Databases 16 Update data in one table with data from another There are 2 ways of doing this. 1) Provide all the tablenames involved straight away UPDATE SpyAccount A, SpyAccRates R SET A.income = A.income + A.income * R.rate WHERE A.atype = R.accType; | accountNumber | income | expenditure | atype | | | | 0.00 | savings | | | | | current | | | | | savings | | | | | savings | | | | | current | | | | | current |

17 10/05/2015Introduction to Databases 17 Update data in one table with data from another The alternative way of updating 2) Update one table from a value calculated in a subquery. UPDATE SpyAccount A SET A.income = (SELECT A.income + A.income * R.rate FROM SpyAccRates R WHERE A.atype = R.accType ); ‘For each row in the SpyAccount table, update income by adding in the income multiplied with the value in the rates table which has a matching account type’ This only works if the subquery comes up with a matching value. Otherwise it gets more complicated. ‘only do the above if a match exists’. Not covered now.

18 10/05/2015Introduction to Databases 18 Creating Views You can create a View which gives you a subset of the data from one or more tables CREATE VIEW SpyIncrease as SELECT accountNumber, income, rate, income*rate as interest FROM SpyAccount, SpyAccRates WHERE atype = accType; | Field | Type | Null | Key | Default | Extra | | accountNumber | varchar(5) | NO | | | | | income | decimal(9,2) | NO | | | | | rate | decimal(6,3) | YES | | NULL | | | interest | decimal(15,5) | YES | | NULL | |

19 10/05/2015Introduction to Databases 19 Creating Views You might create a view to Let someone use the view in their own queries whilst hiding data they do not have permission to see (e.g. date of birth) Simplify complex queries select * from SpyIncrease | accountNumber | income | rate | interest | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

20 10/05/2015Introduction to Databases 20 Using Views You can use a view just like a table in a query SELECT accountNumber, income, interest FROM SpyIncrease Every time, the relevant columns and rows will be extracted from the underlying base tables | accountNumber | income | interest | | | | | | | | | | | | | | | | | | | | | | | |

21 10/05/2015Introduction to Databases 21 Updating using the View This is now quite simple! The underlying rows in the SpyAccount table are updated. UPDATE SpyIncrease SET income = income + interest; | accountNumber | income | rate | interest | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |

22 10/05/2015Introduction to Databases 22 SQL Tutorial There is a good tutorial on the w3schools site Complete reference on the MySQL website


Download ppt "10/05/2015 Introduction to Databases 1 F27DB Introduction to Database Systems More SQL Helen Hastie Room: EMB244 Material available on."

Similar presentations


Ads by Google