Post on 31-Mar-2015
2
DDAATTAABBAASSEE
Goal: Build a Business Application
Tools:Database DesignSQL (queries)Programming
Des
ign
SQ
L
Pro
gram
Des
ign
SQ
L
Pro
gram
Best:
Worst:
3
DDAATTAABBAASSEE
DBMS: Database Management System
DatabaseA collection of data stored in a standardized format,
designed to be shared by multiple users.
Database Management SystemSoftware that defines a database, stores the data, supports
a query language, produces reports, and creates data entry screens.
4
DDAATTAABBAASSEE
Drawbacks of old File methods
Uncontrolled DuplicationWastes spaceHard to update all files
Inconsistent data Inflexibility
Hard to change dataHard to change programs
Limited data sharing Poor enforcement of standards Poor programmer productivity Excessive program maintenance
5
DDAATTAABBAASSEE
Advantages of Database Approach
Minimal data redundancy. Data consistency. Integration of data. Sharing of data. Enforcement of standards. Ease of application development. Data independence.
6
DDAATTAABBAASSEE
Database Management Approach Data is most important
Data defined firstStandard format
Access through DBMSQueries, Reports, FormsApplication Programs3GL Interface
Data independenceChange data definition
without changing codeAlter code without
changing dataMove/split data without
changing code
All Data
DBMS
Program1 Program2QueriesReports
7
DDAATTAABBAASSEE
Modifying Data with DBMS
Add cell number to employee tableOpen table definitionAdd data element If desired, modify reports
Use report writerNo programming
Existing reports, queries, code will all run as before with no changes.
Field Name Data Type Description
EmployeeID Number Autonumber..TaxpayerID Text Federal IDLastName TextFirstName Text . . . Phone Text . . .
CellPhone Text Cellular . . .
8
DDAATTAABBAASSEE
DBMS Components Database engine
StorageRetrievalUpdate
Query Processor Data dictionary Utilities Security
Report writer Forms generator (input
screens) Application generator Communications 3GL Interface
9
DDAATTAABBAASSEE
DBMS Engine, Security, Utilities
DataTables
DatabaseEngine
ProductItemID Description887 Dog food946 Cat food
OrderOrderID ODate9874 3-3-979888 3-9-97
CustomerCustomerID Name1195 Jones2355 Rojas
ProductItemID Integer, UniqueDescription Text, 100 char
CustomerCustomerID Integer, UniqueName Text, 50 char
SecurityUser IdentificationAccess Rights
Utilities
Concurrency andLock Manager
Backup andRecovery
Administration
DataDictionary
13
DDAATTAABBAASSEE
DBMS Report WriterAll Data
Database EngineData Dictionary
Query Processor
Report Writer
ReportFormat
and Query
15
DDAATTAABBAASSEE
DBMS Input FormsAll Data
Database EngineData Dictionary
Query Processor
Form Builder
InputForm
Design
16
DDAATTAABBAASSEE
DBMS Components
All Data
Database EngineData Dictionary
Security
Query Processor
FormBuilder
ReportWriter
CommunicationNetwork
3GLConnector
Program
ApplicationGenerator
17
DDAATTAABBAASSEE
Examples of Commercial Systems
Oracle Ingres Informix (Unix) DB2, SQL/DS (IBM) Access (Microsoft) SQL Server (Microsoft +) Many older (Focus, IMS, ...) Many limited PC (dBASE, Paradox, …)
18
DDAATTAABBAASSEE
Hierarchical DatabaseCustomers
Orders
Item Description Quantity998 Dog Food 12764 Cat Food 11
Items
Customer
Order
Items Ordered
To retrieve data, you must start at the top (customer). When you retrieve a customer, you retrieve all nested data.
20
DDAATTAABBAASSEE
Relational Database
Customer(CustomerID, Name, …
Order(OrderID, CustomerID, OrderDate, …
ItemsOrdered(OrderID, ItemID, Quantity, …
Items(ItemID, Description, Price, …
21
DDAATTAABBAASSEE
Why don’t all developers use a DBMS?
Most new projects (in last 5 years) do use a DBMS Need specialized personnel
ProgrammersDesigners/AnalystsDatabase administrators
Need to define data for organization Cost
PC: $400 - $2000Large: $100,000 +
22
DDAATTAABBAASSEE
DBMS approach
Applications change a lot, but same data. Need for ad hoc questions and queries. Need to reduce development times. Need shared data. Improve quality of data. Enable users to do more development.
23
DDAATTAABBAASSEE
Database System Design
User viewsof data.
Conceptualdata model.
Customer(CustomerID, Name, Address, …)
SalesPerson(EmployeeID, Name, Commission, … )
Order(OrderID, OrderDate, CustomerID, EmployeeID, … )
OrderItem(OrderID, ItemID, Quantity, Price, … )
Item(ItemID, Description, ListPrice, …)
Implementation(relational)data model.
Physicaldatastorage.
Class diagram that shows business entities, relationships, and rules.
List of nicely-behaved tables. Use data normalization to derive the list.
Indexes and storage methods to improve performance.
24
DDAATTAABBAASSEE
The Need for Design
Goal: To produce an information system that adds value for the userReduce costs Increase sales/revenueProvide competitive advantage
Objective: To understand the systemTo improve itTo communicate with users and IT staff
Methodology: Build models of the system
25
DDAATTAABBAASSEE
Initial Steps of Design
1. Identify the exact goals of the system.
2. Talk with the users to identify the basic forms and reports.
3. Identify the data items to be stored.
4. Design the classes (tables) and relationships.
5. Identify any business constraints.
6. Verify the design matches the business rules.
26
DDAATTAABBAASSEE
DefinitionsEntity: Something in the real world that we wish to describe
or track.Class: Description of an entity, that includes its attributes
(properties) and behavior (methods).Object: One instance of a class with specific data.Property: A characteristic or descriptor of a class or entity.Method: A function that is performed by the class.Association: A relationship between two or more classes.
Entity: Customer, Merchandise, SalesClass: Customer, Merchandise, SaleObject: Joe Jones, Premium Cat Food, Sale #32Property: LastName, Description, SaleDateMethod: AddCustomer, UpdateInventory, ComputeTotalAssociation: Each Sale can have only one Customer.
Pet Store Examples
27
DDAATTAABBAASSEE
Associations General
One-to-one (1:1) One-to-many (1:M) Many-to-many (M:N)
Relationships represent business rules Sometimes common-sense Sometimes unique to an
organization
Users often know current relationships, rarely future
Objects related to objects An employee can work in
only one department Many departments can work
on many different products
Objects related to properties An employee can have only
one name Many employees can have
the same last name
1 * AnimalBreed
* *performs
TasksEmp1 *places
SaleCust.
places
1 * Purch.Order
Supplier
sent to
28
DDAATTAABBAASSEE
Why Normalization?
Need standardized data definitionAdvantages of DBMS require careful designDefine data correctly It especially makes it easier to expand database laterMethod applies to most models and most DBMS
Similar to Entity-Relationship Goal: Define tables carefully
Save spaceMinimize redundancyProtect data
29
DDAATTAABBAASSEE
Definitions Relational database: A collection of tables. Table: A collection of columns (attributes) describing an entity.
Individual objects are stored as rows of data in the table. Property (attribute): a characteristic or descriptor of a class or entity. Every table has a primary key.
The smallest set of columns that uniquely identifies any row Primary keys can span more than one column (concatenated keys) We often create a primary key to insure uniqueness (e.g., CustomerID,
Product#, . . .) called a surrogate key.
EmployeeID TaxpayerID LastName FirstName HomePhone Address12512 888-22-5552 Cartom Abdul (603) 323-9893 252 South Street15293 222-55-3737 Venetiaan Roland (804) 888-6667 937 Paramaribo Lane22343 293-87-4343 Johnson John (703) 222-9384 234 Main Street29387 837-36-2933 Stenheim Susan (410) 330-9837 8934 W. Maple
Employee
Properties
Rows/ObjectsClass: Employee
Primary key
30
DDAATTAABBAASSEE
First Normal Form Problems (Data)TransID RentDate CustID Phone LastName FirstName Address City State ZipCode1 4/18/02 3 502-777-7575 Washington Elroy 95 Easy Street Smith's Grove KY 421712 4/30/02 7 615-888-4474 Lasater Les 67 S. Ray Drive Portland TN 371483 4/18/02 8 615-452-1162 Jones Charlie 867 Lakeside Drive Castalian Springs TN 370314 4/18/02 3 502-777-7575 Washington Elroy 95 Easy Street Smith's Grove KY 42171
TransID VideoID Copy# Title Rent1 1 2 2001: A Space Odyssey $1.501 6 3 Clockwork Orange $1.502 8 1 Hopscotch $1.502 2 1 Apocalypse Now $2.002 6 1 Clockwork Orange $1.503 9 1 Luggage Of The Gods $2.503 15 1 Fabulous Baker Boys $2.003 4 1 Boy And His Dog $2.504 3 1 Blues Brothers $2.004 8 1 Hopscotch $1.504 13 1 Surf Nazis Must Die $2.504 17 1 Witches of Eastwick $2.00
1NF splits repeating groups
Still have problems Replication Hidden dependency: If a video has not been
rented yet, then what is its title?
31
DDAATTAABBAASSEE
Second Normal Form Definition
Each non-key column must depend on the entire key. Only applies to
concatenated keys Some columns only depend
on part of the key Split those into a new table.
Dependence (definition) If given a value for the key
you always know the value of the property in question, then that property is said to depend on the key.
If you change part of a key and the questionable property does not change, then the table is not in 2NF.
RentalLine(TransID, VideoID, Copy#, Title, Rent)
Depend only on VideoID
Depends on both TransID and VideoID
32
DDAATTAABBAASSEE
Second Normal Form Example
Title depends only on VideoID Each VideoID can have only one title
Rent depends on VideoID This statement is actually a business rule. It might be different at different stores. Some stores might charge a different rent for each
video depending on the day (or time).
Each non-key column depends on the whole key.
RentalLine(TransID, VideoID, Copy#, Title, Rent)
VideosRented(TransID, VideoID, Copy#)
Videos(VideoID, Title, Rent)
33
DDAATTAABBAASSEE
Second Normal Form Example (Data)
TransID VideoID Copy#1 1 21 6 32 2 12 6 12 8 13 4 13 9 13 15 14 3 14 8 14 13 14 17 1
VideoID Title Rent1 2001: A Space Odyssey $1.502 Apocalypse Now $2.003 Blues Brothers $2.004 Boy And His Dog $2.505 Brother From Another Planet $2.006 Clockwork Orange $1.507 Gods Must Be Crazy $2.008 Hopscotch $1.50
VideosRented(TransID, VideoID, Copy#)
Videos(VideoID, Title, Rent)
RentalForm2(TransID, RentDate, CustomerID, Phone,Name, Address, City, State, ZipCode)
(Unchanged)
34
DDAATTAABBAASSEE
Third Normal Form Definition
RentalForm2(TransID, RentDate, CustomerID, Phone, Name, Address, City, State, ZipCode)
Each non-key column must depend on nothing but the key. Some columns depend on
columns that are not part of the key.
Split those into a new table. Example: Customers name
does not change for every transaction.
Dependence (definition) If given a value for the key
you always know the value of the property in question, then that property is said to depend on the key.
If you change the key and the questionable property does not change, then the table is not in 3NF.
Depend only on CustomerID
Depend on TransID