30
1 D D A A T T A A B B A A S S E E Base Data Types Numbers Integers Reals Text Length International Date/Time Images Bitmap Vector Sound Samples MIDI Video Numbers, Text, and Dates Images Sound Video Input Process Output 12 + 8 = 20 000001100 000001000 ---------------- 000010100 20 0010000000000000000 0100000000000001001 0110000011000011011 0111111111111001111 1111111111111011111 1111111111100011111 8 9 20 7 8 19 5 6 15 000001000 000001001 000010100 ..... pitch, volume time 00101010111 11010101010 01010101010 11110100011 00101011011 00101010111 11010101010 01010101010 11110100011 00101011011 00101010111 11010101010 01010101010 11110100011 00101011011 00101010111 11010101010 01010101010 11110100011 00101011011 00101010111 11010101010 01010101010 11110100011 00101011011

DATABASE 1 Base Data Types Numbers Integers Reals Text Length International Date/Time Images Bitmap Vector Sound Samples MIDI

Embed Size (px)

Citation preview

1

DDAATTAABBAASSEE

Base Data Types Numbers

Integers Reals

Text Length International

Date/Time Images

Bitmap Vector

Sound Samples MIDI

Video

Numbers,Text, andDates

Images

Sound

Video

Input Process Output

12 + 8 = 20000001100000001000----------------000010100 20

001000000000000000001000000000000010010110000011000011011011111111111100111111111111111110111111111111111100011111

8 9 20 7 8 19 5 6 15000001000 000001001 000010100 .....

pitch,volume

time

0010101011111010101010010101010101111010001100101011011

0010101011111010101010010101010101111010001100101011011

0010101011111010101010010101010101111010001100101011011

0010101011111010101010010101010101111010001100101011011

0010101011111010101010010101010101111010001100101011011

2

DDAATTAABBAASSEE

Objects Object Definition--

encapsulation.Object NamePropertiesMethods

Most existing DBMS do not handle inheritance.Combine into one table.Use multiple tables and

link by primary key.More efficient.Need to add rows to

many tables.

Customer

CustomerIDAddressPhone

AddCustomerDropCustomer

Class name

Properties

Methods

Commercial

ContactVolumeDiscount

ComputeDiscount

Government

ContactBalanceDue

BillLateFeesAddCustomer

Inheritance

Polymorphism

3

DDAATTAABBAASSEE

Objects in a Relational Database

CustomerIDAddressPhone

Customer

CustomerIDContactVolumeDiscount

CommercialCustomer

CustomerIDContactBalanceDue

GovernmentCustomer

Separate inherited classes. Link by primary key. Adding a new customer

requires new rows in each table.

Definitely need cascade delete.

4

DDAATTAABBAASSEE

OO Difficulties: Methods

Database Object

CustomerMethod:

Add New CustomerApplication

CustomerNameAddressPhone

Personal Computer

Unix Server

IBM Server

Program code

Database Object

How can a method run on different computers?

Different processors use different code.

Possibility: Java

5

DDAATTAABBAASSEE

SQL3: OO Features

Abstract data type User defined data types. Equality and ordering

functions. Encapsulation: Public,

Private, Protected. Inheritance.

Sub-tables that inherit all columns from another table.

Persistent Stored Modules (Programming Language). Create methods. SQL and extensions. External language.

User defined operators. Triggers for events. External language support

Call-Level Interface (CLI)Direct access to DBMS

Embedded SQLSQL commands in an

external language.

6

DDAATTAABBAASSEE

Abstract Data Types

GeoPointLatitudeLongitudeAltitude

GeoLineNumberOfPointsListOfGeoPoints

Procedure: DrawRegion{

Find region components.SQL: Select …

For each component {Fetch MapLineSet line attributesMapLine.Draw

}}

7

DDAATTAABBAASSEE

SQL3 Sub-Tables

CREATE SET TABLE CommercialCustomer(

Contact VARCHAR,VolumeDiscount NUMERIC(5,2)

)UNDER Customer;

CREATE SET TABLE Customer(

CustomerID INTEGER,Address VARCHAR,Phone CHAR(15)

)

CustomerIDAddressPhone

Customer

ContactVolumeDiscount

CommercialCustomer

Inherits columnsfrom Customer.

8

DDAATTAABBAASSEE

