简体   繁体   English

数据库表设计问题?

[英]Database Table design issue?

I am creating a table with name as a column such that there are multiple rows with same name and corresponding to every name there are other values (columns). 我正在创建一个以name为列的表,以便有多个具有相同名称的行,并且与每个名称相对应的还有其他值(列)。

I have to design the table such that there are two set of values default and latest . 我必须对表进行设计,以使有两组值defaultLatest So user may enquire to get either default/latest set and may edit table to make particular set of values as default replacing the previous defult set. 因此,用户可以查询获取默认/最新集,也可以编辑表以将特定的值集设置为默认值,以替换先前的默认集。

I am thinking of implementing this by keeping a flag column where: 0 means this row is default set and 1 means the latest set and 2 means just a general row. 我正在考虑通过保留一个标志列来实现此目的,其中:0表示该行是默认设置,1表示最新的设置,2表示仅一个通用行。

TABLE: 表:

NAME            ADDRESS         CITY         FLAG

Hansen          Timoteivn 10    Sandnes      1   (LATEST)   
Hansen          street 13       texas        0   (DEFAULT)
Svendson        Borgvn 23       Sandnes      1   (LATEST)
Svendson        street 14       colaba       2   (GENERAL)
Svendson        street 15       Houston      0   (DEFAULT)
Pettersen       Storgt 20       Stavanger    0   (DEFAULT)
Pettersen       Storgt 21       texas        1   (LATEST)

I am thinking of implementing it like this: 我正在考虑像这样实现它:

alter table TABLE_NAME add flag number CONSTRAINT <constraintname>
   CHECK (flag IN (0, 1, 2));

Is there a better approach to do this apart from keeping a flag ?? 除了保留标志之外,还有更好的方法吗? Am i doing it wrong?? 我做错了吗?

Is there a better approach to do this apart from keeping a flag ?? 除了保留标志之外,还有更好的方法吗?

Typically a default setting will be show by a flag column but not information about the latest. 通常,默认设置将通过标志列显示,但不会显示有关最新设置的信息。

To store latest information a table will include 2 additional timestamp columns: Created and Modified (and you might have two additional columns showing who eg CreatedBy, ModifiedBy). 为了存储最新信息,表将包括2个附加的时间戳列: CreatedModified (并且您可能还有两个附加的列,用于显示谁,例如CreatedBy,ModifiedBy)。

If you have these auditing columns then figuring out the latest is just a matter of looking at these timestamps depending on if latest means most recent created or most recent modified. 如果您有这些审核列,那么根据最新时间是最新创建的还是最新修改的时间,找出最新时间戳只是一个问题。

Is there a better approach to do this apart from keeping a flag ?? 除了保留标志之外,还有更好的方法吗? Am i doing it wrong?? 我做错了吗?

If you can't distinguish "default" data from "latest" data any other way, you have to either add a column or add a table. 如果您无法通过其他任何方式将“默认”数据与“最新”数据区分开,则必须添加一列或添加一个表。

But I don't understand why you'd choose to use an integer the way you're using one. 但是我不明白为什么您会选择使用整数的方式来使用整数。 You're representing two distinct values with three distinct integers. 您用三个不同的整数表示两个不同的值。

With your proposal there is nothing from stopping you from storing multiple defaults or latest. 有了您的建议,就不会阻止您存储多个默认值或最新值。 You also do not have a proper primary key. 您也没有正确的主键。

A better (more relational) solution would be to a) add some kind of version to your table and b) create another table with the PK (name + which version is default copy) with those two columns as a primary & foreign key. 更好的(更相关的)解决方案是:a)向表中添加某种版本,并b)使用这两个列作为主键和外键,使用PK(名称+该版本为默认副本)创建另一个表。 "Latest" would simply be the row with the highest version, not a flag. “最新”只是具有最高版本的行,而不是标志。 This would give you better data integrity. 这将为您提供更好的数据完整性。 However it will allow the situation where a given key may not have a default. 但是,这将允许给定键可能没有默认值的情况。

To enforce that you have to redesign your table to have a parent record (name), some child records(other stuff) and a FK on your parent that points to the default. 为了强制执行,您必须重新设计表以使其具有父记录(名称),一些子记录(其他内容)以及指向默认值的父项上的FK。 Depending on whether your DB supports deferred constraints (I don't know MySQL) this may not be possible due to the circular nature. 取决于您的数据库是否支持延迟约束(我不知道MySQL),由于循环性质,这可能无法实现。

Your schema is missing a table for storing unique names. 您的架构缺少用于存储唯一名称的表。

If you create one, then you can have an address table that references it, and given that there can be only one "latest" and one "default" row per "name" you can add columns to the name table to link to the latest and default rows of address. 如果创建一个,则可以有一个引用它的地址表,并且鉴于每个“名称”只能有一个“最新”行和一个“默认”行,您可以在名称表中添加列以链接到最新的和默认的地址行。

Of course if latest means "last added to the table" then adding a timestamp for the creation of the addresses means that you can infer the latest row from that. 当然,如果最新表示“最后添加到表中”,则添加时间戳以创建地址意味着您可以从中推断出最新行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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