Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a row in a table between two existing rows in Sql [closed]

Tags:

mysql

I have a table with 9 records in it but i want to insert a row in between of 5th and 6th record.

like image 771
Renuka Ballal Avatar asked Feb 07 '12 13:02

Renuka Ballal


People also ask

How do you insert a row in a table between two existing rows in SQL?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.

How do I add a row between rows in MySQL?

INSERT INTO table_name(column_1,column_2,column_3) VALUES (value_1,value_2,value_3); In the INSERT INTO query, you should specify the following information: table_name : A MySQL table to which you want to add a new row. (column_1,column_2,column_3) : A list of columns the new row will contain.

Can a new row be inserted in the middle of an existing table?

You can add rows to an existing table in two ways: Use Edit > Add Row to enter a new row one-at-a-time. Use File > Import more rows to bring in rows from a file.

How do you insert a record between in SQL?

INSERT INTO Syntax 1. Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)


2 Answers

if you insist on

UPDATE mytable SET id = id + 1 where id > 5 ORDER BY id ASC

insert into mytable (id,..) values (6,...) 
like image 108
Haim Evgi Avatar answered Oct 06 '22 09:10

Haim Evgi


In general, you don't insert a row at a specific location in a table.

If the row "order" is significant and has some special semantic, have the data reflect that with a proper column in the table structure.

Then use a SELECT ... ORDER BY ... to get rows sorted.

like image 30
Marc Alff Avatar answered Oct 06 '22 07:10

Marc Alff