3

How can I create a constants table in mysql that will match a type (using an int to differentiate among different types) with the full name of the type. The constants table should be read only and is not expected to ever need changing or updating only reading.

CREATE TABLE Accounts (
    AccountType INT,
    ...
);

CREATE TABLE AccountTypes (
    TypeId INT,
    TypeName VARCHAR(255),
);

In Java there is ImmutableMap<Integer><String> (in Guava), or Enums with integer instance variables, and in C++ there is also enum or static const string[...] ... = { ... } where the type integer is the index.

Is this possible, or is there a better alternative?

4

2 回答 2

4
于 2013-05-05T23:19:38.913 回答
0

如果您想拥有多个帐户类型,每个帐户类型都有一些详细信息(例如名称),并且每个帐户都应该与一个帐户类型相关联,您可以创建表格,例如

CREATE TABLE Accounts (
AccountId INT(10) PRIMARY KEY,
AccountType INT(10) NOT NULL, FOREIGN KEY (AccountType) REFERENCES AccountTypes (TypeId),
... 
);

CREATE TABLE AccountTypes (
TypeId INT(10) PRIMARY KEY,
TypeName VARCHAR(255),
);

这将确保添加到 Accounts 的所有行都具有与已注册的 Account Types 之一相对应的 AccountType。

看看http://en.wikipedia.org/wiki/Foreign_key

于 2013-05-04T18:34:28.043 回答