1

Possible Duplicate:
Best way to avoid duplicate entry into mysql database

this is my query to add the user and denying them if there already exists an user with the same name in the user table:

insert into  user values(?, ?, ?, ? ) select * from user 
where NOT EXISTS(select * from user where user.username = ?);
Community
  • 1
  • 1
Lucky
  • 16,787
  • 19
  • 117
  • 151

3 Answers3

1

Just use INSERT IGNORE. If there is a duplicate in a unique column, nothing happens.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
1

i guess you mean same username because names can be same .

so try use this and see

     INSERT INTO  user (column1, column2, column3,...)
     VALUES  (value1, value2, value3,...)
     WHERE NOT EXISTS(
     SELECT username FROM user)

if you mean name , just replace name by username

echo_Me
  • 37,078
  • 5
  • 58
  • 78
0

Why don't define username as primary key if it should be unique?

TieDad
  • 9,143
  • 5
  • 32
  • 58