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 in Database by Neha
• 8,560 points
78 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 by Vaani
• 7,020 points

Related Questions In Database

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
2,390 views
0 votes
0 answers

How do I UPDATE from a SELECT in SQL Server?

INSERT INTO Table (col1, col2, col3) SELECT col1, ...READ MORE

Feb 4 in Database by Vaani
• 7,020 points
20 views
0 votes
1 answer

How Can I use "Date" Datatype in sql server?

There's problem in all of them and ...READ MORE

answered Feb 9 in Database by Neha
• 8,560 points
73 views
0 votes
1 answer

How can I calculate deciles with a range of 12,000 cells in excel?

1. Enter the following formula in cell ...READ MORE

answered Mar 25 in Database by gaurav
• 13,560 points
148 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
23,381 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,390 points
952 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,630 points
878 views
0 votes
1 answer
0 votes
1 answer

How do I UPDATE from a SELECT in SQL Server?

MERGE INTO YourTable T USING ...READ MORE

answered Feb 3 in Database by Vaani
• 7,020 points
57 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 in Database by Vaani
• 7,020 points
62 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP