Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views38 pages

Lesson5 2024

The document provides a comprehensive overview of database programming concepts, including how to loop through tables, read from multiple tables, edit, insert, and delete records. It also covers SQL commands for sorting, filtering, and performing calculations, as well as aggregate functions and subqueries. Additionally, it includes guidelines for homework assignments and resources for further study.

Uploaded by

taxilacat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views38 pages

Lesson5 2024

The document provides a comprehensive overview of database programming concepts, including how to loop through tables, read from multiple tables, edit, insert, and delete records. It also covers SQL commands for sorting, filtering, and performing calculations, as well as aggregate functions and subqueries. Additionally, it includes guidelines for homework assignments and resources for further study.

Uploaded by

taxilacat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Question 2.

2 – Database Programming

May not be used!


.filter
.Sort
.Locate

Slide 2
Restore Database

Slide 3
Database Programming – Loop Through Table

tblModels.First;
while not tblModels.Eof do
begin
if tblModels['GroupID'] = 'A' then
redO.lines.add(IntToStr(tblModels['ModelID']) +
#9 + tblModels['Model name']);
tblModels.Next; //NB!! Outside the if but inside the loop
end;

Slide 4
Read from both tables

Slide 5
Both Tables – Start with table in which condition sits

tblUsername.First;
while not tblUsername.Eof do
begin
if tblUsername['Grade'] = 11 then
begin

end;//if
tblUsername.Next;
end;//while
Slide 6
tblUsername.First;
while not tblUsername.Eof do
begin
if tblUsername['Grade'] = 11 then
begin
tblTeacher.First;
while not tblTeacher.Eof do
begin
if tblUsername['TeacherID'] = tblTeacher['ID'] then
redOutput.Lines.Add( tblUsername['Names'] +
' ‘ + tblTeacher['Teacher']);
tblTeacher.Next;
end;//while teacher
end;//if
tblUsername.Next;
end;//while

Slide 7
Both Tables Final 2021 – Question 2.2.1
Start with the table that shows the top heading

Slide 8
Slide 9
Database Programming Edit Active Record

tblModels.Edit;
tblModels['Model name']:= 'Elantra';
tblModels['A/C']:= bAC;
tblModels['Tank']:= tblModels['Tank']+10;
tblModels.Post;

Slide 10
Database Programming Edit Multiple Records

tbl.First;
while not tbl.Eof do
begin
if tbl['Sale'] = true then
begin
tbl.Edit;
tbl[‘Price’]:= tbl[‘Price’]-tbl[‘Price’]*10/100;
tbl.Post;
End; //if
tbl.Next;
End; //while
Slide 11
Insert

• Always insert the PK unless it is an AutoNumber data type


• PK must be unique
• Always insert the FK in the child field
• FK must have an existing value in the parent table
• All other fields are optional to insert – read the question
• Match data types

Slide 12
Insert - Exemplar 2018 Q2.2.3

Slide 13
Insert

iPrimaryK := tblModels.RecordCount+1;
tblModels.Insert;
tblModels['ModelID'] := iPrimaryK;
tblModels['GroupID'] := tblGroups['GroupID'];
tblModels['Model name'] :='Toyota Etios';
tblModels['A/C'] := True;
tblModels['Doors'] := 5;
tblModels.Post;

Slide 14
Delete the active record

ADOTbl.Delete;

Slide 15
Delete many records

tbl.First;
while not tbl.Eof do
begin
if tbl['Sale'] = true then
tbl.Delete
Else
tbl.Next;
End;
Slide 16
SQL

Slide 17
Provided Code

Slide 18
Sort

SELECT * FROM tblCar ORDER BY Make

SELECT Make, Model FROM tblCar ORDER BY make DESC

• Sort ascending by default


• * Display all fields

Slide 19
Brackets on field names

•Not case sensitive


• Square brackets needed when:
1. Field contains a space
2. Reserved word
3. Contains an operator

SELECT [Number], [A/C], [Owner Name]

Slide 20
Calculations

SELECT CustomerID, Price, Price* 10/100 AS


Discount FROM AccountsTb

SELECT DOB, int((date() – DOB)/365.25) AS AGE


FROM tblStaff

Slide 21
FORMAT data in the output between Select and From

• Format(10/100* cost,"currency") AS Discount

• 02 October 2008:

Format(DOB,"dd mmmm yyyy") AS [Date]

• 02 Oct 2008: Format(DOB,"dd mmm yyyy") AS


[Date]

• 02 - 10 - 08: Format(DOB,"dd - mm - yy") AS [Date]

• 2 - 9 - 08: Format(DOB,"d - m - yy") AS [Date]

Slide 22
Data Types and Where
•Double quotes " " around the values of text data type fields in
the database
SELECT … FROM …WHERE Colour <> "red"

•Hashes # # around Date/Time data types.


SELECT … FROM … WHERE DOB >= #1999/1/1#

•No quotes around Yes/No, Number and Currency fields

SELECT … FROM … WHERE price >= 5000 OR

Discount = true

Slide 23
SELECT....WHERE...BETWEEN

where DOB between #1 Jan 1999# and #31 Dec 1999#


where price between 5000 and 10000
where (price >= 5000) and (price <= 10000)
where surname between "A" and "M"

SELECT....WHERE...IN
WHERE make IN ("SKODA", "SMART")
WHERE make = "SKODA" or make="SMART"

Slide 24
SQL Functions

• DATE() current date


• YEAR(#16/05/2002#) = 2002
• MONTH(DOB) = 5
• DAY(Date()) = 11
• ROUND(Price*15/100,2)
• ROUND(Price*15/100)
String Manipulation
• LEFT(lastname, 1) = “S"
• RIGHT(lastname, 3) = “ith“
• MID(lastname, 2,3) = “mit"
• LEN(lastname) = 5
• Surname & “ “& Left(FirstName,1) Slide 25
Distinct and Null
SELECT DISTINCT colour FROM tblCar;
SELECT … WHERE OWNER is NULL
SELECT …..WHERE OWNER is NOT NULL

SELECT…WHERE...LIKE
Name LIKE "% S%" – Johnathan Smith, Pete southey
Name LIKE "Bob %" – Bob Smith, bob Sponge
Name LIKE "%Smith" – Bob Smith, Peter Smith, Smith

Name LIKE "_im Smith" – Jim Smith, tim smith


Name LIKE "_ _ _ Smith" – Bob Smith, Tim smith
Slide 26
SELECT… Multiple tables
SELECT Groups.GroupID, [Model Name]
FROM Groups, Models
WHERE Groups.GroupID = Models. GroupID
and Rate_Per_Day < 500

Slide 27
Aggregate Functions:
• SELECT avg(price) FROM car;
• SELECT sum(price) FROM car;
• SELECT max(price) FROM car;
• SELECT min(price) FROM car;
• SELECT count(price) FROM car; //not null
• SELECT count(*) FROM car;
//irrespective if null
Slide 28
Group By
SELECT animal, avg(num) as AvgNum
FROM tblSightings GROUP BY animal;

Slide 29
Having

SELECT animal, avg(num) as AvgNum


FROM tblSightings GROUP BY animal
Having avg(num) >= 15;

Slide 30
ORDER

‘SELECT <fields> FROM <table>


WHERE <field condition>
GROUP BY <fields>
HAVING <aggregate function condition>
ORDER BY <fields>’;

Similar Friends Will Go Hang Out


Slide 31
Insert same rules as ADO tables

• Always insert the PK unless it is an AutoNumber data type


• PK must be unique
• Always insert the FK in the child field
• FK must have an existing value in the parent table
• All other fields are optional to insert – read the question
• Match data types with " " and # or nothing

Slide 32
SQL Insert – Match in NOD
‘INSERT INTO tblCars
(ModelID, [model name], [A/C], Range, RegDate,
GroupID) VALUES
('+IntToStr(iID)+', "Toyota Etios", true, 640,
#2018/06/05#,' + QuotedStr(sID)+')';
Qry.execSql;

Slide 33
Update and Delete
'UPDATE tblCar SET colour = "GREEN"
WHERE colour = "BLUE"';

'UPDATE StudentsTB set birth_date= #1986/01/04#,


registered=true WHERE ID = 3 ';

'UPDATE tblCar SET price = price + price * 10/100';

'DELETE FROM tblCar WHERE owner is null';

Qry.execSql;

Run Select to see changes in DB Grid for PAT


Slide 34
Sub queries

Select Name, Surname, Grade from tblLearner


Where Grade = 8 and

LearnerMark <= (Select AVG(LearnerMark)


from tblLearner where Grade = 8)
// 34 <= 60

Slide 35
MessageDlg

Slide 36
Homework

Complete eForm SQL and Database Programming before


Thursday night under Lesson 6

https://tinyurl.com/UJDelphi

Slide 37
Homework

www.education.gov.za

• Download past papers November 2014 – 2020


• Final 2020 Q2
• Final 2019 Q 2
• Final 2018 Q 2
• Exemplar 2018 Q2

Slide 38

You might also like