How to delete primary key (PK) in Mysql?

Asked

Viewed 3,849 times

2

I have the following table in Mysql:

inserir a descrição da imagem aqui

The id column is primary key. I would like to remove the primary key from this table. How can I do this?

Thank you.

1 answer

1


Run this query to remove the desired column:

ALTER TABLE `tabela` DROP `coluna`;

I see you’re a beginner in PT Stackoverflow, but is it really necessary to remove the primary key from your table? Analyze your case well, it is necessary to differentiate the records, so having something that makes them really unique, extremely important and necessary to UPDATES and DELETES, each case is a case, but before removing it, see if this is really necessary as I have already said.

And if you just want to make one SELECT so that this column doesn’t appear, it’s simple, just send a: SELECT nome, profissao, nascimento, sexo, peso, altura, nacionalidade FROM tabela WHERE...

Finally, to just remove the primary key function you must perform these two querys:

//Você deve primeiro remover a propriedade de AUTO_INCREMENT e depois
//remover a chave primária

ALTER TABLE tabela MODIFY coluna INT NOT NULL;
ALTER TABLE tabela DROP PRIMARY KEY;

For it to become a primary key, just run:

ALTER TABLE tabela MODIFY coluna INT NOT NULL PRIMARY KEY AUTO_INCREMENT;
  • To remove the primary key just by deleting the column? It is not possible to remove the key Primary without deleting the column?

  • @Alexborges is like I said, do you want to take her off the table or just that she doesn’t show up on a SELECT? If it’s the second option, use what I said in the second paragraph

  • @Alexborges Or do you just want her to stop being the primary key inside the table?

  • I want her to just stop being the primary key inside the table.

  • @Alexborges I will modify the answer, see now

  • I get it. Thank you.

  • When you run SQL: "ALTER TABLE table MODIFY column INT NOT NULL;", you remove the auto-increment from the column, this?

  • @Alexborges yes, first the AUTO_INCREMENT must be removed for after the primary key function

  • @Alexborges if this solves your problem, please mark the answer as accepted :), so other people who have the same question can also scan them further

  • I am having problems with my account. As soon as I solve, mark your answer as accepted. Thank you.

Show 5 more comments

Browser other questions tagged

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