f27db introduction to database systems lecture 2: using relational databases
Post on 21-Jan-2016
29 Views
Preview:
DESCRIPTION
TRANSCRIPT
21/04/23 Introduction to Databases 1
F27DB Introduction to Database Systems Lecture 2: Using Relational Databases
Helen Hastie h.hastie@hw.ac.ukLT2
Student hours: Tuesday 2pm-3pmTwitter: @IntroDBHW and #IntroDBHW
Material available on Vision
21/04/23 Introduction to Databases 2
Recap
• So far you have seen how to:• Create a table• Add some data• Run some queries to extract the data
• From the first lab you should know how to:• Start MySQL• Run commands from a text file and by typing• Extract data from a single table
• Next• Relational databases• => relationships between multiple tables
21/04/23 Introduction to Databases 3
Extending the Spy database
• We have a new specification from the MySpy agency.
• The agency require to store information about spies, spy masters, and bank accounts• Each spy should have a unique code name, a first
name, last name, date of birth, sex, distinguishing mark, and payment due.
• A spy may also have several skills and several spies may have the same skill.
• A spy master has a unique code name.• A bank account has a unique account number,
income to date, and expenditure to date.• A spy master handles several spies. Each spy and
each spy master has one bank account.
21/04/23 Introduction to Databases 4
The existing spy table
• Each spy should have a unique code name, a first name, last name, date of birth, sex, distinguishing mark, and payment due.
code name
first name
last name
date of birth sex markamount
due
007 James Bond 12 December 1972
M Mole on chin
5050
bud Fanny Charleston
31 July 1983 F scar on cheek
25.67
freddie John Smith 05 September 1954
M one finger missing
312.5
21/04/23 Introduction to Databases 5
Alterations to be made
• A spy might have >1 skill• A spy has a bank account• A spy has a spy master• A spy master has a code, a bank account
and supervises several spies
• All this complexity does not fit into a single table
• Start by identifying entities and drawing an Entity Relationship or E-R diagram
21/04/23 Introduction to Databases 6
E-R diagram
SpycodeNamefirstNamedateOfBirthgendermarkamountDue
SpyMastermCodeNamecontact
SpyAccountaccountNumberIncomeexpenditure
SpySkillListskillCodeskillName
MM
has
M
1 manages
1
has
11
has1
In the E-R diagram, the fact that a spy has other entities - a bank account, a spymaster and skills is ONLY shown by the relationship lines
21/04/23 Introduction to Databases 7
The final tables
SpycodeName firstNamedateOfBirthgendermarkamountDuespyMaster bankAccount
SpyMastermCodeNamebankAccountcontact
SpyAccountaccountNumberIncomeexpenditure
SpySkillListskillCodeskillName
M
1
1
M
1
1
skilled at
has
manages
1
1
SpyWithSkillspyCodeskillCode
1
M
practisedby
Bold : primary keyItalic : foreign keyBold and italic : both
has
Now the relationships are also shown by foreign keys
NB Many to many relationships need an extra ‘linking’ table
21/04/23 Introduction to Databases 8
Foreign keys
• A foreign key is a field in a relational table that matches the primary key column of another table.
• It establishes the links between the tables.
• Examples• bankAccount in SpyMaster• bankAccount in Spy• spyMaster in Spy
21/04/23 Introduction to Databases 9
The final tables
SpycodeName firstNamedateOfBirthgendermarkamountDuespyMaster bankAccount
SpyMastermCodeNamebankAccountcontact
SpyAccountaccountNumberIncomeexpenditure
SpySkillListskillCodeskillName
M
1
1
M
1
1
skilled at
has
manages
1
1
SpyWithSkillspyCodeskillCode
1
M
practisedby
Bold : primary keyItalic : foreign keyBold and italic : both
has
21/04/23 Introduction to Databases 10
Spy DB
SpyMaster
mCodeName
bankAccount contact
M 12345 Drop 5
P 56789 PO Box 23
Q 13579 Jimmie's
SpyAccount
account Number
income
expenditure
12345 0 0
13579 23567 1345.89
23456 2000 1345
34567 345 56.34
45678 3579.57 5280.45
56789 12678 10345
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
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
M
1manages
1
Mskilled at
1
M
practisedby
1
has
1
1
1
has
21/04/23 Introduction to Databases 11
One to many relationships
• One spymaster, several spies• The Spy table contains the code name of the
spy’s spymaster, as a link to the details in the bank account table• The spymaster codename in the Spy table is a
foreign key• It links to a primary key in the SpyMaster table
SpyMaster
code name
bankAccount
contact
M 12345 Drop 5
P 56789 PO Box 23
Q 13579 Jimmie's
Spy
code Name
first Name
lastName etcspy
masterbank
Account
007 James Bond . . . Q 23456
bud Fanny Charleston . . . Q 34567
freddie
John Smith . . . M 45678
21/04/23 Introduction to Databases 12
One to one relationships
• One bank account, one spymaster• The SpyMaster table contains the bank account
number, as a link to the details in the bank account table• The bank account number in the SpyMaster table is a
foreign key• It links to a primary key in the Bank Account table
Bank Account
account numberIn
comeExpend
iture
12345 0 0
13579 23567 1345.89
23456 2000 1345
34567 345 56.34
45678 3579.57 5280.45
56789 12678 10345
SpyMaster
code name
bankAccount
contact
M 12345 Drop 5
P 56789 PO Box 23
Q 13579 Jimmie's
21/04/23 Introduction to Databases 13
Many to many relationships
• A separate table has been created for skills• In this way, each skill is listed only once and so
we get consistency with spelling
• A code number has been invented for each skill• Long primary keys waste space
• skillName is not very long, but is just used to demonstrate
SpySkillList
skillCode skillName
1 top shot
2 skilled with a knife
3 fast runner
4 quick thinker
5 can pilot a helicopter
21/04/23 Introduction to Databases 14
Many to many relationships
• A separate table is created with a list of links – which spy, which skill• This is essential for many-to-many
relationshipsSpyWithSkill
spyCode
skillCode
007 1
007 4
bud 2
bud 3
bud 4
freddie 4
freddie 5
21/04/23 Introduction to Databases 15
Many to many relationships
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
Spycode name
first name
etc
007 James . . .
bud Fanny . . .
freddie John . . .
• Note that the SpyWithSkill table has• 2 foreign keys, linking to 2 different tables• A composite primary key consisting of both
fields• BOTH fields are needed to make a row unique
21/04/23 Introduction to Databases 16
Creating the tables
• In the Create Table command, or as a separate Alter Table command, you can provide the foreign key information showing that one column refers to another
• We use the InnoDB engine to make sure that foreign key references are supported
CREATE TABLE SpyWithSkill (
spyCode VARCHAR(10),
skillCode INT ,
PRIMARY KEY (spyCode, skillCode),
FOREIGN KEY (skillCode) REFERENCES SpySkillsList (skillCode),
FOREIGN KEY (spyCode) REFERENCES Spy (codeName)
)ENGINE=INNODB;
21/04/23 Introduction to Databases 17
AUTO-INCREMENT
• In the SpySkillList table, each skill has been given a unique automatically generated sequence number• New entries get the next number
• To allocate the next number, add ‘AUTO-INCREMENT’ to the column in the CREATE TABLE command• E.g.
skillCode INT AUTO-INCREMENT PRIMARY KEY,
21/04/23 Introduction to Databases 18
INSERTING VALUES WITH AUTO-INCREMENT
• Use a slightly different format of the INSERT command. Because you are not inserting a value for every column, you must specify which ones:• INSERT INTO SpySkillList (skillName)
VALUES (‘explosive expert’);• The skillCode appears magically! SpySkillList
skillCode skillName
1 explosive expert
2 top shot
3 skilled with a knife
4 fast runner
5 quick thinker
6 can pilot a helicopter
21/04/23 Introduction to Databases 19
Foreign key constraints
• You must make sure that the table that you are referencing with ‘Foreign Key’ exists
• Either create all the tables without foreign key constraints, then add in the foreign keys constraints afterwards using the ALTER TABLE command (This way is taken in Brian’s examples)
• Or create tables in suitable order, including foreign key constraints
• Note that you must drop the tables in order so that all the ones containing foreign keys are dropped before the table referenced by the foreign key
• I have done this in the Spy tables• This way works most of the time, but not if 2 tables are
referenced both ways• E.g. Spy has a SpyMaster
• SpyMaster has a second-in-command who is a Spy
21/04/23 Introduction to Databases 20
Suitable order for creating Spy tables
• Have a look back at the Spy tables• What is wrong with this order for table
creation?• Spy• SpyAccount• SpyMaster• SpyWithSkill• SpySkillList
• What would be a more suitable order?
•SpyWithSkill•SpySkillList•Spy•SpyMaster•SpyAccount
21/04/23 Introduction to Databases 21
Creating your database from a text file
• We recommend that you create your database from a text file containing commands to create the table and to insert the data
• In this way, you can • Easily correct errors in your commands and rerun them• Retain a record of what you did• Move db from home to uni, if required
• But to rerun the commands, you must start by dropping the tables• DROP TABLE tablename;• Again, order is important. You can’t drop a table which is
being referenced.
21/04/23 Introduction to Databases 22
Text file - verbose mode
• To see your commands listed on the screen as MySQL runs through them from a text file, you need to be in ‘verbose mode’
• So alter the startup command to be• mysql –u username –D username –h mysql-
server-1 –vp• The last bit has
• v for verbose• p for password
21/04/23 Introduction to Databases 23
Retrieving data from >1 table
• We’d like details of spy masters and their accounts
• How NOT to do it!• SELECT * required columns
…FROM SpyMaster, SpyAccount from these tables
• This gives you EVERY row from the Spy table joined to EVERY row from the BankAccount table, as shown on the next slide
21/04/23 Introduction to Databases 24
Problem
• We ONLY want those rows with the matching account numbers• In bold below
Answer
code nameBank
Accountcontact
Account Number
Income
Expenditure
M 12345 Drop 5 12345 0 0
P 56789 PO Box 23 12345 0 0
Q 13579 Jimmie's 12345 0 0
M 12345 Drop 5 13579 23567 1345.89
P 56789 PO Box 23 13579 23567 1345.89
Q 13579 Jimmie's 13579 23567 1345.89
. . . . . . . . . . . . . . . . . .
M 12345 Drop 5 56789 12678 10345
P 56789 PO Box 23 56789 12678 10345
Q 13579 Jimmie's 56789 12678 10345
21/04/23 Introduction to Databases 25
Solution
• Restrict the rows to those with matching account numbers• SELECT * FROM SpyMaster, SpyAccount
WHERE bankAccount = accountNumber
Answer
code namebank
Accountcontact
account Number
Income
Expenditure
M 12345 Drop 5 12345 0 0
Q 13579 Jimmie's 13579 23567 1345.89
P 56789 PO Box 23 56789 12678 10345
21/04/23 Introduction to Databases 26
Column name uniqueness
• Restrict the rows to have matching account numbers• SELECT * FROM SpyMaster, SpyAccount
WHERE bankAccount = accountNumber
• In the above query, we know which tables the column names come from, because they are unique within the above named tables• E.g.The bankAccount column only exists in the SpyMaster
table
• BUT, supposing we had called them both accNum?• We need a way of distinguishing them
21/04/23 Introduction to Databases 27
Specifying table together with column name
• There are 2 ways to specify which table the column belongs to
• You can also use the table name as well• SELECT * FROM SpyMaster, SpyAccount
WHERE SpyMaster.bankAccount = SpyAccount.accountNumber
• Or use an alias for the table name (shorter!)• SELECT * FROM SpyMaster M , SpyAccount A
WHERE M.bankAccount = A.accountNumber
• It’s quite nice to use the alias all the time, to make it very clear which table the column name belongs to. It is only essential• When the same column name occurs in >1 table• When a query involves using the same table
more than once (not covered today)
21/04/23 Introduction to Databases 28
Less rows, less columns
• As before, you can restrict the number of columns• SELECT mCodeName, income
FROM SpyMaster, SpyAccountWHERE bankAccount = accountNumber
• And restrict the number of rows on other criteria• SELECT income, expenditure
FROM SpyMaster, SpyAccountWHERE bankAccount = accountNumberAND mCodeName = ‘M’;
21/04/23 Introduction to Databases 29
Restricting rows
• We are now using the WHERE clause for 2 reasons• To specify the foreign key between linked
tables• To specify some criteria on the data e.g.
• income < 100• mCodeName = ‘M’
• The order is not important• The DBMS will work out the most efficient way
to execute your query• However, I recommend that you put all the
foreign key links together, at the start, so they come straight after the table names
21/04/23 Introduction to Databases 30
Query 1:N
• For each spymaster, list their code name and their contact point, and the code names of each of the spies that they supervise• SELECT mCodeName, contact, codeName
FROM Spy S, SpyMaster MWHERE S.spyMaster = M.mCodeName;
21/04/23 Introduction to Databases 31
Query 1:N
• There is some duplication in the result • In a real application, you would need to use
scripts or tools to lay the results out nicely.
+-----------+---------+----------+| mCodeName | contact | codeName |+-----------+---------+----------+| M | Drop 5 | freddie | | Q | Jimmy's | 007 | | Q | Jimmy's | 1 | | Q | Jimmy's | bud | +-----------+---------+----------+
21/04/23 Introduction to Databases 32
Using column aliases
• You can rename output columns using aliases• SELECT mCodeName as masterCode, contact, codeName
as SpyCodeFROM Spy S, SpyMaster MWHERE S.spyMaster = M.mCodeName;
+------------+---------+----------+
| masterCode | contact | spyCode |
+------------+---------+----------+
| M | Drop 5 | freddie |
| Q | Jimmy's | 007 |
| Q | Jimmy's | 1 |
| Q | Jimmy's | bud |
+------------+---------+----------+
21/04/23 Introduction to Databases 33
Query M:N
• List the names of the spies with the skill ‘top shot’
• SELECT firstName, lastNameFROM Spy S, SpyWithSkill W, SpySkillList LWHERE S.codeName = W.spyCodeAND W.skillCode = L.skillCodeAND L.skillName = ‘top shot’;
21/04/23 Introduction to Databases 34
Maintaining data integrity
• Some issues. E.g.• What if we give a spy an account number which
doesn’t exist in the Account table?• This won’t be permitted if the Spy table includes a
foreign key referencing the account number in the Account table
21/04/23 Introduction to Databases 35
SQL Tutorial
• There is a good tutorial on the w3schools site• http://www.w3schools.com/sql/
• Complete reference on the MySQL website
21/04/23 Introduction to Databases 36
What next?
• Updating and deleting will be covered in Brian’s sql lecture
• There’s a separate handout for Lab2• Try out all the queries and invent your own• Notice the command to log interactions to a
text file
• Brian’s lectures continue with database material
• Helen’s lectures move on to web pages
top related