0
0
SQLquery~5 mins

Column data types (INT, VARCHAR, DATE, DECIMAL) in SQL

Choose your learning style9 modes available
Introduction
Column data types tell the database what kind of information each column will hold, like numbers, text, or dates. This helps keep data organized and accurate.
When creating a table to store customer information like names and birthdates.
When saving product prices that need decimal points for cents.
When recording dates for events or transactions.
When storing whole numbers like quantities or IDs.
Syntax
SQL
column_name DATA_TYPE(size)
DATA_TYPE defines what kind of data the column holds, like INT for whole numbers or VARCHAR for text.
Size is optional and usually used with VARCHAR to limit text length or DECIMAL to set precision and scale.
Examples
Stores whole numbers like age.
SQL
age INT
Stores text up to 50 characters long.
SQL
name VARCHAR(50)
Stores a date value like a birthday.
SQL
birthdate DATE
Stores numbers with up to 8 digits total and 2 digits after the decimal point, like money.
SQL
price DECIMAL(8,2)
Sample Program
This creates a Products table with different column types, adds two products, and then shows all the data.
SQL
CREATE TABLE Products (
  ProductID INT,
  ProductName VARCHAR(100),
  ReleaseDate DATE,
  Price DECIMAL(10,2)
);

INSERT INTO Products VALUES (1, 'Coffee Maker', '2023-05-10', 79.99);
INSERT INTO Products VALUES (2, 'Blender', '2022-11-20', 49.50);

SELECT * FROM Products;
OutputSuccess
Important Notes
INT is for whole numbers without decimals.
VARCHAR needs a size to limit how many characters it can hold.
DATE stores year-month-day format.
DECIMAL is great for exact numbers like money, where decimals matter.
Summary
Column data types define what kind of data each column holds.
Use INT for whole numbers, VARCHAR for text, DATE for dates, and DECIMAL for precise decimal numbers.
Choosing the right data type helps keep your data clean and useful.