Processing of units of measurement in the database

Asked

Viewed 188 times

-3

In a project I will have units of measurement gram/unit/liter. Not that a product I can sell it by gram and liter, if entered 10kg of meat I will give output 10kg of meat how should I treat it? I must put in the bank the only code?

  • 1

    Rangel, my forte is not BD, but it will depend on the level of granularity you want. In the real case, generally, we can buy 100g grams of meat ne? then instead of putting kg in kg, I think a granularity of grass would be good.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

Usually you normalize the data. The data obtained (input) or presented (output) can be in the unit of magnitude you want, you then convert to a specific quantity that is in the database, the same goes for the type of measure, although it is almost always strange to buy by one type of unit and sell by another (happens, but is rare, almost always is a mistake, in artificial examples has great chance of being an evaluation error), but it seems that you do not have this problem.

People have difficulty understanding what is data input, data output, and data itself. Worry about the die and how to convert inputs and outputs to the die in a standard way. Input and output is for a human to handle. Mathematics exists by itself regardless of what the human understands. So do objects. The computer deals in a different way, let it do its job in the simplest and most normal way possible.

I will use weight as an example. Some products may require the smallest unit to be milligram or even something smaller than this, but they are more rare. Only avoid having the die with fractional part, where possible keep the die whole. So if you can have 27.6 grams, you better keep the data in milligrams. If only one product in the database can have fractionated grams then the correct one would be to use milligrams (of course you can use decigrams or centigrams, but why complicate it?). But probably grams is good for your case.

So if the data is input in kilograms and in the database you store as grams you must multiply that number by 1000 before storing the data without you. When you take the data from the database and use it in some kilogram context you should divide by 1000 to have it the way you want it in that case.

Each product can have a unit, it doesn’t matter much to the database, as long as it maintains a standardized form. Of course the application will have to make use according to the type of unit used. Then you’ll have a column telling you what kind of unit is used in that product. The application will take this and decide how to convert, display and manipulate this data according to the unit used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.