why does LAST_INSERT_ID() returns foreign key

Greg

Im using mysql database. When i add some sample data inside my database when i want to return the last insert id as follow:

SELECT DISTINCT LAST_INSERT_ID() FROM facturatiedatabase.tblinvoice;

It returns customersID instead.

This the table i'm using.

CREATE TABLE tblInvoice (       
        invoiceID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        customerID INT,
        totalPriceExcl decimal(65,2),
        vat decimal(65,2),
        totalPriceIncl decimal(65,2),
        conditionState VARCHAR(100) NOT NULL DEFAULT 'In progress' ,
        createDate Date,
        invoiceNumber int ,
        CONSTRAINT FK_customerID_Invoice FOREIGN KEY (customerID) REFERENCES tblCustomers(customerID)
)ENGINE=InnoDB;

Is there something wrong with my syntax or is this a common problem? And is there a workaround ?

Adrian Lynch

You're using it wrongly.

From the docs:

LAST_INSERT_ID() (with no argument) returns a BIGINT (64-bit) value representing the first automatically generated value that was set for an AUTO_INCREMENT column by the most recently executed INSERT statement to affect such a column. For example, after inserting a row that generates an AUTO_INCREMENT value, you can get the value like this:

Note the example doesn't query a table. It's based on the current connection.

mysql> SELECT LAST_INSERT_ID();
        -> 195

If you do an insert into tblInvoice then run SELECT LAST_INSERT_ID() you'll get back the invoiceID.

So a working example would be:

INSERT INTO tblInvoice (columns) VALUES (data);
SELECT LAST_INSERT_ID(); -- The invoiceID generated from above insert

If what you really want is the last created invoice ID, try:

SELECT MAX(invoiceID) FROM tblInvoice

Not perfect, but should be enough for your needs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to store LAST_INSERT_ID() in database as foreign key in another table

From Dev

Why does this foreign key using inheritance not work?

From Dev

Why does Mysql rename my foreign key?

From Dev

Why does MySQL not allow this foreign key?

From Dev

Why does JPA (Hibernate) foreign key definition on @JoinTable not work?

From Dev

Why does this T-SQL OUTPUT INTO with FOREIGN KEY hack work?

From Dev

Why does mysql automatically add an index for foreign key checks?

From Dev

Why does MYSQL error when I try to add a foreign key?

From Dev

Why is the foreign key not accepted?

From Dev

FirstOrDefault returns null on foreign key

From Dev

SQL foreign key returns NULL

From Dev

MySQL : Stored procedure returns null for last_insert_id

From Dev

MySQL LAST_INSERT_ID() - how does it works

From Dev

Explain to me exactly what last_insert_id(expr) does

From Dev

Why does a Parent table not have a matching Primary or Unique key, when a foreign key is referenced to the parent table?

From Dev

Why does scanf returns control back to the program on pressing Enter key?

From Dev

Why does scanf returns control back to the program on pressing Enter key?

From Dev

Why EF generate foreign key?

From Dev

foreign key does not update on cascade

From Dev

Compound Foreign Key INSERT

From Dev

Insert foreign key value

From Dev

Insert foreign key into table

From Dev

Insert new row - but only if it doesn't exist. If it does exist select the id of the row to be used as foreign key

From Dev

on duplicate key update id=last_insert_id(id) - did the INSERT actually happen?

From Dev

DataContext always returns null as foreign key

From Dev

a query that returns a single row for each foreign key

From Dev

EF sometimes returns null on foreign key types

From Dev

Insert the value of the foreign key ID and not the ID

From Dev

Why does ActiveRecord make an unwanted foreign key constraint on my new table?

Related Related

  1. 1

    How to store LAST_INSERT_ID() in database as foreign key in another table

  2. 2

    Why does this foreign key using inheritance not work?

  3. 3

    Why does Mysql rename my foreign key?

  4. 4

    Why does MySQL not allow this foreign key?

  5. 5

    Why does JPA (Hibernate) foreign key definition on @JoinTable not work?

  6. 6

    Why does this T-SQL OUTPUT INTO with FOREIGN KEY hack work?

  7. 7

    Why does mysql automatically add an index for foreign key checks?

  8. 8

    Why does MYSQL error when I try to add a foreign key?

  9. 9

    Why is the foreign key not accepted?

  10. 10

    FirstOrDefault returns null on foreign key

  11. 11

    SQL foreign key returns NULL

  12. 12

    MySQL : Stored procedure returns null for last_insert_id

  13. 13

    MySQL LAST_INSERT_ID() - how does it works

  14. 14

    Explain to me exactly what last_insert_id(expr) does

  15. 15

    Why does a Parent table not have a matching Primary or Unique key, when a foreign key is referenced to the parent table?

  16. 16

    Why does scanf returns control back to the program on pressing Enter key?

  17. 17

    Why does scanf returns control back to the program on pressing Enter key?

  18. 18

    Why EF generate foreign key?

  19. 19

    foreign key does not update on cascade

  20. 20

    Compound Foreign Key INSERT

  21. 21

    Insert foreign key value

  22. 22

    Insert foreign key into table

  23. 23

    Insert new row - but only if it doesn't exist. If it does exist select the id of the row to be used as foreign key

  24. 24

    on duplicate key update id=last_insert_id(id) - did the INSERT actually happen?

  25. 25

    DataContext always returns null as foreign key

  26. 26

    a query that returns a single row for each foreign key

  27. 27

    EF sometimes returns null on foreign key types

  28. 28

    Insert the value of the foreign key ID and not the ID

  29. 29

    Why does ActiveRecord make an unwanted foreign key constraint on my new table?

HotTag

Archive