SQL Unit 12 Creating Tables, Keys, and Constraints in Access

Preview:

DESCRIPTION

SQL Unit 12 Creating Tables, Keys, and Constraints in Access. Kirk Scott. 12.1 Creating a Blank Database in Access 12.2 Creating a Table Using Access's Graphical User Interface 12.3 Specifying Referential Integrity Using Access's Graphical User Interface 12.4 Entering Data - PowerPoint PPT Presentation

Citation preview

1

SQL Unit 12Creating Tables, Keys, and Constraints in Access

Kirk Scott

2

• 12.1 Creating a Blank Database in Access• 12.2 Creating a Table Using Access's

Graphical User Interface• 12.3 Specifying Referential Integrity Using

Access's Graphical User Interface• 12.4 Entering Data• 12.5 Assignment Description

3

12.2 Creating a Blank Database in Access

• In order to create tables, you need a blank database to put them in.

• In order to do this in Access, click on the Office Icon in the upper left hand corner.

• In the menu there will be a New option. •

4

5

• If you click on the New option, the screen shown on the following overhead will appear.

• The option to name and create the new database is on the right hand side of the screen.

• By default, the database will probably be saved to the Documents folder on the machine you're using.

6

7

• You can type in a different database name if you want to.

• You can also click on the little folder icon and browse for a different destination now

• Or later on you can take the Save option in the menu and specify a location to save it to at that time

8

• In any case, to finish creating a new database, you will need to click on the Create button

• When you do so, the screen shown on the following overhead will appear.

• This is one place/way that tables can be defined.

• Access is trying to be helpful, but as you’ll see, we don’t really want to make use of this screen.

9

10

• The system has provided a table named Table1 as part of the new database you’re creating.

• Click on the X in the upper right hand corner of Table1, closing it.

• This will take you to a completely blank Access screen.

• In that screen, click on the Create tab.• The blank screen with the Create tab highlighted

is shown on the following overhead.

11

12

• There will be several options listed under the Create tab.

• 1. The leftmost icon will be labeled Table. – DO NOT TAKE THIS ONE.

• 2. The icon fourth from the left will be labeled Table Design. – THIS IS THE ONE TO TAKE.

13

• If you mistakenly took number 1 above, the system would take you to a table design screen where a key field has already been put into the table.

• The system is trying to be helpful, but it isn't a help, it's a hindrance…

• The system assumes that you don’t know what a key field is

• We know, and we want to define our own

14

• If you take the fourth icon from the left, as desired, you will end up with a completely blank table design screen

• This screen is shown on the following overhead

• If you reach this point, you are ready for the next section, where table definition is covered.

15

16

12.2 Creating a Table Using Access's Graphical User Interface

• On the following overheads, the SQL commands are given for creating two tables, Mother and Person, in a one-to-many relationship.

• The primary key of the Mother table is embedded as a foreign key in the Person table.

17

• Following the table definitions is a query that would create an index on the lastname field of the Mother table.

• This is done to refresh your memory.• The goal of this unit is to show how to do all of

this using MS Access's graphical user interface.

18

• CREATE TABLE Mother• (SSN TEXT(9),• lastname TEXT(24)• CONSTRAINT motherpkSSN PRIMARY

KEY(SSN))

19

• CREATE TABLE Person• (SSN TEXT(9),• lastname TEXT(24),• motherSSN TEXT(9),• dob DATE,• CONSTRAINT personpkSSN PRIMARY KEY(SSN),• CONSTRAINT personfkmother FOREIGN KEY(motherSSN)• REFERENCES Mother(SSN)• ON DELETE RESTRICT• ON UPDATE CASCADE)

20

• CREATE INDEX motherSSNindexasc• ON Person(motherSSN)

21

• SQL is the preferred alternative for writing queries, but for designing and creating tables, the graphical user interface is preferred.

• If you have understood the discussion of table definitions, constraints, and indexes as described using SQL, then you know the general concepts.

22

• These concepts appear in a different form, and in some cases, using different words, in the graphical user interface.

• This interface is preferred for two reasons, one good and one bad.

• The good reason is that it can be easier to use than the SQL syntax.

23

• The bad reason is that in Microsoft Access table design, if you attempt to do something using SQL, there is no guarantee that in all cases it will work.

• When Microsoft designed Access, the graphical user interface was the primary interface, so if you do something there, it should work.

24

• Here is a logical outline of what is involved in creating tables.

• Specifying referential integrity is done after the related tables are created using a different view in the graphical user interface.

• That will be covered in the next section of the unit.

25

• On the following overheads an outline is given of the steps you would follow when using the graphical user interface.

• It is organized in an order that makes sense when using the graphical user interface

• It includes options that correspond to the points listed above.

26

• This outline is also a preview of everything you will find in the interface, and it lists options that can be skipped.

