Summary: in this tutorial, we will introduce you to the MySQL DECIMAL data type and how to use it effectively in your database table. Show Introduction to MySQL DECIMAL data typeThe MySQL DECIMAL data type is used to store exact numeric values in the database. We often use the DECIMAL data type for columns that preserve exact precision e.g., money data in accounting systems. To define a column whose data type is DECIMAL you use the following syntax:
In the syntax above:
The 2 means that the column can store up to P digits with D decimals. The actual range of the decimal column depends on the precision and scale. Besides the 3 keyword, you can also use 4, 5, or 6 because they are synonyms for 3. Like the INT data type, the 3 type also has 9 and 0 attributes. If we use the 9 attribute, the column with 2 will not accept negative values. In case we use 0, MySQL will pad the display value by 0 up to display width specified by the column definition. In addition, if we use 4 for the 3 column, MySQL will add the 9 attribute to the column automatically. The following example defines amount column with 3 data type.
In this example, the amount column can store 6 digits with 2 decimal places; therefore, the range of the amount column is from 9999.99 to -9999.99. MySQL allows us to use the following syntax:
This is equivalent to:
In this case, the column contains no fractional part or decimal point. In addition, we can even use the following syntax.
The default value of P is 10 in this case. MySQL DECIMAL storageMySQL assigns the storage for integer and fractional parts separately. MySQL uses binary format to store the 3 values. It packs 9 digits into 4 bytes. For each part, it takes 4 bytes to store each multiple of 9 digits. The storage required for leftover digits is illustrated in the following table: Leftover DigitsBytes001–213–425–637–94For example, 9 has 9 digits for the fractional part and 19-9 = 10 digits for integer part. The fractional part requires 4 bytes. The integer part requires 4 bytes for the first 9 digits, for 1 leftover digit, it requires 1 more byte. In total, the 9 column requires 9 bytes. MySQL DECIMAL data type and monetary dataWe often use the 3 data type for monetary data such as prices, salary, account balances, etc. If you design a database that handle the monetary data, the following syntax should be OK.
However, if you want to comply with Generally Accepted Accounting Principles (GAAP) rules, the monetary column must have at least 4 decimal places to make sure that the rounding value does not exceed $0.01. In this case, you should define the column with 4 decimal places as follows:
MySQL DECIMAL data type exampleFirst, create a new table named 2with three columns: id, description, and cost.
Second, insert data into the 2 table.
Third, query data from the 2 table.
Fourth, change the cost column to include the 0 attribute. 0 Fifth, query the materials table again.
As you see, we have many zeros padded in the output values. In this tutorial, we have shown gave you detailed information on MySQL DECIMAL data type and shown you how to apply it to the columns that store exact numeric data such as financial data. Apa itu NULL di MySQL?NULL adalah suatu nilai pada suatu kolom yang berarti tidak mempunyai nilai. NULL tidak sama dengan 0. Sebab angka 0 mempunyai nilai yaitu bernilai 0. NULL juga tidak sama dengan text kosong. NULL adalah suatu nilai yang tidak bernilai sama sekali.
Apa itu null SQL?Nilai null dalam database relasional digunakan ketika nilai dalam kolom tidak diketahui atau hilang. Null bukanlah string kosong (untuk jenis data karakter atau datetime) atau nilai nol (untuk jenis data numerik).
Apa yang dimaksud dengan nilai null?Nilai null mengindikasikan bahwa varian tersebut tidak berisi data yang valid. Null tidak sama dengan Kosong, yang mengindikasikan bahwa variabel belum diinisialisasi. Juga tidak sama seperti string panjang nol (""), yang terkadang disebut sebagai string kosong.
Apa fungsi dari NOT NULL?Atribut NOT NULL dapat digunakan pada hampir semua tipe data, Fungsinya untuk memastikan bahwa nilai pada kolom tersebut tidak boleh kosong. Jika kita menginput data, namun tidak memberikan nilai untuk kolom tersebut, akan menghasilkan error pada MySQL.
|