2014-09-30 slide 1is 257 – fall 2014 physical database design university of california, berkeley...

71
2014-09-30 SLIDE 1 IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

Upload: aldous-obrien

Post on 19-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 1IS 257 – Fall 2014

Physical Database Design

University of California, Berkeley

School of Information

I 257: Database Management

Page 2: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 2IS 257 – Fall 2014

Lecture Outline

• Review–Introduction to SQL–SQLite

• Physical Database Design• Access Methods

Page 3: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 3IS 257 – Fall 2014

Lecture Outline

• Review–Introduction to SQL–SQLite

• Physical Database Design• Access Methods

Page 4: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 4IS 257 – Fall 2014

SQL - History

• Structured Query Language• SEQUEL from IBM San Jose• ANSI 1992 Standard is the version used

by most DBMS today (SQL92)• Basic language is standardized across

relational DBMSs. Each system may have proprietary extensions to standard.

Page 5: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 5IS 257 – Fall 2014

SQL Uses

• Database Definition and Querying– Can be used as an interactive query language– Can be imbedded in programs

• Relational Calculus combines Select, Project and Join operations in a single command: SELECT

Page 6: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 6IS 257 – Fall 2014

SELECT

• Syntax:– SELECT [DISTINCT] attr1, attr2,…, attr3

FROM rel1 r1, rel2 r2,… rel3 r3 WHERE condition1 {AND | OR} condition2 ORDER BY attr1 [DESC], attr3 [DESC]

Page 7: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 7IS 257 – Fall 2014

SELECT

• Syntax:– SELECT a.author, b.title FROM authors a,

bibfile b, au_bib c WHERE a.AU_ID = c.AU_ID and c.accno = b.accno ORDER BY a.author ;

• Examples in Access...

Page 8: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 8IS 257 – Fall 2014

SELECT Conditions

• = equal to a particular value• >= greater than or equal to a particular value• > greater than a particular value• <= less than or equal to a particular value• <> not equal to a particular value• LIKE “*term*” (may be other wild cards in other

systems)• IN (“opt1”, “opt2”,…,”optn”)• BETWEEN val1 AND val2• IS NULL

Page 9: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 9IS 257 – Fall 2014

Using an Aggregate Function

• SELECT DIVECUST.Name, Sum([Price]*[qty]) AS Total FROM (DIVECUST INNER JOIN DIVEORDS ON

DIVECUST.[Customer No] = DIVEORDS.[Customer No]) INNER JOIN DIVEITEM ON DIVEORDS.[Order No] = DIVEITEM.[Order No]

GROUP BY DIVECUST.Name HAVING (((DIVECUST.Name) Like "*Jazdzewski"));

Page 10: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 10IS 257 – Fall 2014

Sorting

• SELECT BIOLIFE.[Common Name], BIOLIFE.[Length (cm)]

FROM BIOLIFE

ORDER BY BIOLIFE.[Length (cm)] DESC;

Note: the square brackets are not part of the standard,But are used in Access for names with embedded blanks

Page 11: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 11IS 257 – Fall 2014

Subqueries

• SELECT SITES.[Site Name], SITES.[Destination no]

FROM SITES

WHERE sites.[Destination no] IN (SELECT [Destination no] from DEST where [avg temp (f)] >= 78);

• Can be used as a form of JOIN.

Page 12: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 12IS 257 – Fall 2014

Aggregate Functions

• Count• Avg• SUM• MAX• MIN• Others may be available in different

systems

Page 13: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 13IS 257 – Fall 2014

Using Aggregate functions

• SELECT attr1, Sum(attr2) AS name FROM tab1, tab2 ...

GROUP BY attr1, attr3 HAVING condition;

Page 14: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 14IS 257 – Fall 2014

GROUP BY

• SELECT DEST.[Destination Name], Count(*) AS Expr1

FROM DEST INNER JOIN DIVEORDS ON DEST.[Destination Name] = DIVEORDS.Destination

GROUP BY DEST.[Destination Name]

HAVING ((Count(*))>1);• Provides a list of Destinations with the

number of orders going to that destination

Page 15: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 15IS 257 – Fall 2014

SQL Commands

• Data Definition Statements– For creation of relations/tables…

Page 16: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 16IS 257 – Fall 2014

Create Table

• CREATE TABLE table-name (attr1 attr-type PRIMARY KEY, attr2 attr-type,…,attrN attr-type);

• Adds a new table with the specified attributes (and types) to the database.

Page 17: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 17

