SQL continued CMSC 461 Michael Wilson. Right back into it Recap: Learned how to log in to...
-
Upload
wilfred-floyd -
Category
Documents
-
view
218 -
download
1
Transcript of SQL continued CMSC 461 Michael Wilson. Right back into it Recap: Learned how to log in to...
SQL continuedCMSC 461Michael 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 <column1, column2, column3> FROM <table_name1>, <table_name2>WHERE <conditions>
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 <column> <op>
<column/constant/formula> <op> 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 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
Complete select example SELECT *
FROM AddressBookWHERE daysSinceLastContact > 20
Complete select exampleaddress daysSinceConta
ctcontactPhoneNumber
numberType contactName
7 RUN! Drive 700 555 8888 Cell Octavio21 Jump Street
40 123 4567 Cell Johnny
Complete select example SELECT contactName
FROM AddressBookWHERE phoneType = ‘Cell’
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
Complete select example SELECT contactName
FROM AddressBookWHERE phoneType = ‘Cell’ and contactName = ‘Octavio’
Complete select exampleaddress daysSinceConta
ctcontactPhoneNumber
numberType contactName
7 RUN! Drive 700 555 8888 Cell Octavio
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 <table>, <table>SET <column> = <value>, <column> = <value> …WHERE <where-clause>
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 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
Update example UPDATE AddressBook
SET phoneNumber = ‘444 7777’WHERE contactName = ‘Phil’
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
Update example UPDATE AddressBook
SET daysSinceLastContact = 0, phoneType = ‘Home’WHERE contactName = ‘Octavio’
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