Nested TablesCREATE TYPE Person AS OBJECT (

LastName VARCHAR2(15),FirstName VARCHAR2(15),Phone VARCHAR2(15) );

CREATE TYPE SaleItem AS OBJECT (ItemID NUMBER, Quantity NUMBER, Price NUMBER);

CREATE TYPE Sale AS OBJECT (SaleID NUMBER,Customer PERSON,SaleItems SaleItem );

Then, create the actual table:CREATE TABLE Sale_table OF Sale

NESTED TABLE SaleItems STORE AS SaleItems_table;

9

DDAATTAABBAASSEE

SQL3: Programming

Database

Data Types Tables, …

Persistent Stored ModulesSQLExtended SQL codeExternal language code

External Programs

Embedded SQLCall-Level Interface

CURSOR … SELECT … FETCH …

10

DDAATTAABBAASSEE

OODBMS Vendors

GemStone Systems, Inc. Hewlett-Packard, Inc. (OpenODB) IBEX Corporation, SA. Illustra (Informix, Inc.) Matisse Software, Inc. O2 Technology, Inc. Objectivity, Inc. Object Design, Inc. ONTOS, Inc. POET Software Corporation UniSQL Unisys Corporation (OSMOS) Versant Object Technology

11

DDAATTAABBAASSEE

Integrated Applications

Choose the best tool for the job. DBMS: Store, retrieve, and

share data. Spreadsheet:

Computations, analysis, and graphs.

Word processor: Formatting, pagination, and reports.

Graphics: Charts and presentations.

Calendars and Project management: Scheduling.

Integration Linking and sharing data

objects. Setting object properties. Calling object methods.

Manual integration Copy objects by hand.

Automatic integration Dynamically link objects. Program access to objects.

12

DDAATTAABBAASSEE

Accessing Application Objects

Database Application

Retrieve dataSELECT….GROUP BY …

Statistical calculations.Transfer to spreadsheet.Execute statistical routines.

Retrieve and store results.

Spreadsheet Application

Put data in cells.Perform regression analysis.

13

DDAATTAABBAASSEE

Static Data Links Copy data from one

application to another.ServerContainer

Changing the original does not affect the copy.

Steps to create: In original data

Mark data objectsSelect Edit | Copy

In container documentMove to insert locationSelect Edit | Pasteor Edit | Paste Special

Container Document

e.g., Spreadsheet

Embedded objectQuery results

Database table

copy

14

DDAATTAABBAASSEE

Dynamic Links

Original data file and compound document are linked.

If the original data file is changed, the linked document automatically updates the content.

Can have multiple links. User must have access to all

of the applications.

Word processorFinal document

Table

Graph

Database

Spreadsheet

Sheet Graph

Query Link

15

DDAATTAABBAASSEE

Storing Objects in the Database

An Access form that holds revisions of a spreadsheet object.

16

DDAATTAABBAASSEE

Programming Links

Dim wsExcel As ObjectDim dbl As Double

Set wsExcel = CreateObject("Excel.Sheet") ' Start ExcelwsExcel.Application.Visible = True ' (optional) make Excel visible

' Tell Excel to gen random datawsExcel.ActiveSheet.Range("A1:A8").Formula = "=NORMSINV(RAND())”wsExcel.ActiveSheet.Cells(9,1).Formula = "=Sum(A1:A8)”

' Compute the totaldbl = wsExcel.ActiveSheet.Range("A9").Value ' Return the resultMsgBox dbl ' Temporary test, display value

wsExcel.SaveAs strFileName ' (optional) Save WorksheetwsExcel.DisplayAlerts = False ' Stop “unsaved” warningswsExcel.Quit ' (optional) Close ExcelSet wsExcel = Nothing ' Free up memory (in Access)

17

DDAATTAABBAASSEE

The Object Browser

Displays objects, properties, and methods from other software.

Only available from the code window.

Must first set Tools | References and check the software package (e.g., Microsoft Excel).

18

DDAATTAABBAASSEE

Spreadsheet

Database

Examples

AccountIDTitleDescriptionSummaryStatementLevel

ChartOfAccounts

AccountIDDateValue

AccountValue

2000BalanceSheet

2000Income

Statement

2001BalanceSheet

2001Income

Statement

Retrievecurrent data.

