Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SQL Principals
Updates in SQL:
allows users to insert, delete and modify tables
The drop command allows the removal of database components, e.g. DROP TABLE Lecture RESTRICT; Options: restrict - specifies that the command must not be carried out if the component being deleted is not empty. cascade the component is removed together with the components that depend on it.
Constraints in RDBMS
UNIQUE, NOT NULL, CHECK Entity integrity - Primary keys Referential integrity - Foreign keys
Primary Keys
A primary key is a set of columns that together makes each row distinct No two rows will have the same value of this primary key (implies that no two rows can be identical duplicates are disallowed) Every table must have one primary key A primary key should not contain a null value since then it may not be possible to identify some tuples.
Intra-relational constraints1
The simplest intra-relational constraints are NOT NULL, UNIQUE, PRIMARY KEY and CHECK The first three can be included in a table definition on each column or after the column definitions If multiple columns are used to form a composite PRIMARY KEY, the key definition must be included in the table definition after the column definitions.
Foreign Keys
A FOREIGN KEY is an inter-relational constraint that references one or more rows in the table in which it is defined to a primary key (strictly a candidate key) in another table. Every non-NULL value that exists in a foreign key attribute must also exist in the table for which it is the primary key. i.e.:
Inter-relational constraints-2
If a row of a table with a foreign key is updated or deleted violations in the referential constraints can occur. Four response modes can be given to the foreign key: cascade, set null, set default, no action, restrict
10
11
12
SQL Union
Find the branch names for each deposit of customers Smith and Chen that have a balance greater than 200 or less than 100: SELECT DISTINCT branchName FROM Deposit WHERE customerName = Smith OR customerName = Chen AND balance > 200 OR balance < 100; SELECT DISTINCT branchName FROM Deposit WHERE balance NOT BETWEEN 100 AND 200 AND customerName IN (Smith,Chen);
Find the names of all customers who have either a loan or an account at the RHB branch: ( SELECT customerName FROM Deposit WHERE branchName = RHB ) UNION ( SELECT customerName FROM Loan WHERE branchName = RHB );
13
14
SQL Intersection
SQL Minus
Find the names of all customers who have both a loan and an account at the RHB branch ( SELECT customerName FROM Deposit WHERE branchName = RHB ) INTERSECT ( SELECT customerName FROM Loan WHERE branchName = RHB );
Return the rows that are in the first table except those rows that also appear in the second table: ( SELECT customerName FROM Deposit WHERE branchName = RHB ) MINUS ( SELECT customerName FROM Borrow WHERE branchName = RHB );
15
16
Specify which column to join and a conditional clause: SELECT Loan.customerName FROM Loan, Customer WHERE Loan.customerName = Customer.customerName AND Loan.amount > 2000 AND Customer.customerCity = Kuala Lumpur;
An alternative way of writing joins: SELECT * FROM Loan JOIN Customer ON Loan.customerName =Customer.customerName;
17
18
Outer Joins
Preserve unmatched rows from one or both tables LEFT [OUTER] JOIN - preserves unmatched rows from the left table RIGHT [OUTER] JOIN - preserves unmatched rows from the right table FULL [OUTER] JOIN - preserves unmatched rows from both tables Rows that dont match are given NULL values for columns from the other table
19 20
21
22
Find all the customers who have an account at same branch at which Patel has an account (also output the branch names) SELECT T.customerName FROM Deposit S, Deposit T WHERE S.customerName = Patel AND S.branchName = T.branchName;
23
24
Built-In Functions
Operate on the set of values in a column of a relation and return a single value. E.g. SUM([DISTINCT] column-name): sum of values AVG([DISTINCT] column-name): average value MIN(column-name): minimum value MAX(column-name): maximum value COUNT([DISTINCT] column-name): number of values excluding NULLS COUNT(*): number of values in column including NULLS - where DISTINCT means only include values that are different
SELECT customerName FROM Loan WHERE branchName = RHB AND customerName IN (SELECT customerName FROM Deposit WHERE branchName = RHB);
25
26
A sub query can sometimes also be expressed as a join. E.g. find the names of all customers who have both a loan and account at the RHB
SELECT Loan.customerName FROM Loan, Deposit WHERE Loan.branchName = RHB AND Deposit.branchName = RHB AND Loan.customerName = Deposit.customerName;
Find all branches that have greater assets than at least one branch located in Kuala Lumpur
SELECT branchName FROM Branch WHERE assets > ANY (SELECT assets FROM Branch WHERE branchCity = Kuala Lumpur);
27
28
Find the names of all branches that have greater assets than every branches located in Kuala Lumpur
SELECT branchName FROM Branch WHERE assets > ALL (SELECT assets FROM Branch WHERE branchCity = Kuala Lumpur);
Find all customers who DO NOT have an account at a branch named RHB:
SELECT S.customerName FROM Customer S WHERE customerName NOT IN (SELECT customerName FROM Deposit T WHERE branchName = RHB);
29
30
Find all customers who DO NOT have an account at a branch named CIMB:
Provide all loan customers in the RHB branch with a $ 200 saving account. Let the loan number serve as the account number for the new savings account
SELECT S.customerName FROM Customer S WHERE NOT EXISTS (SELECT T.customerName FROM Deposit T WHERE branchName = CIMB AND S.customerName = T.customerName);
INSERT INTO Deposit SELECT customerName, branchName, loanNumber, 200 FROM Loan WHERE branchName = RHB;
31
32
33