• After everything is summarized in the outline, the things listed in the outline will be shown with screenshots.

27

Creating tables

• At the top of the design view for a table:– Give each field a name. – As soon as you enter this first field, the bottom of the

design view will expand to include the options listed under the following black bullet.

– If the field is the primary key field, right click on it and specify that

– Alternatively, with that field highlighted, click on the key symbol above

– Give each field a data type

28

• At the bottom of the design view there will be a long list of options:– DO: Specify a Field Size for those data types where the user

can specify a size (like the width in characters of a text field)– SKIP: Specify a field Format– SKIP: Specify a field Input Mask– SKIP: Specify a field Caption– SKIP: Specify a field Default Value– SKIP: Specify a field Validation Rule– SKIP: Specify a field Validation Text

29

– DO: Specify whether the field is required or not. – This is the equivalent of specifying NOT NULL. – If the field is not required, then NULL is allowed. – Notice this unpleasant fact: – Even though you indicate that a field is the

primary key field, this does not automatically change the setting to "Required—Yes".

– To be on the safe side, it would be wise to check "Required—Yes" on the primary key field.

30

– DO: Specify whether Allow Zero Length is OK. – This is a detail that is related to the question of nulls.

– A zero length data value is virtually a null. – Notice this unpleasant fact: – Even though you indicate that a field is the primary

key field, this does not automatically change the setting to "Allow Zero Length—No".

– To be on the safe side, it would be wise to check "Allow Zero Length—No" on the primary key field.

31

– DO: Specify whether the field is Indexed. – There are two options, No Duplicates and Duplicates

Allowed. – Either way, the field is indexed. – Duplicates vs. No Duplicates corresponds to

specifying NOT UNIQUE or UNIQUE in SQL. – Note that when you make something the primary

key, by default, this becomes indexed with no duplicates allowed.

– Other fields can be indexed at will.

32

– SKIP: Specify Unicode Compression– SKIP: Specify IME Mode– SKIP: Specify IME Sentence Mode– SKIP: Specify Smart Tags

33

• When you finish the design, you should close the table by clicking on the X in the upper right hand corner.

• At this time you can save the table in the database and give it a name.

• This is sort of like closing and naming queries. • What follows are screen shots for the completed

designs for the Mother and Person tables.

34

• Keep in mind when looking at the screenshots that the general characteristics listed at the bottom apply to whichever field in the table design is highlighted at the top.

• In the Mother table design, the primary key field, SSN, is highlighted.

35

36

• The Person table design screenshot follows. • Notice that in this screenshot, it's the foreign

key field, motherSSN, which is highlighted. • In addition to other characteristics, it is

indexed, and for the foreign key field, "Duplicates OK" is the desired setting.

37

38

12.3 Specifying Referential Integrity Using Access's Graphical User Interface

• In order to include referential integrity in the database design, you have to go to the rightmost tab at the top of Access

• This is the Database Tools tab• Under this tab is the Relationships icon• This is shown on the following overhead

39

40

• To create a primary key-foreign key relationship between tables in a database, you have to click on the relationships button.

• A dialog box will appear where you get to add the tables of your database to a graphical representation of the database.

• The screenshot below shows the dialog box.

41

42

• This is what the Relationships screen looks like after adding both tables.

43

44

• To create a one-to-many relationship between Mother and Person:– Click on the primary key of the Mother table, SSN– Drag the mouse to the foreign key field of Person,

motherSSN– Drop there

45

• Remember that a primary to foreign key relationship is captured by embedding the primary key of the one table as a foreign key in the many table.

• When using the graphical user interface you click, drag, and drop, from the one/primary key field to the many/foreign key field

46

• After you have taken the first step in creating the relationship, the system will prompt you to see if you want to enforce referential integrity.

• This is done by means of a dialog box, which is shown on the following overhead.

47

48

• Then you have to check the Enforce Referential Integrity box and choose whether updates or deletes should cascade.

• If you do not choose the cascade option, the default is to restrict.

• A representative choice is shown.

49

50

• After you click the create button, the relationships screen will look as shown on the following overhead.

51

52

• The 1 and the infinity symbol on the ends of the line connecting the table are the Access equivalent of a crow's foot.

• The infinity symbol represents the "many" side of the relationship.

53

• When you close the relationships view, you will be prompted whether you want to save the changes.

• Again, this is analogous to closing and saving a query.

• If you did everything correctly, don’t forget to save the changes.

• If you made a mistake on the relationship, do not save the changes, and start over.

54

12.4 Entering Data

• In a previous unit the SQL INSERT key word was explained.

• If you only had an SQL interface, this would be the only way to enter data into a table.

• It can be useful if you need to set up queries that automatically put more than one record into a table at a time, but for a single record the graphical user interface is more convenient.

