功能依赖(第一步)
Functional dependencies (first steps)
我正在练习Functional Dependencies,我正在看一本与数据库相关的书,他们提供了一个例子,它是这样的:
A company performs all kinds of sales through different terminals
(terminals for payment of debit and credit cards). These terminals
belong to different sectors, from hypermarkets to simple candy kiosks.
We are interested in knowing the business name, a contact telephone
number and legal address, including City and State which are
tabulated. The terminals have an identifier that distinguishes them
and are sold to a single store (not reuse between different shops).
The terminals also have an operating system. Each of the terminals
have a certain amount of memory (in MBytes), manufacturing country
(also tabulated) and year of importation. Terminals have a ticket
reprint service, so they need to store the unique number of the
terminal that made the sale, the invoice number associated with it,
the date and time of the sale, the amount, the type of card (debit or
credit), the card number and in how many installments the payment was
made.
很明显,他们给了我一段感情;我必须努力获得功能依赖性,你明白我的意思吗?
这是关系:
Company(BusinessName, FantasyName, BusinessNumber, Telephonenumber,
address, locality, locality_number, state, state_number, terminalid,
operatingsystem, idCountry, Country, ImportationYear, idTicket,
Billnumber, amount, CardNumber, Memory_quantity,
date_and_time_of_sale, card_type, installments_quantity)
所以,首先我替换了那个关系的值;我用字母:
Number of elements inside Company: 22
那么:公司(A, B, C, D, E, F, G , H, I, J , K, L M, N , O, P ,Q, R, S ,T, U, V )
现在,我必须找到依赖项,例如:
A -> B, C
E -> L, M, R
T -> U, V
不太确定,可以帮我定义函数依赖关系吗?。我很困惑:/
编辑:
BusinessName
FantasyName
BusinessNumber
Telephonenumber
address
locality
locality_number
state
state_number
terminalid
operatingsystem
idCountry
Country
ImportationYear
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
编辑#2:
函数依赖:
BusinessNumber -> BusinessName, FantasyName, TelephoneNumber, Address, LocalityNumber, StateNumber, IdCountry
Terminal -> operatingsystem, idCountry, ImportationYear, Memory_quantity
Sales -> amount, CardNumber, date_and_time_of_sale, card_type, installments_quantity
第 1 步 - 识别属性“组”:
根据您提供的属性的平面列表,可以看出这些属性可以按逻辑/“真实世界”/“业务域”所有者分组。例如,这些属性由现实世界的业务实体“拥有”:
BusinessName Business
FantasyName
BusinessNumber Business
Telephonenumber Business
address Business
locality Business (This attribute is redundant as locality_number serves the same purpose)
locality_number Business
state Business
state_number Business (This attribute is redundant as state_number serves the same purpose)
terminalid
operatingsystem
idCountry
Country
ImportationYear
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
这些由支付终端实体“拥有”:
BusinessName
FantasyName
BusinessNumber
Telephonenumber
address
locality
locality_number
state
state_number
terminalid Terminal
operatingsystem Terminal
idCountry Terminal
Country Terminal (Should be renamed to "ManufacturingCountry")
ImportationYear Terminal
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
当我浏览列表时,我想到了这些组:
BusinessName Business
FantasyName Business
BusinessNumber Business (I assume this is a unique identifier, a "natural key")
Telephonenumber Business
address Business
locality Business (This attribute is redundant as locality_number serves the same purpose)
locality_number Business
state Business
state_number Business (This attribute is redundant as state_number serves the same purpose)
terminalid Terminal
operatingsystem Terminal
idCountry Terminal, Business
Country Terminal, Business (This attribute is redundant as idCountry serves the same purpose)
ImportationYear Terminal
idTicket Sale
Billnumber Sale (What is the difference between `idTicket` and `BillNumber`?)
amount Sale
CardNumber Sale -- NEVER STORE CREDIT CARD NUMBERS IN A DATABASE!!!!!
Memory_quantity Terminal
date_and_time_of_sale Sale
card_type Sale
installments_quantity Sale
第 2 步 - 转换为 tables(“关系”)并识别哪些属性是键(自然键或合成键),哪些只是普通数据(依赖数据属性)
CREATE TABLE Businesses (
-- Natural key:
BusinessNumber,
-- Plain ol' data attributes (i.e. dependent attributes):
BusinessName,
FantasyName,
TelephoneNumber,
-- Business address fields (also plain ol' data):
address
locality_number,
state_number,
idCountry
)
CREATE TABLE Terminals (
-- Natural key:
terminalid,
-- Plain ol' data attributes:
operatingsystem,
idCountry,
ImportationYear,
Memory_quantity
)
CREATE TABLE Sales (
-- Natural key:
idTicket,
BillNumber,
-- Plain ol' data attributes:
amount,
CardNumber,
date_and_time_of_sale,
card_type,
installments_quantity
)
我还需要为推断的实体添加tables,例如Country
和Locality
(在现代应用程序开发中我们通常不引用states/countries 通过合成键(又名“代理键”),因为它增加了复杂性而没有增加任何业务价值)。
CREATE TABLE Localities (
-- Synthetic key:
locality_number,
-- Plain ol' data attributes:
Locality
)
CREATE TABLE Countries (
-- Synthetic key:
idCountry,
-- Plain ol' data attributes:
oCountry
)
第 3 步 - 添加外键:
Sales
table 需要修改,以便它可以引用其相关的 Business
和 Terminal
记录(这应该在第 1 步中通过确定terminalId
和 BusinessNumber
是 Business
、Terminal
和 Sale
的属性,顺便说一句——那是我的错误):
CREATE TABLE Sales (
-- Natural key:
idTicket,
BillNumber,
-- Foreign keys:
CompanyNumber,
TerminalId
-- Plain ol' data attributes:
amount,
CardNumber,
date_and_time_of_sale,
card_type,
installments_quantity
CONSTRAINT PK_Sales PRIMARY KEY ( idTicket, BillNumber )
CONSTRAINT FK_Sales_to_Businesses FOREIGN KEY ( CompanyNumber ) REFERENCES Businesses ( BusinessNumber )
CONSTRAINT FK_Sales_to_Terminals FOREIGN KEY ( TerminalId ) REFERENCES Terminals ( TerminalId )
)
(并在 Businesses
和 Terminals
table 中添加 FOREIGN KEY
约束以引用 Localities
和 Countries
)
我正在练习Functional Dependencies,我正在看一本与数据库相关的书,他们提供了一个例子,它是这样的:
A company performs all kinds of sales through different terminals (terminals for payment of debit and credit cards). These terminals belong to different sectors, from hypermarkets to simple candy kiosks. We are interested in knowing the business name, a contact telephone number and legal address, including City and State which are tabulated. The terminals have an identifier that distinguishes them and are sold to a single store (not reuse between different shops). The terminals also have an operating system. Each of the terminals have a certain amount of memory (in MBytes), manufacturing country (also tabulated) and year of importation. Terminals have a ticket reprint service, so they need to store the unique number of the terminal that made the sale, the invoice number associated with it, the date and time of the sale, the amount, the type of card (debit or credit), the card number and in how many installments the payment was made.
很明显,他们给了我一段感情;我必须努力获得功能依赖性,你明白我的意思吗?
这是关系:
Company(BusinessName, FantasyName, BusinessNumber, Telephonenumber, address, locality, locality_number, state, state_number, terminalid, operatingsystem, idCountry, Country, ImportationYear, idTicket, Billnumber, amount, CardNumber, Memory_quantity, date_and_time_of_sale, card_type, installments_quantity)
所以,首先我替换了那个关系的值;我用字母:
Number of elements inside Company: 22
那么:公司(A, B, C, D, E, F, G , H, I, J , K, L M, N , O, P ,Q, R, S ,T, U, V )
现在,我必须找到依赖项,例如:
A -> B, C
E -> L, M, R
T -> U, V
不太确定,可以帮我定义函数依赖关系吗?。我很困惑:/
编辑:
BusinessName
FantasyName
BusinessNumber
Telephonenumber
address
locality
locality_number
state
state_number
terminalid
operatingsystem
idCountry
Country
ImportationYear
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
编辑#2:
函数依赖:
BusinessNumber -> BusinessName, FantasyName, TelephoneNumber, Address, LocalityNumber, StateNumber, IdCountry
Terminal -> operatingsystem, idCountry, ImportationYear, Memory_quantity
Sales -> amount, CardNumber, date_and_time_of_sale, card_type, installments_quantity
第 1 步 - 识别属性“组”:
根据您提供的属性的平面列表,可以看出这些属性可以按逻辑/“真实世界”/“业务域”所有者分组。例如,这些属性由现实世界的业务实体“拥有”:
BusinessName Business
FantasyName
BusinessNumber Business
Telephonenumber Business
address Business
locality Business (This attribute is redundant as locality_number serves the same purpose)
locality_number Business
state Business
state_number Business (This attribute is redundant as state_number serves the same purpose)
terminalid
operatingsystem
idCountry
Country
ImportationYear
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
这些由支付终端实体“拥有”:
BusinessName
FantasyName
BusinessNumber
Telephonenumber
address
locality
locality_number
state
state_number
terminalid Terminal
operatingsystem Terminal
idCountry Terminal
Country Terminal (Should be renamed to "ManufacturingCountry")
ImportationYear Terminal
idTicket
Billnumber
amount
CardNumber
Memory_quantity
date_and_time_of_sale
card_type
installments_quantity
当我浏览列表时,我想到了这些组:
BusinessName Business
FantasyName Business
BusinessNumber Business (I assume this is a unique identifier, a "natural key")
Telephonenumber Business
address Business
locality Business (This attribute is redundant as locality_number serves the same purpose)
locality_number Business
state Business
state_number Business (This attribute is redundant as state_number serves the same purpose)
terminalid Terminal
operatingsystem Terminal
idCountry Terminal, Business
Country Terminal, Business (This attribute is redundant as idCountry serves the same purpose)
ImportationYear Terminal
idTicket Sale
Billnumber Sale (What is the difference between `idTicket` and `BillNumber`?)
amount Sale
CardNumber Sale -- NEVER STORE CREDIT CARD NUMBERS IN A DATABASE!!!!!
Memory_quantity Terminal
date_and_time_of_sale Sale
card_type Sale
installments_quantity Sale
第 2 步 - 转换为 tables(“关系”)并识别哪些属性是键(自然键或合成键),哪些只是普通数据(依赖数据属性)
CREATE TABLE Businesses (
-- Natural key:
BusinessNumber,
-- Plain ol' data attributes (i.e. dependent attributes):
BusinessName,
FantasyName,
TelephoneNumber,
-- Business address fields (also plain ol' data):
address
locality_number,
state_number,
idCountry
)
CREATE TABLE Terminals (
-- Natural key:
terminalid,
-- Plain ol' data attributes:
operatingsystem,
idCountry,
ImportationYear,
Memory_quantity
)
CREATE TABLE Sales (
-- Natural key:
idTicket,
BillNumber,
-- Plain ol' data attributes:
amount,
CardNumber,
date_and_time_of_sale,
card_type,
installments_quantity
)
我还需要为推断的实体添加tables,例如Country
和Locality
(在现代应用程序开发中我们通常不引用states/countries 通过合成键(又名“代理键”),因为它增加了复杂性而没有增加任何业务价值)。
CREATE TABLE Localities (
-- Synthetic key:
locality_number,
-- Plain ol' data attributes:
Locality
)
CREATE TABLE Countries (
-- Synthetic key:
idCountry,
-- Plain ol' data attributes:
oCountry
)
第 3 步 - 添加外键:
Sales
table 需要修改,以便它可以引用其相关的 Business
和 Terminal
记录(这应该在第 1 步中通过确定terminalId
和 BusinessNumber
是 Business
、Terminal
和 Sale
的属性,顺便说一句——那是我的错误):
CREATE TABLE Sales (
-- Natural key:
idTicket,
BillNumber,
-- Foreign keys:
CompanyNumber,
TerminalId
-- Plain ol' data attributes:
amount,
CardNumber,
date_and_time_of_sale,
card_type,
installments_quantity
CONSTRAINT PK_Sales PRIMARY KEY ( idTicket, BillNumber )
CONSTRAINT FK_Sales_to_Businesses FOREIGN KEY ( CompanyNumber ) REFERENCES Businesses ( BusinessNumber )
CONSTRAINT FK_Sales_to_Terminals FOREIGN KEY ( TerminalId ) REFERENCES Terminals ( TerminalId )
)
(并在 Businesses
和 Terminals
table 中添加 FOREIGN KEY
约束以引用 Localities
和 Countries
)