+ 9

How to select primary key from given table columns?

If you have 4 columns in your table as(First Name, Last Name, Salary, Department) then which column would be most suitable to make as primary key and why would you choose that. Basically I just wanted to know how it should be done or is there anything related to foreign key in this situation.

30th Jan 2017, 10:16 PM
Aditya Prakash
Aditya Prakash - avatar
46 Answers
+ 20
You should make one Extra Column Name ID and that should be the primary key. That key could be used also as Surrogate Key when required in your Scripting.
30th Jan 2017, 11:26 PM
Arpan Lunawat
Arpan Lunawat - avatar
+ 7
Like Everton said. Nothing in the columns you have will always have unique values and as such are useless as primary keys.
30th Jan 2017, 10:56 PM
jay
jay - avatar
+ 6
salary will be bad too. you will not be able to have two people on the same salary in the whole company.
30th Jan 2017, 11:00 PM
jay
jay - avatar
+ 6
you could go primary on surname and foriegn on department.. not great but it should work as long as two people with the same last name will never be in the same department
30th Jan 2017, 11:02 PM
jay
jay - avatar
+ 6
I suggest make a Super Key of First Name and Last Name that acts as Primary Key I hope that works………
1st Feb 2017, 4:38 AM
Shirious
Shirious - avatar
+ 6
If a key on all 4 isn't practical just to move on, some people insert a numeric (or alpha) identifier and extract it later (stored procedure or view often easiest). To retain sorting it's zero-padded, e.g.: Jones_001 Bob Jones_002 Bob Jones_A Bob It's dirty but effective. The catch is shuffling that number around. If salary contains a decimal (like 10000.00), you could use digits past the significant ones (I think this could be messier): 10000.00001 10000.00002
1st Feb 2017, 3:03 PM
Kirk Schafer
Kirk Schafer - avatar
+ 6
I suggest you create an integer column for a primary key; unique. You can also use a combination of statements to create an a-zA-Z0-9 value.
4th Feb 2017, 2:18 PM
Mark Foxx
Mark Foxx - avatar
+ 4
its pretty good english. wish i could speak portuguese half as good as you speak english lol
30th Jan 2017, 10:52 PM
jay
jay - avatar
+ 4
mmhmmm we suggested this. think his boss said no.
30th Jan 2017, 11:28 PM
jay
jay - avatar
+ 3
if i had to choose, i would choose to create a new identity column, all these columns can have equal results and you're not going to want this. PS: Sorry for my bad english, i'm brazilian!
30th Jan 2017, 10:48 PM
N1NJ4BR
+ 3
then salary is the best column, i guess... Because salary is the column that can most change. You can have persons with the same first name or last name, and i think it's not just one person per department.
30th Jan 2017, 10:58 PM
N1NJ4BR
+ 3
the jay's solution should work for now, but whenever possible use an identity column for this cases!
30th Jan 2017, 11:05 PM
N1NJ4BR
+ 3
Primary key is a column or a set of columns what can be used to uniquely identify a "single" row in the table. In another words, primary key is an identifier for a record like the social security number is an identifier for a person in US. Now, Given your four columns there were no such column that can be used as primary key. Because Two persons can have same first name and last name. But if are sure enough that in practice it won't happen (depends on your domain/business) then you can use both as primary key. Alternatively, create an id column and give it an integer to identity a single person/record. That is the standard usage today
2nd Feb 2017, 3:39 AM
Anwar
Anwar - avatar
+ 3
best way to learn relational databases is to see the example of northwind in Microsoft access
2nd Feb 2017, 6:31 AM
anwer
anwer - avatar
+ 3
make a simple ID and its type Int ( increamental) and no duplicates and not null
2nd Feb 2017, 7:02 AM
anwer
anwer - avatar
+ 3
a sql statement might look like this Select LastName, FirstName, age from tblDemographics where ID= 5 ; so with one id you can get all the columns or as many as you want for all columns use Select * from tblEmployees where EmployeeID =8;
2nd Feb 2017, 7:05 AM
anwer
anwer - avatar
+ 3
Since you said you can't add columns, and if you're lucky enough to be using postgresql for your sql here. Postgresql has an innate primary key called oid. you can do SELECT oid, * FROM table. Keep in mind that oid eventually wraps so its not exactly a PK with large tables. If you're using anything else your best bet is to make it a multi column PK constraint that uses all 4 columns. Add "CONSTRAINT pk_id PRIMARY KEY (First name, last name, salary, department)". But as pointed out by everyone, in a real world situation this does not work because people have the same name could work in the same department and make the same amount. So even that 4 column PK could be non-unique. Create a real PK column like id when possible.
3rd Feb 2017, 2:10 PM
Dave Cha
Dave Cha - avatar
+ 3
it's better to have another column as ID. after adding that column you can give the data type as int. then you can goto properties of the rable which is at the bottom in visual basic 2010. then you can give the auto increment property for it by making identity specifiation "yes". then by right clicking the column name,you can set the primary may for it. It's not sutable to set names or anyother strings as primary key(PK),because that data can be duplicate. when we set those as PK you could not be able to add duplicate values for it. PK never allows to duplicate values and even if not allows the null values.that's why we made the ID as an auto increment.
4th Feb 2017, 11:49 AM
Nishra Nasar
Nishra Nasar - avatar
+ 2
hahaha i'm learning, but thank you!
30th Jan 2017, 10:53 PM
N1NJ4BR
+ 2
sorry additional column is not allowed then I have to crack this problem
30th Jan 2017, 10:54 PM
Aditya Prakash
Aditya Prakash - avatar