简体   繁体   中英

Auto increment for group of fields in MySQL

Table Library has four columns (counter, studentname,bookname, date) of type (int,string,string,datetime) . primary key is (counter, studentname,bookname) and counter column has auto_increment field set on it.

Above model works good for 1 student where the field counter gets increased for ever book that was borrowed by student. When I add a second student, counter starts from where it was left with the previous student. Instead I want counter to start from 1 for every new student that was not already in the system (further counter column has to auto-increase based on the counter column of that particular student)

Question: How can I make a 'counter' column auto-increment based on a 'student' column (it seems auto increment works on the table level, but I want auto-increment to work on the column level). How can I enforce this counter column to auto increment based on student

First, let me say that I agree with @Ed Cottrell and @tadman about this not beeing a good idea.

However, it can be done if you use MyISAM as your storage engine. See the MySQL documentation .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM