views:

474

answers:

4

Hi,

I've checked everything for errors: primary key, uniqueness, and type. Access just doesnt seem to be able to link the 2 fields i have in my database. can someone please take a look?

http://www.jpegtown.com/pictures/jf5WKxKRqehz.jpg

Thanks.

A: 

You need to create an INDEX. Perhaps look for some kind of create index button and create an index on CompanyID

jetru
my CompanyID is indexed, duplicates ok. my Company.ID field is indexed and no dups.
aZn137
+5  A: 

Actually you need an index on the name fields, on both sides

However, may I suggest that you have way too many joins? In general there should only be one join from one table to the next. It is rare to have more than one join between tables, and exceedingly rare to have more than two.

Have a look at this link: http://weblogs.asp.net/scottgu/archive/2006/07/12/Tip_2F00_Trick_3A00_-Online-Database-Schema-Samples-Library.aspx

Notice how all of the tables are joined together by a single relationship?

Each of the fields labeled PK are primary keys. These are AUTONUMBER fields. Each of the fields labeled FK are foreign keys. These are indexed Number fields of type Integer. The Primary Keys are connected to the Foreign Keys in a 1 to many relationship (in most cases).

99% of the time, you won't need any other kind of joins. The trick is to create tables with unique information. There is a lot of repeated information in your database.

A database that is reorganized in this manner is called a "normalized" database. There are lots of good examples of these at http://www.databaseanswers.org/data_models/

Robert Harvey
ah, light at the end of the tunnel. thank you so much!
aZn137
"In general there should only be one join from one table to the next. It is rare to have more than one join between tables, and exceedingly rare to have more than two." It depends on your approach to inter-table integrity constraints i.e. some people prefer FOREIGN KEYs to CHECK constraints. So see what I mean, take the example you linked to and write a constraint to ensure the common sense rule that the Staff_On_Projects.From_Datetime cannot predate the relevant Projects.Project_Start_Date.
onedaywhen
...one approach would be to add a new UNIQUE constraint to the Projects table's on (project_start_date, project_id), add project_start_date as a new column to the Staff_On_Projects table and reference Projects using (project_start_date, project_id). By 'repeating' the project_start_date in this way, the new rule becomes a simple row-level Validation Rule, which most Access/ACE/Jet users are comfortable with e.g. Staff_On_Projects.project_start_date <= Staff_On_Projects.from_datetime.
onedaywhen
...while you *can* do it with a table-level CHECK constraint, in my experience this is less preferred e.g. ALTER TABLE Staff_On_Projects ADD CHECK (NOT EXISTS (SELECT * FROM Projects AS P1 INNER JOIN Staff_On_Projects AS SOP1 ON P1.project_id = SOP1.project_id WHERE P1.project_start_date <= SOP1.from_datetime));
onedaywhen
While all of these approaches are valid, I question whether they would be useful for someone learning the basics. A PK/FK approach with autonumber/int fields will work 100% of the time, and other, more exotic approaches are not necessary for a solid, reliable, enterprise-worthy database.
Robert Harvey
"A PK/FK approach with autonumber/int fields will work 100% of the time" -- but not fool proof: as the source you linked to puts it, "there is a 'gotcha' which can be deadly. It is all too easy to enter the same record twice. Therefore you must always be sure to enforce a unique constraint on the natural key." But the next sentence is wrong IMO: ". Luckily this is easy to do in Access which can be a good basis for clarifying your design and constraints."
onedaywhen
...using the same example as before, how do you write a sequenced unique key constraint on table Staff_On_Projects to ensure periods for the same employee and project do not overlap? I can't think of any other way than CHECK constraints, which Access does not make easy at all. Table-level CHECK constraints are distinctly not "for someone learning the basics" either.
onedaywhen
+2  A: 

Your relationship diagram shows that you've made the ID fields your primary key in all your tables, but you're not using them for your joins. Thus, they serve absolutely no purpose. If you're not going to use "surrogate keys" (i.e., a meaningless ID number that is generated by the database and is unique to each record, but has absolutely no meaning in regard to the data in your table), then eliminate them. But if you're going to use "natural keys" (i.e., a primary key constructed from a set of real data fields that together are going to be unique for each record), you must have a unique compound index on those fields.

However, there are issues with both approaches:

  1. Surrogate Keys: a surrogate PK makes each record unique. That is you could have a record for David Fenton with ID 1 and a record for David Fenton with ID 2. If it's the same David Fenton, you've got duplicate data, but as far as your database knows, they are unique.

  2. Natural Keys: some types of entities work very well with natural keys. The best such are where there's a single field that identifies the record uniquely. An example would be "employee type," where values might be "associate, manager, etc." In that case, it's a very good candidate for using the natural key instead of adding a surrogate key. The only argument against the natural key in that case is if the data in the candidate natural key is highly volatile (i.e., it changes frequently). While every modern database engine provides "CASCADE UPDATE" functionality (i.e., if the value in the PK field changes, all the tables where that field is a Foreign Key are automatically updated), this imposes a certain amount of overhead and can be problematic. For single-column keys, it's unlikely to be an issue. Now, except for lookup tables, there are very few entities for which a natural key will be a single column. Instead, you have to create a compound index, i.e., an index that spans multiple data fields. In the index dialog in Access table design, you create a compound key by giving it a name in the first column, and then adding multiple rows in the second column (from the dropdown list of fields in your table). The drawback of this is that if any of the fields in your compound unique index are unknown, you won't get uniqueness. That is, if a field has a Null in two records, and the rest of the fields are identical, this won't be counted as a conflict of uniqueness because Null never equals Null. This is because Null doesn't mean "empty" -- it means "Unknown."

Allen Browne has explained everything you need to know about Nulls:

In your graphic, you show that you are trying to link the Company table with the PManager table. The latter table has a CompanyID field, and your Company table has a unique index on its ID field, so all you need is a link from the ID field of the Company table to the CompanyID field of the PManager table. For your example to work (which would be useless, since you already have a unique index on the ID field), you'd need to create a unique compound key spanning both ID and ShortName in the Company table.

Additionally, if ShortName is a field that you want to be unique (i.e., you don't want two company records to have the same ShortName), you should add a unique index to it, whether or not you still use the ID field as your primary key. This brings me back to item #1 above, where I described a situation where a surrogate key could lead you to enter duplicate records, because uniqueness is established by the surrogate key along. Any time you choose to use a surrogate key, you must also add a unique compound index on any combination of data fields that needs to be unique (with the caveat about Null fields as outlined in item #2).

If you're thinking "surrogate keys mean more indexes" you're correct, in that you have two unique indexes on the same table (assuming you don't have the Null problem). But you do get substantial ease of use in joining tables in SQL, as well as substantially less duplication of data. Likewise, you avoid the overhead of CASCADE UPDATE. On the other hand, if you're viewing a child table with a natural foreign key, you don't need to join to the parent table to be able to identify the parent record, because the data that identifies that record is right there in the foreign-key fields. That lack of a need for a join can be a major performance gain in certain scenarios (especially for the case where you'd need an outer join because the foreign key can be Null).

This is actually quite a huge topic, and it's something of a religious argument. I'm firmly in the surrogate key camp, but I use natural keys for lookup tables where the key is a single column. I don't use natural keys for any other purpose. That said, where possible (i.e., no Null problems) I also have a unique index on the natural key.

Hope this helps.

David-W-Fenton
A: 

Just join on the CompanyID. You could also get rid of the Company field in PManager.

Jeff O