1

May I please know would I be able to have 6 primary keys in my database project. I am just beginning to create my database project and know of one and two being possible but not sure for 6. Any help please!

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
niki
  • 13
  • 2
  • looks duplicate of http://stackoverflow.com/questions/217945/can-i-have-multiple-primary-keys-in-a-single-table – ravi May 13 '12 at 05:02

5 Answers5

3

What do you mean by multiple primary key? Can you explain? If in a database you are asking more than one primary key then its fine you can take up per table primary key. but in a same table two or more primary keys are not possible.

A table can have only one primary key and more than one unique key, that would be your composite key.

example:-Employee table

     EmpID(Not null)   ManagerID(null)
     001               001
     002               002

here EmpId is your primary key and manager Id is your composite key.

Like this way you can make your table's columns unique.

David
  • 72,686
  • 18
  • 132
  • 173
sonu
  • 31
  • 2
2

The moment you say more than one then it becomes a composite key. Technically you can have a composite key with more that one column. But I would really love to know what is such a use case which caters such a requirement to have 6 keys as a composite key in a table.

Jim Ferrans
  • 30,582
  • 12
  • 56
  • 83
raddykrish
  • 1,866
  • 13
  • 15
  • We have tables with a large number of attributes (sometimes even up around a hundred) where composite primary keys are made from date, time, system name, sub-system name, period type, device names and so on. In other words, frequently more than six. Sometimes a pain to manage but considered necessary for our purposes. – paxdiablo May 13 '12 at 05:28
  • but i would say have a seperate 32 bit string as a primary key and have constraints for these fields because having such a complex composite key might slow down insertion time of the record..again its a design perspective...good to know your use case..thanks @paxdiablo – raddykrish May 13 '12 at 05:32
  • Thanks a lot for your response. I do need multiple primary in a tables which I set up firstly Customer_num in one table and 2keys in second Selling item_num and Hiring item_num as composite, but wasn't sure would this work well (or maybe they would?) so I set up additional table with 3primary keys for Hiring Item ( just stated what those Items are!?)and one more for sales item_num, and kind of look better but I am not sure is this ok to have 3 in one table. Or shall I leave it at as I initially set up? If you have a time your response will be hugely appreciated. Many thanks – niki May 13 '12 at 07:44
2

I'm going to guess you're trying to ask how to have multiple primary keys in a table, not a database. (Strictly speaking, this makes it a composite key).

You can certainly do this. For example, the syntax in mysql is:

CREATE TABLE tbl_name (
  col1 int,
  col2 int,
  col3 int,
  col4 int,
  col5 int,
  col6 int,
  col7 int,
  col8 int,
  col9 int,
  PRIMARY KEY (col1, col2, col3, col4, col5, col6));

Having said that, unless you really know what you're doing this probably isn't what you should be doing. I've certainly worked with very complicated business models that had large composite keys.

(For example, one client had lots of tables with 5 fields in the composite key, for example, representing salesOrganizationNumber, salesRepNumber, locationId, styleNumber, styleType on certain inventory/ordering line items.)

But your question is so basic, I suspect that you should probably ask a much more detailed sample question about how to design a schema and you'll probably find that your primary keys will be much smaller.

Mike Ryan
  • 4,234
  • 1
  • 19
  • 22
  • Thanks a lot for your response. Yes I do need multiple primary in a tables, set up firstly Customer_num in one table and 2keys in second Selling item_num and Hiring item_num as composite, but wasn't sure would this work well (or maybe they would?) so I set up additional table with 3primary keys for Hiring Item ( just stated what those Items are!?)and one more for sales item_num, and kind of look better but I am not sure is this ok to have 3 in one table. Or shall I leave it at as I initially set up? If you have a time your response will be hugely appreciated. Many thanks – niki May 13 '12 at 07:34
  • I'm getting a bit lost in your description of your tables. I'd suggest putting a shortened version of your table definition statements in your question and we can look at it from there. – Mike Ryan May 13 '12 at 07:50
2

You can have as many primary keys in your database as there are tables - you're allowed one primary key per table.

Of course, you can have multiple indexes per table but they're not considered primary keys.

You can also construct a primary key from multiple columns (as many columns as you like), but this is still one primary key, just a composite one.

It's vastly different from multiple primary keys since the former allows composite parts of the key to be duplicated as long as the complete key is unique. The latter (if allowed) would require each part to be unique since that's the property of a primary key.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
2

Yes you can have multiple keys per table and you can have multiple attributes in each key. Each key by definition must be irreducible, which means it contains the minimal set of columns needed to maintain uniqueness of that key.

Based on your comments, I suspect you are actually enquiring about having ONE key with multiple attributes rather than multiple keys. The difference is important and you should understand that. You might want to consider studying a book or taking a course on database design fundamentals to help you answer questions like this one.

nvogel
  • 24,981
  • 1
  • 44
  • 82
  • Many thanks your answer helped a lot, now I'll reduce my requirement on 3keys as this is not big exercise. Thanks again:) – niki May 13 '12 at 08:47
  • Keys have nothing to do with the size of the exercise and everything to do with data integrity. – nvogel May 13 '12 at 09:37