sql - Storing all loan (insert loan date, update return date) records in one table or 2 tables (with history) is better? -
a simple database design question has been bugging me while, thought i'll ask here.
suppose have database table, "loan" following fields,
studentidentification, loandate, returndate this table used track every student has loaned (not in database). since every student can loan , return , loan again (but not loan multiple times without returning, loan must followed return), composite primary key
used: studentidentifcation , loandate is better store data way or instead have 2 tables,
table 1: loan ( studentidentification, loandate) table 2: loanhistory ( studentidentification, loandate, returndate) in case, loan table's primary key
studentidentification and loanhistory table's primary key
studentidentification, loandate everytime student returns, record in "loan" moved "loanhistory" table returndate updated (done in transaction).
which better?
i create single table, , have filtered index (sql server 2008+) or indexed view (sql server 2005-) enforce there single row each student null return date:
create table loans ( studentid int not null, loandate datetime not null, returndate datetime null, constraint pk_loans primary key (studentid,loandate), constraint ck_loans_notimetravel check (loandate < returndate) ) filtered index:
create unique index ix_loans_singleopen on loans (studentid) returndate null indexed view:
create view dbo.loans_singleopen_dri schemabinding select studentid dbo.loans returndate null go create unique clustered index ix_loans_singleopen on loans_singleopen_dri (studentid) (assuming dbo appropriate schema - needed schemabinding, in turn needed create index)
Comments
Post a Comment