1 What is database? Why do we use databases?. 2 This is a little database. One department has some...

40
1 What is database? What is database? Why do we use Why do we use databases? databases?

Transcript of 1 What is database? Why do we use databases?. 2 This is a little database. One department has some...

Page 1: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

1

What is database?What is database?

Why do we use databases?Why do we use databases?

Page 2: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

2

This is a little database.

One department has some staffs (teachers)

Page 3: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

3

This is a larger database.

Page 4: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

4

Before you can store information using a computer, you need to think about exactly which pieces of information you are going store, and what type of information they are.

The first thing you need to think about are the fields you are going to use. Fields are the categories of information that your database is going to store.

Page 5: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

5

Example:Example: OOur school gives out ur school gives out meritsmerits ( (awardsawards))

to students who have worked well.to students who have worked well. To produce a merits database, you To produce a merits database, you

would include fields such aswould include fields such as::– Student NameStudent Name– Group Group – Subject Subject – DateDate– Reason for MeritReason for Merit– Teacher, etc. Teacher, etc. 

Page 6: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

6

Once you've decided on the fields Once you've decided on the fields you're going to use, you can start you're going to use, you can start entering the data. All the entering the data. All the information for one person or information for one person or thing, i.e. the information for all thing, i.e. the information for all fields gathered together, is called fields gathered together, is called a record. In the merits database a record. In the merits database (table) (table) example, therefore, each example, therefore, each student who had a merit would student who had a merit would have a record in the database. All have a record in the database. All the records are then gathered the records are then gathered together in a tabletogether in a table..

Page 7: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

7

Field TypesField Types Most database programs allow, or rather require, Most database programs allow, or rather require,

that you give each field in your database a type. that you give each field in your database a type. The field type indicates what type of information The field type indicates what type of information is going to be stored in that field.is going to be stored in that field.

Common field types are:Common field types are:– whole numberswhole numbers (1,2,3...) (1,2,3...)– decimal numbersdecimal numbers (1.23,one point two three) (1.23,one point two three)– texttext– DatesDates (2nd March 2003) (2nd March 2003)– Boolean (or yes/no)Boolean (or yes/no)

Page 8: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

8

Page 9: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

9

Notes:Notes: Some more advanced databases also let you Some more advanced databases also let you

store multimedia objects, such as pictures or store multimedia objects, such as pictures or soundssounds

Field types form a basic type of validation Field types form a basic type of validation in that the database won't allow you to in that the database won't allow you to enter, say, text in a date fieldenter, say, text in a date field .(1) .(1)

They also facilitate sorting, as the database They also facilitate sorting, as the database "knows" more about the contents of the "knows" more about the contents of the field. field. (2)(2)

Page 10: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

10

Key Field/Unique KeyKey Field/Unique Key In order to produce a logical structure for your database, In order to produce a logical structure for your database,

there should be something unique about each record in there should be something unique about each record in your database. This will normally be the contents of one your database. This will normally be the contents of one particular field, called the particular field, called the key fieldkey field .(1) .(1)

Sometimes you can't identify a Sometimes you can't identify a single keysingle key field in your field in your database table. database table. (2)(2)

In cases such as this, you can create what is known as a In cases such as this, you can create what is known as a compound keycompound key - a combination of fields that is unique. - a combination of fields that is unique. This can be any number of fieldThis can be any number of fieldss, but should be the , but should be the minimum number required to produced a unique minimum number required to produced a unique description of the record. description of the record. (3)(3)

Page 11: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

11

Cars - Cars - registration number registration number Table(Name, Mother’s name, Address,Table(Name, Mother’s name, Address,

Company name)Company name) Compound key e.g:Compound key e.g:

(first name,city,street,mother’s first (first name,city,street,mother’s first name)name)

Compound key in merit:Compound key in merit:((student, date and subjectstudent, date and subject))

Page 12: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

12

IndexesIndexes The final thing you can create for your fields is The final thing you can create for your fields is

an index. Once you've started using your an index. Once you've started using your database, the data can be stored in the table in database, the data can be stored in the table in any order, probably the order in which you any order, probably the order in which you enteredentered it it. . (1)(1)

