|
Understanding Structured Query Language (SQL)
Adding Data Using INSERT
How do you insert a row of data into a table?
INSERT INTO table (col1, col2, etc) VALUES (value1, value2, etc)
Is the order of the data to be inserted important?
The order of the data MUST match the order of the corresponding columns!
How about multiple rows?
Repeat items in () after VALUES INSERT INTO table (col1, col2, etc) VALUES (value1, value2, etc), (value1, value2, etc), (value1, value2, etc),etc
Do you always have to insert values into all columns?
No ... however if you do not include data for a column which cannot be NULL, you will trigger an error message
What about NOT NULL columns?
Columns marked NOT NULL must have a value, otherwise an error message is triggered
What about defaults assigned when you create the table?
Columns with defaults will be filled automatically if you do not provide data when performing INSERT
What happens if there is a constraint?
You cannot insert a row into the child table before having the related value inserted into the parent table
Performing Advanced Queries Using JOIN
Why would you want to use the JOIN clause?
The JOIN clause is used with the SELECT statement to produce a single consolidated result from two or more tables. Using JOIN is important when you want to give your customers information which is stored across tables.
What are the conditions required for a JOIN?
Each table added to the view needs to have keys in common
What are the most common types of JOIN?
There are many types of JOIN. The most common is the [INNER] JOIN. Also used, but less frequently: LEFT JOIN, RIGHT JOIN, CROSS JOIN and others. We focus only on JOIN in this course
How do you join two tables?
SELECT * FROM table1 JOIN table2 ON table1.key = table2.key
Can you use a WHERE clause with a JOIN?
Yes: just add the WHERE clause at the end
What is meant by an error message indicating a column in the WHERE clause is ambiguous?
You need to add the table name to the column name using a "."
How do you join more than two tables?
SELECT * FROM table1 JOIN table2 ON table2.key = table1.key JOIN table3 ON table3.key = table2.key
What if you only want certain columns?
SELECT table1.col1, table1.col2, table2.col1, table2.col2, table3.col1, table3.col2 FROM table1 AS a JOIN table2 AS b ON b.key = a.key JOIN table3 AS c ON c.key = b.key
Are there any shortcuts when representing table or column names?
SELECT * FROM table1 AS a JOIN table2 AS b ON b.key = a.key JOIN table3 AS c ON c.key = b.key
What about using other clauses and functions?
You can add any desired functions after SELECT key word, and also other clauses at the end, such as ORDER BY, GROUP BY, etc.
LAB: Creating Select, Insert, Update and Delete SQL Statements
What is the purpose of the lab?
In this lab you will be defining a series of SQL statements which will be used in later labs
Create a table "members" ...
Name | Type | Size | Null | And ... |
---|
user_id | int | 8 | N | Primary Key, Auto Increment | name | varchar | 256 | N | Collation: latin1_general_cs | address | varchar | 256 | N | | city | varchar | 64 | N | | state_province | char | 32 | N | | postal_code | char | 10 | N | | country | char | 3 | N | | phone | char | 16 | Y | | balance | decimal | 10,2 | Y | | email | varchar | 250 | N | unique index | password | varchar | 16 | N | index |
Import data ....
- Select the "members" table
- Select "Import"
- Click "Choose File"
- From the WorkingFiles folder for this chapter select sql_members.csv
- Make sure "Format" indicates "CSV"
- Click "Go"
Create SQL statements ...
- Lookup one member if you know the email address
- Add a new member
- Remove a member if you know the email address
- Update member information if you know the email address
- All products sorted by title with a limit of 20 and an offset of 40
- All products on special
- A count of all products
- All products where you know only part of the title or description
- For one member: all purchases including product details
Verify results ...
Select "SQL" and type in your statements Be sure to save these statements in a temporary file as they will be used in other labs!
|
|