views:

97

answers:

3

I have to work with a database to do reporting The DB is quite big : 416 055 104 rows Each row is very light though, just booleans and int ids.

Each row is identify by 3 columns, but at my surprise, there is no Primary Key on it. Only a Clustered Index with a unique constraint.

So Knowing that, I have 2 question.

  1. Could there be ANY good reason for that?
  2. Is there any way I can turn this into a primary key.

Regarding question 2

Creating a new primary key also creates a non-clustered index to associate with (there is already an existing clustered one).
This is not what I am looking for. I want to keep that same index, but also make it a primary key.

  • Is it possible?
  • Would that be faster that creating the whole index again? (I hope so)
  • What could be the consequences? (locks? crash? corrupted data?)
+1  A: 
  1. A unique index can allow null values. A primary key can't.

  2. I believe you can't "mark" an existing index as the primary key. You'd have to drop it and recreate. To avoid stuff, I'd say it'd be good to place a TABLOCKX, HOLDLOCK on the table before doing that.

GSerg
I don't know how they are set, I thought maybe they are 2 different elements, and that the primary key would sort of "reference" the index it uses.. then that would just be a matter of creating a PK and tell it to use the existing one.And ORMs rarely play well without PKs...
Stephane
+1  A: 

Good question.

If you already have a unique index on non nullable columns then you have a candidate key. I'm not aware of any particular benefit of making this an "official" primary key. In fact I have a feeling that not making it a PK will give greater flexibility.

Martin Smith
Well, if I don't have a real primary key, I cannot create foreign keys...
Stephane
@Stephane - Yes you can. The requirement is that there is a unique constraint in the referenced table not a primary key.
Martin Smith
The Unique Constraints in the table I want the PK is composed of 3 columns (DocId, CatId, SysId) while I have only DocId in the table I want the Foreign Key. I'm actually not sure now that the PK would make it work.
Stephane
+1  A: 

There is little or no difference between a PRIMARY KEY and a UNIQUE constraint on non-nullable columns. So if the columns in question are non-nullable then I suggest you do nothing. The main reason to make a candidate key into a primary key is if you have some software (such as a data modelling tool or other development tool) that expects the key to be identified with a PRIMARY KEY constraint.

dportas
That's what I end up doing. This relationship is not mandatory for an ORM either. I can also just write a linq query with a join, That won't be such an issue afterall.
Stephane

related questions