Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Award Sportswear Database Example Presenter: Jolanta Soltis MCSE, MCT, A+ Academic Computing Services 2007 Planning a database Gather information on the purpose of the database – Interview a range of employees • • – Information on each customer’s payment record Records of which products a particular customer will order Create flowcharts Academic Computing Services 2008 2007 Creating Tables Academic Computing Services 2007 Creating Tables continued Structure Data Types Field Properties Primary Key Academic Computing Services 2008 2007 Creating Tables continued Decide what to create – – – – Customers Orders Products Payment Modify table in DesignView – Structure – Data Type – Field Properties Academic Computing Services 2008 2007 Creating Tables continued Determine fields for each table – Id field for each table to serve as a primary key • Products: ProductID,ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued • Orders: OrderID, CustomerID, EmployeeID, OrderDate, RequireDate, ShippedDate, ShipVia, • Customers: CustomerID, CompanyName, ContactName, ContactTitle, Address, City, State, PostalCode, ProductDescription • Order Details: OrderID, ProductID, UnitPrice, Quantity, Discount Academic Computing Services 2008 2007 Table Fields Products: – – – – – – ProductID – AutoNumber - (primary key) ProductName – text ProductDescription – text UnitPrice – Currency UnitsInStock – Number UnitsOnOrder – Number Finish all tables field properties Academic Computing Services 2008 2007 Academic Computing Services 2007 Data Integrity Academic Computing Services 2007 Data Integrity continued Data integrity refers to the accuracy of the data in the database, for example, the data should accurately portray the real world – Goal is 100% accuracy Data integrity problems in databases are often a consequence of data entry errors MS Access support the concept of Data Integrity with the following – – – – Validation Rules Input masks Implementation of the formal concept of Referential Integrity Field Type Checks & Indexes Academic Computing Services 2008 2007 Designing Validation Rules for Database Integrity A Validation Rule is a rule you design into a DBMS such that the data entered by a user must precisely satisfy the rule you establish – MS Access will prevent a user from entering data that does not follow your rules, and will display the validation text you prescribed for the rule the user violated Validation rules can not guarantee 100% database integrity for data entry, but they will help correct many database entry errors Academic Computing Services 2008 2007 Some Validation Rules Within the MS Access DBMS Required Fields user cannot leave this field blank Range of Values value entered must be within a particular range Default Values Legal Values you specify what the legal values are permitted in data entry for a particular attribute Academic Computing Services 2008 2007 Validation Rule 1: To Specify a Required Field Which Cannot Be Left Blank on Data Entry Concept: In Relational Database Design, the attribute(s) which is (are) the primary key should not be "null" for any entity record – With other (non-key) attributes, we may or may not be willing to accept "nulls" as attribute values Concept: Mapping With Access: 1. 2. 3. 4. In Design View, select Field (if not, click on the field-indicator column) In Field Properties, click Required Click Drop-down Arrow Select Yes (or, alternatively, just delete the default No and enter Yes) Academic Computing Services 2008 2007 Validation Rule 2: To Specify a Valid Range of Values for an Attribute in MS Access 1. Select the attribute to apply Validation Rule 2. In Field Properties, click Validation Rule. 3. Enter the appropriate validation expression which expresses the acceptable values for the attribute, such as: – >18000 – = 0 And <=90000 – <#1/15/96# – <[AccountBalance] • { Alternatively, clicking in the Validation Rule Box and then clicking the Build button ( ...) brings you into the Access Expression Builder.} Academic Computing Services 2008 2007 Validation Rule 3: Specifying a Default Value for an Attribute In Design View, select Attribute In Field Properties, click Default Value box, which initially is given a default value of 0 (zero) Enter default value to apply to attribute if the users skips over this attribute This default value applies only to new records, and not to existing records Academic Computing Services 2008 2007 Validation Rule 4: Specifying a Collection of Legal Values for an Attribute In Design View, select attribute to apply the collection of legal values In Field Properties, click Validation Rule box. Enter each legal value in quotation marks , separating each legal value by the word Or, i.e., = "JAN" Or "FEB" Or "MAR" Then click Validation Text box, and enter the message the user should see if the value they enter is not one of the legal values you defined Academic Computing Services 2008 2007 Addendum to Validation Rule 4: Permitting a Null Value as a Legal Value When you specify a collection of legal values, the input must match one of the legal values A null value will not normally match the collection of legal values To permit a null value as a legal value, add, Or is Null (without the quotes) to the Validation Rule – Example: "NY" Or "NJ" Or Is Null Also note that validation rules are not case sensitive Academic Computing Services 2008 2007 Lookup Wizard Academic Computing Services 2007 Lookup Wizard continued Provides a list of values you can choose from when entering data, making data entry easier and ensures consistency of data in that field A Lookup field can get a list of values from a table or query or from a fixed set of values that you have specified Enables you to display a list of values in a field in Datasheet view – Lookup list field looks up its values from an existing table or query or another field in its own table – Value list field displays a fixed set of values that you specify Academic Computing Services 2008 2007 Lookup Wizard continued In Design view click on the Data Type cell of the field that needs to display the Lookup list or on the menu bar choose Lookup Column The name of the foreign key appears in the Field Name column (The foreign key is the primary key of the table form which the values come) Academic Computing Services 2008 2007 Lookup Wizard continued To change the properties of Lookup field, from Field Properties tab, click Lookup tab – Column heads set to yes will display headings Academic Computing Services 2008 2007 Exercise Create Value List by using Lookup Wizard – In the number of columns type 2 to specify two columns for your value list – In the first column type: • 5789 – In the second column type: • 10 14 21 7 Academic Computing Services 2008 2007 Exercise continued On the next page choose a column that contains values that you’d like to store in the database for later use Academic Computing Services 2008 2007 Exercise continued On the final page type a label for the lookup column: Employee Academic Computing Services 2008 2007 Exercise continued Final Result is the cell always displays only the value of the first column of the row you select Academic Computing Services 2008 2007 Academic Computing Services 2007 Input Mask Wizard Academic Computing Services 2007 Input Mask Wizard continued Field template that uses literal display characters that control how data is entered in a field Used to control the format in which the user enters values Academic Computing Services 2008 2007 Input Mask Wizard continued 1. Open table in Design View 2. Click on Input Mask Properties 3. Follow Wizard Instructions Academic Computing Services 2008 2007 Academic Computing Services 2007 Relationships Academic Computing Services 2007 Relationships continued When you create a relationship between tables the related fields do not have to have the same name, but they must have the same data type unless the Primary Key field is an AutoNumber field Academic Computing Services 2008 2007 Relationships continued NAME EMPID SALARY Employee Employee (EMPID, Name, Salary) Academic Computing Services 2008 2007 Relationships continued YEAR AUTOID COLOR Auto AUTO (autoid, year, color) Academic Computing Services 2008 2007 Representing a Has-a Relationship 1:1 and 1:N relationships are transformed into a relational design by creating foreign keys A foreign key is when you take the primary key from one table (on the one-side) and place it into another table (on the many-side) or into the other table for a 1:1 relationship) Academic Computing Services 2008 2007 Transforming 1:1 Relationships Employee 1:1 Auto EMPLOYEE( EmpNum, Name, Phone, ...) AUTO( AutoId, Model, Year, Color, ..., EmpNum) or EMPLOYEE( EmpNum, Name, Phone,..., AutoID) AUTO( AutoID, Model, Year, Color,...) – In both cases, we would want to enforce referential integrity by requiring that the value of a foreign key in one relation/table appear as the value of a primary key in the other relation/table. The Foreign Key can go on either side but, it can be on one side only Academic Computing Services 2008 2007 Questionable 1:1 Mandatory Relationships EMPLOYEE 1:1 SS# 1:1 mandatory relationships should be looked at very closely – sometimes they can be combined into a single relation/table However, sometimes it can be justified NOT to combine the two tables in a 1:1 mandatory relationship into a single table Academic Computing Services 2008 2007 When Not to Combine 1:1 Mandatory Relationships EMPLOYEE 1:1 JOB-EVALUATION For Performance, if Job-Evaluation data are lengthy and used very infrequently For Security, if Job-Evaluation data needs to be protected against unauthorized access Academic Computing Services 2008 2007 When Not to Combine 1:1 Mandatory Relationships continued EMPLOYEE 1:1 JOB-EVALUATION Notice that the relationship here is mandatory in both directions Academic Computing Services 2008 2007 Transforming One to Many Relationships Dormitory 1:N Student Dormitory ( DormitoryName, Location, NumRooms) Student ( StudentId, StuName, ..., DormitoryName) Transformation Rule: The Primary Key of the entity set on the One side of the relationship becomes a foreign key of the entity set on the many side of the relationship Academic Computing Services 2008 2007 Representing 1:N Relationships In each of the following examples, the foreign key goes to the right (into the many-side) Academic Computing Services 2008 2007 Data Structure Diagrams ProfessorName goes into the Student table as a Foreign Key Note - that in a DSD, a crow’s foot on a relationship line indicates a many relationship Academic Computing Services 2008 2007 Transforming Many to Many Relationships Student M:N Class Student( StudentNum, StuName, Phone, ...) Class( ClassNum, ClassName, ...) Stu-class( StudentNum, ClassNum) Rule: Create a new relation, called an Intersection relation, whose only (***unless there is also an attribute belonging to the relationship***) attributes will be a composite primary key consisting of the primary keys of the participants in the relationship. Academic Computing Services 2008 2007 Representing a M:N Relationship Academic Computing Services 2008 2007 Representing a M:N Relationship continued A one-to-many relationship is the most common type of relationship A record in Table A can have many matching records in Table B, but a record in Table B has only one matching record in Table A – One supplier, can supply more than one product, but each product has only one supplier Academic Computing Services 2008 2007 Representing a M:N Relationship continued In a many-to-many relationship, a record in Table A can have many matching records in Table B, and a record in Table B can have many matching records in Table A This type of relationship is only possible by defining a third table (called a junction table) whose primary key consists of two fields — the foreign keys from both Tables A and B Academic Computing Services 2008 2007 Representing a M:N Relationship continued A many-to-many relationship is really two one-tomany relationships with a third table – For example, the Orders table and the Products table have a many-to-many relationship that's defined by creating two one-to-many relationships to the Order Details table. One order can have many products, and each product can appear on many orders In a one-to-one relationship, each record in Table A can have only one matching record in Table B, and each record in Table B can have only one matching record in Table A Academic Computing Services 2008 2007 Representing a M:N Relationship continued This type of relationship is not common, because most information related in this way would be in one table You might use a one-to-one relationship to divide a table with many fields, to isolate part of a table for security reasons, or to store information that applies only to a subset of the main table – For example, you might want to create a table to track employees participating in a fundraising soccer game. Each soccer player in the Soccer Players table has one matching record in the Employees table Academic Computing Services 2008 2007 Referential Integrity Referential integrity is a system of rules that Microsoft Access uses to ensure that relationships between records in related tables are valid, and that you don't accidentally delete or change related data. You can set referential integrity when all three conditions are met: 1. The matching field from the primary table is a primary key or has a unique index Academic Computing Services 2008 2007 Referential Integrity continued 2. The related fields have the same data type. There are two exceptions. An AutoNumber field can be related to a Number field with a FieldSize property setting of Long Integer, and an AutoNumber field with a FieldSize property setting of Replication ID can be related to a Number field with a FieldSize property setting of Replication ID 3. Both tables belong to the same Microsoft Access database. If the tables are linked tables, they must be tables in Microsoft Access format, and you must open the database in which they are stored to set referential integrity. Referential integrity can't be enforced for linked tables from databases in other formats Academic Computing Services 2008 2007 Relationship The employee sells to the customer The customer orders a product The order contains a number of order details Relationships from the central table (Orders) – – Each order is placed by a single customer One customer can place several orders Customer Orders One-to-many Academic Computing Services 2008 2007 Relationship continued Some orders are paid off in installments Each installment is for a single order Orders Payment One-to-many Academic Computing Services 2008 2007 Relationship continued Several different products can be ordered in a single order. Each product can be ordered many times Products Customer many-to-many Academic Computing Services 2008 2007 Relationship continued In order to create a many-to-many relationship between Orders and Product, you create a junction table – Order Details The primary keys of both the Orders and the Products table are included in the Order Details primary key. Order Details is the Junction Table Products Orders many-to-many Order Details Academic Computing Services 2008 2007 Relationship continued In the Relationship window you drag the OrderID in the Order table to the ProductID in the Order Details junction table Drag the ProductID from Products table to ProductID in the OrderDetails table Academic Computing Services 2008 2007 Relationship continued Many-to-many relationship Academic Computing Services 2008 2007 Academic Computing Services 2008 2007 Academic Computing Services 2007 Queries Academic Computing Services 2007 Queries continued Allow you to retrieve the information stored in tables in a variety of ways Academic Computing Services 2008 2007 Criteria for creating Queries Wildcards and Mathematical Expressions: – Comparison Operators = (Equals) > (Greater than) < (Less than) >= (Grater than or equal to) <= (Less than or equal to ) <> (Not equal to ) Between (ex. Between 1 And 1000) In (In [ “NY” , “CA” , “NJ” ] means “is member of a list" Is Null (There is no value entered in the field) Is Not Null (There is a value entered in the field) Like (Like “smi*” means “has a string ‘smi’ at the beginni9ng of the field”) Academic Computing Services 2008 2007 Criteria for creating Queries continued – Logical Operators: And ( “>0 and <=10” ) Or ( “<-2 or >2” ) Not (Not Like “smi*” means “the field does not start with “smi*”) – Wildcards ? Any single character, ex. BL-3435 * Any string of characters, ex. Por* # Any number, ex. 46#7 Academic Computing Services 2008 2007 Criteria for creating Queries continued Examples – – – – – >”01/01/95” Like “S*” Or Like “A*” Like “1/?/01” Where [BirthDate] >= #1/1/60# Between 1 And 5 Academic Computing Services 2008 2007 Simple Query Allows you to bring together related information that are stored in different areas Review from last week – Create Query based on: Customers, OrderDetails, Orders Click on Total button From Group By choose :Sum for Quantity and Avg for Unit Price Run the Query Academic Computing Services 2008 2007 Simple Query continued In the empty column type Amount: CCur ([UnitPrice]*[Quantity] Name of New Column Currency In the Criteria type: [Enter the State code] Query will ask you for the state name Academic Computing Services 2008 2007 Query in Datasheet View In the Datasheet view below are listed the main ways to apply a filter to a query’s record set: – By selection (quick, using a single criterion) – Filter excluding selection (to extract all records that don’t contain a particular value – By form (more than one criterion) – Using advanced filtering or sorting options Academic Computing Services 2008 2007 Totals and Parameter Query Total query performs calculation on groups of data Parameter query performs calculation on groups of data – dialog box that prompts you for criteria each time you run the query Create query in Design View Academic Computing Services 2008 2007 Totals and Parameter Query continued To define a totals query, you need to click the Total button to display the Total row in the design grid. By default, Group By appears in the Total row – By selecting Where and specifying criteria you can filter data Academic Computing Services 2008 2007 Exercise Create a totals query that calculates how many units of each of your company’s product are on order – Create select query with: • Product Name from table Products • Quantity from table Order Details – Click on Sum button • Quantity field - Group By: Sum – Run the query Academic Computing Services 2008 2007 Parameter Query To create, in the Criteria Field, Type a customer name: enter a value in enclosed brackets [] You can designate several fields as parameters: Type the first date: between Type a product name: Academic Computing Services 2007 Exercise continued Academic Computing Services 2007 Exercise continued Create a parameter query to view order details for a particular product during a certain period – Create query with field you need: • • • • • OrderDate from Orders CompanyName from Customers ProductName from Products UnitPrice from OrderDetails Quantity from OrderDetails – Click on Criteria in the OrderDate field – Type: Between [Type the first date:] And [Type the second date:] – Click on the Criteria in the ProductName field – Type: [Type a product name:] – Run the query Academic Computing Services 2008 2007 Exercise continued Final Output Academic Computing Services 2008 2007 Action query Allows you to change and add amounts of data quickly and accurately Four types: – Make-table allows you to create a new table from data in one or more existing tables • Allows to combine the fields you want in a new table and export the table to another database – Append allows you to insert records from the table in one database to a table in another database – Update – Delete Academic Computing Services 2008 2007 Make-Table Query continued Make a table using the fields in your select query: Product Order Details Query Academic Computing Services 2007 Make-Table Query continued Change Query type to Make-Table Query Type Customer Order Details as title Academic Computing Services 2007 Append Queries Two databases: “AwardInventory” and “AwardCustomer” (both have Products table) There are more records in the AwardInventory Products table than in its equivalent in the AvardCustomer You need to link them: – Open AvardCustomer – File – Get External Data – Link Tables – Point to AvardInventory Create a select query, choose fields From Query menu choose Append Query… The Show row in the Query Design window changes to Append To and indicates the fields to which records will be appended. Academic Computing Services 2008 2007 Update Query To update records Create a select query, add fields Go to Query – Update Query In the Update To row type an expression to update – Ex. Unit price field: type [UnitPrice]*1.1 to increase prices – Criteria: type Printed – Run the query Academic Computing Services 2008 2007 Delete Query To remove records Create select query Add Products table To specify the table from which records will be deleted, you drag the asterisk(*) from the Products table to the Field row in the Query Design grid Drag the filled you want to set criteria into the next column – Ex. Product Description: type Blank Academic Computing Services 2008 2007 Queries and Join Properties Access can join tables on fields not present in table relationships as long as they have the same data type: Auto Number, Number Types of Joins – Inner join: This is the most common of joins. It returns only those records from both tables where there is a matching value in the related fields (usually the primary and foreign key values). Academic Computing Services 2008 2007 Queries and Join Properties continued – Outer joins (can be either a Left or a Right Outer Join) tell a query that although some of the rows on both sides of the join correspond exactly, the query should include all of the rows from one table, and also those rows from the other table that share a common value on both sides of the join Academic Computing Services 2008 2007 Practice Summary of orders placed per customer whether they placed order or not. Tables: – Customers (CustomerID, CompanyName), Orders (Order Date) (related by CustomerID filed) – Create a query Academic Computing Services 2007 Academic Computing Services 2007 Indexes Academic Computing Services 2007 Indexes continued To optimize a query’s performance, Index the fields that are most frequently queried, when searching, Access looks up data’s location in the index – The primary key of a table is automatically indexed – Index foreign keys as well Consider indexing a field if it meets the following criteria: – – – – You anticipate searching for values stored in the field You anticipate sorting values in the field You anticipate storing many different values in the field Field’s data type is Text, Memo, Number, Date/Time, AutoNumber, Currency, Yes/No or Hyperlink Academic Computing Services 2008 2007 Indexes continued Open Employees table in Design View Click on Indexes button – Type: Name – Choose FirstName Academic Computing Services 2007 Performance Analyzer Switch to the Database window Academic Computing Services 2007 Performance Analyzer continued Allows you to analyze a query – From Tools menu, click Analyze – Click Performance – Then select Queries The Performance Analyzer gives you all suggestions Academic Computing Services 2007 Forms Used to capture information into tables Create: – Customer Capture Form Academic Computing Services 2008 2007 Reports Allow you to view and print information from tables Create: – Customer’s Payments Academic Computing Services 2008 2007 Pages Used to view data access pages from internet Create – Products Academic Computing Services 2008 2007 Macros Used to automate common tasks involving one or more actions, such as printing a report Crate – Print Payments Academic Computing Services 2008 2007 Modules Construct using Visual Basic for Applications. This provides added functionality to databases Academic Computing Services 2008 2007 If you have any questions, please feel free to contact Academic Computing Services Jolanta Soltis IT Consultant (973) 596-2925 e-mail [email protected] Academic Computing Services 2008 2007