36
Information Systems: Creating Business Value by Mark Huber, Craig Piercy, and Patrick McKeown Field Guide D: The Details of SQL, Data Modeling, and XML

Fg d

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Fg d

Information Systems: Creating Business Value

byMark Huber, Craig Piercy, and Patrick McKeown

Field Guide D:

The Details of SQL, Data Modeling, and XML

Page 2: Fg d

Student ROI (Return on Investment)

Your investment of time and effort in this course will result in your being able to answer these questions:

1. How do knowledge workers use SQL to query a single-table database?

2. How do knowledge workers use logical modeling to create effective relational database systems?

3. How is SQL used to query multiple-table databases?

4. How do knowledge workers use XML to transfer data between software applications?

Page 3: Fg d

What We Will Cover:

Using SQL to Query Relational Databases

Using Logical Modeling to Create a Relational Database

Querying Multitable Databases

Using XML for Data Transfer

Page 4: Fg d

File Processing Systems

Before the development of database systems, most organizations used file processing systems.

With file processing, each application uses it own set of files.

Even though files may be related by an application, they are not necessarily stored or managed together.

Page 5: Fg d

The Data HierarchyThe data hierarchy is a way of organizing stored data in progressively larger levels of complexity

Bit Record

Character File

Field Database

Page 6: Fg d

Database Systems

A database and its DBMS is often referred to as a database system.

People who create and manage organizational database systems are referred to as database administrators.

Knowledge workers usually use application software to access and query the database.

Page 7: Fg d

DBMSs Control Database Access

Page 8: Fg d

Disadvantages of File Processing Systems Data Redundancy: Files stored in multiple

locations, so can have inconsistent data due to changes not being made to all files.

Data Dependence: Files work with specific applications but may not work with others.

Data Inaccessibility: Data may be difficult to access by other applications.

Poor File Management: Difficult to manage files for simultaneous use, secure files, and recover from file problems.

Page 9: Fg d

Advantages of Database Management Systems Data organization is independent of any one

software application. Data can be organized in a manner that reduces

data redundancy. The DBMS can include features for: maintaining

the quality of the data; handling security; and synchronizing access by simultaneous users.

The database system allows for capabilities such as: improved data access; allowing different views of the data for different users; and report generation.

Page 10: Fg d

Using SQL to Query Relational Databases The primary function of a database is to enable

knowledge workers to obtain information from it in a usable form using queries (questions).

To query a relational database, many knowledge workers use Structured Query Language (SQL), which is a computer language for manipulating data in a relational database.

SQL queries also enable database users to add new records, or change or delete records in a database.

Page 11: Fg d

Important Terms

Primary key: a field that holds a unique value for each record in a table.

Foreign key: the primary key from another table that is in the current table. It relates the two tables.

Entity: another name for a table in a relational database. Row: another name for a record in a relational database table. Column: another name for a field in a relational database table. Query: A statement written in SQL that requests matching

records, changes matching records, deletes matching records or adds new records.

Page 12: Fg d

Querying a Single Table Database

To query a single table database, we use an SQL statement of the form:SELECT fields FROM tables WHERE fields match query condition

Where the SELECT keyword designates which fields to display as a result of the query,

the FROM keyword designates which tables to search, and the WHERE keyword specifies the search criteria, or query

condition, to use in finding records. Note that we use uppercase for keywords, to make them stand

out, but otherwise case is not important when using SQL.

Page 13: Fg d

Other Query Keywords

In addition to the SELECT keyword, there are a number of other keywords that we can use to: CREATE a table, to INSERT new records in a table, to DELETE records from a table, and to UPDATE one or more records in a table.

We can also search for records that are like a specific condition as well as computing sums, averages, and so on, for all records that match some criteria.

Page 14: Fg d

Using SQL to Display Specific Information

The SQL to display all of the table, that is, all fields for all of the records is:SELECT * FROM TableName