An index is just like the index in a book - it is an An index is just like the index in a book - it is an extra bit added on to the database to help the extra bit added on to the database to help the database program find records quickly. database program find records quickly. (2)(2)

Page 13: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

13

11 WhiteWhite

22 BlackBlack

33 YellowYellow

44 BrownBrown

55 RedRed

66 GreenGreen

7 7 GrayGray

88 OrangeOrange

99 PinkPink

1010 VioletViolet

22 BlackBlack

44 BrownBrown

77 GrayGray

66 GreenGreen

88 OrangeOrange

99 PinkPink

55 RedRed

1010 VioletViolet

33 YellowYellow

11 WhiteWhite

The key is the name of colours. Index table

Page 14: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

14

ValidationValidation

Validation is the name given to the process Validation is the name given to the process whereby the information entered in the whereby the information entered in the database is checked to ensure that it makes database is checked to ensure that it makes sensesense (understandable) (understandable). . (1)(1)

Page 15: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

15

BBasic typeasic typess of validation of validation

TTypeype:: PresencePresence:: UniquenessUniqueness RangeRange FormatFormat Multiple Choice Multiple Choice Referential Integrity Referential Integrity

Page 16: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

16

TypeType

The use of field types forms a basic type of The use of field types forms a basic type of validation. If you make a particular field validation. If you make a particular field numeric (i.e. a number), for example, then it numeric (i.e. a number), for example, then it won't let you enter any letters or other non-won't let you enter any letters or other non-numeric characters. numeric characters. (1)(1)

Page 17: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

17

PresencePresence

This type of validation might go by This type of validation might go by different names, depending on your different names, depending on your database program - sometimes it's called database program - sometimes it's called something like Allow Blank or mandatorysomething like Allow Blank or mandatory (kötelező)(kötelező) for example. for example. This type of This type of validation forces the user to enter the data validation forces the user to enter the data in that field.in that field. (1) (1)

Page 18: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

18

UniquenessUniqueness

Some database programs allow you to Some database programs allow you to check whether the contents of a particular check whether the contents of a particular field are unique. This might be useful to field are unique. This might be useful to prevent users entering the same information prevent users entering the same information twice. For example, if you were creating a twice. For example, if you were creating a car database, you should make the car database, you should make the registration number field unique as no two registration number field unique as no two cars should have the same number.cars should have the same number.

Page 19: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

19

RangeRange

If you're using a number field, then If you're using a number field, then you might want to limit the range you might want to limit the range of inputs. For example, you might of inputs. For example, you might want to limit prices in a stock want to limit prices in a stock database so that they are all database so that they are all positive, or limit the range of a positive, or limit the range of a percentage field so that the values percentage field so that the values entered are between 0 and 100.entered are between 0 and 100.

Page 20: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

20

FormatFormat

You might have a field in your database that You might have a field in your database that requires an entry in a particular requires an entry in a particular format.format.

A simple example might be a date, or piece of text A simple example might be a date, or piece of text of a certain length.of a certain length.

More complex examples might include things like More complex examples might include things like postcodes, or National Insurance or driving licence postcodes, or National Insurance or driving licence numbers.numbers.

If you're using database management system If you're using database management system (such (such as as AccessAccess)), you can define your own formats using , you can define your own formats using an input mask, which defines the valid charactersan input mask, which defines the valid characters. .

Page 21: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

21

Multiple ChoiceMultiple Choice

A good way to validate fields is to use multiple A good way to validate fields is to use multiple choice responses. These might take the form of a choice responses. These might take the form of a list box, combo box, or radio button. For example, list box, combo box, or radio button. For example, you could create a field that would only allow the you could create a field that would only allow the user to select from Yes or No, or Male or Female. user to select from Yes or No, or Male or Female. This can be an especially useful techniques in This can be an especially useful techniques in database applications such as Access, which allow database applications such as Access, which allow you to dynamically generate the choices.you to dynamically generate the choices.

Page 22: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

22

Choose an item from these scrolldown tables,Choose an item from these scrolldown tables, radio box or spinner: radio box or spinner:

Page 23: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

23

Referential IntegrityReferential Integrity

