How can I define a composite primary key in SQL

0 votes
In SQL, how do I create a composite primary key with two fields?

To generate tables and everything else, I'm using PHP. I'd want to create a table called voting that has the values QuestionID, MemberID, and vote. The fields QuestionID and MemberID make up the Composite primary key.

How do I go about doing this?
Feb 18, 2022 in Database by Neha
• 9,020 points
1,411 views

1 answer to this question.

0 votes

A primary key is unique and it consists of one or more columns (from that table itself). If a primary key consists of two or more columns it is called a composite primary key. It is defined as follows:

CREATE TABLE vote (
  QuestionID INT,
  MemberID INT,
  PRIMARY KEY (QuestionID, MemberID)
);

The pair (QuestionID,MemberID) must then be unique for the table and neither value can be NULL. If you do a query like this:

SELECT * FROM vote WHERE QuestionID = 2

it will use the primary key's index. If however you do this:

SELECT * FROM vote WHERE MemberID = 2
answered Feb 21, 2022 by Vaani
• 7,070 points

Related Questions In Database

0 votes
0 answers

How can I set a custom date time format in Oracle SQL Developer?

Date values are shown in Oracle SQL ...READ MORE

Aug 12, 2022 in Database by Kithuzzz
• 38,000 points
853 views
0 votes
0 answers

How can I select the first day of a month in SQL?

I only need to choose the given ...READ MORE

Aug 14, 2022 in Database by Kithuzzz
• 38,000 points
658 views
0 votes
0 answers

How can I get column names from a table in SQL Server?

I want to search for every column's ...READ MORE

Sep 3, 2022 in Database by Kithuzzz
• 38,000 points
426 views
0 votes
1 answer

Can I have multiple primary keys in a single table?

A Table can have a Composite Primary Key which ...READ MORE

answered Oct 25, 2018 in Database by Frankie
• 9,830 points
3,882 views
0 votes
2 answers

Difference between primary key a composite key

candidate  key: It is the collection of ...READ MORE

answered Oct 14, 2020 in Database by anonymous
31,351 views
0 votes
1 answer

Add primary key to existing table

You have to drop constraint and recreate ...READ MORE

answered Sep 12, 2022 in Database by narikkadan
• 63,600 points
635 views
0 votes
1 answer

Which query to use for better performance, join in SQL or using Dataset API?

DataFrames and SparkSQL performed almost about the ...READ MORE

answered Apr 19, 2018 in Apache Spark by kurt_cobain
• 9,350 points
1,867 views
0 votes
1 answer

How to connect Java program to the MySQL database?

You can connect your Java code with ...READ MORE

answered May 11, 2018 in Java by Parth
• 4,640 points
1,939 views
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3, 2022 in Database by Vaani
• 7,070 points
837 views
0 votes
1 answer

How do I perform an IF THEN statement in an SQL SELECT?

The CASE statement is the closest to IF in ...READ MORE

answered Feb 16, 2022 in Database by Vaani
• 7,070 points
1,708 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP