Table of users with same usernames

Asked

Viewed 59 times

3

In a Mysql database table, I have new user registration, and the primary key of the table is an ID field. However, I may end up creating, even by accident, users with the same username.

For example, "Joao.silva" can be registered to users named "João Carlos da Silva" and "João Silva Neto". Only then the login will only work for one of them.

Is there any configuration that can be done in Mysql so that this table does not allow identical usernames?

  • 4

    Yes, make that column one unique key

  • Well, the correct thing would be to perform a select at the time of registration, checking if a name is the same as trying to register, if there is no normal registration, otherwise displays message that user already exists!

  • @rray Know how I can do this via Adminer or through some SQL statement?

  • http://stackoverflow.com/q/15255304/4190610

2 answers

6


Makes this column a key Unique that will return an error when an existing value is inserted or updated:

ALTER TABLE tabela ADD UNIQUE INDEX `nome_indice` (`coluna`);

4

The safest way would be to add a single Constraint, in no way will you have equal usernames.

In your application you treat code error "1062" which is relative to this single key violation and returns a message stating.

The code to add this key after the created table is as follows:

ALTER TABLE tb_usuarios
ADD CONSTRAINT uc_usuario_unico UNIQUE (campo_user)

Where tb_usuarios is your table, uc_usuario_unico is an identifier for that unique key and campo_user is the field that should be unique.

Browser other questions tagged

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