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 decimal

  • DECIMAL(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 time

  • DATE: Date only

  • TIME: 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

CategoryExamples
NumericINT, FLOAT, DECIMAL
StringVARCHAR, NVARCHAR
DateDATETIME, DATE, TIME
BooleanBIT
ConstraintPurpose
PRIMARY KEYUniqueness
FOREIGN KEYRelational Integrity
CHECKDomain Rules
DEFAULTAuto Value
NOT NULLMandatory 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! πŸ‘€

0
Subscribe to my newsletter

Read articles from Jhansi Siriprolu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jhansi Siriprolu
Jhansi Siriprolu