A Derived Field in an SQLite3 Database

不想你离开。 提交于 2019-12-11 00:32:02

问题


Good Evening everyone,

Today I would like to ask a question relating to derived fields (also known as calculation fields) in SQLite3.

Utilizing two values stored within my database "weight" and "distance" I know it is possible to utilize them to perform calculations to return the value I want utilizing a formula that requires both.

However I am wondering if there is a way to set a field, by SQLite coding, to automatically generate its own value, so once they have entered there weight and distance and the calculation is performed the attribute field for "calories Burned" will then be populated.

Alternatively I have read on several websites that derived fields, for a 3NF database, should not be stored within the database due to the 3NF requirements, if so I will have to keep performing the select calculation when ever I wish to retrieve the value.

Thanks

JHB92


回答1:


You can achieve this goal using a VIEW:

 CREATE VIEW MyTablePlus (Weight, Distance, CaloriesBurned) AS
   SELECT Weight, Distance, ((Weight / Distance) * SomeNumber)
   FROM MyTable

Now, you can performs your SELECT statements on MyTablePlus and your INSERT and UPDATE statements on MyTable.



来源:https://stackoverflow.com/questions/23297967/a-derived-field-in-an-sqlite3-database

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