Presentation on theme: "What is a Database? A collection of organized information on a particular subject. It is organized so that queries and reports based on the information."— Presentation transcript:
What is a Database? A collection of organized information on a particular subject. It is organized so that queries and reports based on the information can be readily performed. Common examples of databases: – telephone book – Student records
Other databases - Company records - Police records - Census records - University records The organization is important to be able to retrieve information from a data base.
Databases and Spreadsheets Databases more for organization of large amounts of non numeric data where queries are more important then computation. Spreadsheets are more powerful computational tools.
. DBMS: A database management system is a collection of software for managing the database and contains software for organizing the data and verifying it, as well as software for retrieving the data through queries as well as creating reports based on the data.
DBMS Software There are different DBMS software versions available: – Microsoft Access (which we’ll be using) – Microsoft SQL Server (more features than Access) – Oracle – Corel Paradox – MySQL (open-source) – a lot more…
A DBMS can: -Create / Delete tables -Modify tables: add records delete records edit records rearrange records change the table structure Retrieve data from a single / multiple table(s) find and display a record answers queries Create reports formatted displays of query results or table contents
Tables and Records The basic organizational tool in the database is the table which is organized into records which are subdivided into fields. Conceptually it looks like: Id Lastname Firstname Age Weight 1 Smith Joe 25 70 2 Lee Jane 57 45 3 Harper Stephen 60 67 The data base may be made up of several tables of interrelated information
A record contains a number of fields: Examples of fields are : Student number (This field is called a key) Last name First Name Degree Program Address Child of Alumnus And field may be of different types text, number, dates etc.
A particularly important field is called a key. A key is a field that uniquely identifies the record. This field is called the primary key of the table. A name would not be a suitable key because it is not unique. Suitable keys are -student number -social insurance number -drivers license number
Structure of the Table The structure of the table is the specification of fields for records in table. The structure specifies the type (or kind of data) for each field. Different types of fields are treated differently – when they are entered – when they are displayed – when they are used
Field Types Examples of field types: – integer (whole numbers, ex: 1, 55, 993) – currency (ex: dollars, pounds, euros) – real number (ex: 1.034, 55.3321, etc) – time (ex: 10:42) – date (03/03/09, or January 1 st 2006). – string (text)
The Four basic operations in a database. Design: – Create the database by defining the tables and Specifying the fields. Data Entry: – Adding the actual data by hand or imported from other files. Easy to make errors. Queries: – Ask questions about the data. Reports: - producing a document with info
Video Store Example We want to create a database for a video store. Questions: – How will information be organized? – What will we represent as tables? – What information is required?
Video Store Example (2) Decision: – Three groups of information (tables). – Videos – Customers – Rental information. For each table: – What information would we like to maintain? – What will be the unique primary key (or composite key)?
Table 1: The Video Table Required information: – Video ID (primary key) – Title – Price – Quantity – Category
Access Database Objects First we’ll see some objects defined in Access Databases. Tables: store your data in rows and columns. All databases contain one or more tables. Queries: retrieve and process your data. They can combine data from different tables, update your data, and perform calculations on your data.
Access Database Objects Forms: control data entry and data views. They provide visual cues that make data easier to work with. Reports: summarize and print your data. They turn the data in your tables and queries into documents for communicating ideas.
Microsoft Access in MS Office First of all, not all versions of Microsoft Office include Access. – only the “Professional” version of office includes Access, NOT the student version. – NOT part of any version of Office for the Mac.
Starting Microsoft Access As with other Microsoft Office applications, you can start Access by: – double-clicking the desktop icon of there is one. – double-clicking an Access file you have created previously. – going through the Start menu.
Creating a New Database In order to create a new database in Access we can first select what type of database we want. Access has several built-in templates such as: – Business – Personal – Sample – Education (Faculty, student).
We wont be using the templates but starting from a blank database. But we could use one of the templates. Faculty – Keep track of information about faculty members such as names, contact information, etc. Student – Names, contact, medical information, etc. For example: Education Databases
Creating a Blank Database Of course if none of these templates meet our requirements, we can create a blank database. Click on Featuring, then the Blank Database option.
Creating a Blank Database (2) After clicking the Blank Database option, a menu will show up on the right that allows you to chose a filename and location for your new blank database.
Access Window (2) The Microsoft Access window should look a bit familiar. – uses the same types of tabs as used by Excel 2007 and also other Microsoft 2007 office products – The table panel actually looks a lot like Excel with Rows (Records) and Columns (Fields).
Access Toolbars As with the other Microsoft software we have seen Access has multiple Tabs or toolbars instead of menus. Like we have done for the other applications, we’ll see some of the Tabs relevant to Access, the others we have seen before.
Create Tab The Create tab contains options to create new: – tables – forms – reports, etc
External Data Tab The External Data tab allows the database creator to import data from other sources such as: – Excel File – Other Access file – Text files – etc…
Database Tools Tab The Database Tools tab allows us to create relationships between tables or with other databases, etc.
Datasheet Tab The Datasheet tab allows us to add new fields, change the type for the fields, edit relationships, etc.
Access Views There are several different views you can use for your database, we’ll see the two most useful: – Datasheet View: which displays the fields and the records. This view is useful to enter records. – Design View: This view allows us to add fields, change the field type, set the Primary key. This view does not show the records. – You can switch view with the Views button.
Tables: Fields and Records Databases in Access contain at least one table. Each column( e.g. Category) is a field. Each row is a record. A record is a collection fields. A field is a single piece of information.
Tables Tables store data The most important building block of a database. If the table is well designed then data should not be duplicated. Having to duplicate data shows poor design Table is composed of records and each record is composed of fields.
Attributes of the Table Fields Primary Key: the unique identifier for each record (row) Data Type: specifies what kind of data in going to be stored in each field.
More on Primary Keys Primary Keys are like fingerprints: there are no two human beings with identical fingerprints. Between different records, the fields that are not part of the primary key may be equal. In the case of composite key, the full set is unique for each record (individually they might have duplicates).
More on Primary Keys (2) When you create a table, access allows you to specify which field will be the primary key. Also, Access can assign a numeric integer data increment in 1 unit automatically, every time you enter a new record. – Called ID by default. – Each record will have a unique ID number. – You can change the field name to a user friendly one such as VIDEO_ID or STUDENT_ID.
Assigning Primary Keys Right click over the field you want to specify as primary key. Click on Primary Key. A key will appear next to the field.
Assigning Primary Keys (2) You can also use the Primary Key button at in the Design toolbar.
We’ll demonstrate how to create a database by using the Video store example we have seen last lecture. The Video Store database had three tables: – Video – Customer – Rental Information. We’ll show step by step how to create this database using Microsoft Access. We’ll first create our Video table.
Step 1: Creating the Database Our first step is to create a blank database document. We’ve seen how to do this last lecture. After opening Access, click on Blank Database and enter a name. Here we’ll use the name VideoStore.
Step 2: Creating the Table By default a new blank database will have one table (Table1). Instead of creating a new one we’ll change the name of this first table by saving it. Click the Office button, then Save. Enter the table name Video. Before Saving: After Saving:
Step 3: Adding Fields We are now ready to add new fields to our Video table. We can add fields in different ways, if you notice in the Datasheet view below, we have the option to “Add New Field” but instead we’ll use the Design View.
Step 3: Adding Fields (2) At the top left of the Access window, click on the View button then select Design View. This will change the view of the window to the Design View which will make it easier for us to enter new fields.
Step 3: Adding Fields (4) Be default new tables will have at least one field: ID. This field is also the default primary key. ID will increment automatically (AutoNumber) as we insert new records. This way, Access can be sure that your table will have a unique field to use for a primary key.
Step 3: Adding Fields (5) We’ll keep this ID field, but we’ll rename it to something more meaningful for us, VideoID. What other fields did we need for our Video table? What type should each field be ($, text, number)? – Title (type: Text) – Price (type: Currency) – Quantity (type: Number) – Category (type: Text). We simply enter each field name and type into our field table.
Step 3: Adding Fields (6) We can also enter descriptions for each field, just in case we forget what each field was for. All our fields are entered for this table.
Adding a New Table To add a new table after the first one, click on the Create tab, then click the Table button.
Adding a New Table (2) To change the name of the new table you must also save it (Office button, then Save). Change the name to Customer.
Customer Table Here is our Customer table after entering all the fields like we did for the Video table.
Issues with entering data: Very dull and prone to errors. DBMS’s have features to help avoiding errors and help entering data. Typically they are called “validation” tools They are found in the bottom part of the design view window
Input masks Also called "pictures" in some DBMS’s Allow for ease in entering text or date data by providing a template which is enforced by the DBMS Thus for example: A postal code such as K1S 5B6 Can have a mask: >L0L 0L0 > Capitals L: a letter required 0 number required
Input masks Also called "pictures" in some DBMS Used for text, date and time only. CharacterDescription 0Number required 9Number or space optional #Number or space (entry optional LLetter required (A through Z) ?Letter (A through Z, entry optional). ALetter or number (entry required). aLetter or number (entry optional). &Any character or a space (entry required). CAny character or a space (entry optional).., : ; – /Decimal placeholder and thousands, date, and time separators. Converts all characters that follow to uppercase. \Causes the character that follows to be displayed as a literal character (for example, \A is displayed as just A).
Examples of input masks (some predefined) >L0L\ 0L0 Postal code 000\ 000\ 000 Social insurance number (999) 000-0000 telephone number (digits or blanks for the 9’s)
Note that one can have variations of an input Mask, not all of which are equally strong in enforcing a particular mask. blank e.g. Postal Code K1S 5B6 (all upper case) >L0L\ 0L0 The best because it forces everything L0L\ 0L0 Does not enforce upper case AAA\ AAA A Letter or number (entry required). aaa\ aaa a Letter or number (entry optional). &\ # & any character or space # any digit or space
Validation rules: e.g. For numbers >45 >45 AND <100 Between 10 and 25 ( this is inclusive) e.g. For text Input mask: L Validation rule: "M" Or "F " A single letter which can only be "M" Or "F“, however this is not case sensitive. It will accept uppercase or lower case.
Validation rule: "Male" Or "Female “ Or “M” or “F” Will accept ony these (not case sensitive) Can we enforce that it should be capitalized but only the first letter? We put a validation rule: "Male" Or "Female “ Or “M” or “F” And an input mask: >L???? The “?” means an optional letter.
Other aspects of “field properties” for text. Validation text: Text you want to use when an error is made. Field size: How many characters are allowed. Default value: The system can always put in this value if you do not use another valid value. Required: The system does not allow you to leave this blank.
Restructuring a table after there is data in it. You must be very careful. You can always add a new field without problems. You can always delete a field. (right click on field in design view and delete) BUT problems with deleting field in related tables. You can change the type of field if it’s compatible with the data. Problems if the change if not compatible with data