开发者

How to refer to a specific table column from an other DB record?

开发者 https://www.devze.com 2023-02-01 08:34 出处:网络
I want to define extra properties for the columns in the tables of my MYSQL database. For example I want to define the measurem开发者_运维问答ent units of the columns of my database.

I want to define extra properties for the columns in the tables of my MYSQL database.

For example I want to define the measurem开发者_运维问答ent units of the columns of my database.

To do that I currently plan to create this table :

COLUMN_UNIT
id
{table_name, column_name} unique constraint
unit_id

(I am not using a composite PK because my framework required an auto incremented ID field)

The other tables that I could have are :

UNIT
id
unit_name

WEATHER_DATA
temperature
wind_speed
...

SENSOR_DATA
intensity
frequency
...

Is using the column name and the table name the best way to refer to other column?

Thanks!


UPDATE :

In my case all the records in a column will have the same property value. It is a column wise property. So for the temperature, all the values will be in degree C for example.


you need to use multiple foreign keys to the UNIT table.. for example, your WEATHER_DATA table could be

WEATHER_DATA
temperature
temperature_unit_id (FK to UNIT)
wind_speed
wind_unit_id (FK to UNIT)

i don't know if your framework allows multiple foreign keys on the same table... If it doesn't, i guess that you could create another table containing the name of the table, the nane of the column and the unit_id.. but that would be =S ugly

Hope it helps

EDIT:

you could use a meta table that are just tables that store extra information of your table.. mysql uses his own metatables to store the number of rows, the column types, etc. Some frameworks use their own meta tables (like Django).. But you could create your own meta tables,they are just like any other table, in your case it could be something like

WEATHER_META
temperature_unit_id FK
wind_unit_id FK

and the WEATHER table would be

WEATHER_DATA
id
temperature
wind_speed
...

Or if you want to, you could create a single Meta table for all your tables. Something like

META_TABLES
table_name
column_name
unit_id
0

精彩评论

暂无评论...
验证码 换一张
取 消