Question

I'm creating a schema in MySQL that (for the purpose of this question) has two tables:

Projects (pid:int) 
Tasks (pid:int,tid:int)

Each Project can have many Tasks, and each Task belongs to one Project. I have pid in Projects set up to be the primary key and to auto-increment as projects are added. Ideally, I'd like to construct Tasks so that the primary key for each task is both the pid and the tid. The tid would increment for tasks belonging to the same project but would start at 1 again for each new project.

(pid,tid) = (1,1)(1,2)(1,3)(2,1)(2,2)(3,1)(3,2)(3,3)(3,4)

I know I can achieve this effect by making a query to find the highest tid for a project and manually incrementing it for the next task; however, I'd like to do it without the query, if possible. Is there a simpler, more efficient way to implement this functionality (eg something with AUTO_INCREMENT)?

I'm programming in Python 3 with the Flask microframework and MySQL 5.7 on Windows 7.

Was it helpful?

Solution

On MyIsam its possible. I just tried from the documentation myself. Just fit it to your needs.

http://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top