SQL continued CMSC 461 Michael Wilson. Right back into it Recap: Learned how to log in to...

27
SQL continued CMSC 461 Michael Wilson

Transcript of SQL continued CMSC 461 Michael Wilson. Right back into it Recap: Learned how to log in to...

Page 1: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

SQL continuedCMSC 461Michael Wilson

Page 2: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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

Page 3: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Select statement This is how you get data out of tables This is the great majority of most

applications SQL calls Basic syntax:

SELECT <column1, column2, column3> FROM <table_name1>, <table_name2>WHERE <conditions>

Page 4: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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…

Page 5: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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

Page 6: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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

Page 7: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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 <column> <op>

<column/constant/formula> <op> is =, !=, >, >=, <, <=

Can also use <> instead of !=

Page 8: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Predicate examples address = ‘21 Jump Street’ age >= 21 bloodType = ‘O+’

Page 9: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Where clauses You can string predicates together using

AND or OR

Page 10: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Stringing together predicates age > 21 AND bloodType = ‘O+’ daysSinceContact > 50 or

myOpinionOfThisPerson = ‘Favorable’

Page 11: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

111 Great Street

1 555 5555 Cell Phil

8 Get Out of Here Way

7 555 7777 Work Bob

7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street

40 123 4567 Cell Johnny

Page 12: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select example SELECT *

FROM AddressBookWHERE daysSinceLastContact > 20

Page 13: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street

40 123 4567 Cell Johnny

Page 14: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select example SELECT contactName

FROM AddressBookWHERE phoneType = ‘Cell’

Page 15: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

111 Great Street

1 555 5555 Cell Phil

7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street

40 123 4567 Cell Johnny

Page 16: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select example SELECT contactName

FROM AddressBookWHERE phoneType = ‘Cell’ and contactName = ‘Octavio’

Page 17: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Complete select exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

7 RUN! Drive 700 555 8888 Cell Octavio

Page 18: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Predicate formulas You can also use formulas in predicates

Example age < (20 + 5)

Why this is useful will make more sense later, however

Page 19: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update statement This will update specific values in

already existing rows in your table Basic syntax:

UPDATE <table>, <table>SET <column> = <value>, <column> = <value> …WHERE <where-clause>

Page 20: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update statement Table selection

You can select multiple tables during one update

Let’s focus on the single table case for now

Page 21: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

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

Page 22: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update statement Where clause

This where clause functions exactly the same as it does with a select statement

Page 23: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

111 Great Street

1 555 5555 Cell Phil

8 Get Out of Here Way

7 555 7777 Work Bob

7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street

40 123 4567 Cell Johnny

Page 24: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update example UPDATE AddressBook

SET phoneNumber = ‘444 7777’WHERE contactName = ‘Phil’

Page 25: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

111 Great Street

1 444 7777 Cell Phil

8 Get Out of Here Way

7 555 7777 Work Bob

7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street

40 123 4567 Cell Johnny

Page 26: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update example UPDATE AddressBook

SET daysSinceLastContact = 0, phoneType = ‘Home’WHERE contactName = ‘Octavio’

Page 27: SQL continued CMSC 461 Michael Wilson. Right back into it  Recap:  Learned how to log in to PostgreSQL  Learned about PostgreSQL data types  Learned.

Update exampleaddress daysSinceConta

ctcontactPhoneNumber

numberType contactName

111 Great Street

1 444 7777 Cell Phil

8 Get Out of Here Way

7 555 7777 Work Bob

7 RUN! Drive 0 555 8888 Home Octavio21 Jump Street

40 123 4567 Cell Johnny