Finally, if you're using a relational database , then Finally, if you're using a relational database , then you can enforce referential integrity to validate you can enforce referential integrity to validate inputs. inputs. This means you can check entries in This means you can check entries in certain fields against values in other tablescertain fields against values in other tables . For . For example, in the merits database, when a new merit example, in the merits database, when a new merit is entered, you could check the names of the is entered, you could check the names of the students and teachers against the student and staff students and teachers against the student and staff tables, to prevent either spelling tables, to prevent either spelling mistakesmistakes, or the , or the entry of merits for students that don't exist.entry of merits for students that don't exist.

Page 24: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

24

So far we've only discussed having So far we've only discussed having one table in a database. In the one table in a database. In the merits database, for example, we've merits database, for example, we've talked about using the following talked about using the following fields to store the information about fields to store the information about the merits:the merits:

Page 25: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

25

Data RedundancyData Redundancy This database structure would do the job, This database structure would do the job,

but think about what would happen as you but think about what would happen as you enter the merits. For each new merit you enter the merits. For each new merit you add, you need to enter all of the teacher and add, you need to enter all of the teacher and student details each time. This would student details each time. This would involve a lot of data being duplicated, and involve a lot of data being duplicated, and therefore take up a lot more disc space. This therefore take up a lot more disc space. This is known as redundancy.is known as redundancy.

Page 26: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

26

Now let’s go back to our dataNow let’s go back to our data

We have toWe have to separat separatee out the out the name of the students, the teachersname of the students, the teachers and the groups and the groups into into new tables.new tables. In these new tables we have to create IDIn these new tables we have to create ID (Identification) numbers to the teachers, the students and (Identification) numbers to the teachers, the students and

the groups.the groups. In the main table (the merits) we no need the columns Name, Group, Year and Tutor, instead of In the main table (the merits) we no need the columns Name, Group, Year and Tutor, instead of

these we need only three simple column with the ID_teacher, ID_student and the ID_groups.. these we need only three simple column with the ID_teacher, ID_student and the ID_groups.. The The tablestables are then linked by are then linked by the ID_teacher,ID_student and the ID_groups. the ID_teacher,ID_student and the ID_groups.

Page 27: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

27

11

1

Page 28: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

28

Page 29: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

29

The tables, - tThe tables, - the logical units, or entities,he logical units, or entities, - - in the in the merits system are the students, the merits system are the students, the teahers the teahers the groupsgroups, and the merits themselves. This means that , and the merits themselves. This means that the details of the students the the details of the students the teachersteachers and the and the groups groups only need to be entered once. When you only need to be entered once. When you enter a new merit, you only need to enter enough enter a new merit, you only need to enter enough information to identify the studentinformation to identify the students, the teacherss, the teachers and the and the groupsgroups - the remaining information can - the remaining information can then be looked up from the then be looked up from the teacher, groupteacher, group and and student tables. student tables.

Page 30: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

30

Degree of Relationship

You will probably have noticed that the relationships have little symbols at the end, either a 1 or the infinity symbol. These indicate the degree of the relationship, where the infinity symbol means many .

The degree indicates the nature of the relationship between the entities - for example, in the merits database shown above, one teacher can give many merits, and each student can have many merits given to them. On the other hand, each merit can only be given by one teacher, and can only be given to one student.

Page 31: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

31

Obviously, there are three possible Obviously, there are three possible types of relationship:types of relationship:

– one-to-oneone-to-one– one-to-many (or vice-versa)one-to-many (or vice-versa)– many-to-manymany-to-many

To examine the types of relationship we need a new To examine the types of relationship we need a new example:example:

Page 32: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

32

In this example, the jokes would be stored In this example, the jokes would be stored entirely in the databaseentirely in the database..

For our joke database, we would For our joke database, we would probably start with a table called "jokes" probably start with a table called "jokes" which would contain a list of jokes. which would contain a list of jokes. OOur ur "jokes" table might have columns for the "jokes" table might have columns for the text of the jokestext of the jokes, the a, the authoruthor, the e-mail of , the e-mail of the authorthe author and the dates and the dates of of the jokes were the jokes were added to the databaseadded to the database..

Page 33: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

33

11 Kevin Kevin YangYang

""Why did the chicken cross theWhy did the chicken cross the road?road?”” To get to the other sideTo get to the other side!!

