⬅ Previous Topic
SQL Syntax and StatementsNext Topic ⮕
CREATE TABLE Statement⬅ Previous Topic
SQL Syntax and StatementsNext Topic ⮕
CREATE TABLE StatementEvery value in a database has a type. Just like in everyday life where names are words and ages are numbers, SQL needs to know what kind of data you're storing—this is where data types come in.
Data types help define how much space a value needs, what operations are allowed on it, and how it behaves. Assigning the correct type is like assigning the right role in a play—it ensures your data behaves as expected.
SQL data types are broadly classified into:
INT
: Whole numbers. Ideal for roll numbers or age.FLOAT
: Numbers with decimals. Useful for percentage or fee amounts.DECIMAL(p,s)
: Fixed-precision decimal. For storing money accurately.CREATE TABLE student_marks (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
maths_marks FLOAT,
science_marks DECIMAL(5,2)
);
CHAR(n)
: Fixed-length string (e.g., 10 characters exactly)VARCHAR(n)
: Variable-length string up to 'n' charactersTEXT
: Large blocks of text (not used often for structured tables)CREATE TABLE students (
roll_no INT PRIMARY KEY,
name VARCHAR(50),
class CHAR(4),
city VARCHAR(30)
);
DATE
: For date only (YYYY-MM-DD)TIME
: For time only (HH:MM:SS)DATETIME
or TIMESTAMP
: For date and timeCREATE TABLE attendance (
roll_no INT,
date_of_entry DATE,
entry_time TIME,
PRIMARY KEY (roll_no, date_of_entry)
);
Although not present in every SQL version (e.g., MySQL handles it as TINYINT), BOOLEAN
is used to store TRUE
or FALSE
values.
CREATE TABLE hostel_allocation (
roll_no INT,
has_room BOOLEAN
);
INSERT INTO students VALUES
(1, 'Asha Rao', '10A', 'Nagpur'),
(2, 'Zain Khan', '9B', 'Lucknow');
SELECT * FROM students;
roll_no | name | class | city
--------+----------+-------+---------
1 | Asha Rao | 10A | Nagpur
2 | Zain Khan| 9B | Lucknow
VARCHAR(30)
for city names instead of TEXT
.VARCHAR(255)
if names will only be 50 characters max.DECIMAL
for money: Avoid rounding errors that come with FLOAT
.SQL data types form the foundation of every table. Choosing the right type ensures data integrity, saves space, and improves performance. Whether it's roll numbers or admission dates, give your data the shape it deserves.
Up next, we’ll explore SQL Constraints — rules that safeguard your tables and ensure correctness, like making sure a roll number is never duplicated.
⬅ Previous Topic
SQL Syntax and StatementsNext Topic ⮕
CREATE TABLE StatementYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.