mysql 增加行显示,如何在现有MYSQL表中的两行之间添加一行?

I created a MYSQL table having 50 rows.

Now I need to add a row between 5 and 6.

But the primary key should change after that 5, new row-6 ,after-wards 7,8

I tried insert row - shows duplicate primary key

How can I add a row between two rows in existing table?

Thanks in advance

Saranya1388

解决方案If I read you right, you have a mySql table with rows thus:

1 me

2 you

3 them

4 him

5 her

6 it

7 whom

8 whoYou want to change this to:

1 me

2 you

3 them

4 him

5 her

6 my new row

7 it

8 whom

9 whoIf this is correct then there is no automatic way to do it. It may not even be possible, depending on your DB design. Did you use auto number? If so, then no, you can''t do it. Is this field used in any other tables? If so, then no, I don''t recommend it.

If not, then you have to:

1) Retrieve all records in the table.

2) Find the last number that needs to be updated.

3) Working backwards from this, increment each number by one, using SQL update commands, until you reach your insert point.

4) Insert your new record.

However, I would really not recommend this. Instead, if you need an order by field, then add one. And update it as described above.

In a real life situation, with multi-user database, this kind of thing is seriously unhealthy for data integrity!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值