I know this is pretty elementary but here it goes.
I would like to know how you know what columns are a primary key in a table that does not have a primary key? Is there a technique or something that I should read?
Thank you in advance
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to take a look at your data structures.
A primary key must:
and it helps if it’s
Check your data – which columns or set of columns can fulfill these requirements??
Once you have those potential primary keys (the “candidate keys”) – think about how you will access the data, and what other data might need to be associated with this one entity in question – what would make sense as a foreign key? Do you want to reference your department by its name? Probably not a good idea, since the name could be misspelled, it might change over time etc. By the department’s office location? Bad choice, too. But something like a unique “department ID” might be a good idea.
If you don’t find any appropriate column(s) in your actual data that could serve as primary key and would make sense, it’s a common practice to introduce a “surrogate key” – an extra column, often an
INT(and often something like an “auto-increment” INT) that will serve as an artificial identifier for each row. If you do this, one common best practice is to never show that artificial key on any data screen – it has no meaning whatsoever to the users of your system – so don’t even show it to them.Checking these requirements, and a lot of experience, will help you find the right primary key.