INSERT

• INSERT INTO table-name (col1, col2, col3, …, colN) VALUES (val1, val2, val3,…, valN);

• INSERT INTO table-name (col1, col2, col3, …, colN) SELECT…

• Column list is optional, if omitted assumes all columns in table definition and order

IS 257 – Fall 2014

Page 18: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 18IS 257 – Fall 2014

Creating a new table from existing tables

• Access and PostgreSQL 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]

Page 19: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 19IS 257 – Fall 2014

How to do it in MySQLmysql> SELECT * FROM foo;+---+| n |+---+| 1 |+---+

mysql> CREATE TABLE bar (m INT AUTO_INCREMENT PRIMARY KEY) AS SELECT DISTINCT n FROM foo;Query OK, 1 row affected (0.02 sec)Records: 1 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM bar;+------+---+| m | n |+------+---+| 1 | 1 |+------+---+

Page 20: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 20

SQLite3

• Light-weight implementation of a relational DBMS (~340Kb)– Includes most of the features of full DBMS– Intended to be imbedded in programs

• Available on iSchool servers and for other machines as open source

• Used as the data manager in iPhone apps and Firefox (among many others)

• Databases are stored as files in the OS

IS 257 – Fall 2014

Page 21: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 21

SQLite3 Data types

• SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container

• Types are:– NULL: The value is a NULL value.– INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8

bytes depending on the magnitude of the value– REAL: The value is a floating point value, stored as an 8-byte IEEE

floating point number.– TEXT. The value is a text string, stored using the database encoding

(UTF-8, UTF-16BE or UTF-16LE). (default max 1,000,000,000 chars)– BLOB. The value is a blob of data, stored exactly as it was input.

IS 257 – Fall 2014

Page 22: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 22

SQLite3 Command line[dhcp137:~] ray% sqlite3 test.dbSQLite version 3.6.22Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> .tablessqlite> create table stuff (id int, name varchar(30),address varchar(50));sqlite> .tablesstuffsqlite> insert into stuff values (1,'Jane Smith',"123 east st.");sqlite> select * from stuff;1|Jane Smith|123 east st.sqlite> insert into stuff values (2, 'Bob Jones', '234 west st.');sqlite> insert into stuff values (3, 'John Smith', '567 North st.');sqlite> update stuff set address = "546 North st." where id = 1;sqlite> select * from stuff;1|Jane Smith|546 North st.2|Bob Jones|234 west st.3|John Smith|567 North st.

IS 257 – Fall 2014

Page 23: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 23

Wildcard searchingsqlite> select * from stuff where name like '%Smith%';1|Jane Smith|546 North st.3|John Smith|567 North st.sqlite> select * from stuff where name like 'J%Smith%';1|Jane Smith|546 North st.3|John Smith|567 North st.sqlite> select * from stuff where name like 'Ja%Smith%';1|Jane Smith|546 North st.sqlite> select * from stuff where name like 'Jones';sqlite> select * from stuff where name like '%Jones';2|Bob Jones|234 west st.sqlite> select name from stuff ...> ;Jane SmithBob JonesJohn Smithsqlite>

IS 257 – Fall 2014

Page 24: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 24

Create backups

sqlite> .dumpPRAGMA foreign_keys=OFF;BEGIN TRANSACTION;CREATE TABLE stuff (id int, name varchar(30),address varchar(50));INSERT INTO "stuff" VALUES(1,'Jane Smith','546 North st.');INSERT INTO "stuff" VALUES(2,'Bob Jones','234 west st.');INSERT INTO "stuff" VALUES(3,'John Smith','567 North st.');COMMIT;sqlite> .schemaCREATE TABLE stuff (id int, name varchar(30),address varchar(50));

IS 257 – Fall 2014

Page 25: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 25

Creating Tables from Tablessqlite> create table names as select name, id from stuff;sqlite> .schemaCREATE TABLE names(name TEXT,id INT);CREATE TABLE stuff (id int, name varchar(30),address varchar(50));sqlite> select * from names;Jane Smith|1Bob Jones|2John Smith|3sqlite> create table names2 as select name as xx, id as key from stuff;sqlite> .schemaCREATE TABLE names(name TEXT,id INT);CREATE TABLE names2(xx TEXT,"key" INT);CREATE TABLE stuff (id int, name varchar(30),address varchar(50));sqlite> drop table names2;sqlite> .schemaCREATE TABLE names(name TEXT,id INT);CREATE TABLE stuff (id int, name varchar(30),address varchar(50));

