Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL - Creating donorof database offline

Similar presentations


Presentation on theme: "MySQL - Creating donorof database offline"— Presentation transcript:

1 MySQL - Creating donorof database offline
Please use speaker notes for additional information. In this example, I am offline and creating a donor database. I called it donorof to indicate that it was donor created offline.

2 First, I am creating the database called donorof and then I am creating a table within the database called donorofl. Note the use which selects the correct database before creating the table. Note that SQL allows you to put commands etc in capital letters if you choose to make them stand out - that is actually my preference, but I chose to do it this way in this example. This example shows the creation of the database, selecting the database for use and the creation of a table within the database. Note that varchar is used for character fields, date is used for a date field with the format year-month-day, and numeric with the total number of characters and the number of decimal places is used for a numeric field with decimals. Note that instead of numeric, I could have used the type decimal. Be sure to read notes on data types.

3 I made an error and entered donorof1 instead of donorofl the first time. Note the error that comes back refers to the database and the table within the database and tells us that table does not exist. When I did it right, I got the description. Note that I entered numeric and it shows decimal. MySQL says it handles these the same. Note that I entered state as varchar(2) and it got changed to char(2). MySQL does this when there are less than 4 characters. The SQL command desc donorofl shows a description of the table that I just created. Note that idno is the primary key.

4 Note that the date format is year, month, day
Note that the date format is year, month, day. The numeric field is not enclosed in quotes and varchar and date fields are enclosed in quotes. Remember, the select * says show all fields. Note the wrap in the select is splitting the display of the yrgoal. I have just inserted a record into the table donorof1. After inserting the record, I did a select all from the table to show me the record I just inserted.

5 I have now inserted 3 more records into the table
I have now inserted 3 more records into the table. Note that Carl Hersey has no yrgoal so I entered null - another option would have been to enter an empty string. I made a mistake the first time I tried to enter Carl Hersey. I forgot to enter the date. Notice the error message which says that the column count doesn't match value count - meaning I did not have the correct number of columns/fields. Note that instead of null, I could have decided to do "" when there was no data. Be sure to read about the subtle differences between null and empty string. After reading the definition, I decided that I would change from null to empty string since I really mean that there was not donation - you can see this in a future slide. A quote from the MySql manual “Both statements insert a value into the phone column, but the first inserts a NULL value and the second inserts an empty string. The meaning of the first can be regarded as ``phone number is not known'' and the meaning of the second can be regarded as ``she has no phone''.”

6 Note that the entry of Nancy Taylor did not have the comma after the column with MA in the first line - I pressed enter too fast. To make up for this mistake, I entered the comma as the first thing on the next line. This slide shows the entry of the last two records and then a display of the records using select * from donorofl;

7 Note that I am not sure whether I want null or empty string
Note that I am not sure whether I want null or empty string. I am experimenting so I decided to change to empty string. I can always change back after doing more research. Besides, this gave me a chance to use the update command. I am updating the donorofl table and setting the yrgoal equal to the empty string on the record that has an idno of The where is critical here, without it I would have changed the yrgoal on all of the records - not a good plan!

8 Results of the update. Note the impact of the update, where it used to say null, it now says Not sure I like that - I would have liked to see nothing there for an empty string.

9 The select is only showing 4 columns: idno, name, stadr and city.
Note the error when I used stradr instead of the correct column name stadr. The select is only showing 4 columns: idno, name, stadr and city. The where means it is only showing the records/rows where the city is equal to Fall River.

10 Be sure you are reading about SQL as it is implemented in MySQL- it will help tremendously in understanding these examples. Note for one thing that the order of the fields can be whatever the programmer wants the order of the display. In this example, I want the five specified fields to show for all records/rows where the yrgoal is either greater than or equal to 100.

11 One of these queries uses the and and one of these queries uses the or.
And means that both must be true. Or means that either of the criteria can be true.

12 N Y yrgoal > 75 city = Fall River Y N datefst > 2001-01-01
In this query, yrgoal must be greater than 75 or the record/row will not be selected. If yrgoal is greater than 75 then processing is continued by checking to see if either the city is Fall River or the datefst is greater than If either of these is true, then the record is selected. datefst > Select N Y Select

13 This slide shows the creation of a table called donorofl and the insertion of records into the table. Note that when I first did it I accidentally called the date contamt (you can't see that, but I did) as well as calling the amount field contamt. When I tried to use contamt as part of the key it caught the duplication of the data. Note that on the next slide you will not see the fourth insert because I crashed before I hit enter.

14 This shows the new table and its contents.
Notice that I started up MySQL again and said to use the database donorof. Then I did a select from the donationof table and finally in the second image, I did a description of donationof. More about primary keys on the next slide.

15 We have now added the fifth record.
Looking at the description, you can see that primary key was established on all three of the fields. This gives us the primary key made up of all three fields. Remember a primary key must give us a unique record. There could be two contributions from the same idno. There could be two contributions from the same idno to the same driveno. However, as the analyst on this project, I have decided that there will not be two contributions from the same idno to the same driveno on the same date. If there is, I will hold one until the next day. Alternatively, I could have used a different format for the date to include time which would make contdate add that unique feature because two contributions will not be processed at exactly the same time. Note that contamt relates to all three parts of the key. It is a contribution from a particular idno, to a particular drive on a particular date. The rules of third normal form say that any data kept in this table must relate to all parts of the primary key. I have met the requirement.

16 Note that I have now created the driveofl table with the primary key of just the driveno since the driveno is going to be unique. Note that I made driveno varchar(3) and it got changed to char(3). MySQL changes all varchar columns with a length less than 4 to char.

17 Data has been entered and a select shows the three records that were entered.

18 From donationofl From driveofl
In this example I am using the where clause to relate two tables together based on their driveno. Note that the information shown comes from two tables. Note that driveno could be taken from either table. When it occurs in multiple tables you specify which one by saying the table name. the column name. From donationofl From driveofl

19 From Oracle: This example relates 3 tables.
I was surprised when I only got two records, so I ran the same query in Oracle and got 4 records. Interesting to note the difference. Oracle based its comparison on donation where there are 4 records - each shows up. MySQL based its query on the fact that there were three unique records in drive, or at least that is what it looks like to me. For you who are familiar with my Oracle examples, note that donationone and driveone are my names for the original set of records which is the same (except for some date changes) as the records I inputted into MySQL (donor did not change so I could use the original name). SQL> select donor.idno, name, yrgoal, contamt, donationone.driveno, drivename, lastyear 2 from donor, donationone, driveone 3 where donor.idno = donationone.idno and donationone.driveno = driveone.driveno; IDNO NAME YRGOAL CONTAMT DRI DRIVENAME LASTYEAR 11111 Stephen Daniels Kids Shelter 23456 Susan Ash Kids Shelter 12121 Jennifer Ames Animal Home 33333 Nancy Taylor Health Aid


Download ppt "MySQL - Creating donorof database offline"

Similar presentations


Ads by Google