Date post: | 16-Dec-2015 |
Category: |
Documents |
Upload: | anastasia-warner |
View: | 222 times |
Download: | 0 times |
18/04/23 Introduction to Databases 1
F27DB Introduction to Database Systems More SQL
Helen [email protected]
Room: EMB244Material available on Vision (modified from slides
by Monica Farrow)
18/04/23 Introduction 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
18/04/23 Introduction to Databases 3
The tables now
SpycodeName firstNamedateOfBirthgendermarkamountDuespyMaster bankAccount
SpyMastermCodeNamebankAccountcontact
SpyAccountaccountNumberIncomeExpenditureatype
SpySkillListskillCodeskillName
1..*
1
1
1..*
1
1
skilled at
has
manages
1
1
SpyWithSkillspyCodeskillCode
1
0..*
practisedby
Bold : primary keyItalic : foreign keyBoth : both
has
Now the relationships are also shown by foreign keys
NB Many to many relationships need an extra ‘linking’ table
SpyAccRatesaccTyperate
1
1..*
18/04/23 Introduction to Databases 4
Spy
code Name
first Name
lastName dateOfBirthgender
markamount
Duespy
Masterbank
Account
007 James Bond 12 December 1972
M Mole on chin 5050 Q 23456
bud Fanny Charleston 31 July 1983 F scar on cheek 25.67 Q 34567
freddie John Smith 05 September 1954
M one finger missing
312.5 M 45678
Spy DBSpyMaster
mCodeName
bankAccount contact
M 12345 Drop 5
P 56789 PO Box 23
Q 13579 Jimmie's
SpyAccount
account Number
income
expenditure atype
12345 0 0 savings
13579 23567 1345.89 current
23456 2000 1345 savings
34567 345 56.34 savings
45678 3579.57 5280.45 current
56789 12678 10345 current
SpySkillList
skillCode skillName
1 top shot
2 skilled with a knife
3 fast runner
4 quick thinker
5 can pilot a helicopter
SpyWithSkill
spyCode
skillCode
007 1
007 4
bud 2
bud 3
bud 4
freddie 4
freddie 5
SpyAccRates
accType rate
current 0.015
savings 0.02
18/04/23 Introduction to Databases 5
Extra SQL needed
• Create new tableCREATE TABLE SpyAccRates (accType VARCHAR(10) PRIMARY KEY, rate DECIMAL( 6,3));
• Insert 2 recordsINSERT 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 accountsUPDATE SpyAccount SET atype = 'current'; UPDATE SpyAccount SET atype = 'savings' WHERE income < 400;
18/04/23 Introduction 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, SpyAccountWHERE bankAccount = accountNumberAND income = 0;
mCodename
bankAccount
Income
M 12345 0
18/04/23 Introduction 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 | +---------------+----------+
18/04/23 Introduction 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 LWHERE W.skillCode = L.skillCodeAND L.skillName = ‘quick thinker’;
+----------+
| COUNT(*) |
+----------+
| 4 |
+----------+
+----------+
| COUNT(*) |
+----------+
| 2 |
+----------+
18/04/23 Introduction to Databases 9
Sum
• Find the total amount due to all spiesSELECT sum(amountDue) FROM Spy;
+----------------+| sum(amountDue) |+----------------+| 338.17 | +----------------+
18/04/23 Introduction 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 | +--------+----------------+
18/04/23 Introduction to Databases 11
Income by type
• Display the total income of all account holders, grouped according to their type of accountSELECT atype, sum(income)
FROM SpyAccount
GROUP BY atype
+---------+-------------+| atype | sum(income) |+---------+-------------+| current | 39824.57 | | savings | 2345.00 | +---------+-------------+
18/04/23 Introduction to Databases 12
Low income by type
• Limit the enquiry to those accounts with under 4000 in incomeSELECT atype, sum(income)
FROM SpyAccount
WHERE income < 4000
GROUP BY atype
+---------+-------------+| atype | sum(income) |+---------+-------------+| current | 3579.57 | | savings | 2345.00 | +---------+-------------+
18/04/23 Introduction 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 | +---------------+----------+-------------+---------+
18/04/23 Introduction 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 RWHERE A.atype = R.accType;
+---------------+-----------+| accountNumber | interest |+---------------+-----------+| 12345 | 0.20000 | | 13579 | 353.65500 | | 23456 | 40.20000 | | 34567 | 7.10000 | | 45678 | 53.84355 | | 56789 | 190.32000 | +---------------+-----------+
18/04/23 Introduction 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 newIncomeFROM SpyAccount A, SpyAccRates RWHERE A.atype = R.accType;
+---------------+-------------+| accountNumber | newIncome |+---------------+-------------+| 12345 | 10.20000 | | 13579 | 23930.65500 | | 23456 | 2050.20000 | | 34567 | 362.10000 | | 45678 | 3643.41355 | | 56789 | 12878.32000 | +---------------+-------------+
18/04/23 Introduction 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
awayUPDATE SpyAccount A, SpyAccRates RSET A.income = A.income + A.income * R.rateWHERE 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 | +---------------+----------+-------------+---------+
18/04/23 Introduction 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 ASET 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/04/23 Introduction to Databases 18
Creating Views
• You can create a View which gives you a subset of the data from one or more tablesCREATE 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 | | +---------------+---------------+------+-----+---------+-------+
18/04/23 Introduction 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 | +---------------+----------+-------+-----------+
18/04/23 Introduction to Databases 20
Using Views
• You can use a view just like a table in a querySELECT 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 | +---------------+----------+----------+
18/04/23 Introduction to Databases 21
Updating using the View
• This is now quite simple! The underlying rows in the SpyAccount table are updated.UPDATE SpyIncreaseSET 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 | +---------------+----------+-------+-----------+