how can i set id primarykey auto_increment same value that automatically generating into my id field will also generate in other field in same table.
How could it possible?
Use mysql_insert_id which returns last auto increment ID...
1) insert into first table, use mysql_insert_id() to get auto increment ID
2) after getting auto increment ID from 1st table, insert on second table with the ID you got.
for more refer http://php.net/manual/en/mysqli.insert-id.php
Related
I've created a database in MySQL which has a user_id that is primary key and auto incremented. Now my question is can I insert a static data in a column that is auto incremented? For example I want to insert a data "U-01" instead of just 1 only.
The column must be unauto increment, then you can insert this data!
I have a table - 'A' which has primary key - id which is auto incremented.I also have a table - 'B' which also has the column 'id'. Now I want to add entries to the table A so that value of id attribute of table 'A' which just got inserted to the table also gets inserted to B while other attributes of B can remain NULL.I know I can just make one table with all the columns but is there a way to do this with two tables??
Use the mysqli_insert_id () function right after you run the insert to table a. That will give you the value of the auto incremented id from table a which you can then populate in the surrogate key column of table b.
Read about it here:
http://php.net/manual/en/mysqli.insert-id.php
When I delete the last row in a MySQL table and then insert a new one, why is the id still incremented as though the first weren't deleted? Is there a way to prevent this from happening so it can be as though the original row weren't there? Here's an example
Teachers
id first_name
0 joe
1 mike
2 jim
If I delete "jim" then insert "bob" then bob's id will be 3 and there will be no entry with an id of 2.
Because your id column was configured as autoincremental, and can't have the same value, unless you specify this manually
Deleting any given row will not change the current auto increment value.
You can view the current value of the auto increment variable for the table by running
SHOW TABLE STATUS LIKE 'mytable';
And you can change the auto increment value by running
ALTER TABLE 'mytable' AUTO_INCREMENT = '1234';
you may also use ALTER TABLE tablename AUTO_INCREMENT = 2 if after deleting the row and before inserting the new one, in order to make the id incrementation start at 2
you need to execute this command after every delete operation , this will reset your auto_increment to 1 witch will be set automatically the the last available id ( 2 in your example )
ALTER TABLE table_name AUTO_INCREMENT = 1
if I use this SQL:
UPDATE formulare SET EV_id=59, EV_status=5 WHERE EV_id=57 AND ID_uziv=12;SELECT LAST_INSERT_ID();
I will get 0 as last insert id.
I'm using php mysqli_insert_id and here is said that:
The mysqli_insert_id() function returns the ID generated by a query
on a table with a column having the AUTO_INCREMENT attribute.
If the last query wasn't an INSERT or UPDATE statement
or if the modified table does not have a column with the AUTO_INCREMENT attribute,
this function will return zero.
my table formualre has auto increment column, so I don't know wher the problem is
LAST_INSERT_ID() won't work if no new auto increment value was created.
The solution is something like this:
UPDATE formulare
SET EV_id=LAST_INSERT_ID(59),
EV_status=5
WHERE EV_id=57
AND ID_uziv=12;
SELECT LAST_INSERT_ID();
Note: I guess, that EV_id is the auto_increment primary key.
Otherwise you should do a query like:
UPDATE formulare
SET key_col = LAST_INSERT_ID(key_col),
EV_id=59,
EV_status=5
WHERE EV_id=57
AND ID_uziv=12;
SELECT LAST_INSERT_ID();
From the documentation :
The ID generated for an AUTO_INCREMENT column by the previous query on
success, 0 if the previous query does not generate an AUTO_INCREMENT
value, or FALSE if no MySQL connection was established.
As your update didn't create a new record, it didn't generate any AUTO_INCREMENT value.
UPDATE query updates the existing record, it doesn't return any new ID.
mysqli_insert_id retrieves the ID generated for an AUTO_INCREMENT column by the
previous query (usually INSERT).
There was no INSERT query, that's the reason, you won't get any Id after executing UPDATE query.
For more info, refer mysql_insert_id
I have a table with 5 rows. Every time a user enters data into a form, it is entered into the table. My first column is called id and holds the number of the post. What I want to do is get the value of id from the previous row, add one to it and set it as the value in the current post's id field. How do I do this?
Just set that field as primary key and auto-increment, it will automatically do this for you. You won't have to fetch the previous row and add that field value to next one.
The SQL query you need is:
SELECT max(id)
FROM tableName;
Set attribute auto increment for "ID" field in the table that contains 5 columns.
You can use sql query like
"INSERT INTO my_table (id auto_increment,primary key(id))";
then you can get...
and eachtime you need not worry to insert id ,it will automatically increments
I would not recomend doing this as it could lead to a race condition.
Change the table structure and set the id field to be the primary key and set it to auto increment. This way anytime a new row is added, it will auto-magically be assigned the next ID.
see this answer on details of how to set auto increment.
here is the query to alter your table and it will set your field or column as primary key and also auto increment it.
ALTER TABLE tbl ADD id INT PRIMARY KEY AUTO_INCREMENT;