170 likes | 280 Views
18 – Structured Query Language. Session Aims & Objectives. Aims To introduce the fundamental ideas involved in using SQL Objectives, by end of this week’s sessions, you should be able to: Use SQL in your programs to create more complex record-sets. Example: People Database. Person.
E N D
Session Aims & Objectives • Aims • To introduce the fundamental ideas involved in using SQL • Objectives,by end of this week’s sessions, you should be able to: • Use SQL in your programs to create more complex record-sets
Example: People Database Person
Example: People v1 • Display Surname of all people in list box: Option Explicit Const cs = "Provider … " Private Sub Form_Load() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "Person", cs lstPeople.Clear Do Until rs.EOF lstPeople.AddItem rs.Fields("Surname").Value rs.MoveNext Loop rs.Close Set rs = Nothing End Sub
Example: People v2 • Display Surname of Male people in list box: Option Explicit Const cs = "Provider …" Private Sub Form_Load() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "Person", cs Me.lstPeople.Clear Do Until rs.EOF If rs.Fields("Gender").Value = True Then lstPeople.AddItem rs.Fields("Surname").Value End If rs.MoveNext Loop rs.Close Set rs = Nothing End Sub
Example: People v3 SQL statement • Display Surname of Male people in list box: Option Explicit Const cs = "Provider …" Private Sub Form_Load() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "SELECT * FROM Person WHERE Gender = True", cs Me.lstPeople.Clear Do Until rs.EOF lstPeople.AddItem rs.Fields("Surname").Value rs.MoveNext Loop rs.Close Set rs = Nothing End Sub
SQL: Queries • main purpose of databases: • get information back out: searching • Structured Query Language • dedicated to interacting with databases • 3rd Generation Language (such as VB, C++) • code describes how to do task • 4th Generation Language (such as SQL) • code describes what to do (not how to do it)
SQL: SELECT statement • SELECT statement • used to get data • can be embedded in VB, via rs.Open:rs.Open "Person", csrs.Open "SELECT * FROM [Person]", cs all fields
SQL: WHERE & ORDER BY • WHERE clause • used to restrict data SELECT * FROM [People] WHERE [age]>=18; • ORDER BY clause • used to change order of data SELECT * FROM [People] ORDER BY [Surname];
SQL: strings (text data) • Possible confusion:SELECT * FROM Person WHERE Surname = Smith this will look for field called Smith - gives error need single (SQL) quotes to signify literal textSELECT * FROM Person WHERE Surname = 'Smith'
SQL & MS access queries • MS Access • Queries: select data from database • really SQL select statements • can use queries to test SQL code MS Access: People.mdb
People Database (with Hobbies) Person Hobby
SQL: Joining tables ID Surname Forenames Phone email HobbyID Description PersonID 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 1 Archery 1 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 2 Herpetology 1 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 3 Music 1 1 Dixon Mark 01752 232556 mark.dixon@plymouth.ac.uk 6 Hitting people with swords 1 2 Smith John 01752 111111 john.smith@john.smith.ac.uk 4 Football 2 2 Smith John 01752 111111 john.smith@john.smith.ac.uk 5 Rugby 2 SELECT *FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records
SQL: Joining tables ID Surname 1 Dixon 1 Dixon 1 Dixon 1 Dixon 2 Smith 2 Smith SELECT [ID], [Surname]FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID];
SQL: DISTINCT records ID Surname 1 Dixon 2 Smith SELECT DISTINCT [ID], [Surname]FROM [Person], [Hobby]WHERE [Person].[ID] = [Hobby].[PersonID];
Example: People v4 • User controls what is displayed: Option Explicit Const cs = "Provider …" Private Sub optAll_Click() Dim rs As ADODB.Recordset Set rs = New ADODB.Recordset rs.Open "Person", cs Me.lstPeople.Clear Do Until rs.EOF Me.lstPeople.AddItem rs.Fields("Surname").Value rs.MoveNext Loop rs.Close Set rs = Nothing End Sub Private Sub optMale_Click() ‘ You fill in this code. End Sub Private Sub optFemale_Click() ‘ You fill in this code. End Sub
Example: People v5 • User controls what is displayed: • V4 has 38 lines • do same with 23 Option Explicit Const cs = "Provider …" Private Sub optAll_Click() ‘ You fill in this code. End Sub Private Sub optMale_Click() ‘ You fill in this code. End Sub Private Sub optFemale_Click() ‘ You fill in this code. End Sub