473,394 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Choosing primary key / Unique Index

I have a Parts Table with the following structure..
I need to decide which makes the primary key of the table.
I cant have any sequences nor can I add any new fields in this Table.

Table Structure ::

Create table Parts
(Parts_desig char(10),
Sequence_no integer,
Effective_Date Date,
Amount integer,
Expiry_Date Date,
Status char(1));
First I had

Parts_desig,Sequence_no,Effective_Date,Amount as the Primary Key

Part001, 0, jan-1-2006, 100, Mar-31-2006, Y
Part001, 0, jan-1-2006, 100, Dec-31-2006, N

can not be added Since it shows Primary Key Violation and a part with
same
Parts_desig,Sequence_no,Effective_Date,Amount can exist but with a
status N.

Then I had
Parts_desig,Sequence_no,Effective_Date,Amount,Expi ry_Date as the
Primary Key

Part001, 0, jan-1-2006, 100, Mar-31-2006, Y
Part001, 0, jan-1-2006, 100, Dec-31-2006, Y

is not valid data coz a part cannot exist with same effective date and
status Y.

Conditions
-----------

A Part may be Valid in an Year for different amounts with diferent
status.
E.g.

Part001 with sequence 0 may be valid from Jan 1 2006 to Dec 31 2006
for year 2006 with status N.
Part001 with sequence 0 may be valid from Jan 1 2007 to Mar 31 2006
for year 2007 with status Y.
Part001 with sequence 0 may be valid from Apr 1 2007 to Dec 31 2006
for year 2007 with status Y.

We are validating from the GUI, but I need to check it from back end
also to make sure that only valid data is entered.

Can I have a set of primary key which is different from the unique
index ??

regards.
RainDeEr

Sep 7 '06 #1
7 3104
rAinDeEr wrote:
I have a Parts Table with the following structure..
I need to decide which makes the primary key of the table.
I cant have any sequences nor can I add any new fields in this Table.

Table Structure ::

Create table Parts
(Parts_desig char(10),
Sequence_no integer,
Effective_Date Date,
Amount integer,
Expiry_Date Date,
Status char(1));
First I had

Parts_desig,Sequence_no,Effective_Date,Amount as the Primary Key

Part001, 0, jan-1-2006, 100, Mar-31-2006, Y
Part001, 0, jan-1-2006, 100, Dec-31-2006, N

can not be added Since it shows Primary Key Violation and a part with
same
Parts_desig,Sequence_no,Effective_Date,Amount can exist but with a
status N.

Then I had
Parts_desig,Sequence_no,Effective_Date,Amount,Expi ry_Date as the
Primary Key

Part001, 0, jan-1-2006, 100, Mar-31-2006, Y
Part001, 0, jan-1-2006, 100, Dec-31-2006, Y

is not valid data coz a part cannot exist with same effective date and
status Y.

Conditions
-----------

A Part may be Valid in an Year for different amounts with diferent
status.
E.g.

Part001 with sequence 0 may be valid from Jan 1 2006 to Dec 31 2006
for year 2006 with status N.
Part001 with sequence 0 may be valid from Jan 1 2007 to Mar 31 2006
for year 2007 with status Y.
Part001 with sequence 0 may be valid from Apr 1 2007 to Dec 31 2006
for year 2007 with status Y.

We are validating from the GUI, but I need to check it from back end
also to make sure that only valid data is entered.
Then I'd think you will have to pick all columns to be part of the primary
key / unique constraint.
Can I have a set of primary key which is different from the unique
index ??
When designing tables at the conceptual level, completely ignore indexes.
An index belongs to the internal level of database design.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Sep 7 '06 #2
Hi,

I tried making all the keys Primary but the following is an invalid
data set coz
Part001 is having the same effective_date in an year, with status Y
where as the inserts into DB shouldnt allow that.

Part001 0 2006-01-01 100 2006-02-02 Y
Part001 0 2006-01-01 100 2006-06-06 Y
Part001 0 2006-01-01 100 2006-12-31 N
Part001 0 2006-01-01 100 2006-12-31 Y

Thanks.....

Sep 7 '06 #3
"rAinDeEr" <ta**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Can I have a set of primary key which is different from the unique
index ??

regards.
RainDeEr
The Primary Key as a whole must be unique.

Since you have effective_date and expiray_date as columns you should include
the effective_date in the PK, and create insert and update triggers to make
sure there are no overlapping date ranges for the same part (taking into
account all the other columns in the PK).
Sep 7 '06 #4
rAinDeEr wrote:
Hi,

I tried making all the keys Primary but the following is an invalid
data set coz
Part001 is having the same effective_date in an year, with status Y
where as the inserts into DB shouldnt allow that.

Part001 0 2006-01-01 100 2006-02-02 Y
Part001 0 2006-01-01 100 2006-06-06 Y
Part001 0 2006-01-01 100 2006-12-31 N
Part001 0 2006-01-01 100 2006-12-31 Y

Thanks.....
Sorry but you are just going to have to have the primary key being all
fields and then use different methods (e.g. triggers or logic in your GUI)
to add the remaining date constraint. The problem is that the dates are
atomic objects but you have a further restriction that is based upon
structure within the dates (the year).

Are you sure you cannot modify the structure of the table?

--
Bill Medland
Sep 7 '06 #5
Thanks Mark, Knut, Bill for the response....
Solved the issue....with Triggers

Thanks.....

Sep 8 '06 #6
It seems I have to aviod triggers..
can i have a check constraint instead ??

Any suggestions.

Thanks

Sep 13 '06 #7
rAinDeEr wrote:
It seems I have to aviod triggers..
can i have a check constraint instead ??
No, unfortunately not. A check constraint is actually a row constraint.
Thus, you only have the values of the current row to evaluate if the
constraint is satisfied or violated. You can't query other (or the same)
tables. But since you want to prevent overlapping intervals, you have to
check against other rows in the table.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Sep 14 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Westcoast Sheri | last post by:
Which will be a faster lookup of an item by, "color" in the following mySQL tables: "unique key," "primary key," or just plain "key"?? CREATE TABLE myTable ( number int(11) NOT NULL default '0',...
4
by: Peter Scott | last post by:
I created a table that has a column in that needs to contain a full Unix file path. Since 2048 was too long for a VARCHAR, I made it TEXT. I since populated the table. Now I want to make the...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
1
by: Raquel | last post by:
Here is the scenario: 1. I create a unique index on a table. 2. I create a primary key on that table with same columns as the ones in the unique index by Alter table add primary key statement....
6
by: Bob Stearns | last post by:
I was under the impression that the primary key had to be a unique index. Since I usually create my primary indices before my primary keys, in order to get the indices in the same schema as their...
4
by: deko | last post by:
I have a Make Table query that creates a fairly large table. The Make Table query populates the new table with one AutoNumber field (which is taken form another unrelated table as part of the...
7
by: Philip | last post by:
Hey all, (Access 2000) I've been having a horror story with this design problem. My Database is Structured like This: AUTHORS, BOOKS, PAGES. Those are the Tables and each Item in each table...
115
by: LurfysMa | last post by:
Most of the reference books recommend autonum primary keys, but the Access help says that any unique keys will work. What are the tradeoffs? I have several tables that have unique fields. Can...
3
by: vj_dba | last post by:
Hi Group, I have a Primary key in my table. It's clear Primary key wont allow duplicates, this primary key creates one index for retrival. Suppose if my table is having a Unique index also....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.