每次将物体放入容器并从容器中取出时,我都需要知道。我怎样才能为每个字段设置一个字段'time_in'和'time_out'(单独的列),并在每个字段中存储可变数量的DATETIME对象?是否可以在MySQL中存储一组DATETIME类型?
答案 0 :(得分:2)
你在这里有一个经典的1:n关系。这可以这样建模:
create table item
(
item_id integer not null primary key
);
create table movement
(
item_id integer not null,
time_in timestamp not null,
time_out timestamp not null,
primary key (item_id, time_in),
foreign key (item_id) references item (item_id)
);
insert into item
values
(1),
(2),
(3);
insert into movement (item_id, time_in, time_out)
values
(1, timestamp '2014-02-12 00:00:00', timestamp '2014-02-12 14:17:10'),
(1, timestamp '2014-02-12 14:03:00', timestamp '2014-02-12 16:07:40'),
(1, timestamp '2014-02-12 18:00:42', timestamp '2014-02-12 22:10:05'),
(2, timestamp '2014-02-12 01:00:00', timestamp '2014-02-12 06:06:19'),
(2, timestamp '2014-02-12 15:16:17', timestamp '2014-02-12 16:17:18'),
(3, timestamp '2014-02-12 01:01:01', timestamp '2014-02-12 03:03:03'),
答案 1 :(得分:2)
我可能会设计一个表来存储time_in
和time_out
,如下所示:
object_time
object_time_id - Int - Auto Increment - Primary Key
object_id - Int
object_time - Datetime
object_time_in_or_out - Char(1) - Default '' - 'I' for In , 'O' for Out
实例化对象时(在构造函数中)为对象生成unique id
。从那时起,每time_in
和time_out
都会在上表中添加一个新行。
如果您有time_in
和time_out
两个单独的列,要更新time_out
,您必须跟踪time_in
对应的行。只需一列就可以消除这种情况。