Data Types & Constraints 🛡️
Two Ways to Learn: Visual Designer + SQL Code
Two Ways to Master Data Types & Constraints! 🎯
In this lesson, you'll learn two ways to work with data types and constraints: 1. The Visual Way (using SSMS right-click menus) and 2. The Code Way (writing SQL). Real pros know BOTH!
- ✅ Choose the right data type for your columns
- ✅ Add constraints using SSMS Designer (right-click)
- ✅ Add constraints using SQL code
- ✅ Create tables with proper data types
- ✅ Understand when to use each method
- ✅ Build professional-grade databases!
Part 1: Data Types - Choosing the Right Box
🖱️ Method 1: Visual (SSMS Designer)
- In Object Explorer, right-click Tables → New Table
-
In the designer, for each column:
- Type the Column Name
- Choose Data Type from dropdown
- Check Allow Nulls or uncheck it
- Press Ctrl + S to save, name your table
💻 Method 2: SQL Code
-- Create table with data types
CREATE TABLE Students (
StudentId INT IDENTITY(1,1),
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
Age INT,
GPA DECIMAL(3,2),
EnrolledDate DATE,
IsActive BIT
);
📌 Key Data Types:
INT= Whole numbers (1, 2, 3)NVARCHAR= Text (names, emails)DECIMAL= Numbers with decimals (money)DATE= DatesBIT= True/False (0 or 1)
| Data Type | What It Stores | Example | When to Use |
|---|---|---|---|
| INT | Whole numbers | 25, 1000, -5 | Age, quantity, ID |
| DECIMAL(10,2) | Numbers with decimals | 99.99, 15.50 | Money, GPA, prices |
| NVARCHAR(50) | Text (variable length) | "John", "Smith" | Names, emails, addresses |
| DATE | Dates only | 2024-01-15 | Birthday, enrollment date |
| BIT | True/False | 0 or 1 | Active, paid, confirmed |
Part 2: Constraints - The Rules
🔑 PRIMARY KEY
What it does: Uniquely identifies each row (like an ID card)
Visual Way
- Open table in designer
- Right-click the column
- Select Set Primary Key
- A key icon appears! 🔑
Code Way
CREATE TABLE Students (
StudentId INT PRIMARY KEY IDENTITY(1,1)
);
🚫 NOT NULL
What it does: Column cannot be empty (must have a value)
Visual Way
- Open table in designer
- Uncheck "Allow Nulls" box
- Column becomes required!
Code Way
FirstName NVARCHAR(50) NOT NULL
🆔 UNIQUE
What it does: No duplicate values allowed (like fingerprints)
Visual Way
- Open table in designer
- Right-click column → Indexes/Keys
- Click Add → Set Type = Unique Key
- In the grid, set Columns to your column
Code Way
Email NVARCHAR(100) UNIQUE
✅ CHECK
What it does: Validates data against a condition
Visual Way
- Open table in designer
- Right-click → Check Constraints
- Click Add
- Type expression:
Age >= 18
Code Way
Age INT CHECK (Age >= 18)
🔗 FOREIGN KEY
What it does: Links to another table (like a relationship)
Visual Way
- Open table in designer
- Right-click → Relationships
- Click Add
- Choose Tables and Columns Specification
- Select the parent table and column
Code Way
DeptCode NVARCHAR(10),
CONSTRAINT FK_Students_Dept
FOREIGN KEY (DeptCode)
REFERENCES Departments(DeptCode)
Part 3: Adding Constraints Later
🖱️ Visual Way
- Right-click your table → Design
- Right-click in designer → Check Constraints
- Click Add → Type your expression
- Click Close → Save the table
- ✅ Constraint added!
💻 Code Way
-- Add CHECK constraint to existing table
ALTER TABLE Students
ADD CONSTRAINT CK_Students_Age
CHECK (Age >= 18);
-- Add UNIQUE constraint
ALTER TABLE Students
ADD CONSTRAINT UQ_Students_Email
UNIQUE (Email);
📌 Why this is useful: You can add rules to existing data without deleting anything!
CK_Students_Age)
so you can easily identify them later!
Let's Practice! 🎮
Your Mission:
-
Using SSMS Designer (Visual Way)
- Create a new table called "Books"
- Add columns: BookId, Title, Author, Year, Price
- Set BookId as PRIMARY KEY
- Set Title NOT NULL
- Add CHECK constraint: Year >= 1900
- Add CHECK constraint: Price > 0
-
Using SQL (Code Way)
- Write the SQL to create the same table
- Include all constraints
- Run the query to create it
💡 What You'll Learn:
- ✅ How to use SSMS Designer
- ✅ How to write SQL for tables
- ✅ How to add PRIMARY KEY
- ✅ How to add NOT NULL
- ✅ How to add CHECK constraints
- ✅ Both ways to become a pro!
-- ===== BOOKS TABLE - COMPLETE EXAMPLE =====
-- Way 1: Visual Designer Steps
-- 1. Right-click Tables → New Table
-- 2. Add columns:
-- BookId: INT (uncheck Allow Nulls, set as Primary Key)
-- Title: NVARCHAR(200) (uncheck Allow Nulls)
-- Author: NVARCHAR(100) (check Allow Nulls)
-- Year: INT (check Allow Nulls)
-- Price: DECIMAL(10,2) (check Allow Nulls)
-- 3. Right-click → Check Constraints → Add
-- Expression: Year >= 1900
-- Expression: Price > 0
-- 4. Press Ctrl+S, name it "Books"
-- Way 2: SQL Code
CREATE TABLE Books (
BookId INT IDENTITY(1,1) PRIMARY KEY,
Title NVARCHAR(200) NOT NULL,
Author NVARCHAR(100),
Year INT CHECK (Year >= 1900),
Price DECIMAL(10,2) CHECK (Price > 0)
);
GO
-- Insert some sample books
INSERT INTO Books (Title, Author, Year, Price)
VALUES
('C# Programming', 'John Doe', 2020, 49.99),
('Clean Code', 'Robert Martin', 2008, 39.99);
GO
-- View the data
SELECT * FROM Books;
GO
-- Adding constraints later (if needed)
ALTER TABLE Books
ADD CONSTRAINT CK_Books_Year
CHECK (Year >= 1900);
🎉 What You Learned Today!
Data Types
INT, NVARCHAR, DATEPRIMARY KEY
Unique IDsNOT NULL
Required columnsCHECK
Validate dataVisual Way
SSMS DesignerCode Way
SQL QueriesMaster!
Both ways!🎯 You're now a master of data types and constraints - using BOTH visual and code methods!