SQL continued CMSC 461 Michael Wilson
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
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
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…
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
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
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 !=
Predicate examples address = ‘21 Jump Street’ age >= 21 bloodType = ‘O+’
Where clauses You can string predicates together using AND or OR
Stringing together predicates age > 21 AND bloodType = ‘O+’ daysSinceContact > 50 or myOpinionOfThisPerson = ‘Favorable’
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Complete select example SELECT * FROM AddressBook WHERE daysSinceLastContact > 20
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Complete select example SELECT contactName FROM AddressBook WHERE phoneType = ‘Cell’
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Complete select example SELECT contactName FROM AddressBook WHERE phoneType = ‘Cell’ and contactName = ‘Octavio’
Complete select example address daysSinceContact contactPhoneNumber numberTypecontactName 7 RUN! Drive CellOctavio
Predicate formulas You can also use formulas in predicates Example age < (20 + 5) Why this is useful will make more sense later, however
Update statement This will update specific values in already existing rows in your table Basic syntax: UPDATE, SET =, = … WHERE
Update statement Table selection You can select multiple tables during one update Let’s focus on the single table case for now
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
Update statement Where clause This where clause functions exactly the same as it does with a select statement
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Update example UPDATE AddressBook SET phoneNumber = ‘ ’ WHERE contactName = ‘Phil’
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive CellOctavio 21 Jump Street CellJohnny
Update example UPDATE AddressBook SET daysSinceLastContact = 0, phoneType = ‘Home’ WHERE contactName = ‘Octavio’
Update example address daysSinceContact contactPhoneNumber numberTypecontactName 111 Great Street CellPhil 8 Get Out of Here Way WorkBob 7 RUN! Drive HomeOctavio 21 Jump Street CellJohnny