I have the following table
id year name activation
1 2013 TEST 1
id A_I
year, name UNIQUE
name, activation UNIQUE
I use this query to INSERT/UPDATE data:
INSERT INTO LISTE_DATI
(year, name, activation)
VALUES
('$varray[1]', '$varray[2]', '$varray[3]')
ON DUPLICATE KEY UPDATE
year= '$yr',
name= '$na',
activation= '$act'
If I send this data to the table:
$yr = 2014
$na = TEST
$act = 0
the query INSERT data in the table. This is ok for me!
If I send this data to the table:
$yr = 2015
$na = TEST
$act = 1
the query UPDATES the first row (2013/TEST/1) in the table.
In this case I'd like to have an INSERT too.
How can I adjust it?
You are telling your INSERT query, that when it finds a duplicate (UNIQUE) key, to instead update that row.
You are inserting (2012, 'TEST', 1). This is a duplicate key; the name, activation key, your 2nd UNIQUE key! You already have a row with 'TEST', 1; the row with id=1.
The INSERT query updates that row, since it's a duplicate key.
You need to modify the keys on your table so that it reflects the data you want in it. What do you want the INSERT query to consider a duplicate? Create your UNIQUE keys based on that.
Related
Is there a way to auto-increment in MYSQL after deleting a row from the database?
For example:
There is a table with 3 columns: StudentID, Student Name, and Contact details. Here StudentID will be the primary key which will keep incrementing after adding values in each column.
The PHP code will look as follows:
<?php
require_once "Delete_Form.php";
if ($_GET || id['id']) {
$id = mysqli_real_escape_string($db, $_GET['id']);
} else {
echo 'Value was not brought over';
}
echo $id;
$result = mysqli_query($db,"SELECT StudentID, StudentName, Contact FROM student WHERE
StudentID='$id'");
$row = mysqli_fetch_row($result);
$sql= "DELETE FROM `student` WHERE `student`.`studentID` = $id";
echo "<pre>\n$sql\n</pre>\n";
mysqli_query($db,$sql);
echo 'Success -Continue...';
return;
Once we delete an entry from the database the Auto-Incrementation of StudentID will mess up i.e if the last entry had a StudentID of 12 and then we delete the same then the next row we enter will have StudentID of 13.
We can always do ALTER TABLE `student` AUTO_INCREMENT = 1 which will reset it but that will solve the problem temporarily only.
Is there a way to add a PHP statement in the above code to reset auto increment whenever we delete a row?
Do it never.
Primary key in a table identifies the row uniquely during the whole table lifetime. Pay attention - TABLE lifetime, not ROW lifetime. The fact that the row was deleted changes nothing - the value identifies this deleted row nevertheless.
If you need rows enumeration without the gaps then create special column for this purposes or enumerate in a query.
PS. By the way, synthetic AI PK must be hidden for the user at all - this column destination is row identifying and foreign keys subsystem work. It must not have any additional meaning.
I used INSERT INTO SELECT to copy values (multiple rows) from one table to another. Now, my problem is how do I insert rows with its corresponding IDs from different tables (since it's normalized) into a gerund table because it only outputs one row in my gerund table. What should I do to insert multiple rows and their corresponding IDs in the gerund table.
My code for the gerund table goes like this.
$insert = "INSERT INTO table1 SELECT * FROM sourcetable"; // where id1 is pk of table1.
$result =mysqli_query($conn,$insert)
$id1=mysqli_insert_id($conn);
Now table 1 has inserted multiple rows same as the other 2 tables.
Assuming id.. are the foreign keys
INSERT INTO gerundtable (pk, id1,id2,id3) VALUES ($id1,$id2,$id3);
My problem is it doesn't yield multiple rows.
According to MySql documentation:
For a multiple-row insert, LAST_INSERT_ID() and mysql_insert_id() actually return the AUTO_INCREMENT key from the first of the inserted rows. This enables multiple-row inserts to be reproduced correctly on other servers in a replication setup.
So, grab the number of records being copied, and the LAST_INSERT_ID() and you should be able to map exact IDs with each copied row.
In the lines of:
$mysqli->query("Insert Into dest_table Select * from source_table");
$n = $mysqli->affected_rows; // number of copied rows
$id1 = $mysqli->insert_id; // new ID of the first copied row
$id2 = $mysqli->insert_id + 1; // new ID of the second copied row
$id3 = $mysqli->insert_id + 2; // new ID of the third copied row
...
$mysqli->query("INSERT INTO gerundtable (pk, id1,id2,id3) VALUES ($id1,$id2,$id3)");
Thank you for trying to understand and also answering my question. I resolved my own code. I used while loop to get the ids of every row and didn't use INSERT INTO SELECT.
Here is the run down. SInce I'm just using my phone bare with my way posting.
$sqlselect = SELECT * FROM table1;
While($row=mysqli_fetch_array(table1){
$insertquery...
$id1=mysqli_insert_id($conn)
$insertgerundtable = INSERT INTO gerundtable VALUES ( $id1, $id2);
}
Following is my database in mysql:
Id Username Password
1 admin admin
2 jay jay1
3 suman xyza
4 chintan abcde
This is my code in php:
$fetchid = mysql_query(" SELECT MAX(Id) As max From user;");
$row = mysql_fetch_array($fetchid);
$largest = $row['max'];
$largest++;
$user= $_POST['username'];
$pass= $_POST['password'];
$result = mysql_query(" INSERT INTO `proshell`.`user` (
`Id` ,
`Username` ,
`Password`
)"."
VALUES (
'".$largest."', '".$user."', '".$pass."'
);");
Problem:
Now if I delete row with Id=1 and then re-enter the data then it should use ID=1 then Again I reinsert the data it use ID=5
It works like this:
if I delete row with Id=1 and then re-enter the data the Id it gets is 5 but then 1 is free so,
What should I write to perform that task.
First, if you set your Id column to AUTO_INCREMENT you don't need the following part in your code at all:
$fetchid = mysql_query(" SELECT MAX(Id) As max From user;");
$row = mysql_fetch_array($fetchid);
$largest = $row['max'];
$largest++;
Because AUTO_INCREMENT will automatic add new value to your ID colume.
But if you don't set it to AUTO_INCREMENT, the above code will grab the MAXIMUM ID value (in this case, 4).
When you re-enter your data again after you delete the row 1, the MAXIMUM ID still 4, so your new ID value will be 5 (from $largest++;).
.....
If you really need to use consecutive ids as you PK, you need to re-write you code but I suggest you to use UUID for you ID column instead.
You can easily generate UUID by using uuid().
How about the UUID performance? Refer to Dancrumb's answer about this:
A UUID is a Universally Unique ID. It's the universally part that you should be considering here.
Do you really need the IDs to be universally unique? If so, then UUIDs
may be your only choice.
I would strongly suggest that if you do use UUIDs, you store them as a
number and not as a string. If you have 50M+ records, then the saving
in storage space will improve your performance (although I couldn't
say by how much).
If your IDs do not need to be universally unique, then I don't think
that you can do much better then just using auto_increment, which
guarantees that IDs will be unique within a table (since the value
will increment each time)
see. UUID performance in MySQL?
EDIT: I don't suggest you run query on the whole table just to find the MAX ID value before inserting new value everytime, because it will give you a performance penalty (Imagine that if you have million rows and must query on them everytime just to insert a new row, how much workload causes to your server).
It is better to do the INSERT just as INSERT, no more than that.
EDIT2:
If you really want to use consecutive ids, then how about this solution?
Create new TABLE just for store the ids for insert (new ids and the ids that you deleted).
For example:
CREATE TABLE cons_ids (
ids INT PRIMARY KEY,
is_marker TINYINT DEFAULT 0
);
then initial ids with values from 1-100 and set marker to be '1' on some position, e.g. 80th of whole table. This 'marker' uses to fill your ids when it's nearly to empty.
When you need to INSERT new Id to your first table, use:
$result = mysql_query("SELECT ids, marker FROM cons_ids ORDER BY ids ASC LIMIT 1;");
$row = mysql_fetch_row($result);
and use $row[0] for the following code:
INSERT INTO yourtable (Id, Username, Password)
VALUES ($row[0], $username, $password);
DELETE FROM cons_ids
WHERE ids = $row[0];
This code will automatically insert the lowest number in cons_ids as your Id and remove it from the cons_ids table. (so next time you do insert, it will be the next lowest number)
Then following with this code:
if ($row[1] == 1) {
//add new 100 ids start from the highest ids number in cons_ids table
//and set new marker to 80th position again
}
Now each time you delete a row from your first table, you just add the Id from the row that you deleted to cons_ids, and when you do INSERT again, it will use the Id number that you just deleted.
For example: your current ids in cons_ids is 46-150 and you delete row with Id = 14 from first table, this 14 will add to your cons_ids and the value will become 14, and 46-150. So next time you do INSERT to your first table, your Id will be 14!!.
Hope my little trick will help you solve your problem :)
P.S. This is just an example, you can modify it to improve its performance.
First of all, as I understand, you are selecting highest column ID which should be always the last one (since you set auto-increment on ID column).
But what are you trying to do is actually filling up holes after delete query, right?
If you are really looking for such approach, try to bypass delete operation by making new boolean column where you flag record if it is active or not (true/false).
SQL table change:
Id Username Password Active
1 admin admin false
2 jay jay1 true
3 suman xyza false
4 chintan abcde true
PHP request:
$fetchid = mysql_query(" SELECT MIN(Id) As min FROM user WHERE active = false;");
$result = mysql_query(" INSERT INTO `proshell`.`user` (
`Id` ,
`Username` ,
`Password`
`Active`
)"."
VALUES (
'".$largest."', '".$user."', '".$pass."', 'true'
);");
I am inserting data into a database fine with the user entering a reference number eg 1234. Can I change my insert to not require the user to input the value and for the last value entered to be checked and then the reference number being inserted be incremented by one and then inserted with the other data. Bit of a new bee. Here is my current code
$Reference_No = $_POST['Reference_No'];
$Property_Name = $_POST['Property_Name'];
$Property_Area = $_POST['Property_Area'];
mysql_query("INSERT INTO properties (Reference_No, Property_Name, Property_Area)
VALUES ('$Reference_No', '$Property_Name', '$Property_Area')");
You need to make the Reference_No an AUTO_INCREMENT.
Step 1:Create table
CREATE TABLE properties (
Reference_No int AUTO_INCREMENT ,
Property_Name varchar(255),
Property_Area varchar(255),
PRIMARY_KEY (Reference_No)
)
Step 2 : Set the start for auto increment of primary key if you like
ALTER TABLE properties AUTO_INCREMENT=1234;
Step 3: Insert the data into the table
INSERT INTO properties (Property_Name, Property_Area)
VALUES ('$Property_Name', '$Property_Area')");
interogate the database for the Reference NO (where property name matches if you need it)
$reference_no_query = mysql_query("SELECT Reference_No FROM properties WHERE Property_Name = $Property_Name");
pull the Reference No out of the database
$Reference_no = mysql_fetch_array($reference_no_query)
display the Reference no
echo $Reference_no('Reference_no');
you can (and should) tie the data to a variable then echo the var like this:
$Reference_no_display = $Reference_no('Reference_no');
then display it directly from the variable anywere and as many times as you want in the page below the query:
echo $Reference_no_display;
This seems to do the trick for the final bit
printf("Last inserted record has id %d\n", mysql_insert_id());
I'm working on a school manager script.
I don't know how to insert a custom unique id of subscription...
I just use this function to show it's unique subscription's id when showing his/her full informations from the database:
$year = date('Y');
$ID = substr($student->dateNaissance,8,10).$student->id_etudiant."/".$year;
The function is combined of 3 things:
The 2 last digits of the year of birth (example: 01/01/1981.. i take only this -->81 using the substr function)
The row id from the table on the database (ex: 50).
And the year of subcription(example: 2013)
all that gives me , for example, as result 8150/2013
what i want here is when inserting the student data into the database , i want this unique ID to be inserted as well..
The problem here is i don't know how to get the last id of a row !
Yeah, I tried to insert the student data and then update the id_subscription using this:
if(isset(....){
......
$mysqli->query("INSERT INTO table (a,b,c, ...etc) VALUES('','',''..etc)");
$year = date('Y');
$mysqli_query("SELECT * FROM table_name");
$studentID = $mysqli_insert_id();
$ID = substr($student->dateNaissance,8,10).$ID."/".$year;
$mysqli->query("UPDATE table_name SET id_subscription = $ID");
}
But its not working :\
By the way: in my table Im using an auto_increment id + the subscription_id in which i want to insert the customized id I showed above.
Assuming that row id is an auto_increment field, then you'd have to do it in two stages:
start transaction
insert everything into the DB EXCEPT your id field
use last_insert_id() to get the mysql-generated ID field
build your own id field
update the record with this new id
commit the transaction.
On Database Structure:
1. you can use auto-incremented primary key in your table and store the data(student_id) as a seperate column(recommended).
On getting 'the last id of a row':
1. Use mysql_insert_id() .. check this out