Email as Primary key

Asked

Viewed 234 times

0

I created a database, and in the courses I did we always used id as Primary key,has problem using the email as the primary key, because that way would prevent the registration of a similar email!

  • Only if you use email as a username, because if you use an email and a username you will happen to be able to use multiple usernames with the same name

  • As shown in the duplicate question you can use email, but you should not, and it is not for the reason you cited in the answers below. It’s because it can be changed, it can be used by more than one person, it can be that the person wanted to have two records, because you don’t control, because it tends to be longer than it should for a primary key, because not everyone has email or wants to provide one, and the list goes on. There’s a link showing that in general people do not understand the implications of using a certain data.

1 answer

6


No problem using email as PRIMARY KEY, however, it is not recommended.

There is a way to avoid that a data is recorded more than once, for this you can inform, during the creation of the table, the value UNIQUE

CREATE TABLE IF NOT EXISTS `usuarios` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(60) NOT NULL,
  `nome` int(60) NOT NULL,
  `idade` tinyint(4) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

If you have already created your table.

ALTER TABLE `usuarios` ADD UNIQUE(`email`);
  • Thanks man, thank you very much, I hadn’t really thought about it, it would really be a problem!!

Browser other questions tagged

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