To display only a subset of the fields, the standard form of this query is (where queries can run on to the next line):SELECT FieldName1, FieldName2, … FROM TableName

To display these records in some order, other than in increasing order of the primary key, we can add the Order By clause:SELECT FieldName1, FieldName2, … FROM TableName ORDER BY FieldName

Page 15: Fg d

Displaying Selected Fields for Matching Records

In many cases, we may only want to display selected fields for records that match some condition.

To do this, we need to use the WHERE keyword followed by some query condition involving one of six comparison operators: equals (=) greater-than (>) less-than (<) greater-than-or-equal to (>=) less-than-or-equal-to (<=) not-equal-to (<>) plus a fieldname and a value.

The general form is:SELECT FieldName1,FieldName2, … FROM TableName WHERE Query Condition

Page 16: Fg d

Using the LIKE Operator

Using the equals sign in a SELECT query looks for an exact match.

To look for an “almost match” or to look for information about a group of products, the LIKE operator is appropriate.

The LIKE operator uses the wildcard character as a replacement for unknown or non-existing characters in attempting to find matches to a group of characters.

The wildcard character is usually either the asterisk (*) in Microsoft Access or the percent sign (%) in other database management systems.

The general form of this type of query is: SELECT FieldName1,Fieldname2,… FROM TableName WHERE FieldName LIKE ‘*value*’

Page 17: Fg d

Inserting, Deleting, or Changing Records

To insert a record into a table, you would use an SQL statement of the form:INSERT INTO TableName Values (value1, value2, …)

Note that the values must be entered in the exact order as the fields in the record, separated by commas. If there are null or missing values, the corresponding comma must be entered.

To delete an existing record from a database table, you would use an SQL statement of the form:DELETE FROM TableName WHERE FieldName = value

To change values in a row of a database, you can use the UPDATE and SET keywords in the form:UPDATE TableName SET FieldName1 = value WHERE FieldName2 = value

Page 18: Fg d

Using Aggregate Functions in SQL

SQL can be used to compute certain values in the table using five different aggregate functions—COUNT, AVG, SUM, MIN, and MAX.

In each case, you must use a dummy field name for the result of the computation.

The form for the AVG function to find the average value of matching records is:SELECT AVG(fieldname) AS DummyName FROM TableName WHERE Query condition

The SUM, MAX, and MIN functions have the same form as the AVG function.

The COUNT function to find the number of matching records uses a different form:SELECT COUNT(*) AS DummyName FROM TableName WHERE Query condition

Page 19: Fg d

Using Logical Modeling to Create a Relational Database

Most relational databases include many tables, not just one, to avoid redundancy.

This redundancy can result in the database table taking up unneeded storage space as well as causing problems when trying to insert new records, delete existing records, or update records.

These problems, typically referred to as anomalies, can harm the integrity of the database records.

To create relational databases that avoid these problems with redundancy, logical modeling is used.

The first step in logical modeling is to create an Entity-Relationship Diagram (ERD) that allow us to focus on the “big picture,” that is, the entities and the relationships.

Page 20: Fg d

Entity-Relationship Diagramming

The symbols for an ERD are (where 1:1 means a one-to-one relationship and 1:M means a one-to-many relationship):

Page 21: Fg d

Example ERDs

A one-to-many relationship with one vendor selling many products

A many-to-many relationship with no primary key-foreign-key relationship. Many customers buy many products.

Page 22: Fg d

Using a Relational Entity for M:M Relationships We convert an M:M

relationship into two 1:M relationships by adding the PURCHASE relational entity between the CUSTOMER and the PRODUCT entities. This enables us to have the required primary and foreign keys to carry out queries.

Page 23: Fg d

Relational Data Models

The next step after creating an ERD is to convert it into a corresponding relational data model. For example, the relational data model for the 1:M ERD is shown to the right.

Note that the relational diamond is replaced by a “crow’s foot” representing the “many” end of the relationship.

Page 24: Fg d

Many-to-Many Relational Models

