Jump to content

Assigning Unique Range Values With Dates


Recommended Posts

I am implementing a form using CActiveForm (Yii) and my database is in MySQL.

My requirement is to implement a solution which would enroll and assign my users (candidates, filling the form) with date_of_test and and unique roll_number based on test_locations.

I have multiple test_locations and they have specific seating capacity for a day e.g Arizona = 1000, Florida = 200. Now based on the seating capacity i need to assign roll_number (unique) and date_of_test to a candidate, e.g in a candidate table data would something be like:

id | roll_number | date_of_test | state |   <--- ID is auto incremented by MySQL
1  | 999         | 11/20/2013  | Arizona
2  | 50          | 11/20/2013  | Florida
3  | 1000        | 11/20/2013  | Arizona
4  | 1001        | 11/21/2013  | Arizona   <--- Next Date given to candidate

Test locations are assigned to candidates by application based on their present location i.e if someone is from Arizona state his test location would automatically be selected as Arizona (implemented via simple dependent dropdown).

I am finding really hard to implement this logic of assigning roll_number and date_of_test to candidates, as i don't want anyone to have same roll number and anyone given a date_of_test more than the seating capacity of a location. Maybe Yii/PHP/MySQL has some function which i am not aware would assist me in implementing it easily. Thus, requiring assistance.


Link to comment
Share on other sites

Instead of the primary key being the auto_incrementing ID col, define the table as



roll_number INT NOT NULL auto_increment,

date_of_test DATE,

PRIMARY KEY (state, roll_number)


If the second part of the PK is auto_increment then it will number from 1 to N for each state.


The date for each will be

basedate + floor((roll_number-1)/state_capacity) days
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.