View previous topic :: View next topic |
Author |
Message |
anbesivam Beginner
Joined: 09 Aug 2006 Posts: 66 Topics: 14
|
Posted: Tue Aug 17, 2010 4:30 am Post subject: DB2 - Primary Key & Not Null with Default |
|
|
Hi,
In following way my existing table was created:
Code: | CREATE TABLE Product
(Product_id CHAR(4) FOR SBCS DATA NOT NULL,
Product_Name CHAR(3) FOR SBCS DATA NOT NULL,
Product_Description CHAR(4) FOR SBCS DATA NOT NULL
WITH DEFAULT,
CONSTRAINT Product_id
PRIMARY KEY (Product_id) |
After dropping above table, I would like to add another priamary key in the end of the table like follows:
Code: | CREATE TABLE Product
(Product_id CHAR(4) FOR SBCS DATA NOT NULL,
Product_Name CHAR(3) FOR SBCS DATA NOT NULL,
Product_Description CHAR(4) FOR SBCS DATA NOT NULL
WITH DEFAULT,
Product_Number CHAR(10) FOR SBCS DATA NOT NULL WITH DEFAULT,
CONSTRAINT Product_id
PRIMARY KEY (Product_id, Product_Number) |
Note: Here I have given the new field 'Product_Number' as Primary key as well as 'NOT NULL WITH DEFAULT'.
Is this possible ? if yes, how the table will be created ? Please let me know.
Thank you. |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Tue Aug 17, 2010 6:34 am Post subject: |
|
|
why did you waste a post for a question that you could have performed in spufi and determined the answer yourself.
suggestions:
1. since you are creating the table, then put the key columns together, it is easier on the eyes.
2. if you are going to call a column ..._number and there are going to be alpha char present, call it a ...._code.
If there are only going to be numerics present, then define it as a numeric datatype. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
anbesivam Beginner
Joined: 09 Aug 2006 Posts: 66 Topics: 14
|
Posted: Tue Aug 17, 2010 7:18 am Post subject: |
|
|
Hi dbzTHEdinosauer,
Thanks for your response.
I cannot create the table on my own in my shop. That's why I posted that question. I can only request for the creation of table to DB2 Team. Before sending the request, I would like verify.
Hope you understand. |
|
Back to top |
|
|
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Tue Aug 17, 2010 7:32 am Post subject: |
|
|
i am not a dba, so I always have to play in spufi to determine what actually happens.
sorry for the earlier post, though my suggestions stand.
hopefully, someone here will provide you with the answers. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
|
Nic Clouston Advanced
Joined: 01 Feb 2007 Posts: 1075 Topics: 7 Location: At Home
|
Posted: Tue Aug 17, 2010 10:15 am Post subject: |
|
|
You should be able to create the table with your own high level name. How do the DBAs expect you to come to them with valid table requests if you cannot verify that they are valid. _________________ Utility and Program control cards are NOT, repeat NOT, JCL. |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Wed Aug 18, 2010 7:48 am Post subject: |
|
|
If the question you're asking is, will DB2 allow more than one column defined in a primary key, the answer is yes. But I look at this and immediately start thinking about normalization. Product_Id and Product_Number define a unique Product? What is the difference between your Product_Id and Product_Number? Hopefully you're just making up an example. Remember, the key, the whole key, and nothing but the key. |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Wed Aug 18, 2010 2:56 pm Post subject: |
|
|
Quote: |
Remember, the key, the whole key, and nothing but the key.
|
So help me Codd. |
|
Back to top |
|
|
anbesivam Beginner
Joined: 09 Aug 2006 Posts: 66 Topics: 14
|
Posted: Wed Aug 18, 2010 11:10 pm Post subject: |
|
|
Thanks to all for your suggestions.
I have discussed with my DBA and he mentioned that we need have the 'Not Null with Default' for the new column even though it is a primary key.
Thanks. |
|
Back to top |
|
|
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Aug 19, 2010 8:05 am Post subject: |
|
|
Have you discussed with your DBA if adding the additional field to the primary key is the correct action? You could be creating a situation that could create years of headaches if you don't discuss normalization. I don't often encounter situations where the primary key needs to be modified after a table has been created, so I'm somewhat skeptical that the appropriate level of analysis has been done. |
|
Back to top |
|
|
papadi Supermod
Joined: 20 Oct 2009 Posts: 594 Topics: 1
|
Posted: Thu Aug 19, 2010 3:02 pm Post subject: |
|
|
Quote: |
so I'm somewhat skeptical that the appropriate level of analysis has been done.
| As am i. . .
Probably not the same situation, but long ago i was invited to review an application that "they thought" was ready to deploy once a "little problem" was resolved. They had 2 teams - one that worked on the part of the system that was "Product_Id" based and the other wrote the code that was "Product_Number" driven.
All went well until they tryed to run fully integrated testing. Seems like the Product_Number group "understood" that the Product_Id would be populated before their code would be invoked. The other developers "understood" just the reverse - the Product_Number would be available before their code was invoked by some user. . . It was a classic Chicken and Egg situation. Can't get a chicken without an egg - can't get an egg without a chicken. . .
Guess what - my recommendation was that they had to regroup, redesign, and explain to the client why this would not be ready as planned.
Hopefully, this is not going to be the situation in this case. . .
Is it possible that the Product_Number could become a new unique key - not part of the primary key? _________________ All the best,
di |
|
Back to top |
|
|
|
|