To model the many-to-many ERD, we add the PURCHASE entity and then use it to link the CUSTOMER and PRODUCT entities.

Page 25: Fg d

Complete Relational Data Model

We can combine the two models to link all four entities into one data model.

Page 26: Fg d

Querying Multitable Databases

The most common operation in querying multitable databases is the JOIN operation in which we create one table from two. For example, to query the PRODUCT-VENDOR data model shown earlier, the JOIN query would be:

SELECT * FROM Product, Vendor WHERE Product.VendorID = Vendor.VendorID This would result in a table containing all fields from both the PRODUCT and VENDOR tables where there is a match between the primary key in the VENDOR table and the foreign key in the PRODUCT table.

Page 27: Fg d

More Multitable Queries

To list products, vendor name, and item cost, in alphabetical order of item name, the query is: SELECT ItemName, VendorName, ItemCost FROM Product, Vendor WHERE Product.VendorID = Vendor.VendorID ORDER BY ItemName To output the item name, vendor name, item cost, discount, and the net cost, the query is:SELECT ItemName, VendorName, ItemCost, Discount, ItemCost*(1-Discount) as NetCost FROM Product, Vendor WHERE Product.VendorID = Vendor.VendorID ORDER BY ItemName

Page 28: Fg d

Creating Views

In most database systems, queries can be saved and reused.

A saved query is often referred to as a view. A query can be saved as a view and then used again

by itself or as part of another query. A query can be saved as a view for reasons of

security. By creating a view only showing fields that we want others to see, we can protect confidential information in the data such as customer information.

Page 29: Fg d

Using XML

Most companies today require the ability to share data and resources over the Web.

For many years companies have been using a system known as electronic data interchange (EDI) that is expensive and useful only to the very large companies.

With the rapid growth of the Internet, organizations have turned to the use of XML as a way of carrying out the same processes.

An XML file can be processed purely as data by a program, it can be stored with similar data on another computer, or, like an HTML file, it can be displayed.

Page 30: Fg d

Comparing XML to HTML

Feature HTML XML

Type of text Structured with meaning defined

Formatted with meaning inferred

Definition of structure

User-defined Predetermined

Retrieval Context-sensitive Limited

Searchability Searchable by text or meaning

Searchable only by text or format

Hypertext linkage

Extensive Limited

Page 31: Fg d

Setting Up an XML Document

The first step to creating an XML file is to decide which tags to use to describe the data that it is transferring over the Internet.

As with HTML, the tags are enclosed in greater-than and less-than symbols (< >).

However, unlike HTML, each beginning tag must have an ending tag. For example, if using a <PARTID> tag to describe the part ID, then there must be a matching <PARTID> tag.

These tags must come immediately before and after the data item, to ensure that there is no ambiguity or inconsistency about the description.

It is a good idea to provide a formal definition of all the data elements in the XML file. This can be done in one of two ways—using the Document Type Definition method or the XML Schema method.

Both can be incorporated into the XML file or created as separate files with DTD or XSD extensions, respectively.

Page 32: Fg d

Example Schema (XSD) File (companynew.xsd)

Page 33: Fg d

Example XML File Corresponding to companynew.xsd

Page 34: Fg d

Using Stylesheet Files

To display an XML file in a more readable form on a Web browser, we need to use an XML stylesheet (XSL) file.

This file uses a combination of HTML and XML tags and is itself an XML file.

Page 35: Fg d

Combining the Stylesheet and XML Files

Page 36: Fg d

Copyright 2008 John Wiley & Sons, Inc.

All rights reserved. Reproduction or translation of this work beyond that permitted in section 117 of the 1976 United States Copyright Act without express permission of the copyright owner is unlawful. Request for further information should be addressed to the Permissions Department, John Wiley & Sons, Inc. The purchaser may make back-up copies for his/her own use only and not for distribution or resale. The Publisher assumes no responsibility for errors, omissions, or damages caused by the use of these programs or from the use of the information herein.