php - Managing IDs in mySQL
I have a users table in my database. The structure is this:
fam_id
user
name
username
The fam_id
is primary with AUTO_INCREMENT and goes like 1, 2, 3....
The user
is the telegram's userID, which I use to identify the users.
The name
and the username
are the name and the username in telegram.
Ok, everything is ok, I insert suer, name, username, Mysql generates the fam_id. But now I need to give to some users the same fam_id.
So my question is: Is there a way to generate a casual id, which mySQL can't give to anyone else, but that can be changed by me to another's user fam_id, having so to users with the same fam_id?
I simply tried by making fam_id AUTO_INCREMENT and changing with UPDATE, but it doesn't work.
Answer
Solution:
Well, because fam_id
is a primary key and it's AUTO_INCREMENT
, you can not and should not change it. If you really, really need this funtionality you described, you can add a column, like my_id
.
Then at your implementation, after you inseted a new user...
res = MySQL.query(INSERT INTO users ... )
... MySQL returns a result set. Have a look at it and do something like this:
temp = res[0].fam_id
MySQL.query(UPDATE users SET my_id = temp WHERE fam_id = temp)
PS: This is js syntax, but I'm sure php has something similar
Source