SQL, Databases, Etc - University of Illinois at Chicago

Download Report

Transcript SQL, Databases, Etc - University of Illinois at Chicago

Databases & SQL
By: Julia Tartakovsky
CS 340, Fall 2004
Acknowledgement (for help with the J# code
connecting to an Oracle DB): Yosef Lifshits
1
Database Management System
(DBMS)
A DBMS is:
– A collection of interrelated data (a database), and
– A set of programs to access the data
DBMS provides an environment that is both convenient
and efficient to use.
Database Applications:
–
–
–
–
–
–
Banking: all transactions
Airlines: reservations, schedules
Universities: registration, grades
Sales: customers, products, purchases
Manufacturing: production, inventory, orders, supply chain
Human resources: employee records, salaries, tax deductions
2
Purpose of Database System
In the early days, database applications were built on top of file systems
– We are managing data in this way even today
– E.g. using Windows Explorer to find your files
Drawbacks of using file systems to store data:
– Data redundancy and inconsistency
Multiple file formats, duplication of information in different files
– Difficulty in accessing data
Need to write a new program to carry out each new task
– Data isolation — multiple files and formats
– Integrity problems
Integrity constraints (e.g. account balance > 0) become part of
program code
Hard to add new constraints or change existing ones
3
Purpose of Database Systems
(Cont.)
Drawbacks of using file systems (cont.)
– Atomicity of updates
Failures may leave database in an inconsistent state with partial
updates carried out
E.g. transfer of funds from one account to another should either
complete or not happen at all
– Concurrent access by multiple users
Concurrent accessed needed for performance
Uncontrolled concurrent accesses can lead to inconsistencies
– E.g. two people reading a balance and updating it at the same
time
– Security problems
Database systems offer solutions to all the above problems
4
Application Architectures
Two-tier architecture: E.g. client programs using ODBC/JDBC to
communicate with a database
Three-tier architecture: E.g. web-based applications, and
applications built using “middleware”
5
Levels of Abstraction
Physical level describes how a record (e.g., customer) is stored.
Logical level: describes data stored in database, and the
relationships among the data.
type customer = record
name : string;
street : string;
city : integer;
end;
View level: application programs hide details of data types. Views
can also hide information (e.g., salary) for security purposes.
6
View of Data
An architecture for a database system
7
Schemas and Instances
Analogous to types and variables in programming languages
Schema – the logical structure of the database
– e.g., the database consists of information about a set of customers and
accounts and the relationship between them
– Analogous to type information of a variable in a program
– Physical schema: database design at the physical level
– Logical schema: database design at the logical level
Instance – the actual content of the database at a particular point in time
– Analogous to the value of a variable
Physical Data Independence – the ability to modify the physical schema
without changing the logical schema
– Applications depend on the logical schema
– In general, the interfaces between various levels and components
should be well defined so that changes in some parts do not seriously
influence others.
8
Data Models
A collection of conceptual tools for describing
–
–
–
–
data
data relationships
data semantics
data constraints
Entity-Relationship model
Relational model
Other models:
– object-oriented model
– Object-relational model
– semi-structured data models, XML
9
Entity-Relationship Model
Example of schema in the entity-relationship model:
10
Entity Relationship Model (Cont.)
E-R model of real world
– Entities (objects)
E.g. customers, accounts
– Relationships between entities
E.g. Account A-101 is held by customer Johnson
Relationship set depositor associates customers with
accounts
Widely used for database design
– Database design in E-R model usually converted to
design in the relational model (coming up next) which
is used for storage and processing
11
Relational Model
A collection of tables
Example of tabular data in the relational model
Customerid
customername
192-83-7465
Johnson
019-28-3746
Smith
192-83-7465
Johnson
321-12-3123
Jones
019-28-3746
Smith
customerstreet
customercity
Attributes
accountnumber
Alma
Palo Alto
A-101
North
Rye
A-215
Alma
Palo Alto
A-201
Main
Harrison
A-217
North
Rye
A-201
12
A Sample Relational Database
13
What is SQL?
SQL (pronounced "ess-que-el") stands for Structured Query
Language.
SQL is used to communicate with a database.
SQL statements are used to perform tasks such as:
– update data on a database (an action query)
– retrieve data from a database (a select query)
14
What is SQL? (cont.)
Relational database management systems that use SQL:
–
–
–
–
–
Oracle
Sybase
Microsoft SQL Server
Access
Ingres
Standard SQL commands:
–
–
–
–
–
–
"Select“
"Insert"
"Update“
"Delete“
"Create“
"Drop"
15
SQL
SQL: widely used non-procedural language
– E.g. find the name of the customer with customer-id
192-83-7465
select customer.customer-name
from customer
where customer.customer-id = ‘192-83-7465’
– E.g. find the balances of all accounts held by the
customer with customer-id 192-83-7465
select account.balance
from depositor, account
where depositor.customer-id = ‘192-83-7465’ and
depositor.account-number = account.account-number
16
SELECT
For more examples and an interactive environment, go to:
http://sqlcourse.com/select.html
SELECT cust omer_id, name
CUST OMER
FROM
WHERE birth_year>=1975
C US TO MER
cu stom e r i d n am e
birth ye ar
addre ss
RESULT
1
John
1975
Kowloon
2
Smit h
1955
Kowloon
1
John
3
Joan
1980
Hong Kong
3
Joan
4
Bobby
1976
Hong Kong
4
Bobby
5
Shirley
1974
Hong Kong
custome r id name
17
SELECT with “distinct”
Eliminate identical
records.
RES ULT
SELECT
FROM
address
CUST OMER
addre ss
Kowloon
C US TO MER
cu stom e r id
n am e
bi rth ye ar
Kowloon
addre ss
Hong Kong
Hong Kong
1
John
1975
Kowloon
2
Smit h
1955
Kowloon
3
Joan
1980
Hong Kong
4
Bobby
1976
Hong Kong
5
Shirley
1974
Hong Kong
Hong Kong
SELECT di sti nct address
CUST OMER
FROM
RESULT
addre ss
Kowloon
Hong Kong
18
Aggregate with distinct
C US TO MER
cu stom e r id
n am e
1
John
bi rth ye ar
addre ss
1975
Kowloon
2
Smit h
1955
Kowloon
3
Joan
1980
Hong Kong
4
Bobby
1976
Hong Kong
5
Shirley
1974
Hong Kong
For more examples and an interactive
environment, go to:
SELECT
FROM
count (address)
CUST OMER
ANSW ER: 5
SELECT count(distinct address)
CUST OMER
FROM
ANSW ER: 2
http://sqlcourse2.com/agg_functions.html
19
Group-by
Grouping tuples with identical attributes.
RES ULT - GRO UP BY cu stom e r_id
AC C O UNT
accoun t idcu stom e r id
A1
A2
A3
A4
A5
type
1
saving
1
check
2
3
3
saving
saving
check
balance
accoun t i dcu stom e r id
type
A1
1
saving
20k
A2
1
check
5k
type
balance
saving
35k
type
balance
20k
5k
35k
100k
balance
accoun t i d cu stom e r id
A3
2
accoun t i d cu stom e r id
30k
A4
3
saving
100k
A5
3
check
30k
For more examples and an interactive
environment, go to:
http://sqlcourse2.com/groupby.html
20
Summarized information by groups
GRO UP BY cu stom e r_id
SELECT customer_id, COUNT (*)
accoun t i dcu stom e r id
FROM
ACCOUNT
A1
1
GROUP BY customer_id
A2
1
accoun t i d cu stom e r id
A3
2
accoun t i d cu stom e r id
RESULT
type
balance
saving
20k
check
5k
type
balance
saving
35k
type
balance
A4
3
saving
100k
A5
3
check
30k
cu stome r id C O UNT
1
2
2
1
3
2
21
Join
AC C O UNT
C US TO MER
cu stom e r id
SELECT
FROM
WHERE
*
CUST OMER, ACCOUNT
CUST OMER.customer_id =
ACCOUNT .cust omer_id
The result table
removes
meaningless
tuples from the
cartesian product.
n am e
1
John
2
Smit h
3
Joan
accoun t i dcu stom e r id balance
A1
1
20k
A2
1
5k
A3
2
35k
A4
3
100k
JOIN
RES ULT
AC C O UNT
C US TO MER
cu stom e r id
n am e
accoun t idcu stom e r id balance
1
John
A1
1
20k
1
John
A2
1
5k
2
Smit h
A3
2
35k
3
Joan
A4
3
23
100k
Join with group-by and having
JOIN
RES ULT
SELECT
FROM
WHERE
*
CUST OMER, ACCOUNT
CUST OMER.customer_id =
ACCOUNT .cust omer_id
AC C O UNT
C US TO MER
cu stom e r id
CUST OMER.name,
sum(ACCOUNT .balance)
FROM
CUST OMER, ACCOUNT
WHERE CUST OMER.customer_id =
ACCOUNT .cust omer_id
GROUP BY CUST OMER.name
HAVING C O UNT(*)=2
SELECT
n am e
1
John
A1
1
20k
1
John
A2
1
5k
2
Smit h
A3
2
35k
3
Joan
A4
3
100k
C US TO MERAC C O UNT
n am e
balan ce
John
20k
John
5k
n am e
Smith
QUESTION: What is the meaning of this query?
accoun t idcu stom e r id balance
JO IN RESULT
balan ce
name
SUM
John
25k
35k
n am e
balan ce
Joan
100k
24
CREATE Table
CREATE TABLE table-name (attr1 attr-type
PRIMARYKEY, attr2 attr-type,…,attrN attr-type);
Adds a new table with the specified attributes
(and types) to the database.
For more examples and an interactive
environment, go to:
http://sqlcourse.com/create.html
25
Access Data Types
Numeric (1, 2, 4, 8 bytes, fixed or float)
Text (255 max)
Memo (64000 max)
Date/Time (8 bytes)
Currency (8 bytes, 15 digits + 4 digits decimal)
Autonumber (4 bytes)
Yes/No (1 bit)
OLE (limited only by disk space)
Hyperlinks (up to 64000 chars)
26
Access Numeric types
Byte
– Stores numbers from 0 to 255 (no fractions). 1 byte
Integer
– Stores numbers from –32,768 to 32,767 (no fractions) 2
bytes
Long Integer (Default)
– Stores numbers from –2,147,483,648 to 2,147,483,647 (no
fractions). 4 bytes
Single
– Stores numbers from -3.402823E38 to –1.401298E–45 for
negative values and from 1.401298E–45 to 3.402823E38
for positive values.
4 bytes
Double
– Stores numbers from –1.79769313486231E308 to –
4.94065645841247E–324 for negative values and from
1.79769313486231E308 to 4.94065645841247E–324 for
positive values.
15
8 bytes
Replication ID
– Globally unique identifier (GUID)
N/A
16 bytes
27
Oracle Data Types
CHAR (size) -- max 2000
VARCHAR2(size) -- up to 4000
DATE
DECIMAL, FLOAT, INTEGER, INTEGER(s),
SMALLINT, NUMBER, NUMBER(size,d)
– All numbers internally in same format…
LONG, LONG RAW, LONG VARCHAR
– up to 2 Gb -- only one per table
BLOB, CLOB, NCLOB -- up to 4 Gb
BFILE -- file pointer to binary OS file
28
Creating a new table from existing tables
Syntax:
– SELECT [DISTINCT] attr1, attr2,…, attr3
INTO newtablename FROM rel1 r1, rel2 r2,…
rel3 r3 WHERE condition1 {AND | OR}
condition2 ORDER BY attr1 [DESC], attr3
[DESC]
29
ALTER Table
ALTER TABLE table-name ADD COLUMN
attr1 attr-type;
… DROP COLUMN attr1;
Adds a new column to an existing
database table.
30
INSERT
INSERT INTO table-name (attr1, attr4, attr5,…,
attrK) VALUES (“val1”, val4, val5,…, “valK”);
Adds a new row(s) to a table.
INSERT INTO table-name (attr1, attr4, attr5,…,
attrK) VALUES SELECT ...
For more examples and an interactive
environment, go to:
http://sqlcourse.com/insert.html
31
DELETE
DELETE FROM table-name WHERE
<where clause>;
Removes rows from a table.
For more examples and an interactive
environment, go to:
http://sqlcourse.com/delete.html
32
UPDATE
UPDATE tablename SET attr1=newval,
attr2 = newval2 WHERE <where clause>;
changes values in existing rows in a table
(those that match the WHERE clause).
For more examples and an interactive
environment, go to:
http://sqlcourse.com/update.html
33
DROP Table
DROP TABLE tablename;
Removes a table from the database.
For more examples and an interactive
environment, go to:
http://sqlcourse.com/drop.html
34
J# Source Code Connecting to an Oracle DB
package juliadb1;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
// ODP.NET Oracle managed provider
/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/**
* Clean up any resources being used.
*/
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}
#region Windows Form Designer generated code
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
35
Screenshot of J# Program Output
36
Questions?
For a neat SQL tutorial and an
interactive environment, go to:
http://sqlcourse2.com/intro2.html
37