top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Is it possible to create a table in MYSQL where more then one fields can be defined as Primary key ?

0 votes
400 views
Is it possible to create a table in MYSQL where more then one fields can be defined as Primary key ?
posted Apr 17, 2017 by Ajay Shetty

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

I hope I understood your question correctly -
1) One table can not have more then one primary key.

create table User(
   name  TEXT  NOT NULL , 
   email  TEXT  NOT NULL ,
   PRIMARY KEY (email)
);

2) Its possible to have one primary key and rest as keys (unique key).

create table User(
   name  TEXT  NOT NULL , 
   email  TEXT  NOT NULL ,
   PRIMARY KEY (email),
   KEY (email)
);

3) Its also possible to have a combination of two column as a single primary key where individual value can be repeated in the same table but not the combination

create table User(
   name  TEXT  NOT NULL , 
   email  TEXT  NOT NULL ,
   PRIMARY KEY (name, email)
);
answer Apr 17, 2017 by Salil Agrawal
0 votes

Hi...

In MySql there is not possible to two primary key in same table. but you can use multiple unique key in same table.

example:

create table User(
   name  TEXT  NOT NULL , 
   email  TEXT  NOT NULL ,
   PRIMARY KEY (email)
);
answer Nov 27, 2019 by Siddhi Patel
Similar Questions
0 votes

Is it possible to connect database with c code, if yes then how?

If possible share the sample code for connecting to a MySQL database and run a sample query....

+1 vote

In my table I have three unique column this can be referred by other table.
Which key I have to use for this.

+1 vote

I am using for MySQL in ubuntu and created more then 7 tables. I haven't used the primary key in the tables.
Now my question is will joiner work without primary keys? If possible please share the query?

...