[ Pobierz całość w formacie PDF ]
.The RefCustInOrders field defined CustNo as aforeign key that relates to the CustNo field in the Customer table.A second way to view this key is in the Database Desktop.Set the Working Directory fromthe File menu to BCDEMOS.Open up the Orders table in the Database Desktop and selectTable | Info structure from the menu.Drop down the Table Properties and select ReferentialIntegrity, as shown in Figure 12.3.FIGURE 12.1.Viewing the BCDEMOS database in the Database Explorer.FIGURE 12.2.The primary and foreign fields of the Orders table.FIGURE 12.3.Selecting Referential Integrity in the Database Desktop.Double-click RefCustInOrders to bring up the Referential Integrity dialog shown inFigure 12.4.file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch12.htm (5 of 24) [10/10/2000 1:13:25 AM] Ch 12 -- Understanding Relational DatabasesFIGURE 12.4.The CustNo field in the Orders table relates to the CustNo field in theCustomer table.The fields in the left side of this dialog belong to the Orders table.On the right is a list ofall the tables in the database.In the center, you can see that the CustNo field has beenselected from the Orders table and the CustNo field has been selected from theCustomer table.The primary key of the Customer table is related to the foreign key ofthe Orders table.Now go back to the Database Explorer and open up the Indices branch of the Orders table,as shown in Figure 12.5.Note that you can see the names of the indexes, here labeled as and asCustNo.The fields found in the indexes are also displayed.For instance, you can see thatthe primary index consists of the OrderNo field and the secondary index consists of theCustNo field.FIGURE 12.5.The primary and CustNo indexes on the Orders table.I am showing these to you so that you will begin to see the distinction between keys andindexes.The two concepts are distinct.For further proof of this, open up the IBLOCALdatabase in the Database Explorer.Use SYSDBA as the user name, and masterkey as thepassword.Now open up the Employee project table as shown in Figure 12.6.Note that thereare separate listings for the index, primary key, and foreign keys.FIGURE 12.6.The Employee_Project table has three indexes, one primary key, andtwo foreign keys.In practice, almost all keyed fields will also have indexes.This leads people to think the twoconcepts are the same.However, indexes are about searching and sorting, and keys areabout referential integrity.These distinctions will become blurred at times, but it helps ifyou can keep it in your mind that they are different ideas.The actual details concerningthese distinctions will become clear in the next few pages.You can also see the indexes for a table inside the Database Desktop.To get started, open upthe Orders table and select Table | Info Structure from the menu.The fields with the starsbeside them are part of the primary index.Drop down the Table Properties combo box toview the secondary indexes.Double-click the indexes you see to view the details of theirdesign.If you want to change the structure of a table, choose Table | Restructure from themenu, rather than Table | Info Structure.Most of the time, I find the Database Desktop is the right tool to use when I want to createor modify a table, and the Database Explorer is the right tool to use when I want to view thestructure of a table.However, I often find myself jumping back and forth between the twotools, to get the best features of each.Later in the book I will talk about case tools, whichare generally superior to either of the products discussed in this section.However, there areno case tools that ship with BCB, so I emphasize the universally available tools in this text.Throughout the ensuing discussion, you might have occasion to use the Database Explorerfile:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch12.htm (6 of 24) [10/10/2000 1:13:25 AM] Ch 12 -- Understanding Relational Databasesto examine the structure of the Customer, Orders, Items, and Parts tables.These arethe tables I use when defining what relational databases are all about.Rule Numero Uno: Create a Primary Key for Each Table!The last two sections have introduced you to some of the key concepts in relationaldatabases.If there is one lesson to take out of this chapter, it is the importance of creating aunique numerical key in the first field of most tables you create.This field is called aprimary key.In both Paradox and InterBase, it is impossible to create a primary key withoutalso simultaneously creating an index.If you want to have a list of addresses in a table, don't just list the Address, City,State, and Zip.Be sure to also include an integer-based CustNo, AddressNo, orCode field.This field will usually be both an index and the first field of the database.It isthe primary key for your table, and must be, by definition, unique.That is, each recordshould have a unique Code field associated with it.The primary keyServes as the means of differentiating one record from anotherIs also in referential integrityCan also help with fast searches and sortsAs I said earlier, the distinction between indexes and keys becomes blurred at times.However, they are distinct concepts and you should endeavor to discover the differences.NOTE: In this discussion I am taking a liberty in saying that you have to createa primary key for a table in a relational database.In fact, you can simply createa field that contains a unique integer value.It doesn't have to be an index.However, making a unique index for the field will speed up the operation ofyour database, and it will help enforce rules that make it easy to create robustrelational databases.In particular, the restraints on a primary key make itimpossible for you to create two fields in one table with the same primary key.Just to make sure this is clear, I'll go ahead and list out the right and wrong way to create atable.Right MethodCustNo: IntegerLastName, FirstName, Address, City, State, Zip: stringWrong MethodLastName, FirstName, Address, City, State, Zip: stringThe first example is "correct" because it has a primary index called CustNo.It is declaredas a unique Integer value.The second example is "wrong" because it omits a simplenumerical field as the primary index.file:///D|/DOWNLOAD/charlie_calvert's_borland_c++_builder_unleashed/ch12.htm (7 of 24) [10/10/2000 1:13:25 AM] Ch 12 -- Understanding Relational DatabasesNOTE: I put the words "correct" and "wrong" in quotes because there reallyare no hard-and-fast rules in this discipline.There are occasions when youmight not want to create a table that has a simple integer as a primary index.However, ninety-nine percent of the time, that's exactly what you want to do.At the height of a warm May spring day, there is such a thing as a rose bushthat has no buds or flowers.However, the whole point of rose bushes in May isthat they flower.I doubt we would feel quite the same way about roses if theydid not have beautiful blooms.In the same way, relational databases withoutprimary indexes wouldn't garner quite so much attention as they do now.I should add that not all primary indexes are numeric fields.For instance, manytables might use alpha fields containing values such as HDA1320WW35180 [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • funlifepok.htw.pl
  •