Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Introduction Part 1. ISD for well structured data relational and other DBMS Info storage (modeling, normalization) Info retrieval (Relational algebra, Calculus, SQL) DB integrated APIs ISD for systems with non-uniformly structured data Basics of web-based IS (www, web2.0, ) Markups, HTML, XML Design tools for Info Sys: UML
Agenda
Structured Query Language (SQL) DB APIs
n m n m
n 1 1 n
SQL as a DDL
A critical element of any design is to store the definitions of its components. In DB design, we deal with tables, using table names, attribute names etc. Each of these terms should have unambiguous syntax and semantics. A systematic way to specify and store these meta-data is by the use of a Data Definition Language The information about the data is stored in a Data Dictionary SQL provides a unified DDL + a Data Manipulation Language (DML).
constraint,
An index is a special file for faster DB look-up, when searching the specified table for some data using the specified attribute.
create table loan ( l_no char(10), amount double, br_name char(30) references branch(b_name), primary key (loan_number) ); BORROWS( cust_ssn, loan_num) create table borrows ( cust_ssn char(11), loan_num char(10), primary key (cust_ssn, loan_num), constraint borrows_c1 foreign key cust_ssn references customer( cssn), constraint borrows_c2 foreign key loan_num references loan( l_no) );
select attribute1, , attribute_n from table_1, , table_m where selection_or_join_condition1, , selection_or_join_condition_r group by attribute_i having aggregate_function( attribute_j, ) order by attribute_k
customer ssn
111-12-0000
amount
1000 2000 1500 500 900 1300
branch_name
Downtown Redwood Pennyridge Mianus Round Hill Pennyridge
branch_name Redwood
Pennyridge
BORROWS
customer 111-12-0000 222-12-0000 333-12-0000 444-00-0000 666-12-0000 111-12-0000 999-12-0000 loan_no L17 L23 L15 L93 L17 L11 L17 L16
select customer, loan.* from borrows, loan where loan_no = loan_number and amount > 1200
777-12-0000
WHERE clause: multiple q-conditions and, or, not comparing cell values: >, =, !=, <, etc.
worker
select E.e_name as worker, M.e_name as boss from employee as E, employee as M where E.mgr_ssn = M.e_ssn
Adams Black
BORROWS
select c_ssn from deposit where c_ssn in ( select customer from borrows)
Notes: in performs a set membership test
BORROWS
select c_ssn from deposit where c_ssn not in ( select customer from borrows)
Notes: not in is true if in is false.
c_ssn
888-12-0000
555-00-0000 888-12-0000 000-12-0000
LOAN
loan_number L17 L23 L15 L93 L11 L16 amount 1000 2000 1500 500 900 1300 branch_name Downtown Redwood Pennyridge Mianus Round Hill Pennyridge
select branch_name from branch where not exists ( select * from loan where branch.branch_name = loan.branch_name)
1. Correlated: where clause of inner query refers to outer query 2. exists is true is there is >= 1 row in evaluating inner query; not exists is true is exists is false
branch_name
Downtown Redwood Pennyridge Mianus Round Hill Pownal
assets (m)
9.0 2.1 1.7 0.4 8.0 0.3 3.7 7.1
select branch_name, avg( amount) as Avg, max( amount) as Max, count( branch_name) as no_loans from loan group by branch_name branch_name Avg Max order by no_loans desc Pennyridge 1400 1500
Downtown Redwood 1000 2000 500 900 1000 2000 500 900
no_loans 2 1 1 1 1
1. Aggregating functions: avg, max, min, sum, count 2. avg/max return average/max for each group
BORROWS
select loan_number, amount, count( loan_number) as no_debtors from loan, borrows where loan_number = loan_no and amount <= 1500 group by loan_number having count(loan_number) >= 2
loan_number L17 1000
amount
no_debtors 3
having conditions are only applied to data after rows have been grouped order by used with group by will be applied to groups.
select c_ssn from deposit where datediff( yy, accessDate, getdate( ) ) > 5
c_ssn
000-12-0000
ac_num A201
accessDate Mar 1, 98
888-12-0000
select ssn, name, street, city from customer where name LIKE J% or street LIKE [^mnp]% or city LIKE %[ ]%
Wildcards: % zero or more chars [asd] match one char out of list [asd] [^asd] matches any one char except a, s, d.
000-12-0000
ssn
name
street
city
111-12-0000
777-12-0000 888-12-0000 999-12-0000
Jones
Adams Johnson Brooks
Main
Spring Alma Senator
Harrison
Pittsfield Palo Alto Brooklyn
BORROWS
customer 111-12-0000 222-12-0000 333-12-0000 444-00-0000 666-12-0000 111-12-0000 999-12-0000 777-12-0000 loan_no L17 L23 L15 L93 L17 L11 L17 L16
CUSTOMER
ssn 111-12-0000 222-12-0000 333-12-0000 444-12-0000 555-12-0000 666-12-0000 777-12-0000 888-12-0000 999-12-0000 000-12-0000 name Jones Smith Hayes Curry Turner Williams Adams Johnson Brooks Lindsay street Main North Main North Putnam city Harrison Rye Harrison Rye Stamford Princeton Pittsfield Palo Alto
800 amount
LOAN Spring
Alma
L17 L23
Nassau
loan_number
Brooklyn
2000
Pittsfield 1500
500 700 1300
BORROWS
customer 111-12-0000 222-12-0000 333-12-0000 444-00-0000 666-12-0000 111-12-0000 999-12-0000 777-12-0000 loan_no L17 L23 L15 L93 L17 L11 L17 L16
CUSTOMER
ssn 111-12-0000 222-12-0000 333-12-0000 444-12-0000 555-12-0000 666-12-0000 777-12-0000 888-12-0000 999-12-0000 name Jones Smith Hayes Curry Turner Williams Adams Johnson Brooks street Main North Main North Putnam Nassau Spring Alma Senator city Harrison Rye Harrison Rye Stamford Princeton Pittsfield Palo Alto Brooklyn banker 321-32-4321 321-32-4321 321-32-4321 333-11-4444 888-99-9999 333-11-4444 123-45-6789 888-99-9999 123-45-6789 b_type CRM CRM CRM LO DO LO LO DO LO
Views in SQL
A view is a virtual table defined on a given Database:
The columns of the view are either (i) columns from some (actual or virtual) table of the DB or (ii) columns that are computed (from other columns)
Main uses of a view:
Views in SQL..
Create a view showing the names of employees, their ssn, telephone number, their manager's name, and how many years they have worked in the bank.
create view bank_employee as select e.e_ssn as ssn, e.e-name as name, e.tel as phone, m.e-name as manager, datediff( yy, start_date, getdate( )) as n_years from EMPLOYEE as e, EMPLOYEE as m where e.mgr_ssn = m.e_ssn select * from bank_employee
ssn 111-22-3333 333-11-4444 123-45-6789 555-66-8888 987-65-4321 888-99-9999 321-32-4321 777-77-7777 name Jones Smith Lee Turner Jones Chan Adams Black phone 12345 54321 54321 55555 87621 87654 77777 99111 manager Adams Jones Jones Adams Chan Black Black null n_years 15 12 12 8 15 30 30 30
Operations on Views
View definition is persistent once you define it, the definition stays permanently in the DB until you drop the view. The DBMS only computes the data in a view when it is referenced in a SQL command (e.g. in a select command) no physical table is stored in the stored memory corresponding to the view. You can use the view in any SQL query just the same as any other table, BUT (1) You cannot modify the value of a computed attribute (2) If an update/delete command is execute, the underlying data in the referenced table of the view is updated/deleted. [this can cause unexpected changes in your DB]
Database APIs
Most people use DBs, but always through some computer program interface (API).
Most DBMSs will provide program libraries (a collection of a set of complied functions) with functions to: - Connect to the DBMS - Select a DB - Send a SQL command, and receive the response in some standard data structure.
Each DBMS provides one library for each programming language. On Windows (and several other) systems, these libraries are called ODBC
odbc (DLL) your code odbc func more code Client App
DBMS DB
Bank tables..
BRANCH
branch_name Downtown Redwood Pennyridge Mianus Round Hill Pownal North Town Brighton city Brooklyn Palo Alto Horseneck Horseneck Horseneck Bennington Rye Brooklyn assets 9000000 2100000 1700000 400000 8000000 300000 3700000 7100000
EMPLOYEE
e_ssn e_name tel start_date mgr_ssn
111-22-3333
333-11-4444 123-45-6789 555-66-8888 987-65-4321 888-99-9999 321-32-4321 777-77-7777
Jones
Smith Lee Turner Jones Chan Adams Black
12345
54321 54321 55555 87621 87654 77777 99111
Nov-2005
Mar-1998 Mar-1998 Aug-2002 Mar-1995 Feb-1980 Feb-1990 Jan-1980
321-32-4321
111-22-3333 111-22-3333 321-32-4321 888-99-9999 777-77-7777 777-77-7777 null
CUSTOMER
ssn 111-12-0000 222-12-0000 333-12-0000 name Jones Smith Hayes street Main North Main city Harrison Rye Harrison banker 321-32-4321 321-32-4321 321-32-4321 b_type CRM CRM CRM
DEPOSIT
c_ssn 888-12-0000 222-12-0000 333-12-0000 ac_num A101 A215 A102 accessDate Jan 1, 09 Feb 1, 09 Feb 28, 09
444-12-0000
555-12-0000 666-12-0000 777-12-0000 888-12-0000 999-12-0000 000-12-0000
Curry
Turner Williams Adams Johnson Brooks Lindsay
North
Putnam Nassau Spring Alma Senator Park
Rye
Stamford Princeton Pittsfield Palo Alto Brooklyn Pittsfield
333-11-4444
888-99-9999 333-11-4444 123-45-6789 888-99-9999 123-45-6789 888-99-9999
LO
DO LO LO DO LO DO
555-00-0000
888-12-0000 111-12-0000 000-12-0000
A305
A201 A217 A101
Mar 10, 09
Mar 1, 98 Mar 1, 09 Feb 25, 09
BORROWS
customer 111-12-0000 loan_no L17 L23 L15 L93 L17 L11 L17 L16
LOAN
loan_number L17 L23 amount 1000 2000 branch_name Downtown Redwood
L15
L93 L11 L16
1500
500 900 1300
Pennyridge
Mianus Round Hill Pennyridge
Not all tables of our normalized design are shown; please create and populate for practice.