Академический Документы
Профессиональный Документы
Культура Документы
○ creating a database
○ creating a table definition
○ changing a table definition
○ adding a primary key or foreign key to a table
All of the above using the SQL sub-set:
– CREATE DATABASE, CREATE TABLE
– ADD PRIMARY KEY
1.8.3.1 Data Definition Language
1. CREATING TABLE
Example
SELECT CustomerName,City
FROM Customers;
1.8.3.2 Data Manipulation Language
2. SELECT *, means to list every field in the table
Example
SELECT *
FROM Customers;
1.8.3.2 Data Manipulation Language
3. SQL WHERE Clause
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Example
SELECT *
FROM Customers
WHERE Country='Mexico';
1.8.3.2 Data Manipulation Language
3.1 Selecting data based on date data type.
Example : Write a query to list all orders placed after January 2017.
SELECT *
FROM orders
WHERE order_date >#02/01/2017#;
Question : Write a query to list all orders placed in the month of February
2017.
Answer
SELECT *
FROM orders
WHERE order_date >=#02/01/2017# AND ORDER_DATE <=#02/28/2017#;
1.8.3.2 Data Manipulation Language
3.2 Selecting data based on numeric data type.
Example : Write a query to list all orders with amounts more than $100.
SELECT *
FROM orders
WHERE order_amount >=100
Question : Write a query to list all products with a packing units of more
than 5.
Answer
SELECT *
from product
where units_per_pack >=5
1.8.3.2 Data Manipulation Language
Operators in The WHERE Clause
The following operators can be used in the WHERE clause:
Operator Description
= Equal
<> Not equal. Note: In some versions of SQL this operator may be
written as !=
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN To specify multiple possible values for a column
1.8.3.2 Data Manipulation Language
4. SQL ORDER BY Keyword
SQL ORDER BY Syntax
SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC, column_name ASC|DESC;
Example 1
SELECT *
FROM Customers
ORDER BY Country;
Example 2
SELECT *
FROM Customers
ORDER BY Country DESC;
Example 3
SELECT *
FROM Customers
ORDER BY Country ASC, Customer_Name DESC;
1.8.3.2 Data Manipulation Language
Selecting data from more than one table : INNER JOIN
1.8.3.2 Data Manipulation Language
5. SQL INNER JOIN Keyword
The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns in both tables.
SQL INNER JOIN Syntax
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;
Example
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID
ORDER BY Customers.CustomerName;
Note: The INNER JOIN keyword selects all rows from both tables as long as there is a match
between the columns. If there are rows in the "Customers" table that do not have matches
in "Orders", these customers will NOT be listed.
1.8.3.2 Data Manipulation Language
Question : Write a query to list all orders placed by customers, sorted in
ascending order of customer name.
SELECT Customers.Customer_Name, Orders.Order_ID
FROM Customers
INNER JOIN Orders
ON Customers.Customer_ID=Orders.Customer_ID
ORDER BY Customers.Customer_Name;
1.8.3.2 Data Manipulation Language
Question : Write a query to list all orders placed by customer 0001.
SELECT *
from customers
inner join orders
on customers.customer_ID =
ORDERS.CUSTOMER_ID
WHERE customers.customer_ID="0001" ;
1.8.3.2 Data Manipulation Language
Question : Write a query to list all products supplied by supplier 0001.
SELECT *
from SUPPLIER
inner join PRODUCT
on SUPPLIER.SUPPLIER_ID =
PRODUCT.SUPPLIER_ID
WHERE SUPPLIER.SUPPLIER_ID="0001" ;
1.8.3.2 Data Manipulation Language
Example : Write a query to list the number of orders placed by each customer.
Example
SELECT customer_id, count(orders.order_id) as TotalNUmberoforders
from orders
group by customer_id;
Question : Write a query to list the number of orders for each product.
Answer
Question : Write a query to enter two additional product information into the product table.
1.8.3.2 Data Manipulation Language
8. SQL DELETE Statement
The DELETE statement is used to delete records in a table.
SQL DELETE Syntax
DELETE FROM table_name
WHERE some_column=some_value;
Notice the WHERE clause in the SQL DELETE statement!
The WHERE clause specifies which record or records that should be deleted.
If you omit the WHERE clause, all records will be deleted!
Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';
Delete All Data
It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
DELETE FROM table_name;
or
DELETE * FROM table_name;
Note: Be very careful when deleting records. You cannot undo this statement!
Example
Example
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';
1.8.3.2 Data Manipulation Language
Past Year Question ( Topical Past Year 1.8)