A very naive question. I need to store currency in the database. Both the value and 开发者_如何学运维the code. To solve this, do people generally make 2 columns, one storing the value and other the code? Or is there an inbuilt type I can use?
-thanks
You will need to use two columns. I would store the monetary amount in one column and the alpha currency code in another column. In some cases, you will have multiple amounts on a single row. e.g. shipping amount and tax amount may both be on the invoice record. You will need to decide if these will share the same currency or if you need two columns.
You should use the ISO standard currency codes.
Some databases have a Money type, which can be used to store the value. However, if you want to store a code (do you mean dollars, euro's etc?) then you would need to use a second column.
Update: PostgreSQL does have a money type, although it looks like it would only support one type of currency so it still does not really meet your needs.
The relational databases I know (Oracle, Postgres, MySQL) do not have built-in currency support, and I don't expect any others do. You will have to do it yourself with 2 columns.
There may be tools specific to your db. In general I would think storing the currency as a real value and a currency code indicator. There may be a standardized currency code list somewhere that you can use or you can just make one up based on the currencies you know you will deal with.
Really if you want to indicate a type of any sort you will need a column to store the type.
People might be able to provide more DB specific help if you indicate the DB you are using.
精彩评论