0

Why do we need a primary key? For programming IN SQL , Is it possible to have more than one primary key or cannot be any key

DATABASE

26th Feb 2020, 6:03 PM
HARSHVARDHAN NITNAWARE
7 Answers
+ 1
Primary key is used in a table to uniquely identity a record. It helps you avoid duplicate insertions. No, it is not possible to have more than one primary key in a table because multiple primary keys together are called a composite key which behave a little differently. Yes, you can have a table without a primary key but it is something that is not encouraged and is a bad practice. Edit: Also a primary key in one table can be used as a foreign key in some other table which will help you write join queries.
26th Feb 2020, 6:52 PM
Avinesh
Avinesh - avatar
+ 1
For any table, there'll be a unique column and that is known as primary key. We need a primary key in order to identify every entry with unique I'd. No, it's not possible to have more than one primary key..
26th Feb 2020, 7:11 PM
Dheeraj
Dheeraj - avatar
0
So what is the difference between primary and foreign key? What's it main purpose?
27th Feb 2020, 4:55 AM
HARSHVARDHAN NITNAWARE
0
A primary key in one table is used as a foreign key in some other table. This helps you establish a relationship between the two tables. This is one of the reasons it is called Relational Database Management Systems (RDBMS).
27th Feb 2020, 5:36 AM
Avinesh
Avinesh - avatar
0
As stated previously, a primary key is the unique identifier for a particular table. It prevents you from duplicating records in the table. For example, you have a table of students and the primary key is the student number. You would not be allowed to enter two students into the table that have the same student number. But you could have two students with other attributes that are the same - like same name. Both of these students could be entered into the table but only if their student numbers are different from each other and all records in the table. A Foreign key is a reference. It can exist in another table that already has a primary key of its own. This reference key allows you to join data from two tables that otherwise would not be combined. Continuing the example...you have another table for payments made by students. The primary key might be payment id, and the foreign key would be the student number. You may have many payments for the same student, but each payment made was separate.
27th Feb 2020, 5:54 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
0
ex: select s.student_name, s.student_number, p.payment_id, p.payment_date, p.payment_amount from students s, payments p where s.student_number = p.student_number; —(primary key in students table = foreign key in payments table) output: Joe 888 d1ft 28-Feb-20 130.89 Cal 434 ji3a 22-Feb-20 645.32 Joe 888 d1ft 31-Jan-20 145.21
27th Feb 2020, 6:01 AM
Elizabeth Kelly
Elizabeth Kelly - avatar
0
Elizabeth Kelly Nice effort you are putting there but my only concern is that he might not understand things that you are showing him through queries. He is a beginner and has not yet started the course on sololearn. But just letting you know so that your efforts don't go in vain.
27th Feb 2020, 7:07 AM
Avinesh
Avinesh - avatar