21
SQL Review Tonga Institute of Higher Education

SQL Review

  • Upload
    medea

  • View
    32

  • Download
    0

Embed Size (px)

DESCRIPTION

SQL Review. Tonga Institute of Higher Education. SQL Introduction. SQL (Structured Query Language) a language that allows a developer to work with data in a database. - PowerPoint PPT Presentation

Citation preview

Page 1: SQL Review

SQL Review

Tonga Institute of Higher Education

Page 2: SQL Review

SQL Introduction SQL (Structured Query Language) a language

that allows a developer to work with data in a database.

This presentation serves to provide a basic review of SQL statements. Advanced concepts are covered in IT244 – Database Management Systems

All examples will use the Northwind Access database. This database is available from www.microsoft.com.

Page 3: SQL Review

Working with Data in a Database

SQL answers 4 common questions:

How do I get data from a database? SELECT How do I add new data to a database? INSERT How do I change data in a database? UPDATE How do I delete data from a database? DELETE

Page 4: SQL Review

SELECT Statement Basics

How do I get data from a database? Use SELECT!

SELECT <Column Names> FROM <Table Name> WHERE <Criteria>

Returns data from the columns of the rows that match the criteria.

Page 5: SQL Review

SELECT Column Names

Specify the column names that you wish to receive data from.

Multiple column names should be separated by commas. SELECT CompanyName, ContactName, Phone FROM Customers

An asterick (*) can be used to get data from all the columns. SELECT * From Customers

Page 6: SQL Review

SELECT Criteria

Criteria can be added to get specific rows of data. For example, this code returns rows that include

CompanyNames from Customers that have a contact name Maria.

SELECT CompanyName FROM Customers WHERE ContactName = ‘Maria’

In this case, nothing is returned because there is no customer with the Contact Name of exactly ‘Maria’

Page 7: SQL Review

SELECT Criteria Wildcards Criteria wildcards can be used to

find rows that contains certain pieces of data.

For example, this code returns all CompanyNames from Customers that INCLUDE a contact name Maria.

SELECT CompanyName FROM Customers WHERE ContactName like ‘%Maria%’

Returns 2 rows both include Maria in the ContactName column.

Any characters can be after Maria

Any characters can be before Maria

Often % is used for wildcardsSometimes * is used for wildcards

Page 8: SQL Review

SELECT Summary Using the SELECT statement, we can query our

database. For example, to get all the column data from all

rows with a contact name that includes Maria, use this code:

SELECT * FROM Customers WHERE ContactName like ‘%Maria%’

Page 9: SQL Review

Comprehension Check

SELECT Statement

Page 10: SQL Review

INSERT Statement Basics

How do I add new data to a database? Use INSERT!

INSERT INTO <Table Name> (<Column Names>) VALUES (<Data>)

Inserts a new row with data in the columns specified.

Page 11: SQL Review

INSERT Data

INSERT INTO Customers (CustomerID, CompanyName) VALUES (‘TICO’, ‘Ti Company and Associates’)

Each column name to the left of VALUES matches a piece of data to the right.

Use commas to separate each column name and piece of data.

String require single quotes. Sometimes, double quotes may be used.

Make sure you insert data for required columns.

Page 12: SQL Review

INSERT Summary Using the INSERT statement, we can insert new

rows into our database. For example, to add a new customer with the ID

TICO and name of Ti Company and Associates, use this code:

INSERT INTO Customers (CustomerID, CompanyName) VALUES (‘TICO’, ‘Ti Company and Associates’)

Page 13: SQL Review

Comprehension Check

INSERT Statement

Page 14: SQL Review

UPDATE Statement Basics

How do I change data in a database? Use UPDATE!

UPDATE <Table Name> SET <Column Name> = ‘<New Value>’ WHERE <Criteria>

Updates rows that match the criteria by changing the data in the columns specified to the new value.

Page 15: SQL Review

UPDATE Details

UPDATE Customers SET ContactName = ‘Sione Tukuia’, Phone = ‘13533’ WHERE CustomerID = ‘ANATR’

Use commas to separate each data update. String require single quotes. Sometimes, double

quotes may be used. Make sure the criteria is correct. If no criteria is

included, then all data will be updated.

Page 16: SQL Review

UPDATE Summary Using the UPDATE statement, we can update

data in our database. For example, to change the contact name and

phone number of the customer with ID ANATR, use the following code:

UPDATE Customers SET ContactName = ‘Sione Tukuia’, Phone = ‘13533’ WHERE CustomerID = ‘ANATR’

Page 17: SQL Review

Comprehension Check

UPDATE Statement

Page 18: SQL Review

DELETE Statement Basics How do I delete data from a database? Use

DELETE!

DELETE FROM <Table Name> WHERE <Criteria>

Deletes rows that match the criteria.

DELETE FROM Customers WHERE ContactName like ‘%Maria%’

Page 19: SQL Review

DELETE Details

DELETE FROM Customers WHERE CustomerID = ‘ANATR’

Make sure the criteria is correct. If no criteria is included, then all data will be deleted.

Page 20: SQL Review

DELETE Summary

Using the DELETE statement, we can delete data in our database.

For example, to delete the customer with ID ‘ANATR’, use the following code:

DELETE FROM Customers WHERE CustomerID = ‘ANATR’

Page 21: SQL Review

Comprehension Check

DELETE Statement