What kind of data model models function parameters and results?

送分小仙女□ 提交于 2019-12-11 17:56:28

问题


The following table...:

CREATE TABLE v (
    height int,
    width int,
    depth int,
    volume int,
    PRIMARY KEY (height, width, depth);
)

... could be used to store the inputs and outputs of a function of 3 variables named volume: volume(height, width, depth) = height * width * depth.

What kind of data model am I using here? Is it Entity-Attribute-Value?


回答1:


You appear to be mathematically oriented. When Codd(Date) introduced the relational model, he(they) reshaped the existing mathematical language to the new field.

In your case: value could be a function of the three parameters: val = f(h,w,d). But for "relations" (database tables) things are slightly different: the function is only defined if {h,w,d} actually EXISTS in the table. In mathematics, the function is defined over the entire R3 domain. In relational algebra, the key of a table ({h,w,d}) is defined over a more restricted domain (or the product of a set of domains). Most of the DBMS/SQL world is involved with these restrictions. (constraints, domain constraints, whatever) The UNIQUE constraint is perhaps the most fundamental constraint: it guarantees that there is at most one tuple with a particular {h,w,d}. As a consequence there is only one function value. DBMS people call the non-key fields of a relation "functionally dependant" on the FK ({h,w,d}}: given the set of keys, there can be at most one row that corresponds to it (and at most one "function value")

EAV is just a class of data models to "simulate" that an object has a variable number of attributes, without changing the definitions of the tables involved. But, on the table level, it just means adding an extra table (actually two) with the attibutes and the values. Data modelling is only topology in disguise.




回答2:


No, it's not an EAV model. You're just modeling dimensions of a rectangular solid, and choosing to store the volume as well. In the real world, you'd probably include units of measurement and some check constraints.

CREATE TABLE v (
    height_cm int not null check (height_cm > 0),
    width_cm int not null check (width_cm > 0),
    depth_cm int not null check (depth_cm > 0),
    volume_cm3 int not null check (volume_cm3 = height_cm * width_cm * depth_cm),
    PRIMARY KEY (height_cm, width_cm, depth_cm)
);

(I once worked next to a guy who had to handle issues with a relational db/XML/XSLT monstrosity that was designed by a badly formed committee. Among other things, it calculated shipping costs based in part on how many containers would fit in a railroad car. Users quickly discovered they could save shipping costs by simply giving one dimension of each package a negative number.)

If you drop the column "volume_cm3", the table is clearly in 5NF. (No non-key attributes at all.) But if you include the column "volume_cm3" and its constraints, what normal form is this in? Still in 5NF?




回答3:


The V table you show is just a table. It has a 3-part key, and one data column, dependent entirely on the key. Volume is derived by a formula using the three measures (and maybe some others) which are in the key. The functional dependency of Volume is the three part key. It is not an attribute value pair. It also does not violate any normal form. It's just a derived table with a three part key and one data column. It is possible to overthink this question.



来源:https://stackoverflow.com/questions/11147703/what-kind-of-data-model-models-function-parameters-and-results

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!