Roger Federer Roger Federer - 5 months ago 29
SQL Question

I have a table which has a primary key that is made of three columns. How do I index this table?

Betting houses function like this: every week there are two rounds, and in each round when someone wants to make a ticket he gets a number for that round (first ticket of that round(1), second (2)...). I have a ticket table of all entered tickets of a betting-house company.

This table has 30 columns of all the details of each ticket. The primary key is (location, round, number) I explained this number as being the number of the ticket within that round, and the location is the location of the bet-house (a betting company has many bet-houses in different locations). I want to select each ticket one by one, but I do not know how to have some sort of organized way. If the a single column with auto-increment integer type that would be easy. I just want to some how index this triplet primary key to go through a loop to select each entry incrementally. I am using C# to do this?


As you did not provide the real purpose of your task, I can think of 2 situations.

Firstly, you want to read the table in some smaller batches for processing/evaluation. In this case you have to make sure the table is immutable and run SELECT ... ORDER BY number, round, bethouse OFFSET 0 LIMIT 1000, OFFSET 1000 LIMIT 1000, OFFSET 2000 LIMIT 1000 and so on until you come to the last row.

Secondly, you want to process the new incoming rows. In that case there is no way you can solve that with only the multicolumn PK. You have to add a column with an autoincrement value or a date column (i.e. when the row was created). Then you order the results by this column and watch for new rows with WHERE mycolumn > {LastReadValue}.

Hope that helps.