Eric
Eric

Reputation: 1291

How to insert data into mysql with order

If Insert SQL without "order by", the records in the sql are arranged by insert time.

I have a table: authorization( pin, code)

However, I run INSERT INTO authorization(pin, code) VALUES ('12', 'abcd'). After insert few records, I query all the data and find that these records have no order.

How can I insert data with the insert time order.

Thank you

Upvotes: 0

Views: 322

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269623

Tables in SQL have no inherent ordering. That is the definition of the language. What you want to do is to add an autoincrement id column into your data:

create table authorization (
    AuthorizationID int auto_increment primary key,
    pin . . .
    code . . .
    . . .
);

Then when you insert as:

insert into authorization(pin, code)
    . . .;

A new id will be generated automatically. This id will remember the insert order. By the way, you might also want a timestamp column, if you want to remember the exact time of the insert as well.

Upvotes: 1

Related Questions