我正在尝试维护一个地址历史表:
CREATE TABLE address_history (
person_id int,
sequence int,
timestamp datetime default current_timestamp,
address text,
original_address text,
previous_address text,
PRIMARY KEY(person_id, sequence),
FOREIGN KEY(person_id) REFERENCES people.id
);
Run Code Online (Sandbox Code Playgroud)
我想知道是否有一种简单的方法可以自动编号/约束sequence,address_history从而自动从1开始计数person_id.
换句话说,第一行person_id = 1会得到sequence = 1; person_id = 1会得到第二排sequence = 2.第一行person_id = 2,将sequence = 1再次获得.等等.
还有更好/内置的方式来维护这样的历史吗?