Download presentation
Presentation is loading. Please wait.
Published byWilfred Floyd Modified over 9 years ago
1
SQL continued CMSC 461 Michael Wilson
2
Right back into it Recap: Learned how to log in to PostgreSQL Learned about PostgreSQL data types Learned how to create tables and insert data into tables
3
Select statement This is how you get data out of tables This is the great majority of most applications SQL calls Basic syntax: SELECT FROM, WHERE
4
Select statement Selecting columns This tells what column values you want coming back in your results Can be any subset of the columns available in a table If you want all columns, you can type * instead of listing out the columns SELECT * FROM test_table WHERE…
5
Select statement Selecting tables You can select from any number of tables Let’s focus on the use case of just one for now We will address multi-table selects later
6
Select statement Where clause This is exactly like the conditions piece of the relational algebra select You are stringing together specific conditions that you want the returned data to meet
7
Where clauses Where clauses consist of a series of predicates These predicates are the terms discussed during the relational algebra lecture The format is the same is =, !=, >, >=, <, <= Can also use <> instead of !=
8
Predicate examples address = ‘21 Jump Street’ age >= 21 bloodType = ‘O+’
9
Where clauses You can string predicates together using AND or OR
10
Stringing together predicates age > 21 AND bloodType = ‘O+’ daysSinceContact > 50 or myOpinionOfThisPerson = ‘Favorable’
11
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street 1 555 5555CellPhil 8 Get Out of Here Way 7 555 7777WorkBob 7 RUN! Drive 700 555 8888CellOctavio 21 Jump Street 40 123 4567CellJohnny
12
Complete select example SELECT * FROM AddressBook WHERE daysSinceLastContact > 20
13
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 7 RUN! Drive 700 555 8888CellOctavio 21 Jump Street 40 123 4567CellJohnny
14
Complete select example SELECT contactName FROM AddressBook WHERE phoneType = ‘Cell’
15
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street 1 555 5555CellPhil 7 RUN! Drive 700 555 8888CellOctavio 21 Jump Street 40 123 4567CellJohnny
16
Complete select example SELECT contactName FROM AddressBook WHERE phoneType = ‘Cell’ and contactName = ‘Octavio’
17
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 7 RUN! Drive 700 555 8888CellOctavio
18
Predicate formulas You can also use formulas in predicates Example age < (20 + 5) Why this is useful will make more sense later, however
19
Update statement This will update specific values in already existing rows in your table Basic syntax: UPDATE, SET =, = … WHERE
20
Update statement Table selection You can select multiple tables during one update Let’s focus on the single table case for now
21
Update statement Column value setting Column = new value The new value can be another column, a value, or a formula (similar to how predicates work in where clauses) Can set multiple columns in one statement
22
Update statement Where clause This where clause functions exactly the same as it does with a select statement
23
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street 1 555 5555CellPhil 8 Get Out of Here Way 7 555 7777WorkBob 7 RUN! Drive 700 555 8888CellOctavio 21 Jump Street 40 123 4567CellJohnny
24
Update example UPDATE AddressBook SET phoneNumber = ‘444 7777’ WHERE contactName = ‘Phil’
25
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street 1 444 7777CellPhil 8 Get Out of Here Way 7 555 7777WorkBob 7 RUN! Drive 700 555 8888CellOctavio 21 Jump Street 40 123 4567CellJohnny
26
Update example UPDATE AddressBook SET daysSinceLastContact = 0, phoneType = ‘Home’ WHERE contactName = ‘Octavio’
27
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street 1 444 7777CellPhil 8 Get Out of Here Way 7 555 7777WorkBob 7 RUN! Drive 0 555 8888HomeOctavio 21 Jump Street 40 123 4567CellJohnny
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.