This question already has answers here:
Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?
(6 answers)
Closed 9 years ago.
mysql database.
Table have index on field "Code".
I need to insert to table new rows.
What works faster?
1)
simple index on field Code - for fast select
befor insert check rows : SELECT COUNT(*) FROM table WHERE Code = 'NewCode';
simple insert(if rows not found): Insert into table values ('NewCode')
2)
unique index on field Code - for insert
Insert IGNORE into table values ('NewCode')
For me more secure (and can be a backup of changes - better way) is the first, but I think that the action is similar.
Consult http://dev.mysql.com/doc/refman/5.5/en/insert.html for more detali
paladinux
Related
This question already has answers here:
How to copy a row and insert in same table with a autoincrement field in MySQL?
(16 answers)
Closed 8 months ago.
my project: PHP
i have a table. i want copy one row into same table with diffrent primary key.
sql code: INSERT INTO messages SELECT * FROM messages WHERE id='$id'
when i click on submit show :
Error: INSERT INTO messages SELECT * FROM messages WHERE id='12' Duplicate entry '12' for key 'PRIMARY'
Because you're trying to insert every field in that row, which includes the primary key. If you want to specify a subset of fields, specify a subset of fields:
INSERT INTO messages (ColumnA, ColumnB, Etc)
SELECT ColumnA, ColumnB, Etc
FROM messages WHERE id='$id'
That way you're not also inserting a value into id.
(This is of course assuming that the database auto-generates the primary key. If it doesn't, you'd need to insert whatever value would be required for a primary key.)
As an aside, the use of what appears to be a PHP variable (and explicit quotes) in your SQL code strongly implies that you are likely vulnerable to SQL injection. Right now is the best time to correct that.
This question already has answers here:
How to treat MAX() of an empty table as 0 instead of NULL
(3 answers)
SELECT max(x) is returning null; how can I make it return 0?
(7 answers)
Closed 1 year ago.
I am trying to insert different content based on if a SQL table has Rows. So far the Query will check what the max value of sortingOrder is and then + 1. However this query will break if there is no rows in the table. How can I implement a if statement to check if the table has no rows and then if it doesn't set the sortingOrder to '1'.
INSERT INTO faq (question, answer, sortingOrder)
VALUES ('$questionData', '$answerData', (SELECT MAX(sortingOrder) FROM faq C) +1)
Thanks
The best solution is to make sortingOrder an AUTO_INCREMENT column. The database will assign the values automatically, incrementing them for each row.
If you can't do that for some reason, you can check if the subquery returns NULL and replace it with 1.
INSERT INTO faq (question, answer, sortingOrder)
SELECT '$questionData', '$answerData', IFNULL(MAX(sortingOrder)+1, 1)
FROM faq
This question already has answers here:
How can I do 'insert if not exists' in MySQL?
(11 answers)
Closed 5 years ago.
I am struggling a bit with something that I think should be simple to solve.
I first check the database to see if it exists. I would like it to insert the date if it does not exist but I am not sure how to structure the IF statement part. Many thanks
$date='2017-05-13';
$select_date = mysqli_query($link, "SELECT * from `marker` WHERE `date`='$date' ");
$insert_date = mysqli_query($link, "INSERT INTO `marker` (`date`,`value`) VALUES ('$date','1') ");
In general, for this type of operation, you want to use on duplicate key update. This starts with a unique index:
CREATE UNIQUE INDEX unq_marker_date ON marker(date);
Then the database guarantees only one row per date. You can then do the insert as:
INSERT INTO `marker` (`date`, `value`)
VALUES ('$date', '1')
ON DUPLICATE KEY UPDATE `date` = VALUES(`date`);
The ON DUPLICATE KEY part does nothing except prevent an error for a duplicate date.
This question already has answers here:
Easy mysql question regarding primary keys and an insert
(4 answers)
Closed 9 years ago.
so I want to link two tables with a common column "messageID". so first I insert into table 1
to get the Auto incremented id, then take that ID with LAST_INSERT_ID function and give that as the id for table 2:
$db->("INSERT INTO table_1 VALUES('','$message')");
$db->("INSERT INTO table_2 VALUES(LAST_INSERT_ID(),'$message');
but here's my concern, there might be two users running this script simultaneously, so in the few milliseconds between the two queries exicuting the LAST_INSERT_ID could have changed, so now the two id's are different. Is there any way I can prevent this possibility. I know it is not possible to insert into two tables with one query, which was my first thoughts. Any ideas much appreciated. Thank you
The LAST_INSERT_ID is local to the connection session, so it will not conflict with a different user making an insert.
You could try using scope_identity, which would be something like this:
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
);
$arg = ... $myARR['LAST_ID'] ... //however you want to get the LAST_ID from your query to PHP here
$db->("INSERT INTO table_2 VALUES(#arg,'$message');
or
$db->("DECLARE #LAST_ID int
INSERT INTO table_1 VALUES('','$message')"
SET #LAST_ID = SCOPE_IDENTITY()
INSERT INTO table_2 VALUES(#LAST_ID,'$message')
);
LAST_ID would be the value of the Auto incremented id
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php/MySQL insert row then get 'id'
I have an inset command such as
insert (name) values($name);
I have id column as autoincrement. How can I get the id of the inserted record after inserting.
insert (name) values($name);
SET #lastid = LAST_INSERT_ID();
select blah_blah from table where id = #lastid;
To get that back into php, you do a normal mysql select on it like this:
select #lastid;
For mysql you can use mysql_insert_id to get the id of the last inserted row.