IS 257 – Fall 2014

Page 26: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 26

Using SQLite3 from Python

• SQLite is available as a loadable python library– You can use any SQL commands to create,

add data, search, update and delete

IS 257 – Fall 2014

Page 27: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 27

SQLite3 from Python[dhcp137:~] ray% pythonPython 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import sqlite3>>> sqlite3.version'2.3.2’>>> sqlite3.sqlite_version'3.3.14'>>>

IS 257 – Fall 2014

Page 28: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 28

SQLite3 from Python[dhcp137:~] ray% pythonPython 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) [GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import sqlite3 as lite>>> import sys>>> con = None>>> try:... con = lite.connect('newtest.db')... cur = con.cursor()... cur.execute('SELECT SQLITE_VERSION()')... data = cur.fetchone()... print "SQLite version: %s" % data... except lite.Error, e:... print "Error %s:" % e.args[0]... sys.exit(1)... finally:... if con:... con.close()... <sqlite3.Cursor object at 0x46eb90>SQLite version: 3.3.14>>> IS 257 – Fall 2014

Page 29: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 29

SQLite3 from Python#!/usr/bin/python2.7# -*- coding: utf-8 -*- import sqlite3 as lite import sys # our data is defined as a tuple of tuples…cars = (

(1, 'Audi', 52642), (2, 'Mercedes', 57127), (3, 'Skoda', 9000), (4, 'Volvo', 29000), (5, 'Bentley', 350000), (6, 'Hummer', 41400), (7, 'Volkswagen', 21600)

) con = lite.connect(’newtest.db') with con:

cur = con.cursor() cur.execute("DROP TABLE IF EXISTS Cars") cur.execute("CREATE TABLE Cars(Id INT, Name TEXT, Price INT)") cur.executemany("INSERT INTO Cars VALUES(?, ?, ?)", cars)

IS 257 – Fall 2014

Page 30: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 30

Another Example#!/usr/bin/python # -*- coding: utf-8 -*- import sqlite3 as lite import sys

con = lite.connect(':memory:')

with con: cur = con.cursor() cur.execute("CREATE TABLE Friends(Id INTEGER PRIMARY KEY,

Name TEXT);") cur.execute("INSERT INTO Friends(Name) VALUES ('Tom');") cur.execute("INSERT INTO Friends(Name) VALUES ('Rebecca');") cur.execute("INSERT INTO Friends(Name) VALUES ('Jim');") cur.execute("INSERT INTO Friends(Name) VALUES ('Robert');")

lid = cur.lastrowid print "The last Id of the inserted row is %d" % lid

IS 257 – Fall 2014

Page 31: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 31

Retrieving Data#!/usr/bin/python # -*- coding: utf-8 -*-

import sqlite3 as lite import sys

#connect to the cars database…con = lite.connect(’newtest.db')

with con: cur = con.cursor() cur.execute("SELECT * FROM Cars") rows = cur.fetchall() for row in rows:

print row

ray% python2.7 retrnewtest.py(1, u'Audi', 52642)(2, u'Mercedes', 57127)(3, u'Skoda', 9000)(4, u'Volvo', 29000)(5, u'Bentley', 350000)(6, u'Hummer', 41400)(7, u'Volkswagen', 21600)(8, u'Citroen', 21000)ray%

IS 257 – Fall 2014

Page 32: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 32

Updating data

cur.execute("UPDATE Cars set Price = 450000 where Name = 'Bentley'")

cur.execute("SELECT * FROM Cars") rows = cur.fetchall() for row in rows:

print row

(1, u'Audi', 52642)(2, u'Mercedes', 57127)(3, u'Skoda', 9000)(4, u'Volvo', 29000)(5, u'Bentley', 450000)(6, u'Hummer', 41400)(7, u'Volkswagen', 21600)(8, u'Citroen', 21000)ray%

IS 257 – Fall 2014

Page 33: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 33

Add another row…

[dhcp137:~] ray% python2.7 Python 2.7.2 (default, Oct 11 2012, 20:14:37) [GCC 4.2.1 Compatible Apple Clang 4.0 …>>> import sqlite3 as lite>>> import sys>>> >>> con = lite.connect(’newtest.db')>>> >>> with con:... cur = con.cursor()... cur.execute("INSERT INTO Cars VALUES(8,'Citroen',21000)")... <sqlite3.Cursor object at 0x107fafc00>>>>

IS 257 – Fall 2014

Page 34: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 34

From the SQLite3 command line[dhcp137:~] ray% sqlite3 newtest.dbSQLite version 3.6.22Enter ".help" for instructionsEnter SQL statements terminated with a ";"sqlite> select * from cars;1|Audi|526422|Mercedes|571273|Skoda|90004|Volvo|290005|Bentley|3500006|Hummer|414007|Volkswagen|216008|Citroen|21000sqlite>

INSERT more data…sqlite> select * from cars;1|Audi|526422|Mercedes|571273|Skoda|90004|Volvo|290005|Bentley|4500006|Hummer|414007|Volkswagen|216008|Citroen|2100010|Audi|5100011|Mercedes|5500012|Mercedes|5630013|Volvo|3150014|Volvo|3100015|Audi|5200017|Hummer|4240016|Hummer|42400

IS 257 – Fall 2014

Page 35: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 35

Use Aggregates to summarize data

#!/usr/bin/python2.7# -*- coding: utf-8 -*-import sqlite3 as liteimport sys

con = lite.connect('newtest.db')with con:

cur = con.cursor() cur.execute("SELECT Name, AVG(Price)

FROM Cars GROUP BY Name") rows = cur.fetchall() for row in rows:

print row

ray% python2.7 aggnewtest.py(u'Audi', 51880.666666666664)(u'Bentley', 450000.0)(u'Citroen', 21000.0)(u'Hummer', 42066.666666666664)(u'Mercedes', 56142.333333333336)(u'Skoda', 9000.0)(u'Volkswagen', 21600.0)(u'Volvo', 30500.0)

IS 257 – Fall 2014

Page 36: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 36IS 257 – Fall 2014

Database Design Process

ConceptualModel

LogicalModel

External Model

Conceptual requirements

Conceptual requirements

Conceptual requirements

Conceptual requirements

Application 1

Application 1

Application 2 Application 3 Application 4

Application 2

Application 3

Application 4

External Model

External Model

External Model

Internal Model

PhysicalDesign

Page 37: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 37IS 257 – Fall 2014

Physical Database Design

• Many physical database design decisions are implicit in the technology adopted– Also, organizations may have standards or an

“information architecture” that specifies operating systems, DBMS, and data access languages -- thus constraining the range of possible physical implementations.

• We will be concerned with some of the possible physical implementation issues

Page 38: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 38IS 257 – Fall 2014

Physical Database Design

• The primary goal of physical database design is data processing efficiency

• We will concentrate on choices often available to optimize performance of database services

• Physical Database Design requires information gathered during earlier stages of the design process

Page 39: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 39IS 257 – Fall 2014

Physical Design Information

• Information needed for physical file and database design includes:– Normalized relations plus size estimates for them– Definitions of each attribute– Descriptions of where and when data are used

• entered, retrieved, deleted, updated, and how often

– Expectations and requirements for response time, and data security, backup, recovery, retention and integrity

– Descriptions of the technologies used to implement the database

Page 40: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 40IS 257 – Fall 2014

Physical Design Decisions

• There are several critical decisions that will affect the integrity and performance of the system– Storage Format– Physical record composition– Data arrangement– Indexes– Query optimization and performance tuning

Page 41: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 41IS 257 – Fall 2014

Storage Format

• Choosing the storage format of each field (attribute). The DBMS provides some set of data types that can be used for the physical storage of fields in the database

• Data Type (format) is chosen to minimize storage space and maximize data integrity

Page 42: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 42IS 257 – Fall 2014

Objectives of data type selection

• Minimize storage space• Represent all possible values• Improve data integrity• Support all data manipulations• The correct data type should, in minimal

space, represent every possible value (but eliminate illegal values) for the associated attribute and can support the required data manipulations (e.g. numerical or string operations)

Page 43: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 43IS 257 – Fall 2014

Access Data Types (Not MySQL)

• 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)

Page 44: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 44IS 257 – Fall 2014

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

Page 45: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 45IS 257 – Fall 2014

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

Page 46: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 46IS 257 – Fall 2014

MySQL Data Types

• MySQL supports all of the standard SQL numeric data types. These types include the exact numeric data types (INTEGER, SMALLINT, DECIMAL, and NUMERIC), as well as the approximate numeric data types (FLOAT, REAL, and DOUBLE PRECISION). The keyword INT is a synonym for INTEGER, and the keyword DEC is a synonym for DECIMAL

• Numeric (can also be declared as UNSIGNED)– TINYINT (1 byte)– SMALLINT (2 bytes)– MEDIUMINT (3 bytes)– INT (4 bytes)– BIGINT (8 bytes)– NUMERIC or DECIMAL– FLOAT – DOUBLE (or DOUBLE PRECISION)

Page 47: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 47IS 257 – Fall 2014

MySQL Data Types

• The date and time types for representing temporal values are DATETIME, DATE, TIMESTAMP, TIME, and YEAR. Each temporal type has a range of legal values, as well as a “zero” value that is used when you specify an illegal value that MySQL cannot represent– DATETIME '0000-00-00 00:00:00'– DATE '0000-00-00'– TIMESTAMP (4.1 and up) '0000-00-00 00:00:00'– TIMESTAMP (before 4.1) 00000000000000– TIME '00:00:00'– YEAR 0000

Page 48: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 48IS 257 – Fall 2014

MySQL Data Types

• The string types are CHAR, VARCHAR, BINARY, VARBINARY, BLOB, TEXT, ENUM, and SET

• Maximum length for CHAR is 255 and for VARCHAR is 65,535

• VARCHAR uses 1 or 2 bytes for the length• For longer things there is BLOB and TEXT

Page 49: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 49IS 257 – Fall 2014

MySQL Data Types

• A BLOB is a binary large object that can hold a variable amount of data.

• The four BLOB types are TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. These differ only in the maximum length of the values they can hold

• The four TEXT types are TINYTEXT, TEXT, MEDIUMTEXT, and LONGTEXT. These correspond to the four BLOB types and have the same maximum lengths and storage requirements

• TINY=1byte, BLOB and TEXT=2bytes, MEDIUM=3bytes, LONG=4bytes

Page 50: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 50IS 257 – Fall 2014

MySQL Data Types

• BINARY and VARBINARY are like CHAR and VARCHAR but are intended for binary data of 255 bytes or less

• ENUM is a list of values that are stored as their addresses in the list– For example, a column specified as ENUM('one', 'two', 'three')

can have any of the values shown here. The index of each value is also shown:

• Value = Index• NULL = NULL• ‘’ = 0• 'one’ = 1• ‘two’ = 2• ‘three’ = 3

– An enumeration can have a maximum of 65,535 elements.

Page 51: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 51IS 257 – Fall 2014

MySQL Data Types

• The final string type (for this version) is a SET• A SET is a string object that can have zero or more

values, each of which must be chosen from a list of allowed values specified when the table is created.

• SET column values that consist of multiple set members are specified with members separated by commas (‘,’)

• For example, a column specified as SET('one', 'two') NOT NULL can have any of these values: – '' – 'one' – 'two' – 'one,two‘

• A set can have up to 64 member values and is stored as an 8byte number

Page 52: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 52IS 257 – Fall 2014

Controlling Data Integrity

• Default values• Range control• Null value control• Referential integrity (next time)• Handling missing data

Page 53: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 53IS 257 – Fall 2014

Designing Physical Records

• A physical record is a group of fields stored in adjacent memory locations and retrieved together as a unit

• Fixed Length and variable fields

Page 54: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 54IS 257 – Fall 2014

Designing Physical/Internal Model

• Overview• terminology• Access methods

Page 55: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 55IS 257 – Fall 2014

Physical Design

• Internal Model/Physical Model

OperatingSystem

Access Methods

DataBase

User request

DBMSInternal ModelAccess Methods

External Model

Interface 1

Interface 3

Interface 2

Page 56: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 56IS 257 – Fall 2014

Physical Design

• Interface 1: User request to the DBMS. The user presents a query, the DBMS determines which physical DBs are needed to resolve the query

• Interface 2: The DBMS uses an internal model access method to access the data stored in a logical database.

• Interface 3: The internal model access methods and OS access methods access the physical records of the database.

Page 57: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 57IS 257 – Fall 2014

Physical File Design

• A Physical file is a portion of secondary storage (disk space) allocated for the purpose of storing physical records

• Pointers - a field of data that can be used to locate a related field or record of data

• Access Methods - An operating system algorithm for storing and locating data in secondary storage

• Pages - The amount of data read or written in one disk input or output operation

Page 58: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 58IS 257 – Fall 2014

Lecture Outline

• Review–Introduction to SQL–SQLite

• Physical Database Design• Access Methods

Page 59: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 59IS 257 – Fall 2014

Internal Model Access Methods

• Many types of access methods:– Physical Sequential– Indexed Sequential– Indexed Random– Inverted– Direct– Hashed

• Differences in – Access Efficiency– Storage Efficiency

Page 60: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 60IS 257 – Fall 2014

Physical Sequential

• Key values of the physical records are in logical sequence

• Main use is for “dump” and “restore”• Access method may be used for storage

as well as retrieval• Storage Efficiency is near 100%• Access Efficiency is poor (unless fixed

size physical records)

Page 61: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 61IS 257 – Fall 2014

Indexed Sequential

• Key values of the physical records are in logical sequence

• Access method may be used for storage and retrieval

• Index of key values is maintained with entries for the highest key values per block(s)

• Access Efficiency depends on the levels of index, storage allocated for index, number of database records, and amount of overflow

• Storage Efficiency depends on size of index and volatility of database

Page 62: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 62IS 257 – Fall 2014

Index Sequential

Data File

Block 1

Block 2

Block 3

AddressBlockNumber

1

2

3

ActualValue

Dumpling

Harty

Texaci

...

AdamsBecker

Dumpling

GettaHarty

MobileSunociTexaci

Page 63: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 63IS 257 – Fall 2014

Indexed Sequential: Two Levels

Address

7

8

9

Key Value

385

678

805

001003

.

.150

705710

.

.785

251..

385

455480

.

.536

605610

.

.678

791..

805

Address

1

2

Key Value

150

385

Address

3

4

Key Value

536

678

Address

5

6

Key Value

785

805

Page 64: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 64IS 257 – Fall 2014

Indexed Random

• Key values of the physical records are not necessarily in logical sequence

• Index may be stored and accessed with Indexed Sequential Access Method

• Index has an entry for every data base record. These are in ascending order. The index keys are in logical sequence. Database records are not necessarily in ascending sequence.

• Access method may be used for storage and retrieval

Page 65: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 65IS 257 – Fall 2014

Indexed Random

AddressBlockNumber

2

1

3

2

1

ActualValue

Adams

Becker

Dumpling

Getta

Harty

BeckerHarty

AdamsGetta

Dumpling

Page 66: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 66IS 257 – Fall 2014

Btree

F | | P | | Z |

R | | S | | Z |H | | L | | P |B | | D | | F |

Devils

AcesBoilersCars

MinorsPanthers

Seminoles

Flyers

HawkeyesHoosiers

Page 67: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 67IS 257 – Fall 2014

Inverted

• Key values of the physical records are not necessarily in logical sequence

• Access Method is better used for retrieval• An index for every field to be inverted may

be built• Access efficiency depends on number of

database records, levels of index, and storage allocated for index

Page 68: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 68IS 257 – Fall 2014

Inverted

AddressBlockNumber

1

2

3

ActualValue

CH 145

CS 201

CS 623

PH 345

CH 145101, 103,104

CS 201102

CS 623

105, 106

Adams

Becker

Dumpling

Getta

Harty

Mobile

Studentname

CourseNumber

CH145

cs201

ch145

ch145

cs623

cs623

Page 69: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 69IS 257 – Fall 2014

Direct

• Key values of the physical records are not necessarily in logical sequence

• There is a one-to-one correspondence between a record key and the physical address of the record

• May be used for storage and retrieval• Access efficiency always 1• Storage efficiency depends on density of

keys• No duplicate keys permitted

Page 70: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 70IS 257 – Fall 2014

Hashing

• Key values of the physical records are not necessarily in logical sequence

• Many key values may share the same physical address (block)

• May be used for storage and retrieval• Access efficiency depends on distribution of

keys, algorithm for key transformation and space allocated

• Storage efficiency depends on distibution of keys and algorithm used for key transformation

Page 71: 2014-09-30 SLIDE 1IS 257 – Fall 2014 Physical Database Design University of California, Berkeley School of Information I 257: Database Management

2014-09-30 SLIDE 71IS 257 – Fall 2014

Comparative Access Methods

IndexedNo wasted space for databut extra space for index

Moderately Fast

Moderately FastVery fast with multiple indexesOK if dynamic OK if dynamic

Easy but requiresMaintenance ofindexes

FactorStorage spaceSequential retrieval on primary keyRandom Retr.Multiple Key Retr.Deleting records

Adding records

Updating records

SequentialNo wasted space

Very fast

ImpracticalPossible but needsa full scancan create wasted spacerequires rewriting fileusually requires rewriting file

Hashedmore space needed foraddition and deletion ofrecords after initial load

Impractical

Very fast

Not possiblevery easy

very easy

very easy