Forecast

Pastdata.

19

DDAATTAABBAASSEE

Financial Forecast

Crosstab query linked to spreadsheet:=MSAccess|'C12Finance.mdb;Query Query4'!All

Excel

Date Accounts PayableAccounts ReceivableAccrued PayablesBonds (long term)Cash Common StockCost of Sales31-Mar-96 5632 18161 2140 12350 6783 47945 1444130-Jun-96 7541 18078 2564 12525 7513 48577 1713930-Sep-96 8374 19629 2931 12652 9177 49018 1860931-Dec-96 6700 21731 2144 12845 8915 49764 1810931-Mar-97 8471 24913 2711 13014 10938 50014 2066030-Jun-97 10103 24476 3233 13212 10443 50367 2349630-Sep-97 10742 29927 3975 13319 12113 50671 2387031-Dec-97 12107 32423 4359 13522 14114 50977 2632031-Mar-98 11206 31649 4146 13659 13742 51440 2873430-Jun-98 12735 35938 4457 13728 13926 52012 2830130-Sep-98 12140 41978 4249 13853 18335 52751 3281131-Dec-98 13663 45333 5055 13937 19280 53392 3595431-Mar-99 14397 48498 5183 14092 16918 53823 3891130-Jun-99 16347 49090 5231 14163 20863 54093 4418130-Sep-99 19985 55421 7594 14277 20032 54805 4874431-Dec-99 22986 61558 8505 14494 26796 55415 4997031-Mar-00 20803 61002 7073 14655 25027 56031 5474630-Jun-00 20933 73450 6908 14773 28378 56884 5508830-Sep-00 30111 73089 9937 14907 30679 57401 6406531-Dec-00 32872 87341 11764 14982 33562 57864 76530

Forecast 30915 91400 10816 15240 36211 58229 75805From sales 30582 87427 10685 35918 74054Best Est 30749 89413 10751 15240 36065 58229 74929

20

DDAATTAABBAASSEE

Word Report ExampleStart Word.

Add a document.

Set tab stops.

Open query.

Read each row.

Format and “Print” to Word.

End Loop.

Close Query.

Define Footer.

Save and Close Word document.

Clear variables.

Access

21

DDAATTAABBAASSEE

Word Report: SetupDim objWord As Word.Application Dim cnn As ADODB.ConnectionDim rst As ADODB.RecordsetDim rngfoot As Variant If (Tasks.Exists("Microsoft Word") = True) Then Set objWord = GetObject(, "Word.Application")Else Set objWord = CreateObject("Word.Application")End IfobjWord.Visible = True objWord.Documents.AddSet dbs = CurrentProject.ConnectionSet rst = CreateObject(“ADODB.Recordset”)rst.Open “Select * From Query5”,cnn

Define variables.Start Word.Add a document.

22

DDAATTAABBAASSEE

Word Report: Loop' Set the tabs for the columnsSelection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(0.5), _ Alignment:=wdAlignTabLeft, Leader:=wdTabLeaderSpacesSelection.ParagraphFormat.TabStops.Add Position:=InchesToPoints(3), _ Alignment:=wdAlignTabRight, Leader:=wdTabLeaderSpaces ' Read all of the data rows and put them into the documentDo While Not rst.EOF Selection.TypeText Text:=rst(“AccountID”) & vbTab & rst(“Title”) _ & vbTab & Format(rst(“Value”), "Currency") Selection.TypeParagraph rst.MoveNextLooprst.Close

Set tab stops.Open query.

Read each row.Format and “Print” to Word.

End Loop.Close query.

23

DDAATTAABBAASSEE

Word Report: Footer' Add a footer for each page with File name and creation DateSet rngfoot = objWord.ActiveDocument.Sections(1). _

Footers(wdHeaderFooterPrimary).RangeWith rngfoot .Delete .Fields.Add Range:=rngfoot, Type:=wdFieldFileName, Text:="\p" .InsertAfter Text:=vbTab & vbTab .Collapse Direction:=wdCollapseStart .Fields.Add Range:=rngfoot, Type:=wdFieldCreateDateEnd With 'objWord.Documents.Save'objWord.CloseSet objWord = Nothing

Define Footer.Save and Close Word document.Clear variables.

24

DDAATTAABBAASSEE

Word Report: Output

