430 likes | 550 Views
Lecture 8. SQL. PROF. Sin-Min LEE Department of Computer Science. Data Definition. The SQL Data Definition Language (DDL) allows us to create and destroy database objects such as schemas, domains, tables, views, and indexes. The ISO standard also allows the creation of assertions,
E N D
Lecture 8 SQL PROF. Sin-Min LEE Department of Computer Science
Data Definition The SQL Data Definition Language (DDL) allows us to create and destroy database objects such as schemas, domains, tables, views, and indexes. The ISO standard also allows the creation of assertions, character sets, collations and translations.
The main SQL data definition language statements are: • CREATE SCHEMA • DROP SCHEMA • CREATE DOMAIN • ALTER DOMAIN • DROP DOMAIN
CREATE TABLE • ALTER TABLE • DROP TABLE • CREATE VIEW • DROP VIEW While CREATE INDEX and DROP INDEX are provided by DBMS
How is it used ? • As a Data Definition Language • Create the database and its table structure • Create the tables - CREATE TABLE command • Entity integrity • Referential integrity • automatically enforced
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 SELECT AcctNo, Amount FROM ATMWithdrawals WHERE Amount < 50;
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 SELECT AcctNo, Amount FROM ATMWithdrawals WHERE Amount < 50; This is the WHERE clause. The WHERE clause will be evaluated for each record in the table.
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 Is the amount field of this record less than $50? YES! Amount < 50
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 Is the amount field of this record less than $50? NO! Amount < 50 Ignore this record!
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 3 2 101 $40.00 11/1/2000 10:05:00 Is the amount field of this record less than $50? YES! Amount < 50
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 Is the amount field of this record less than $50? YES! Amount < 50
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 Amount < 50 Is the amount field of this record less than $50? NO! Ignore this record!
Query Answer table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 Final Query Answer table AcctNo Amount 102 $25.00 101 $40.00 100 $40.00 SELECT AcctNo, Amount FROM ATMWithdrawals WHERE Amount < 50; Next we consider the attributes listed in the SELECT clause. We throw away all attributes that are not listed in the SELECT clause. Thus the final query answer is:
ATMWithdrawals table TransactionID CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/2000 9:45:00 2 1 102 $150.00 11/10/2000 13:15:00 3 2 101 $40.00 11/1/2000 10:05:00 4 2 100 $40.00 11/1/2000 10:07:00 5 2 100 $200.00 11/8/2000 14:14:00 • Final Query Answer table • AcctNo Amount • $25.00 • 101 $40.00 • 100 $40.00 Given this table: SELECT AcctNo, Amount FROM ATMWithdrawals WHERE Amount < 50; This query Produces this query answer:
ATMWithdrawal TransactionId CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/00 9:45:00 AM 2 1 102 $150.00 11/10/00 1:15:00 PM 3 2 101 $40.00 11/1/00 10:05:00 AM 4 2 100 $40.00 11/1/00 10:07:00 AM 5 2 100 $200.00 11/8/00 2:14:00 PM Another SQL Query (using one table) SELECT * FROM ATMWithdrawal WHERE TransactionId = 3; The five rows are considered, one by one, to see if TransactionId = 3 (to see if the WHERE clause evaluates to true).
ATMWithdrawal TransactionId CustId AcctNo Amount WithdrawalDate 1 1 102 $25.00 11/1/00 9:45:00 AM 2 1 102 $150.00 11/10/00 1:15:00 PM 3 2 101 $40.00 11/1/00 10:05:00 AM 4 2 100 $40.00 11/1/00 10:07:00 AM 5 2 100 $200.00 11/8/00 2:14:00 PM TransactionId CustId AcctNo Amount WithdrawalDate 3 2 101 $40.00 11/1/00 10:05:00 AM SELECT * FROM ATMWithdrawal WHERE TransactionId = 3; Query Answer is:
Third, the SELECT clause tells us which attributes to keep in the query answer. First, the FROM clause tells us the input tables. Second, the WHERE clause is evaluated for all possible combinations from the input tables. How an SQL query is evaluated SELECT AcctNo, Amount FROM ATMWithdrawal WHERE Amount < 50;
SQL query using two tables SELECT C.Name, A.Balance FROM Customer As C, CheckingAccount As A WHERE C.Id = A.Owner and A.Balance > 750; How does this work? Which rows, from which tables, are evaluated in the WHERE clause?
Customer Id Name Phone Address PinNumber 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount AccountNo Owner Balance DateOpened 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 SELECT C.Name, A.Balance FROM Customer C, CheckingAccount A WHERE C.Id = A.Owner and A.Balance > 750; We must check every combination of one row from Customer with one row from CheckingAccount!
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 1 Smith 503 555 1111 xxx Beaverton 1111 100 2 $10,000.00 1/19/00 WHERE C.Id = A.Owner and A.Balance > 750;
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 1 Smith 503 555 1111 xxx Beaverton 1111 101 2 $500.00 7/10/00 WHERE C.Id = A.Owner and A.Balance > 750;
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 1 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 101 2 $500.0 7/10/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 102 1 $1,000.00 8/1/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 3 Wei 503 555 3333 zzz Portland 3333 100 2 $10,000.00 1/19/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 3 Wei 503 555 3333 zzz Portland 3333 101 2 $500.00 7/10/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 3 Wei 503 555 3333 zzz Portland 3333 102 1 $1,000.00 8/1/00 WHERE C.Id = A.Owner and A.Balance > 750; 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Customer 1 Smith 503 555 1111 xxx Beaverton 1111 2 Jones 503 555 2222 yyy Beaverton 2222 3 Wei 503 555 3333 zzz Portland 3333 CheckingAccount 100 2 $10,000.00 1/19/00 101 2 $500.00 7/10/00 102 1 $1,000.00 8/1/00 SELECT C.Name, A.Balance FROM Customer C, CheckingAccount A WHERE C.Id = A.Owner and A.Balance > 750; Input rows that evaluate to true in the WHERE clause: 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00
Input rows that evaluate to true in the WHERE clause: 1 Smith 503 555 1111 xxx Beaverton 1111 102 1 $1,000.00 8/1/00 2 Jones 503 555 2222 yyy Beaverton 2222 100 2 $10,000.00 1/19/00 SELECT C.Name, A.Balance FROM Customer C, CheckingAccount A WHERE C.Id = A.Owner and A.Balance > 750; Final query answer: Smith $1,000.00 Jones $10,000.00