Download presentation
Presentation is loading. Please wait.
Published byVictor Wiggins Modified over 9 years ago
1
-- Introduction to database principles Maoying Wu (bi203.sjtu@gmail.com)bi203.sjtu@gmail.com March 11, 2013 Chapter 3: Basic SQL
2
Outline Data Definition Language (DDL) Basic Query Structure Set Operations Aggregate functions Null values Nested subqueries Complex queries Views Modification of the Database Join Operations
3
Data definition language (DDL) The schema for each relation ( 关系模式 ) The domain of values associated with each attribute ( 列类型 ) Integrity constraints ( 完整性约束 ) The set of indices to be maintained for each relation ( 索引 ) Security and authorization information for each relation ( 权 限 ) The physical storage structure for each relation ( 存储引擎 )
4
Data Types in SQL char(n): Fixed length character string, with user-specified length n varchar(n): Variable length character string with user-specified maximum length n int: Integer (a finite subset of the integers that is machine-dependent) smallint: Small integer (a machine-dependent subset of the integer domain type) numeric(p, d): Fixed point number, with user-specified precision of p digits, with d digits to the right of decimal point real, double precision: floating point and double-precision floating point number, with machine-dependent precision float(n): Floating point number, with user-specified precision of at least n digits To get more details, see the help page for data types in MySQL
5
Create table CREATE TABLE r(A 1 D 1, A 2 D 2, …, A n D n, [integrity-constraint 1], … [integrity-constraint k]); r is the name of the relation each A i is an attribute name in the schema for relation r D i is the data type for the domain for attribute A i
6
Integrity constraints NOT NULL PRIMARY KEY (A 1, …, A k )
7
DROP TABLE DROP TABLE r; Delete all information for relation r from the database
8
ALTER TABLE ALTER TABLE r ADD A k D k ; Add attributes to an existing relation All tuples in the relation are assigned null as the values for the added attribute ALTER TABLE r DROP A; Drop attributes of a relation not supported by many DBMS
9
Basic Queries SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: SELECT A 1, A 2, …, A n FROM r 1, r 2, …, r m WHERE P; This query is equivalent to the relational algebra expression:
10
The SELECT Clause The SELECT clause lists the attributes for the result of a query Projection operation in relational algebra Keyword DISTINCT can be used to eliminate the duplicate records SELECT DISTINCT loan_number FROM loan; Keyword ALL can be used to preserve all the duplicate records SELECT ALL loan_number FROM loan;
11
SELECT Clause (Cont.) “*”means “all attributes”: SELECT * FROM loan; Arithmetic expressions (+,-,*,/) can be involved in the expression following SELECT SELECT loan_number, amount*100 FROM loan;
12
The WHERE Clause WHERE clause specifies conditions the result must satisfy corresponding to the selection predicate of the relation algebra Logical operations like AND, OR and NOT can be used to combine multiple conditional expressions SELECT loan_number FROM loan WHERE branch_name=‘Shanghai’ AND amount>1200;
13
WHERE Clause BETWEEN can be used to specified the range SELECT loan_number FROM loan WHERE amount BETWEEN 90000 AND 100000; SELECT loan_number FROM loan WHERE (amount > 90000) AND (amount > 100000);
14
FROM Clause FROM specifies the relations involved in the query corresponding to the Cartesian product operations of the relational algebra SELECT * FROM borrower, loan; Find the name, loan_number and loan amount of all customers having a loan at Shanghai branch: SELECT customer_name, borrower.loan_number, amount FROM borrower, loan WHERE borrower.loan_number = loan.loan_number and branch_name=‘Shanghai’;
15
AS Clause Synopsis: old_name AS new_name corresponding to the renaming operation in relational algebra: SELECT customer_name, borrower.loan_number AS loan_id, amount FROM borrower, loan WHERE borrower.loan_number = loan.loan_number; SELECT customer_name, T.loan_number, S.amount FROM borrower AS T, loan AS S WHERE T.loan_number = S.loan_number;
16
string operations SQL includes a string-matching operator for comparisons on character string: LIKE %: matches any string _: matches any character Find the names of all customers whose street includes the substring “Main”: SELECT customer_name FROM customer WHERE customer_street LIKE ‘%MAIN%’; If a percentage sign is included in a string, escape character can be used: LIKE ‘MAIN\%’; NOTE: Many more functions can be used in string operations.
17
ORDER BY: Ordering the results SELECT DISTINCT customer_name FROM borrower, loan WHERE borrower.loan_number = loan.loan_number AND branch_name=‘Shanghai’ ORDER BY customer_name; The default ordering rule is ascending, but we can also use ‘DESC’ to specify the descending order: ORDER BY customer_name DESC;
18
Set Operations Set operations UNION, INTERSECT and EXCEPT correspond to the relational algebra, ∩,and -. In MySQL, NO INTERSECT and EXCEPT but we can use IN and NOT IN as an alternative.
19
Set operations Find all customers with a loan, an account, or both: (SELECT customer_name FROM depositor) UNION (SELECT customer_name FROM borrower); Find all customers who have both loan and account (SELECT customer_name FROM depositor) INTERSECT (SELECT customer_name FROM borrower); Find all customers who have an account but no loan (SELECT customer_name FROM depositor) INTERSECT (SELECT customer_name FROM borrower);
20
Aggregate functions AVG: average value MIN: minimum value MAX: maximum value SUM: sum of values COUNT: number of values
21
Aggregate functions: examples Find the average account balance at the Shanghai branch: SELECT avg(balance) FROM account WHERE branch_name = ‘Shanghai’; Find the number of tuples in the customer relation SELECT count(*) FROM customer; Find the number of depositors in the bank SELECT count(distinct customer_name) FROM depositor;
22
Aggregate: GROUP BY Find the number of depositors for each branch: SELECT branch_name, count(distinct customer_name) FROM depositor, account WHERE depositor.account_number=account.account_number GROUP BY branch_name; NOTE : The attribute names after the GROUP BY should appear after SELECT, while outside the aggregate functions.
23
HAVING Qualifying Results by Categories Find the names of all branches where the average account balance is more than 1200 SELECT branch_name, avg(balance) FROM account GROUP BY branch_name HAVING avg(balance) > 1200; Distinctions between HAVING and WHERE Predicates in HAVING are applied after forming GROUPS, while predicates in WHERE are applied before forming GROUPS.
24
NULL values NULL signifies an unknown value or a value that does not exist. The predicate IS NULL can be used to check for NULL values. SELECT loan_number FROM loan WHERE amount IS NULL; The result of any arithmetic expression involving NULL is NULL 1+NULL returns NULL Many aggregate functions ignore NULL values
25
NULL (cont.) Any comparison with NULL returns UNKNOWN LOGIC involving UNKNOWN: OR (UNKNOWN OR true) = true, (UNKNOWN OR false) = UNKNOWN, (UNKNOWN OR UNKNOWN) = UNKNOWN AND (true AND UNKNOWN) = UNKNOWN, (false AND UNKNOWN) = false, (UNKNOWN AND UNKNOWN) = UNKNOWN NOT (NOT UNKNOWN) = UNKNOWN Any UNKNOWN in WHERE is treated as false IS UNKNOWN is used to evaluate if the expression is UNKNOWN.
26
NULL value in aggregate Get the total all loan amounts: SELECT SUM(amount) FROM loan; Above statement ignores all NULL amounts Result is NULL is all is NULL. All aggregate operation except count(*) ignore tuples with NULL values on the aggregated attributes.
27
Nested subqueries SQL provides a mechanism for the nesting of subqueries A subquery is a SELECT-FROM-WHERE expression that is nested within another query A common use of subqueries is to perform tests for set membership, set comparison, and set cardinality
28
Example subqueries Find all customers who have both an account and a loan at the bank. SELECT distinct customer_name FROM borrower WHERE customer_name in (SELECT customer_name FROM depositor); Find all customers who have a loan at the bank but do not have an account at the bank SELECT distinct customer_name FROM borrower WHERE customer_name NOT IN (SELECT customer_name FROM depositor);
29
Example subqueries Find all customers who have both an account and a loan at the Shanghai branch SELECT distinct customer_name FROM borrower, loan WHERE borrower.loan_number = loan.loan_number AND branch_name = ‘Shanghai' and (branch_name, customer_name ) IN (SELECT branch_name, customer_name FROM depositor, account WHERE depositor.account_number = account.account_number ); NOTE: The formulation above is simply to illustrate SQL features.
30
Set comparisons SOME ( 至少存在其中之一 ) Find all branches that have more assets than some branch located in Beijing SELECT distinct T.branch_name FROM branch as T, branch as S WHERE T.assets > S.assets and S.branch_city = 'Beijing' ; Same query SELECT branch_name FROM branch WHERE assets > SOME(SELECT assets FROM branch WHERE branch_city = 'Beijing') ;
31
Set comparisons ALL ( 所有的都 ) Find all branches that have more assets than all branch located in Beijing SELECT branch_name FROM branch WHERE assets > ALL (SELECT assets FROM branch WHERE branch_city = 'Beijing') ;
32
Test for empty results EXISTS returns true if the argument subquery is non-empty EXISTS(select-clause) NOT EXISTS(select-clause)
33
Example query Find all customers who have an account at all branches located in Beijing. SELECT DISTINCT S.customer_name FROM depositor as S WHERE NOT EXISTS ( (SELECT branch_name FROM branch WHERE branch_city = 'Beijing') and branch_name NOT IN(SELECT R.branch_name FROM depositor as T, account as R WHERE T.account_number = R.account_number and S.customer_name = T.customer_name )); Note that
34
WITH Clause The WITH clause provides a way of defining a temporary view whose definition is available only to the query in which the WITH clause occurs. Find all accounts with the maximum balance WITH max_balance (value) AS SELECT max(balance) FROM account SELECT account_number FROM account, max_balance WHERE account.balance = max_balance.value;
35
Views ( 视图 ) Consider a person who needs to know a customer’s name, loan number and branch name, but has no need to see the loan amount. This person should see a relation described, in SQL, by (SELECT customer_name, borrower.loan_number, branch_name FROM borrower, loan WHERE borrower.loan_number = loan.loan_number ) A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view
36
Views: Definition A view is defined using the CREATE VIEW statement which has the form CREATE VIEW v AS is any legal SQL expression. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. When a view is created, the query expression is stored in the database; the expression is substituted into queries using the view.
37
Example using view A view consisting of branches and their customers: CREATE VIEW all_customer AS (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number ) union (select branch_name, customer_name from borrower, loan where borrower.loan_number = loan.loan_number ); Find all customers of the Shanghai branch from view select customer_name from all_customer where branch_name = ‘Shanghai' ;
38
DELETE Delete all accounts at the Guangzhou branch DELETE FROM account WHERE branch_name = ‘Guangzhou’; Delete all accounts at every branch located in Shanghai. DELETE FROM account WHERE branch_name in (SELECT branch_name FROM branch WHERE branch_city=‘Shanghai’);
39
INSERT Add a new account INSERT INTO account VALUES (‘A-9732’,’Minhang’, 12500); INSERT INTO account(branch_name, balance, account_number) VAlUES (‘Minhang’, 12500, ‘A-9732’); INSERT INTO account VAlUES (‘A-777’, ‘Minhang’, NULL);
40
INSERT: Example Gifts for customers INSERT INTO account select loan_number, branch_name, 200 from loan where branch_name = ‘Jiaoda‘; INSERT INTO depositor select customer_name, loan_number from loan, borrower where branch_name = ‘Jiaoda' and loan.account_number = borrower.account_number;
41
UPDATE Increase all accounts with balances over 10,000 by 6%, all other accounts receive 5% increase UPDATE account SET balance = balance * 1.06 WHERE balance > 10000; UPDATE account SET balance = balance * 1.05 WHERE balance <= 10000; The order is important Better using the CASE…WHEN statement
42
UPDATE (Cont.) CASE WHEN…THEN statement UPDATE account SET balance = CASE WHEN balance <= 10000 THEN balance*1.05 ELSE balance*1.06 END;
43
JOIN INNER JOIN LEFT OUTER JOIN RIGHT OUTER JOIN FULL OUTER JOIN
44
Relations: borrower and loan loan borrower borrower information missing for L-260; Loan information missing for L-155
45
JOIN: Examples (1) loan inner join borrower on loan.loan_number = borrower.loan_number loan left outer join borrower on loan.loan_number = borrower.loan_number;
46
JOIN: Examples (2) loan natural inner join borrower loan natural right outer join borrower
47
JOIN: Examples (3) loan full outer join borrower using (loan_number) Find all customers who have either an account or a loan (but not both) at the bank: SELECT customer_name FROM (depositor natural full outer join borrower) WHERE account_number is NULL or loan_number is NULL;
48
bank: schema
49
Tables: loan and borrower
50
Table: customer, account, depositor
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.