Designing a Database An Introduction for beginning VB Developers Copyright ©2003, Tore Bostrup

Download Designing a Database An Introduction for beginning VB Developers Copyright ©2003, Tore Bostrup

Post on 11-Dec-2015

213 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • Slide 1

Designing a Database An Introduction for beginning VB Developers Copyright 2003, Tore Bostrup Slide 2 6/23/2003Copyright 2003, Tore Bostrup2 What is a Database There are different types of databases Relational Databases Object Databases Hierarchical Databases Network Databases Etc. But usually, we talk about Relational Databases Slide 3 6/23/2003Copyright 2003, Tore Bostrup3 What is a Relational Database A Relational Database is briefly described as a collection of Tables Each Table is made up of Rows of data that is organized in Columns Think of an Excel Spreadsheet where the first row contains the column names, only the columns that are named are used, and each column only contains one type of data. Slide 4 6/23/2003Copyright 2003, Tore Bostrup4 What makes it Relational Relational comes from the way that different tables relate to each other. A column that exists in two tables can be used to relate the two tables by storing the same value in both tables: Slide 5 6/23/2003Copyright 2003, Tore Bostrup5 Relationship between two Tables OrderIDCustID(Info) 3001101 3002126 CustIDCust Name Cust Addr 1B&H 101PPCC 126ACME Comp Slide 6 6/23/2003Copyright 2003, Tore Bostrup6 Relationship Types One-to-One This is not very common, and mostly used for optimization. Usually, only one table is needed. One-to-Many This is the most common relationship. For instance a Customer may have MANY Orders, but each Order is only for ONE Customer. Many-to-Many Also relatively common. For example, an Order may contain MANY Products, and a Product may be part of MANY Orders. Slide 7 6/23/2003Copyright 2003, Tore Bostrup7 Some Other Concepts Primary Key A Primary Key is one or more columns that together are sufficient to identify a single row in a table. It is Unique within the table It always has a value Slide 8 6/23/2003Copyright 2003, Tore Bostrup8 Some Other Concepts Foreign Key A Foreign Key is a column in a table that contains a copy of a value from another table in order to establish a relationship to that table. The Foreign Key usually refers to the Primary Key in the other table (to make sure it only points to one row). Slide 9 6/23/2003Copyright 2003, Tore Bostrup9 Designing the Database We often design the database using Diagrams called Entity-Relationship or ER Diagrams. Tables are shown as boxes, and Relationships are shown as lines. Orders Customers Products Customers place Orders Orders contain Products Slide 10 6/23/2003Copyright 2003, Tore Bostrup10 What Tables Do I Need? Step 1: What kinds of Things are you keeping information about? For starters, each kind of thing tends to be a Table: In an order system, wed probably at least keep information about Orders and Customers. We may also want a catalog of Products that can be ordered Slide 11 6/23/2003Copyright 2003, Tore Bostrup11 What Tables Do I Need Step 2: Determine the data items that you need for each kind of thing, watch out for information that can be repeated Multi-valued columns should be avoided Repeated columns should be avoided No horizontal repetitions should be included Vertical repetitions (multiple rows with same value in a column) should only represent a relationship to another table. Slide 12 6/23/2003Copyright 2003, Tore Bostrup12 Repeated Values A value is considered repeated only if it represents the same measurement. The fact that two people are the same height does not mean that the data is repeated in this sense. A repeated value that is part of the Primary Key is allowed if the Primary Key consists of multiple columns. Slide 13 6/23/2003Copyright 2003, Tore Bostrup13 Repeated Values Examples of repeated values: OrderNoLine1ItemLine1QtyLine1PriceLine2ItemLine2QtyLine2Price 245PN7681$35PN6563$15 OrderNoItemNoCustomerItemQtyPrice 2451SteelCoPN7681$35 2452SteelCoPN6563$15 2461Acme CorpPN3711$2.99 2462Acme CorpPN0157$5 Slide 14 6/23/2003Copyright 2003, Tore Bostrup14 What Tables Do I Need Step 3: Once you have completed defining your kinds of things and fixed any repeating data for those things, draw your relationships. Step 4: Resolve Many-to-Many relationships (we will get back to this). Slide 15 6/23/2003Copyright 2003, Tore Bostrup15 The Relationships Draw the tables Draw the Relationships Define the Relationship Types CustomersOrders CustomersOrdersProducts CustomersOrdersProducts 1NNM Slide 16 6/23/2003Copyright 2003, Tore Bostrup16 The Many-to-Many Relationship Consider the Products table the products that can be ordered. Assume it is simply a catalog with a list of the products available. Each Order may contain MANY Products, and each Product can be a part of MANY Orders. But how do we represent this? Slide 17 6/23/2003Copyright 2003, Tore Bostrup17 Representing Many-to-Many Remember, a Relationship is defined by keeping a copy of the value of a column from the other table, usually that other tables Primary Key. So each row in a table can only reference a single row in the related table And we arent supposed to repeat the data so we shouldnt have multiple rows for the same item. Slide 18 6/23/2003Copyright 2003, Tore Bostrup18 OOPS We Need Another Table The solution is really pretty simple (as long as you know what to do): Introduce an intermediate table between Orders and Products. Relate it as Many-to-One to EACH of the two tables (Orders & Products) This table only needs the Primary Key from the two tables, and their combinations become the representation of a Many-to-Many relationship between Orders and Products Slide 19 6/23/2003Copyright 2003, Tore Bostrup19 The Magic Intermediary Table And voila we have a Many-to-Many relationship between the Orders and the Products table. It is common to name the table with the name of both of the tables it connects. But in an order system, you will typically see this table named OrderDetail. And it will typically contain additional data such as UnitPrice and Quantity. Orders OrderID CustomerID Other Products ProductID ProductName Other ProductOrders OrderID ProductID 1 MN1 Slide 20 6/23/2003Copyright 2003, Tore Bostrup20 Microsoft Access MS Access actually has many different personalities: A Database Manager A Database Design Tool An Application Development Tool (like VB6) An ad hoc Query Designer A Report Designer Slide 21 6/23/2003Copyright 2003, Tore Bostrup21 Our Main Interests As VB6 Developers, our main interests in MS Access are As a Database Design Tool As a Database Manager for small database applications typically for a departmental, workgroup or single user application. Slide 22 6/23/2003Copyright 2003, Tore Bostrup22 Getting Started with Access The Database Wizard lets you create a starter database AND application (developed in the MS Access Application Development Environment) without any prior knowledge. This can be good it gives you a head start on your design if one of the databases are appropriate for your needs. Slide 23 6/23/2003Copyright 2003, Tore Bostrup23 Getting Started The Wizards also can keep you from learning how to design your own database, and you may decide to use the Access Development Environment. The development environment is easy to use for simple and standard things, but it gets difficult to make it do things the way you want it to (you cant quite get control over it). PS: That is MY opinion Slide 24 6/23/2003Copyright 2003, Tore Bostrup24 MS Access Database Wizards Access contains several database Wizards: Asset Tracking Contact Management Event Management Expenses Inventory Control Ledger Order Entry Resource Scheduling Service Call Management Time and Billing Slide 25 6/23/2003Copyright 2003, Tore Bostrup25 To use the Database Wizard Open MS Access Specify Access database wizards, pages, and projects and Click OK Select the Databases tab Select the database type you want and click OK Give it a file name and click Create Click on Next Additional screens let you select which tables and what data elements you want in the database. Slide 26 6/23/2003Copyright 2003, Tore Bostrup26 The MS Access Order Entry DB Slide 27 6/23/2003Copyright 2003, Tore Bostrup27 Creating a Table in MS Access Open an existing database, or create a blank Database. You have three choices Create table in Design view Create table by using wizard Create table by entering data Slide 28 6/23/2003Copyright 2003, Tore Bostrup28 Create table by entering data This allows you to simply start entering data as if you were typing into an Excel spreadsheet. However: No column names are defined (they will be called Field1, Field2, etc.) Access makes a guess as to their data type based on the data entered. Slide 29 6/23/2003Copyright 2003, Tore Bostrup29 Create table by using wizard Starting in the middle, this is similar to the Database Wizard, but it only applies to a single table and doesnt create any application code. Think of it as creating a new table from predefined table templates. Slide 30 6/23/2003Copyright 2003, Tore Bostrup30 Create table in Design view The Table Designer lets you define the column names, their data types, and other information about the columns. Slide 31 6/23/2003Copyright 2003, Tore Bostrup31 If You Only have VB6 Ever since VB3 or 4, VB has included a sample application called VISBAS. In VB6, this application is on the MSDN Library CD, in the Samples\VB98\visbas folder. An executable VISBAS.EXE is installed in the same folder as VB6, and available under the VB6 menu Add-Ins > Visual Data Manager The executable is fine for Access 97 databases, but not for Access 2000 databases. Slide 32 6/23/2003Copyright 2003, Tore Bostrup32 Making VISBAS Work If you need to work with Access 2000 (or newer) databases, you need to recompile it with reference to Microsoft DAO 3.6 Object Library Copy the visbas folder to you HD Remove the Read Only setting from the visbas folder and the files in it. Open visbas.vbp Slide 33 6/23/2003Copyright 2003, Tore Bostrup33 VISBAS for Access 2000 Contd Select menu Project > R