Presentation is loading. Please wait.

Presentation is loading. Please wait.

Structured Query Language (SQL) William Klingelsmith

Similar presentations


Presentation on theme: "Structured Query Language (SQL) William Klingelsmith"— Presentation transcript:

1 Structured Query Language (SQL) William Klingelsmith

2 Access Supplemental Content
Supplemental Content Document: Or on the web site under “Student Resources” & “Supplemental Content” Structured Query Language (p12 – 14)

3 Structured Query Language (SQL)
Standard computer language for retrieving and updating data within database systems. Database applications like MS Access, MS SQL, Oracle, DB2, Sybase, MySQL, etc. use SQL. We have been using graphical tools to develop and manipulate database objects thus far…

4 SQL Continued… Access has written the SQL statements behind the scenes for us. The SQL language can be separated into two sub sets: Data Manipulation Language (DML) and the Data Definition Language (DDL). We will look mostly at DML aspects.

5 Data Definition Language (DDL)
The DDL are the SQL commands that define the structure of a database: CREATE TABLE – creates a database table DROPT TABLE – deletes a database table ALTER TABLE – modifies a database table 11/13/2018Date CS101 Sample Presentation

6 Data Manipulation Language (DML)
The DML is SQL Queries (or commands) that will manipulate the data within a database: SELECT– extends or “selects” data from a database table(s) UPDATE– changes or “updates” data from a database table DELETE – removes or “deletes” row(s) from a database table INSERT INTO – adds or “inserts” row(s) of data into a database table 11/13/2018Date CS101 Sample Presentation

7 SELECT <columns> FROM<tables>
SELECT Statement Retrieves all of the data in the fields specified from the specified database table. Syntax is: SELECT <columns> FROM<tables> <columns> is a comma separated list of column names to be returned (AKA Fields) <tables> is the tables where the <columns> are located…

8 Let’s try it the old way first…
Obtain and open Customer_orders.accdb Create a query in design view Add the Customer table only Add all fields Run the query Click “View” and change to “SQL View” to see what was done behind the scenes…

9 SELECT Exercise Close the Query without saving changes
Create a new query in design view. Don’t add any tables Change to “SQL View” Type SELECT * FROM customer Run the query by clicking “!”… Words in capital are “reserved words”

10 SELECT Exercise Now, let’s revise our SQL SELECT…
Go back to SQL View and make it read: SELECT first_name, last_name FROM customer Run the Query We are shown only the fields we requested from the table we told it to use…

11 One more… Say we wanted to have it ALIAS a column for us which combined first and last name field values and separated them with a space… SELECT (First_Name + “ ” +Last_Name) AS [Full Name] FROM customer Run it! We are presented a field that does not actually exist in the table as we gave it as alias.

12 WHERE Clause for Criteria
This allows us to specify criteria at the command line so that only things matching will be returned. The following are legal operators: = <> < > <= >= Between Like

13 WHERE Clause Let’s modify our last select to add criteria to it…
Run it. SELECT (First_Name + " " + Last_Name) AS [Full Name] FROM customer WHERE Last_Name=“Doe” Have a look in regular Design View and widen out the first field. See the alias name and the criteria

14 CS101 Sample Presentation
JOIN Clause Links tables together to form relationships through the tables’ primary and foreign keys There are three main types of joins: INNER JOIN LEFT JOIN RIGHT JOIN 11/13/2018Date CS101 Sample Presentation

15 CS101 Sample Presentation
INNER JOIN Returns all rows from both tables where there is a match and will exclude the rows where a match is NOT made We will now do an INNER JOIN for customer and address tables. These tables are relational based on primary and foreign keys. 11/13/2018Date CS101 Sample Presentation

16 CS101 Sample Presentation
INNER JOIN Exercise We will use the syntax as table.field to specify what tables to pull field values from. Let’s look at the customer and address tables to see the fields called “address_id” in each. . . “address_id” is a primary key to address table and foreign key to customer table. 11/13/2018Date CS101 Sample Presentation

17 CS101 Sample Presentation
INNER JOIN Exercise Let’s say we wanted to join the customers with their addresses to make a multi-table query . . . We would like the following fields [Customer] table first_name last_name [Address] table Street zipcode 11/13/2018Date CS101 Sample Presentation

18 CS101 Sample Presentation
INNER JOIN Exercise We need to create an inner join on the customer and address tables on the address_id common field When expressing the Join, the syntax will include “customer.address_id” and “address.address_id” . . . 11/13/2018Date CS101 Sample Presentation

19 CS101 Sample Presentation
Create the SQL Command Create a query without adding any tables Flip to SQL view and create this command: SELECT customer.first_name, customer.last_name, address.street, address.zipcode FROM customer INNER JOIN address ON Customer.address_id = address.address_id; 11/13/2018Date CS101 Sample Presentation


Download ppt "Structured Query Language (SQL) William Klingelsmith"

Similar presentations


Ads by Google