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,940 points
464 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
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 in Database by Kithuzzz
• 20,660 points
110 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 in Database by Kithuzzz
• 20,660 points
61 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 in Database by Kithuzzz
• 20,660 points
56 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
2,656 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
25,432 views
0 votes
1 answer

Add primary key to existing table

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

answered Sep 12 in Database by narikkadan
• 37,660 points
67 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
1,053 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
1,003 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 in Database by Vaani
• 7,020 points
168 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
227 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