Error trying to create table in mysql - Multiple Primary key defined

Asked

Viewed 387 times

0

I’m creating a database and one of the tables doesn’t want to be created with prayer.

The following error appears:

Error Code: 1068. Multiple Primary key defined

What’s wrong with my code?

CREATE TABLE `controle_ferramentas` (
  `requisitante_cpf` int NOT NULL primary key,
  `data_hora_emprestimo` datetime DEFAULT NULL,
  `data_hora_devolucao` datetime DEFAULT NULL,
  `area_producao_cultivo_id` int NOT NULL primary key,
  `almoxarifado_id` int NOT NULL primary key,
  FOREIGN KEY (`area_producao_cultivo_id`) REFERENCES `area_producao_cultivo` (`id`),
  FOREIGN KEY (`almoxarifado_id`) REFERENCES `almoxarifado` (`id`),
  FOREIGN KEY (`requisitante_cpf`) REFERENCES `funcionarios` (`cpf`)
);
  • If you want to define a composite primary key then do not put the PRIMARY KEY attribute in each of the columns but put a separate PRIMARY KEY clause: PRIMARY KEY (requisitante_cpf, area_producao_cultivo_id, almoxarifado_id). You just have to check if it really makes sense for your model.

2 answers

3

Your code contains:

area_producao_cultivo_id int NOT NULL (primary key), 
almoxarifado_id int NOT NULL (primary key)

If the error says "Multiple Primary key defined", I believe it can only have a "Primary key" - "primary key".

Try to leave only one primary, usually I leave only the "id" at the beginning as Primary key.

0

Your error is giving 'Multiiple Primary key'' because per table there can only be a primary key, if you have the requester’s Cpf then this will be a primary key and for the other fields you want to turn into primary key, for the sake of organization, would be from other tables, and to select everything you use select and Where. By definition there can only be one primary key per table.

You may have several constraints Unique but Primary key only one per table.

I hope I’ve helped!!

  • understood, I will keep only one primary key, I’m sorry the level of the question, is that I’m starting in the area. thanks!

  • Don’t even worry about it, I’m a beginner :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.