55

• To insert data using the graphical user interface, simply open a table.

• The default view is the datasheet view. • In this view you can enter the values into the

rows. • It looks and feels pretty much like entering

data into a spreadsheet.

56

Data entry and referential integrity

• When doing data entry you have one last thing to worry about:

• Referential integrity. • Referential integrity says that you can't put a

value into a foreign key field that doesn't exist in the corresponding primary key field.

57

• In practical terms, this restriction has this consequence:

• Suppose you are enforcing referential integrity on tables in a one-to-many relationship

• You have to put the records into the "one" table before putting the corresponding records into the "many" table.

58

• In this example, you have to enter Mother information before entering Person information.

• If you try to do it in the other order, you'll get an error message.

• A screenshot of the Mother table is given below with some data in it.

59

60

• Here is a screenshot of the Person table with some matching data.

• A given motherSSN may occur more than once. • It's also possible to have a record with a null

motherSSN.• What is not possible is a Person record with a

MotherSSN value that doesn’t exist in the Mother table.

61

62

12.5 Assignment Description

63

12.5.1 Background

• The assignment for Unit 12 is different from the preceding assignments.

• Rather than a list of queries, it is a small project where you have to create some tables, and so on.

• The assignment instructions will be reviewed• Then an example will be covered.

64

12.5.2 Assignment Instructions

• This section covers the instructions for the assignment.

• The next section gives a specific example of an assignment problem.

• There is a general instruction sheet.• Also, each individual version of the

assignment comes with specific instructions

65

Version Specific Instructions

• You should have received a sheet telling you which version of the assignment you’ll be doing

• This sheet includes:• 1. A small E-R diagram which illustrates a

database scenario.• 2. A brief verbal description of anything that

might not be clear from the names of the tables and fields in the scenario.

66

• 3. Schemas for the tables, including field names and types along with an indication of whether they're primary key or foreign key fields.

• 4. One example record for each table.

67

• This is what you're not given:• 1. You are not given the widths of the text

fields. • You will have to pick reasonable values for these

yourselves.• 2. Aside from the one example record for each

table, you are not given data for the tables. • You will need to make some up.

68

• This assignment includes table creation and some constraints.

• You should be using Access's graphical user interface for this assignment.

• In the long run it doesn't make much sense to do these things using SQL when a graphical alternative is available.

• If you are ever really going to use Access, it is worth trying the graphical user interface and getting a little practice.

69

• For this part of the assignment you will make up a new .mdb or .accdb file and turn that in for credit.

• Do the following things:• 1. Create all of the tables in the given database

scenario. • Do this by taking the "Create table in design

view" option in Access.• 2. Make up suitable widths for the text fields.

70

• 3. When you create the tables, make sure you specify the primary key field.

• You can do this by right clicking on the field and choosing the primary key option.

• 4. When you create the tables, make sure you specify that foreign keys can't be null.

• In Access, this is signified by "Required: Yes".

71

• 5. Also put an index on every foreign key field.

• Notice that there are such things as indexes with duplicates and those with no duplicates.

• For a foreign key field you want to allow duplicates.

72

• 6. Once the tables have been created, go to the Database Tools tab and click on the "Relationships" tool in the toolbar.

• Add your tables to the relationships area and enforce referential integrity on all pk-fk pairs.

• You do this by clicking on the "one" field, dragging, and dropping on the "many" field.

• Then check the box to enforce referential integrity and check the box to cascade on updates.

73

• 7. Once you've created the tables and set up referential integrity, pop the tables open and enter data into them.

• Enter the example record given for each, and make up 2 more rows containing suitable data for each table.

• You have to be careful when making up data so that foreign key fields do not contain values that are not in the corresponding primary key fields.

• If you violate referential integrity when entering data, you will get an error message.

74

• Remember that in order to do this successfully, you will also have to enter primary key data before foreign key data

75

12.5.3 Example Assignment

Doctor Diagnosis Patient Insurance Company

76

• Verbal Description of Scenario: • The insurance company's payment rate is a

percent, like 80%, expressed as a decimal, .80.

77

Table Schemas and Sample Records:

• Doctor, (123, William Smith, ophthalmology, 562-3478)

• Idno text pk• Docname text• Specialty text• Phone text

78

• Patient, (789, Sara Jones, 456 Karluk St., 111)• Idno text pk• Patname text• Address text• Insurancecoidno text fk

79

• Insuranceco, (111, Blue Cross Blue Shield, 782-3406, 100.00, .80)

• Idno text pk• Coname text• Phone text• Deductible currency• Paymentrate number

80

• Diagnosis, (101, astigmatism, 5/1/2007, 123, 789)

• Idno text pk• Date date• Doctoridno text fk• Patientidno fk

81

The End

Recommended