[quoted text, click to view] Anita <anonymous@devdex.com> wrote in message news:<405a4a42$0$199$75868355@news.frii.net>...
> Hi All,
>
> I have just read the book ("Troubleshooting SQL"
> by Forrest Houlette) about the importance of
> primary key (PK) in a table. Forrest says:
>
> "Duplicate rows can be fiendish. You can
> encounter situations in which the database
> will refuse to delete a duplicate because
> it can not uniququely identify the row.
> Normally, DELETE will delete anything
> that matches its criteria, both single
> rows and multiple rows"
>
> Have you ever met the above problem ?
> Have you ever experienced other problems caused
> by the absence of PK also ?
>
> Currently, there are about 80% tables with unique
> keys and 20% tables contain duplicate keys in
> my database. General journal table is one of the
> tables that contains duplicate keys. Users may
> enter duplicate rows in this table.
> I also do not put a PK constraint on each table.
> The reason for not putting the constraint :
>
> 1. Users access database via an application program
> only. Database integrity is controlled by this program.
> There are no attributes (like foreign key constraints)
> that link the tables. All tables are independent.
>
> 2. Almost all keys are made up of more than one column.
> Without PK constraint, efficient indexes can be
> created. For example:
>
> CREATE TABLE Stock_card (
> Year char(4) NOT NULL ,
> Prefix char(3) NOT NULL ,
> TransactionNo char(20) NOT NULL ,
> Date datetime NOT NULL ,
> Description char(50) NOT NULL ,
> ProductNo char(15) NOT NULL ,
> WarehouseNo char(8) NOT NULL ,
> BatchNo char(15) NOT NULL ,
> Quantity decimal(15, 2) NOT NULL ,
> Code char(1) NOT NULL )
>
> The unique key of Stock_card table is -
> (Year,Prefix,TransactionNo,ProductNo,
> WarehouseNo,BatchNo)
> If PK constraint is set then a PK_index is
> automatically created on 6 columns. This index is too
> wide and almost useless. Accessing individual row
> of this table is not needed.
>
> Then I create index A on (Date, Prefix, TransactionNo)
> and cluster index B on (ProductNo,WarehouseNo,Date)
> All the queries benefit from these indexes.
> Column year and BatchNo are not included in these
> indexes because they contain many duplicate values.
>
> My application does delete rows by criteria quite often.
> So far, I have no problem. But, after reading Forrest book, I worry.
> Will I also find the similar problem in the future ?
>
> Anyone information and opinion about this post
> will be greatly appreciated.
>
> Thanks in advance
>
> Anita Hery
>
>
>
>
>
>
>
>
> *** Sent via Developersdex
http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!
At least in MSSQL, the database engine will not stop you deleting rows
in a table with no primary key. However, a common issue is that
certain tools (eg. Enterprise Manager) will not allow deletions or
updates on a table without a primary key, because they cannot
guarantee that the row you selected in the interface is really the row
that will be deleted.
In a relational database, all tables should have a primary key. Your
first reason above is slightly suspect - can you guarantee that no one
will ever modify data except through your application? If you can,
then the approach may work, but frequently developers, DBAs etc. make
ad hoc changes to data - without referential integrity (and CHECK
constraints for domain integrity etc.), you can't ensure that their
changes will respect the data model. And then you also have to
import/export data, run reports etc. - many tools try to be
'intelligent' based on the foreign key information they can read from
the database. You also have the issue that you need to rely on
documentation to see how to code in the database, and technical
documentation is often not maintained well, but if your constraints
and relations are explicit in the database, then developers can
immediately see how tables are related, what values are acceptable in
a certain column etc.
For what it's worth, I currently work in an environment where the data
architect decided on a similar approach to you - no foreign keys, no
constraints, all columns NULLable etc., and it is extremely difficult
to be productive.
As for your second reason, multi-column keys are fine, but if you have
many of them, it suggests you may not have fully normalized your data
model. It's not possible to say for sure, of course, without detailed
knowledge of your environment and business. Surrogate (artificial)
keys are often used for tables with very wide natural primary keys.