SQL中的链接列表 [英] Linked List in SQL

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

问题描述

在mysql数据库中存储链接列表的最佳方式是什么,以便插入是简单的(即,您不必每次重新索引一堆东西),并且可以轻松地将列表按顺序拉出。

Whats the best way to store a linked list in a mysql database so that inserts are simple (i.e. you dont have to reindex a bunch of stuff every time) and such that the list can easily be pulled out in order.

推荐答案

在您的表中存储一个称为position的整数列。为您的列表中的第一个项目记录一个0,第二个项目的第一个项目记录一个等。数据库中的该列索引,当您要拉出值时,按该列排序。

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中的链接列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