We used the CourseID rather than the Title in the DELETE statement criteria because:
- The CourseID is the primary key for identifying each unique record in the Course table. It will unambiguously identify the single record we want to delete.
- The Title could potentially match multiple records if the same course title was used more than once. Using the Title would risk deleting multiple records instead of just the intended one.
- Best practice is to always use the primary key field(s) to identify records when querying, updating, or deleting in order to ensure only the intended record(s) are affected.
So in summary, by using the CourseID we can be certain to only delete the single intended record, rather than
2. What is SQL?
SQL – Structured Query Language is a language used to manipulate data held within a
database.
Database applications such as Access simply place a graphical user interface
which manipulates and executes various SQL operations.
There are lots of slight variations of SQL syntax but for this course we will be using
MySQL
3. SQL Operations covered at National 5
We will look at SQL operations which are designed to query, insert and edit data within
a database.
In SQL these operations can be performed by the following commands.
❏SELECT - Returns particular data
❏UPDATE – Updates value/values within a row/rows.
❏INSERT – Inserts new rows (records) into a table.
❏DELETE – Deletes rows from a table.
5. SELECT SQL Statements
SELECT statements are used to retrieve information from a database.
At National 5 to being with our SELECT statements will have 4 main parts
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
6. Some examples
We will use data from the instructor database as an example
CourseID Title Date Days Capacity Cost
Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling
1/10/1
7
2 18 £250 4
MBT01
Mountain
Biking
8/9/17 5 12 £170 2
7. SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 1 is to SELECT the fields we want:
SELECT Title,Date, Days,Capacity,Cost
8. SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 2 is to SELECT the table(s) that our fields are from we want:
SELECT Title,Date, Days,Capacity,Cost FROM Course
9. SELECT QUERY - One Condition
Show the Title, Date, Days, Capacity and Cost of all courses that cost under £500 and
display the results cheapest first.
Step 3 is to specify the criteria we want to use for our query
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500
10. SELECT QUERY - One Condition
We want to show all courses that cost under £500, we would want to see the Title,Date,
Days, Capacity and Cost.
Step 4 is to specify any sort order
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost
ASC
You can sort in ascending (ASC) or descending (DESC) order
11. SELECT QUERY - One Condition
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC
Fields to be returned
Tables where the fields are
Criteria
Sort Order
12. SELECT QUERY - One Condition
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 ORDER BY Cost ASC
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling 1/10/17 2 18 £250 4
MBT01 Mountain
Biking
1/9/17 5 12 £170 2
13. SELECT QUERY - One Condition
Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned
Title Date Days Capacity Cost
Mountain
Biking
1/9/17 5 12 £170
Abseiling 1/10/17 2 18 £250
Adv. Kayaking 1/9/17 6 8 £450
14. SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 1 is to SELECT the fields we want:
SELECT Title,Date, Days,Capacity,Cost
15. SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 2 is to SELECT the table(s) that our fields are from we want:
SELECT Title,Date, Days,Capacity,Cost FROM Course
16. SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and
display them with the longest course first.
Step 3 is to specify the criteria we want to use for our query
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
17. SELECT QUERY - More than one condition
We want to show the course details that are under £500 that last 5 days or less and display
them with the longest course first.
Step 4 is to specify any sort order
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
18. SELECT QUERY - More than one condition
1. The fields you want to display in your results
2. Which tables that the fields are in
3. The criteria for your search
4. Any sort order that you want to apply
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
Fields to be returned Tables where the fields are
Criteria
Sort Order
19. Search Results
SELECT Title,Date, Days,Capacity,Cost FROM Course WHERE Cost <500 AND Days <= 5
ORDER BY DAYS DESC
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
KAY02 Adv. Kayaking 1/9/17 6 8 £450 2
WAL01 Gorge Walking 1/9/17 10 6 £600 3
ABS02 Abseiling 1/10/17 2 18 £250 4
MBT01 Mountain
Biking
1/9/17 5 12 £170 2
20. Search Results
Only the fields we specified (Title,Date, Days,Capacity,Cost) are returned
Title Date Days Capacity Cost
Mountain
Biking
1/9/17 5 12 £170
Abseiling 1/10/17 2 18 £250
22. UPDATE SQL Statements
UPDATE statements are used to amend data currently in the database
An UPDATE statement will have 3 main parts
1. Specify the table that has the information to be updated
2. What the new values are going to be
3. The criteria for which records to update
23. Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 1 Specify the table that has the information to be updated
UPDATE Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
Needs to be 8
24. Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 2 What the new values are going to be
UPDATE Course SET Capacity = 8
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
25. Update Query - Single Record
A few of the bikes for the BMX Intro course are being repaired and the new capacity for
the course will be 8. We will update the record to show 8 for capacity
Step 3 The criteria for which records to update
UPDATE Course SET Capacity = 8 WHERE CourseID = ‘BMX01’
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
26. Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 1 Specify the table that has the information to be updated
UPDATE Course
27. Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 2 What the new values are going to be
UPDATE Course SET Date = ‘2017/12/11’
Dates are in
yyyy/mm/dd format
28. Update Query- Multiple Records
There has been an issue with the training facility and all courses that are due to run on
the 10th of December have had to be changed to the 11th of December
Step 3 What the new values are going to be
UPDATE Course SET Date = ‘2017/12/11’ WHERE DATE = ‘2017/12/10’
This will change every course that was on the 10th December
29. Update Query- Partial Fields
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be limited to a capacity of 10.
Step 1 Specify the table that has the information to be updated
UPDATE Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX01 BMX Intro 1/9/17 3 12 £500 1
30. Update Query- Partial Fields
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be a limited to a capacity of 10.
Step 2 What the new values are going to be
UPDATE Course SET Capacity = 10, Instructor ID = 3
31. Update Query- Multiple Records
There has been a change of staffing and all of the BMX Intro courses will now be taught
by another instructor (ID 3) and will be a limited to a capacity of 10.
Step 3 What the new values are going to be
UPDATE Course SET Capacity = 10, Instructor ID = 3 WHERE Title = ‘BMX Intro’
33. INSERT SQL Statement
INSERT INTO table
VALUES (value1, value2, value3, ...);
The order of the values to be inserted must match the column order
An alternative method allows you to specify the order of the fields and their data values
in the format below:
INSERT INTO table (column3, column1, column2, ...)
VALUES (value3, value1, value2, ...);
34. INSERT Query - Data for every field
A new instructor joins and his details need to be added. He will have an ID of 5 and his
name is D Thomas, he was born on the 1/5/86 and is a Grade 5
Step 1 is to specify which table to insert the data
INSERT INTO Instructor
Instructor
ID
Name DOB Grade
5 D Thomas 1/5/86 5
35. INSERT Query - Data for every field
A new instructor joins and their details need to be added. They will have an ID of 5 and
their name is D Thomas, they were born on the 1/5/86 and they are a Grade 5
Step 2 is to specify the values to be inserted
INSERT INTO Instructor VALUES (5,‘D Thomas’, ‘1985/05/01’,5)
Instructor
ID
Name DOB Grade
5 D Thomas 1/5/86 5
Dates are in
yyyy/mm/dd format
String values are surrounded by an ‘
36. Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 1 is to specify which table to insert the data
INSERT INTO Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 4 10 £400 3
37. Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 2 is to specify which fields you are entering data for
INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID)
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 4 10 £400 3
38. Partial INSERT Query
The organisation is running a new BMX Course, it’s course ID is BMX05 and will be a BMX
Advanced, it will run for 4 days and can take 10 people. It will be ran by Instructor R Hay
( ID 3) and will cost £400. They haven’t decided a date yet.
Step 3 is to specify the values for each field
INSERT INTO Course (CourseID,Title,Days,Capacity,Cost,InstructorID) VALUES
(‘BMX05’,‘BMX Advanced’,4,10,400,3)
40. DELETE SQL Statements
DELETE statements are used to delete data currently in the database
A DELETE statement has 2 main parts
1. Specify the table that has the information to be deleted
2. The criteria for which records have to be deleted
41. DELETE SQL Statement
DELETE FROM tbl_name
[WHERE where_condition]
The DELETE statement deletes rows from the table and returns the number of
deleted rows.
The conditions in the WHERE clause identify which rows to delete.
If there is no WHERE clause, all rows in the table(s) are deleted.
42. DELETE Query - Single Record
The BMX Advanced course on the 4th May is now cancelled so should be deleted
Step 1 Specify the table that has the information to be deleted
DELETE FROM Course
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 2017/5/4 4 10 £400 3
43. DELETE Query - Single Record
The BMX Advanced course on the 4th May is now cancelled so should be deleted
Step 2 The criteria for which records have to be deleted
DELETE FROM Course WHERE CourseID = ‘BMX05’
CourseID Title Date Days Capacity Cost Instructor
ID
BMX05 BMX Advanced 2017/5/4 4 10 £400 3
44. Why did we use the
CourseID and not the Title?