本文介绍了SQL 中的链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 MySQL 数据库中存储链表的最佳方法是什么,以便插入简单(即您不必每次都重新索引一堆东西)并且可以轻松地将列表拉出订购?

What's the best way to store a linked list in a MySQL database so that inserts are simple (i.e. you don't have to re-index a bunch of stuff every time) and such that the list can easily be pulled out in order?

推荐答案

在您的表中存储一个名为位置"的整数列.为列表中的第一项记录 0,为第二项记录 1,依此类推.索引数据库中的该列,当您想要提取值时,按该列排序.

Store an integer column in your table called 'position'. Record a 0 for the first item in your list, a 1 for the second item, etc. Index that column in your database, and when you want to pull your values out, sort by that column.

 alter table linked_list add column position integer not null default 0;
 alter table linked_list add index position_index (position);
 select * from linked_list order by position;

要在索引3处插入一个值,修改第3行及以上的位置,然后插入:

To insert a value at index 3, modify the positions of rows 3 and above, and then insert:

 update linked_list set position = position + 1 where position >= 3;
 insert into linked_list (my_value, position) values ("new value", 3); 

这篇关于SQL 中的链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 19:02