SQL Server Kickoff β What Data Fits Where?


SQL Server Data Types and Constraints
Welcome to Day 1 of the 30-Day SQL Server Revision Challenge! π This series is perfect for those looking to brush up on SQL Server fundamentals in a structured, quick 30 minutes and fun way.
Today, weβre starting with the building blocks of any database: Data Types
and Constraints
. Letβs dive in. π
π§± SQL Server Data Types
SQL Server provides various data types to define the kind of data each column can store. Here are the most commonly used:
π Numeric Data Types
INT
: Integer values (whole numbers)FLOAT
: Approximate numeric with floating decimalDECIMAL(p,s)
: Precise numeric with scale (e.g.,DECIMAL(5,2)
= 999.99)
π€ Character Data Types
VARCHAR(n)
: Variable-length non-Unicode string (up to 8,000 chars)NVARCHAR(n)
: Variable-length Unicode string (international chars)
π Date and Time Types
DATETIME
: Stores both date and timeDATE
: Date onlyTIME
: Time only
βοΈ Misc
BIT
: Boolean values (0 = false, 1 = true)
π¦ SQL Constraints
Constraints are rules applied to table columns to enforce data integrity.
1. PRIMARY KEY π
Ensures each record is unique and not NULL.
CREATE TABLE Employees (
EmpID INT PRIMARY KEY,
Name VARCHAR(100)
);
2. FOREIGN KEY π
Links one tableβs column to anotherβs primary key.
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
EmpID INT FOREIGN KEY REFERENCES Employees(EmpID)
);
3. CHECK β
Enforces domain-level constraints.
Age INT CHECK (Age >= 18)
4. DEFAULT π
Assigns default values.
IsActive BIT DEFAULT 1
5. NOT NULL β
Prevents NULLs in a column.
Email VARCHAR(100) NOT NULL
π§ Pro Tip:
If youβre ever unsure about what data type to use, start small and scale as needed. Use NVARCHAR
for multilingual support and DECIMAL
when precision matters (e.g., for money).
π Summary Cheat Sheet
Category | Examples |
Numeric | INT, FLOAT, DECIMAL |
String | VARCHAR, NVARCHAR |
Date | DATETIME, DATE, TIME |
Boolean | BIT |
Constraint | Purpose |
PRIMARY KEY | Uniqueness |
FOREIGN KEY | Relational Integrity |
CHECK | Domain Rules |
DEFAULT | Auto Value |
NOT NULL | Mandatory Field |
π‘ Day 1 Challenge:
Create a table named Students
with the following:
StudentID (Primary Key)
Name (Not Null)
Age (Check: 5 to 100)
EnrollmentDate (Default: Current Date)
Share your solution in the comments or post it using the tag #30DaySQLChallenge
. Letβs build this together! π
Stay tuned for Day 2: SELECT Essentials tomorrow! π
Subscribe to my newsletter
Read articles from Jhansi Siriprolu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
