![]() ![]() Then, when you retrieve the records, specify an order-by on the ordinal column to get them back in order. The simplest method would be to assign an ordinal value to each record in the table (e.g. I'm assuming your goal is to preserve an order in retrieval, rather than requiring storage as an actual linked list. There are a few approaches I can think of right off, each with differing levels of complexity and flexibility. The example just illustrates possible usage. There may be better ways to do this and there are, depending on your specific needs. These files could be tax forms, health insurance papers, field trip permissions slips etc Let's say they have a table called file cabinetĮach column contains a primary key for the cabinet and a foreign key for the files. Let's say you have a Bureaucracy that keeps forms. So if you've ever wanted to make an array of foreign keys? Well this is one way to do it! You can make a primary table that points to the first column in the linked-list table, and then instead of the "information" field, you can use a foreign key to the desired information table. Linked List is especially powerful in one-to-many relationships. MSSQL won't let you do it in one step, because the table doesn't exist yet for the foreign key to reference. *I put them into two seperate files, because it has to be done in two steps. Register_foreign_key.sql alter table dbo.linkedlist00Īdd foreign key (key2) references dbo.linkedlist00(key1) Key1 int primary key not null identity(1,1), You may want to do some research for the version of SQL you are using!):Ĭreatetable.sql create table linkedlist00 ( Here's some actual code I've prepared (all actual code worked on MSSQL. This makes the best method to enter many entries at a time, then go back and set the foreign keys accordingly (or build a GUI that just does that for you) After you enter the second column, you can go back and set key2 of the first column to the primary key of the second column. When you first enter a column in for the table, you'll need to make sure key2 is set to null or you'll get an error. Key2 from col2 is set to null because it doesn't point to anything key2 is a foreign key leading to the key1 of col2 So your columns will link something link something like this Key2 is a foreign key linking to itself in the next column. The best way I've found so far is to create a single table for the linked list using the following format (this is pseudo code): This is something I've been trying to figure out for a while myself. Probably not what you're asking for here though. If you have many lists, a quick way to serialise and deserialise your list to text/binary, and you only ever want to store and retrieve the entire list, then store the entire list as a single value in a single column. This can be relatively easily implemented in a stored proc. Then you need to iterative logic in the retrieval of the list in order to get the items in order. ![]() If you want to perform lots of manipulations on the list and not many retrievals you may prefer to have an ID column pointing to the next item in the list, instead of using a position column. However, this requires a little more effort to work out when to increment the following elements, so you lose simplicity but gain performance if you will have many inserts.ĭepending on your requirements other options might appeal, such as: Since operations on the list can require multiple commands (eg an insert will require an INSERT and an UPDATE), ensure you always perform the commands within a transaction.Ī variation of this simple option is to have position incrementing by some factor for each item, say 100, so that when you perform an INSERT you don't always need to renumber the position of the following elements. Insert into linked_list (list_id, position, data) Update linked_list set position = position + 1 where position >= 3 and list_id = 1 So to insert an item into list 1 at index 3: begin transaction To manipulate the list just update the position and then insert/delete records as needed. create table linked_listĪlter table linked_list add primary key ( list_id, position ) Then you can use ORDER BY on the position column to retrieve in the desired order. The simplest option would be creating a table with a row per list item, a column for the item position, and columns for other data in the item. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |