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 h.hastie@hw.ac.uk 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..* 1 1 1 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 1234500savings 13579235671345.89current 2345620001345savings 3456734556.34savings 456783579.575280.45current 567891267810345current SpySkillList skillCodeskillName 1top shot 2skilled with a knife 3fast runner 4quick thinker 5can pilot a helicopter SpyWithSkill spy Code skill Code 0071 4 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 | +---------------+----------+ | 12345 | 0.00 | | 13579 | 22221.11 | | 23456 | 655.00 | | 34567 | 288.66 | | 45678 | -1700.88 | | 56789 | 2333.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) | +----------------+ | 338.17 | +----------------+

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 | 25.67 | | M | 312.50 | +--------+----------------+

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 | 39824.57 | | savings | 2345.00 | +---------+-------------+

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 | 3579.57 | | savings | 2345.00 | +---------+-------------+

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 | +---------------+----------+-------------+---------+ | 12345 | 10.00 | 0.00 | savings | | 13579 | 23577.00 | 1345.89 | current | | 23456 | 2010.00 | 1345.00 | savings | | 34567 | 355.00 | 56.34 | savings | | 45678 | 3589.57 | 5280.45 | current | | 56789 | 12688.00 | 10345.00 | 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 | +---------------+-----------+ | 12345 | 0.20000 | | 13579 | 353.65500 | | 23456 | 40.20000 | | 34567 | 7.10000 | | 45678 | 53.84355 | | 56789 | 190.32000 | +---------------+-----------+

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 | +---------------+-------------+ | 12345 | 10.20000 | | 13579 | 23930.65500 | | 23456 | 2050.20000 | | 34567 | 362.10000 | | 45678 | 3643.41355 | | 56789 | 12878.32000 | +---------------+-------------+

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 | +---------------+----------+-------------+---------+ | 12345 | 10.20 | 0.00 | savings | | 13579 | 23930.66 | 1345.89 | current | | 23456 | 2050.20 | 1345.00 | savings | | 34567 | 362.10 | 56.34 | savings | | 45678 | 3643.41 | 5280.45 | current | | 56789 | 12878.32 | 10345.00 | 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 | +---------------+----------+-------+-----------+ | 12345 | 10.20 | 0.020 | 0.20400 | | 13579 | 23930.66 | 0.015 | 358.95990 | | 23456 | 2050.20 | 0.020 | 41.00400 | | 34567 | 362.10 | 0.020 | 7.24200 | | 45678 | 3643.41 | 0.015 | 54.65115 | | 56789 | 12878.32 | 0.015 | 193.17480 | +---------------+----------+-------+-----------+

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 | +---------------+----------+----------+ | 12345 | 10.20 0.20400 | | 13579 | 23930.66 |358.95990 | | 23456 | 2050.20 | 41.00400 | | 34567 | 362.10 | 07.24200 | | 45678 | 3643.41 | 54.65115 | | 56789 | 12878.32 |193.17480 | +---------------+----------+----------+

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 | +---------------+----------+-------+-----------+ | 12345 | 10.40 | 0.020 | 0.20800 | | 13579 | 24289.62 | 0.015 | 364.34430 | | 23456 | 2091.20 | 0.020 | 41.82400 | | 34567 | 369.34 | 0.020 | 7.38680 | | 45678 | 3698.06 | 0.015 | 55.47090 | | 56789 | 13071.49 | 0.015 | 196.07235 | +---------------+----------+-------+-----------+

22 10/05/2015Introduction to Databases 22 SQL Tutorial There is a good tutorial on the w3schools site http://www.w3schools.com/sql/ 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