1

I have defined a new content type, statetype and a CCK field, state.
I need to use the CCK field as primary key in this table because I have another table and I need use that field as foreign key. (This code and table is not Drupal and i wrote it myself, but it uses a Drupal table.) I should change state field to primary key in the "statetype" table but Drupal already defines vid as primary key. Is there any problem if I define multiple primary keys (vid, state) or if I remove vid as primary key?

  • This question is not specific to Drupal; primary keys are primary keys independently from Drupal. The answers you get would be valid even if you would be using another CMS; the only parts specific to Drupal are the name of the table, and its schema. – kiamlaluno Apr 3 '11 at 9:44
  • Remember to accept answers for your questions; if users don't accept answers for their questions, this site doesn't have any chance to go past the beta phase. – kiamlaluno Apr 30 '11 at 12:19
0

A database table can have at most one primary key, but it can have multiple unique keys.
Replacing the primary key in that table is not a good idea, because Drupal code rely on the fact the primary key is vid. You cannot set any field as primary key: it must a field that contains unique values for each row; if the field you choose can have the same value for multiple rows, then it's not a candidate for primary key.

| improve this answer | |
  • thank u. my state field is unique. can i change priary key from vid to state. is there any problem? – user780 Apr 3 '11 at 10:12
  • no. @kiamlaluno says: "..., because Drupal code rely on the fact the primary key is vid." – dobeerman Apr 3 '11 at 20:37
  • Sort of correct, and sort of wrong. A composite primary key can consist of several columns. See lists.mysql.com/mysql/832 – Fuzzy76 Apr 6 '11 at 11:12
  • @Fuzzy76: What I reported is still valid, even in the case the primary key is made of more fields; a database cannot have two rows with the same value for the primary key, and cannot have two primary keys. – kiamlaluno Apr 6 '11 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.