Presentation is loading. Please wait.

Presentation is loading. Please wait.

Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language.

Similar presentations


Presentation on theme: "Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language."— Presentation transcript:

1 Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language

2 Mark Dixon, SoCCE SOFT 131Page 2 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

3 Mark Dixon, SoCCE SOFT 131Page 3 Example: People Database Person PersonIDSurnameForenamesGenderPhoneeMail 1DixonMarkYes01752 232556mark.dixon@plymouth.ac.uk 2SmithJohnYes01752 111111john.smith@john.smith.ac.uk 3JonesSallyNo01752 888888sally.jones@sally.jones.com 4BloggsFredYes01752 123123fred.bloggs@aaaaaa.com 5AndersonGennyNo01752 987987genny@bbbb.cccc.com 6SmithBobYes01752 898898bob.smith@bob-smith.com

4 Mark Dixon, SoCCE SOFT 131Page 4 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

5 Mark Dixon, SoCCE SOFT 131Page 5 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

6 Mark Dixon, SoCCE SOFT 131Page 6 Example: People v3 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 statement

7 Mark Dixon, SoCCE SOFT 131Page 7 SQL: Queries main purpose of databases: –get information back out: searching Structured Query Language –dedicated to interacting with databases 3 rd Generation Language (such as VB, C++) –code describes how to do task 4 th Generation Language (such as SQL) –code describes what to do (not how to do it)

8 Mark Dixon, SoCCE SOFT 131Page 8 SQL: SELECT statement SELECT statement –used to get data –can be embedded in VB, via rs.Open: rs.Open "Person", cs rs.Open "SELECT * FROM [Person]", cs all fields

9 Mark Dixon, SoCCE SOFT 131Page 9 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];

10 Mark Dixon, SoCCE SOFT 131Page 10 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 text SELECT * FROM Person WHERE Surname = 'Smith'

11 Mark Dixon, SoCCE SOFT 131Page 11 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

12 Mark Dixon, SoCCE SOFT 131Page 12 People Database (with Hobbies) IDSurnameForenamesPhoneemail 1DixonMark01752 232556mark.dixon@plymouth.ac.uk 2SmithJohn01752 111111john.smith@john.smith.ac.uk 3JonesSally01752 888888sally.jones@sally.jones.com 4BloggsFred01752 123123fred.bloggs@aaaaaa.com 5AndersonGenny01752 987987genny@bbbb.cccc.com HobbyIDDescriptionPersonID 1Archery1 2Herpetology1 3Music1 4Football2 5Rugby2 6Hitting people with swords1 Hobby Person

13 Mark Dixon, SoCCE SOFT 131Page 13 SQL: Joining tables SELECT * FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; Two tables Matching records IDSurnameForenamesPhoneemailHobbyIDDescriptionPersonID 1DixonMark01752 232556mark.dixon@plymouth.ac.uk1Archery1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk2Herpetology1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk3Music1 1DixonMark01752 232556mark.dixon@plymouth.ac.uk6Hitting people with swords1 2SmithJohn01752 111111john.smith@john.smith.ac.uk4Football2 2SmithJohn01752 111111john.smith@john.smith.ac.uk5Rugby2

14 Mark Dixon, SoCCE SOFT 131Page 14 SQL: Joining tables IDSurname 1Dixon 1 1 1 2Smith 2 SELECT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID];

15 Mark Dixon, SoCCE SOFT 131Page 15 SQL: DISTINCT records SELECT DISTINCT [ID], [Surname] FROM [Person], [Hobby] WHERE [Person].[ID] = [Hobby].[PersonID]; IDSurname 1Dixon 2Smith

16 Mark Dixon, SoCCE SOFT 131Page 16 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

17 Mark Dixon, SoCCE SOFT 131Page 17 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


Download ppt "Mark Dixon, SoCCE SOFT 131Page 1 18 – Structured Query Language."

Similar presentations


Ads by Google