[email protected]@mail.jokejoke

22 Kevin Kevin YangYang

What do you get if you cross a What do you get if you cross a kangeroo with a sheep? kangeroo with a sheep? A wooly A wooly jumper!jumper!

ky@[email protected]

33 Joan Joan SmithSmith

What do you call a waitress What do you call a waitress balancing 6 beers on her head ? balancing 6 beers on her head ? Beatrix!Beatrix!

ky@[email protected]

Table of jokes

But, I hope that to this time everybody know that there is something problem with this single table.What if an author changed her email address? She might begin submitting new jokes using the new address, but all the old jokes would still have the old address attached to them. Looking at your database, you might just think there were two different people with the same name Smith submitting to your database.

Page 34: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

34

What the above two tables show are three jokes and two authors. The AID column (short for "Author ID") of the Jokes table provides a relationship between the two tables (indicating that Kevin Yank submitted jokes 1 and 2 and Joan Smith submitted joke 3). Notice also that, since each author now only appears once in the database, and appears independently of the jokes he or she has submitted, we have avoided all the problems outlined above

11 11

22 11

33 22

AID

11 Kevin YangKevin Yang

22 Yoan SmithYoan Smith

AID

Page 35: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

35

one-to-one relationshipone-to-one relationship

In the case of a simple one-to-one relationship, In the case of a simple one-to-one relationship, a single table is all that is needed. An example a single table is all that is needed. An example of a one-to-one relationship that we have seen of a one-to-one relationship that we have seen is the email address of each author in our joke is the email address of each author in our joke database. Since there will be one e-mail database. Since there will be one e-mail address for each author, and one author for address for each author, and one author for each e-mail address, there is no reason to split each e-mail address, there is no reason to split the addresses off into a separate table.the addresses off into a separate table.

Page 36: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

36

many-to-one relationship many-to-one relationship A many-to-one relationship is a little more A many-to-one relationship is a little more

complicated, but we have seen one of these complicated, but we have seen one of these already as well. Each joke in our database is already as well. Each joke in our database is associated with just one author, but many jokes associated with just one author, but many jokes may have been written by that one author. This may have been written by that one author. This joke-author relationship is many-to-one. joke-author relationship is many-to-one.

Page 37: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

37

one-to-many relationshipone-to-many relationship In our database so far, we have assumed that In our database so far, we have assumed that

each author only has one email address.(1) each author only has one email address.(1) If we did, however, want to support multiple If we did, however, want to support multiple

email addresses, we would be faced with a email addresses, we would be faced with a one-to-many relationship (one author may one-to-many relationship (one author may have many email addresses, but each email have many email addresses, but each email address belongs to exactly one author).address belongs to exactly one author).

Page 38: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

38

Many-to-Many RelationshipsMany-to-Many Relationships

You decide to place your jokes into You decide to place your jokes into categories such as "Knock-Knock Jokes", categories such as "Knock-Knock Jokes", "Crossing the Road Jokes", "Lawyer "Crossing the Road Jokes", "Lawyer Jokes", and "Political Jokes". Remembering Jokes", and "Political Jokes". Remembering our rule of thumb from earlier, you identify our rule of thumb from earlier, you identify joke categories as a different type of joke categories as a different type of "thing", and create a new table for them:"thing", and create a new table for them:

Page 39: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

39

11 Crossing roadCrossing road

22 LawyersLawyers

Now comes the very difficult task of giving categories to your jokes. It occurs to you that a "political" joke might also be a "crossing the road" joke, and a "knock-knock" joke might also be a "lawyer" joke. A single joke might belong to many categories, and each category will contain many jokes.

This is a many-to-many relationship.

Page 40: 1 What is database? Why do we use databases?. 2 This is a little database. One department has some staffs (teachers)

40

The correct way to represent a many-to-many relationship is to use a "lookup table". This is a table that contains no actual data, but which serves to define pairs of entries that are related. Here's what the database design would look like for our joke categories:

JIDJID CIDCID

11 11

11 22

22 22

The JokeLookup table associates joke ID's (JID) with category ID's (CID). Looking at the above example, we can see that the joke beginning with " Why

did the chicken …" belongs to both the „Crossing road" and "Lawyer" categories.