Numeric data type refers to storing number-based values such as whole numbers or decimal numbers — like 65, 4.5, etc.
When you create a table in MySQL, you define the numeric data type (such as INTEGER, DECIMAL, FLOAT, etc.) at that time so that only number-type values can be inserted into that column.
Syntax:
CREATE TABLE table_name (
column_name INT,
column_name2 DECIMAL(precision, scale),
column_name3 FLOAT
);
Example:
Imagine you want to create a table to keep product information, where product_id stores whole numbers, price stores decimal values and rating stores floating-point numbers. In this case, you use numeric data types.
Query:
CREATE TABLE products (
product_id INT, -- whole number (e.g., 101, 102…)
price DECIMAL(8,2), -- decimal number (e.g., 499.99, 1200.50…)
rating FLOAT -- floating number (e.g., 4.3, 3.75…)
);
Insert Values into the Table:
INSERT INTO products (product_id, price, rating)
VALUES (101, 499.99, 4.3);
Output:
product_id | price | rating
-------------------------------
101 | 499.99 | 4.3
Explanation:
product_idwill only take integer values.pricewill take decimal values with up to 8 digits total and 2 digits after the decimal point.ratingstores fractional or decimal values as floating-point numbers.
