![]() |
Relationships and Data Integrity |
|
Relational Databases |
|
Introduction |
|
When performing data entry, a user's job should be as easy and smooth as possible. One of the aspects that take care of this is the flow of information from one list to another. In other words, data stored in one list can be made available to another list so the user not only doesn't have to enter the same piece of information in different lists but also the should be able to access, from one list or table, data stored in another table. To allow the information to flow from one list to another, there should (must) exist a relationship between both tables. A relationship is made possible through a type of link from one table to another. This is the essence of relational databases. |
The purpose of the primary key is to keep records distinct from one another. When performing data entry, you can let the user manage this and you can use some techniques to exercise a certain level of control. The data type of a primary key can be almost type, certainly a string or a number. For example, you can use an employee number or else. The most important rule is that each record must have a unique value to distinguish it from another record. In most cases, if you let the user specify the value(s) of the primary key, this can be overwhelming. Instead of going through this configuration process, you can rely on Microsoft Access to automatically create a unique value for each record of a particular column.
A column that holds unique values that can differentiate one record from another is called a primary key. In most cases, you can create one column that would serve as the primary key of the table. To create a primary key, if working from the Design View of the table, you can right-click the column that will play this role and click Primary Key:
You can also click anywhere in the field and click the Primary Key button on the Table Design toolbar. After doing this, a picture with a key would appear on the left box of the column name. In some cases, you can use more than one column, that is, a combination of columns, to serve as the primary key. To do this, click the gray box on the left of one of the desired columns, press and hold Ctrl (or Shift). Click the gray box of the other column(s) to select. To make this combination the primary key, right-click the selection and click Primary key. Each of the selected columns would now display a pictured key on its gray box:
Based on a habit that follows a certain logic, the name of a column that holds the primary key usually ends with ID. Because a primary key is very important and common in a relational database, in Microsoft Access, if you don't specify a primary key and start saving the table, a warning would inform you that your table doesn't have a primary key.
The table Design View is the most common and probably the best place to create a primary key for a table. Still, you can use the table wizard to create a primary key. You make have noticed that the first column in the Sample Fields list of each Sample Table has a name that ends with ID:
When creating a table, if you select such a column and start creating the table, in the second page, the wizard would suggest that you let it create a primary key:
If you agree to let the wizard create the primary key, it would use the first ID column that was added to the table. |
|
|
|
|
Programmatically Creating a Primary Key |
Each of the libraries we have reviewed in the previous lessons provides a technique of specifying a column as the primary key or its table. If you are using either the Microsoft Access Object Library or DAO, to specify that a column is used as the primary, when calling the CreateField() method of the table as we saw in Lesson 10, pass a third argument as adKeyPrimary. Here is an example:
Private Sub cmdCreateTable_Click()
Dim curDatabase As Object
Dim tblStudents As Object
Dim colStudentNumber As Object
Dim colFullName As Object
Set curDatabase = CurrentDb
Set tblStudents = curDatabase.CreateTableDef("Students")
Set colStudentNumber = tblStudents.CreateField("StudentNumber", DB_LONG, adKeyPrimary)
tblStudents.Fields.Append colStudentNumber
Set colFullName = tblStudents.CreateField("FullName", DB_TEXT)
tblStudents.Fields.Append colFullName
curDatabase.TableDefs.Append tblStudents
DoCmd.SelectObject acTable, "Students", True
End Sub
To create a primary using SQL, add the PRIMARY KEY (case-insensitive) expression on the right side of the column definition. Here is an example:
Private Sub cmdCreateTable_Click()
DoCmd.RunSQL "CREATE TABLE SeasonalEmployees(" & _
"ContractorNo LONG NOT NULL PRIMARY KEY, " & _
"AvailableOnWeekend LOGICAL NULL, " & _
"OwnsACar BIT, " & _
"CanShareOwnCar YESNO);"
End Sub
Remember that you can use the data type to influence how the numbers would be assigned.
|
|
Private Sub Form_Load()
DoCmd.RunSQL "CREATE TABLE Transactions(" & _
"TransactionID COUNTER(1001, 1) NOT NULL PRIMARY KEY, " & _
"TransactionDate DATE, " & _
"EmployeeID LONG, " & _
"CustomerID LONG, " & _
"TransactionTypeID LONG, " & _
"DepositAmount DOUBLE, " & _
"DepositTypeID LONG, " & _
"WithdrawalAmount DOUBLE, " & _
"WithdrawalTypeID LONG, " & _
"ServiceCharge DOUBLE, " & _
"ChargeReasonID LONG, " & _
"Notes MEMO);"
End Sub
|
|
|
||
| Previous | Copyright © 2005-2010 FunctionX, Inc. | Next |
|
|
||