Hi all I have the following query which works nicely but i know need to add the unique id generated in the insert table to the destination table
insert into claims.third_party(tp_names,tp_insurer,tp_registration)
select c.tpnames,c.tpinsurers,c.tpregistration from claims as c;
so in other words i need to return the unique id and add it to the source table creating a foreign key link
You can use LAST_INSERT_ID() to get the last auto increment value.
SELECT LAST_INSERT_ID();
http://dev.mysql.com/doc/refman/5.6/en/information-functions.html#function_last-insert-id
UPDATE:
Use ROW_COUNT() alongside LAST_INSERT_ID() and you will have the range of keys generated.
[LAST_INSERT_ID(), (LAST_INSERT_ID() + ROW_COUNT())]
Related
I created a table in MySql
CREATE TABLE newuser(id VARCHAR(10) PRIMARY KEY,sname VARCHAR(20));
When I INSERT record it works fine
INSERT INTO newuser VALUE('abc123','monika');
But sometimes I don't want to supply id in the INSERT query and sometimes I want to supply. In case I don't supply id MySql automatically generate one.
What can I do to get both below query works?
INSERT INTO newuser VALUE('abc123','monika');
INSERT INTO newuser VALUE('nikita');
'I don't understood ANYTHING' - very new then.
Firstly second insert statement is invalid please review https://dev.mysql.com/doc/refman/8.0/en/insert.html -
'If you do not specify a list of column names for INSERT ... VALUES or INSERT ... SELECT, values for every column in the table must be provided by the VALUES list, SELECT statement, or TABLE statement.'
Secondly uuid is a function in which 'A UUID is designed as a number that is globally unique in space and time.' https://dev.mysql.com/doc/refman/8.0/en/insert.html
You can easily select uuid() to see what it produces.
You will need to increase the id size
If you wish to use it in an insert
insert into users values (uuid(),<sname>);
I have a following map table, with a unique key on the name column:
id | name
1 aaa
2 bbb
3 ccc
I want to get a newly created ID if I insert a new value to this table, like this:
INSERT IGNORE INTO map VALUES(null, 'ddd');
I know I can do this with getLastId() function. But I also want to get ID if a name already exists, and getLastId() function returns 0 in such case:
INSERT IGNORE INTO map VALUES(null, 'ccc');
Is this possible to do with one SQL, and without checking if a record exists, etc?
I think you need to go the extra mile for that one:
check if exists - when so: get ID of that name
if not: SELECT LAST_INSERT_ID();
Depends on what qualifies as a single query.
When you make use of INSERT INTO ... ON DUPLICATE KEY UPDATE you can control the LAST_INSERT_ID() in such a manner that it will return the new id on insert and the id of the row "updated" on update:
INSERT INTO map (name) VALUE ('ccc')
ON DUPLICATE KEY UPDATE id = LAST_INSERT_ID(id);
That will make the LAST_INSERT_ID() then return the map.id of the row you're interested in.
Whatever the API is that you name as the getLastId() (I don't know it so I can't provide more pointers on it) this may not work and you would then need to fire a second query to obtain it:
SELECT LAST_INSERT_ID();
See as well:
MySQL ON DUPLICATE KEY - last insert id? (Stackoverflow Q&A, Apr 2009)
In MySQL, will a REPLACE INTO query work with the LAST_INSERT_ID function?
Essentially, I have to insert into one table, only where it doesn't exist, but then either way insert into the second table with a foreign key constraint of the first table.
So ie.
REPLACE INTO TABLE1(NAME) VALUES('unique');
SET #table1_id = LAST_INSERT_ID();
INSERT INTO TABLE2(TABLE1_ID, VALUE) VALUES(#table1_id, 'Test Value');
Will that function as intended on both an insert and an update?
Yes, REPLACE INTO query affects the result of LAST_INSERT_ID() function.
Documentation states:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
That means if INSERT affects LAST_INSERT_ID() then REPLACE should do it too.
I have tested it with MySQL 8.
I'm trying to add a value to a table but not without checking if the value already exists. This is what I have so far:
IF NOT EXISTS (
SELECT series.seriesName
FROM series
WHERE series.seriesName='Avengers'
)
BEGIN
INSERT INTO series (seriesName) VALUES 'Avengers'
END;
Database is a MySQL db on Ubuntu
You can use IGNORE keyword here.
It could look like:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
The important thing is to create a unique key on seriesName field as it seems that you want it to be unique.
INSERT IGNORE doesn't make the insert when key value already exists.
If you would like to be able to get id (primary key value) for row that wasn't inserted (already existed), you can do the following trick:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
ON DUPLICATE KEY UPDATE seriesID= LAST_INSERT_ID(seriesID)
Then you will be able to get the ID with LAST_INSERT_ID() function no matter if the row was inserted or not.
I need two id columns in the same table that create unique values upon insert. Since MySQL allows only one column to auto-increment, I need to do something like max(id)+1 for the other column.
Here's the SQL I expected to work:
INSERT INTO invoices (invoiceid)
VALUES ((SELECT MAX(invoiceid)+1 FROM invoices))
The select statement works independently, but within my INSERT, it's not allowed. I get the error : You can't specify target table 'invoices' for update in FROM clause
You want to use INSERT INTO .... SELECT FROM instead of INSERT INTO...VALUES():
INSERT INTO invoices (invoiceid)
SELECT MAX(invoiceid)+1
FROM invoices
My question for you would be why are you not use an AUTO INCREMENT field to generate the invoiceid value? That is what it is for, then you will not have to create this when inserting data.