CS 80: Internet Programming
Instructor: Mark Edmonds
Authors
,
AuthorISBN
, and Titles
.SELECT
FROM
WHERE
Example for the Books database:
/* query AuthorID and LastName columns from Authors table */
SELECT AuthorID, LastName
FROM Authors;
Gets the AuthorID and LastName from the Authors table
A more complicated example:
/* query with constraint that copyright is greater than 2010 */
SELECT Title, EditionNumber, Copyright
FROM Titles
WHERE Copyright > '2010';
*
) may be used to
select all columns, e.g. SELECT * FROM
Titles
selects all columns from the Titles
tableWHERE Copyright > '2010'
line
above uses an operator >
<, >, <=, >=,
=, !=
and LIKE
LIKE
indicates a pattern matching
qualification on a query%
) indicates a search
for zero or more characters at the percent character's
position (a wildcard)_
) indicates a single
wildcard at the position of the underscoreSelect all authors whose last name begins with a 'D'
/* query with last names that start with 'D' */
SELECT AuthorID, FirstName, LastName
FROM Authors
WHERE LastName LIKE 'D%';
Select all authors whose last name starts with any character, followed by an 'o', followed by any number of additional characters
/* query with last names that have a second character of 'o' */
SELECT AuthorID, FirstName, LastName
FROM Authors
WHERE LastName LIKE '_o%';
ORDER BY
Can order by multiple columns - sorts by outer sort first, then each additional sorting specified (e.g. last name then by first name)
/* query and order by ascending order */
SELECT columnName1, columnName2, ...
FROM tableName
ORDER BY column ASC;
/* query and order by descending order */
SELECT columnName1, columnName2, ...
FROM tableName
ORDER BY column DESC;
Solution:
/* query and order by descending order */
SELECT AuthorID, FirstName, LastName
FROM Authors
ORDER BY LastName DESC;
Solution:
/* query that ends with 'How to Program' and sort by ascending order */
SELECT ISBN, Title, EditionNumber, Copyright
FROM Titles
WHERE Title LIKE '%How to Program'
ORDER BY Title ASC;
INNER JOIN
which
merges rows from two tables by matching values in columns
that are common to both tables
/* query that merges data from multiple tables */
SELECT columnName1, columnName2, ...
FROM table1
INNER JOIN table2
ON table1.columnName = table2.columnName;
Solution:
/* query that merges data from multiple tables */
SELECT FirstName, LastName, ISBN
FROM Authors
INNER JOIN AuthorISBN
ON Authors.AuthorID = AuthorISBN.AuthorID
ORDER BY LastName, FirstName;
Note: the use of Authors.AuthorID
and
AuthorISBN.AuthorID
is called a
qualified name. When tables have
matching column names, we must be more specific by
stating which table we want to use with each column
(think about this: why we didn't have to quality the
ISBN
in the SELECT
part of
the query?)
Insert inserts a row into a table
Syntax:
/* insertion syntax */
INSERT INTO tableName ( columnName1, columnName2, ..., columnNameN )
VALUES ( value1, value2, ..., valueN );
Example:
/* insertion example */
INSERT INTO Authors ( FirstName, LastName )
VALUES ( 'Sue', 'Red' );
Update modifies existing data in a table
Syntax:
/* update syntax */
UPDATE tableName
SET columnName1 = value1, columnName2 = value2, ..., columnNameN = valueN
WHERE criteria;
Example:
/* update example */
UPDATE Authors
SET LastName = 'Black'
WHERE LastName = 'Red' AND FirstName = 'Sue';
WHERE
portion of the previous example?Question: What else could we have used for the
WHERE
portion of the previous example?
/* update example */
UPDATE Authors
SET LastName = 'Black'
WHERE AuthorID = 6;
Removes a row from a table
Syntax:
/* delete syntax */
DELETE FROM tableName
WHERE criteria;
Example:
/* delete syntax */
DELETE FROM Authors
WHERE LastName = 'Black' AND FirstName = 'Sue';