10 Current Assets $136,886.0015 Fixed Assets $45,673.0020 Current L iabilities $98,963.0025 Long Term Liabilities $14,982.00

Footer6/15/01 Accounting Summary

In practice, you will add more codefor conditions and formatting.

25

DDAATTAABBAASSEE

Office Integration Tips

MS Office 97/Visual Basic Programmer’s Guide ISBN: 1-57231-340-4 http://www.microsoft.com

With (long object) .property .method

End With

For Each c IN Range c.Do Something

Next c

For Each c in wsExcel.ActiveSheet.Range("A1:F8")If Abs(c.Value) < 0.01 Then c.Value = 0

Next

26

DDAATTAABBAASSEE

Sally’s Pet Store: Income Statement

Retrieve from database

Entered by hand

Formula for calculationsshould be generated bythe template.

Sally's Pet Store Starting DateIncome Statement 1/1/2001

Ending Date3/31/2001

Revenue Animal Sales $15,533.59 Merchandise Sales $5,230.44Total $20,764.03

Operating Costs and Expenses Animal Purchases $4,464.45 Merchandise Purchases $4,616.73 Operating and selling $4,510.00 General and administrative $1,200.00Total $14,791.18

Operating Income $5,972.85

Nonoperating income and expenses Interest Income $0.00 Interest Expense ($2,000.00)Total ($2,000.00)

Income before taxes $3,972.85

Income taxes $517.00

Net Income $3,455.85

27

DDAATTAABBAASSEE

Pet Store: Income Statement Form

Need code to(1) Start Excel(2) Enter template

Text/labelsCalculations

(3) Use SQL to total salesWithin given datesTransfer to spreadsheet

28

DDAATTAABBAASSEE

Pet Store: Open ExcelGlobal goExcel As Excel.ApplicationPublic Function OpenMSExcel() As VariantOn Error Resume Next Set goExcel = GetObject(, "Excel.Application") If (goExcel Is Nothing) Then Set goExcel = New Excel.Application End If If (goExcel Is Nothing) Then MsgBox "Can't start Excel", , "Unexpected Error" OpenMSExcel = False Else If (Not goExcel.Visible) Then goExcel.Visible = True End If OpenMSExcel = True End If DoEvents

End Function

29

DDAATTAABBAASSEE

Pet Store: Code to Build TemplatePrivate Sub cmdIncome_Click() Dim cnn As ADODB.Connection ‘ Declare variables If Not OpenMSExcel() Then ' Open Excel if we can Exit Sub End IfOn Error GoTo Err_cmdIncome_Click

goExcel.Workbooks.Add ' Create a new workbook With goExcel.ActiveSheet ' Which sets a default sheet .Cells(1, 1).ColumnWidth = 30.5 ' Set up the basic template/text .Cells(1, 1).Value = "Sally's Pet Store" .Cells(1, 1).Font.Bold = True

.Cells(8, 2).Value = "=B6+B7” ‘ One of many calculations

.Range("B6:B28").Select ‘ Format the cells goExcel.Selection.NumberFormat = "$#,##0.00;($#,##0.00)" .Range("B2,B4").Select goExcel.Selection.NumberFormat = "m/d/yy"

30

DDAATTAABBAASSEE

Pet Store: Code to Retrieve Data strWhere = "Between #" & [StartDate] & "# And #" & [EndDate] & "#);" ' First do the Animal Sales strSQL = "SELECT Sum(SaleAnimal.SalePrice) AS SumOfSalePrice " strSQL = strSQL & "FROM Sale INNER JOIN SaleAnimal ON

Sale.SaleID = SaleAnimal.SaleID " strSQL = strSQL & "WHERE (Sale.SaleDate " & strWhere Set cnn = CurrentProject.Connection Set rst = CreateObject(“ADODB.Recordset”) rst.Open strSQL, cnn, adOpenStatic, adLockReadOnly rst.MoveFirst .Cells(6, 2).Value = rst(“SumOfSalePrice”) rst.Close ' Second do the Merchandise sales ' Third do the Animal purchases ' Fourth do the Merchandise purchases End WithExit_cmdIncome_Click: Exit SubErr_cmdIncome_Click: MsgBox Err.Description, , "Unexpected Error" Resume Exit_cmdIncome_Click End Sub