I’m trying to create a MySQL database that will hold all of my users and logins.
The table columns are:
User_id
passkey
points
The user_id of each user has to be unique, and same with their passkey. Points is just an integer value that I will edit from time to time. Now, when I get a user to my site, I have to check if he is a new user or an old one. I check by seeing if their user_id is in the database already, and if not, I create a new entry to the table that inputs their user_id, passkey, and 0 for points. So, when I create the table, do I still have to specify unique for the user_id and passkey, even though I’ll be checking first before creating new entries?
And how would I check if the user_id is already in the system? I’m thinking something like:
SELECT * FROM customers
WHERE user_id=’test’
And then count the rows, and if it is zero, I create a new entry, right? I’m trying to make sure I get everything right before I run my code. Thanks.
It’s a good idea to anyway mark the fields as
UNIQUE, because in the case you miss something in your code, the DB will still catch the error before things break down. I’m not sure why you want the passkey to be unique though, is there anything wrong with two users having the same passkey?Your query is fine, but I guess you don’t really need the actual user details when checking, so you can just ask for the count:
and if the returned value is > 0, the
user_idalready exists.