views:

169

answers:

3

create table A (id int(10) not null, val1 varchar(255), primary key (id));

[a] create table B (a_id int(10) not null, val2 varchar(255), foreign key (a_id) references A (id));

[b] create table B (id int(10) not null, a_id int(10) not null, val2 varchar(255), foreign key (a_id) references A(id), primary key (id));

By choosing [a], I can avoid creation of the "id" surrogate key in table 'B'. Which is the preferred approach for creating table 'B' from a modeling perspective?

+1  A: 

As i understand it : in [a], you are creating a 1:1 relationship, in [b] you are not. They aren't alternatives!

In case [b], if table A would hold invoices, table B could be used for invoicelines whereas in [a] this cannot, since there can only be one record for each record in table A. (Thus only 1 invoiceline per invoice)

So, if you really want an answer, use [b], your [a] construct could be replaced with one table only, and is probably not what you mean. (also since you didn't set a primary key, the same as the FK in a 1:1 relationship)

Peter
This seems not to be true, as a_id is neither declared primary nor unique. Both solutions do the same. [b] allows for unique identification of a record in table B using its id. In [a] one cannot uniquely identify and therefore manipulate a single record, apart from using val2 too, which does not have to be unique either.
Ralph Rickenbach
@malach, thanks for the tip. Guess I overlooked the fact that i will not be able to modify a single record in table 'B' without a unique primary key.
Joshua
+1  A: 

For surrogate keys in general,

A professor in my Computer Science course says "No".

Practical experience for me, says, yes.

I rather use one despite the increase space just for the sake of easy to read SQL statements, and more flexibility in case of requirement changes.

Extrakun
That's not the question here. But as far as the case against Surrogate keys go it has nothing to do with increased space. (not that I am against them perse)
Peter
Well, space's not the big concern here for the associate professor who said 'no' when I suggest a surrogate key to a design, it's more of correctness of design, which I think it's the main issue of contention.
Extrakun
+4  A: 

You never need a surrogate key (hence it's name). It looks like you're mixing a logical model and a physical model. For your logical model, you presumably would have

CREATE TABLE A (
    Val1 varchar() not null,
    constraint PK_A KEY (Val1) --?
)

CREATE TABLE B (
    Val1 varchar() not null,
    Val2 varchar() not null,
    constraint PK_B KEY (Val2), --? or Val1,Val2?
    constraint FK_A FOREIGN KEY (Val1) references A
)

(The above in made-up SQL, but hopefully you see the point)

Now, for a physical model, you might introduce surrogates wherever they make sense - where the logical keys are long (e.g. varchars). But whether you do or not is really up to you. And remember to enforce the logical keys still

Damien_The_Unbeliever
+1 for mentioning surrogate keys and phsyical model. The logical design should never use surrogate keys.
gbn