¿Cómo podría duplicar mi auto_incremento? BASE DE DATOS PHP

Hey how would I be able to duplicate my only auto increment key to another key, basically I want my (' id ') to display the same information on my (' user_id '), here is the code:

CREATE TABLE IF NOT EXISTS `".$db_table_prefix."users` (
`user_id` int(10) NOT NULL,                 
`user_name` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
KEY `user_id` (`id`)

How would I be able to input the same information from my id to my user_id?

preguntado el 09 de septiembre de 13 a las 23:09

This begs the question, Why? You are duplicating data which is never a good cause. It is easy to accomplish this, when you INSERT or UPDATE a row but it just doesn't seem to make sense. Maybe explain the purpose for this table and data... -

Uh... why? It's pointless to duplicate data like this within the same table. -

id y user_id are redundant in your users mesa, como id IS the user's ID. -

2 Respuestas

Not sure what you mean but if you want to have the same value repeted two times in the same record It's pointless and redundant.

You can use the SQL aliases to achive what you want:

SELECT id as user_id FROM ...

If you really need to sync up the two field of your table you can do:

UPDATE table SET user_id = id WHERE user_id != id

Respondido el 09 de Septiembre de 13 a las 23:09

Not sure why you would want to do this, but if you want to duplicate the information después de an INSERT you would need to fetch the new ID and then perform an UPDATE

// get the newly inserted ID
$new_id = $db->insert_id;

// perform the update on the table
$db->query("UPDATE users SET user_id=".$db->escape($new_id)." WHERE id=".$db->escape($new_id));

Also, in your table definition the fields don't match: int(11) vs int(10).

Respondido el 09 de Septiembre de 13 a las 23:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.