004. SQL Server Management Studio (SSMS) – Walkthrough
1. Connecting to a SQL Server Instance
Walkthrough
- Open SSMS from Start Menu
- In “Connect to Server” dialog:
- Server type: Database Engine (default)
- Server name:
- Local:
. or (local)
- Named instance:
.\SQLEXPRESS
- Remote:
server.domain.com or 192.168.1.100
- Authentication:
- Windows (uses your AD credentials)
- SQL Server (enter username/password)
- Click Connect
Pro Tip
- Local instances run on your machine, remote instances are on other servers
- Use
SQLCMD -L in Command Prompt to discover local instances
[Screenshot: SSMS login dialog with localhost and Windows Auth selected]
2. Writing and Executing SQL Queries
Walkthrough
- File > New > Query with Current Connection (or Ctrl+N)
- Type SQL:
SELECT * FROM Employees WHERE Department = 'Sales'
- Execute:
- F5: Runs entire script
- Highlight text + F5: Runs selection only
Pro Tip
- Ctrl+R: Toggle results pane
- Ctrl+K+C/U: Comment/uncomment code
- Use
GO between batches
[Screenshot: Query window with executed results]
3. Creating and Managing Databases
Walkthrough
- Right-click Databases > New Database
- Configure:
- Name (e.g.,
CustomerDB)
- Files tab: Adjust initial size/autogrowth
- Options tab: Set recovery model (SIMPLE/FULL)
- Click OK
Explore
- Right-click DB > Properties > Filegroups for partitioning
- Check “Compatibility level” for version-specific features
[Screenshot: New Database dialog with file configuration]
4. Creating and Managing Tables
Walkthrough
- Right-click Tables > New > Table
- Design columns:
- Name (e.g.,
ProductID)
- Data type (e.g.,
int, varchar(50))
- Allow NULLs (checkbox)
- Set Primary Key: Right-click column > Set Primary Key
- Save (Ctrl+S) > Name table (e.g.,
Products)
Pro Tip
- Use Table Designer for GUI editing
- Use CREATE TABLE script for version control
[Screenshot: Table designer with column definitions]
5. Creating Relationships (Foreign Keys)
Walkthrough
- Open Database Diagrams > New Diagram
- Add related tables (e.g.,
Orders and Customers)
- Right-click
Orders.CustomerID > Relationships
- Configure:
- Primary key table:
Customers
- Foreign key table:
Orders
- Enforce referential integrity (recommended)
Best Practice
- Script relationships for team consistency:
ALTER TABLE Orders ADD CONSTRAINT FK_Orders_Customers
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
[Screenshot: Relationship dialog with tables linked]
6. Viewing and Filtering Data
Walkthrough
- Right-click table > Select Top 1000 Rows
- Filter:
- Right-click column header > Filter
- Enter criteria (e.g.,
Salary > 50000)
- Edit data:
- Right-click table > Edit Top 200 Rows
- Modify cells directly
Configuration
- Change default row limit:
Tools > Options > SQL Server > Results Grid
[Screenshot: Data grid with filter applied]
7. Creating Stored Procedures & Functions
Walkthrough
- Right-click Programmability > Stored Procedures > New
- Template appears - modify:
CREATE PROCEDURE GetEmployeeByDept
@DeptName varchar(50)
AS
BEGIN
SELECT * FROM Employees
WHERE Department = @DeptName
END
- Execute (F5) to create
- Test:
EXEC GetEmployeeByDept 'HR'
Key Use
- Encapsulate business logic
- Improve security (grant EXEC vs direct table access)
[Screenshot: Stored Procedure script window]
8. Using Execution Plans for Query Optimization
Walkthrough
- Open query window
- Click Include Actual Execution Plan (Ctrl+M)
- Run query (F5)
- Analyze:
- Table Scans (bad) > Add indexes
- Key Lookups (expensive) > Covering indexes
- Cost percentages (focus on high-cost ops)
Pro Tip
- Right-click plan > Missing Index Details for recommendations
[Screenshot: Execution plan with high-cost operators]
9. Using Object Explorer Efficiently
Walkthrough
- Navigate hierarchy:
- Databases: User/system DBs
- Security: Logins/users
- Server Objects: Linked servers
- Right-click actions:
- Script As: Generate CREATE/ALTER scripts
- Dependencies: Impact analysis
- Filter: Find objects quickly
Hidden Gem
- Drag objects to query window to auto-generate references
[Screenshot: Object Explorer with right-click menu]
10. Managing Users, Roles, and Permissions
Walkthrough
- Create Login:
- Security > Logins > New Login
- Map to Windows AD or SQL auth
- Database Access:
- User Mappings tab > Check target DB
- Assign roles (
db_datareader, db_owner)
- Object Permissions:
- Right-click DB > Properties > Permissions
Critical Knowledge
- Server roles (e.g.,
sysadmin) control server-wide access
- Database roles (e.g.,
db_owner) control DB-specific access
[Screenshot: New Login dialog with role assignments]
11. Backing Up and Restoring Databases
Walkthrough
Backup:
- Right-click DB > Tasks > Back Up
- Configure:
- Type: Full/Differential/Log
- Destination: File path or device
- Compression: On (recommended)
Restore:
- Right-click Databases > Restore Database
- Select backup file > Options tab:
- “Overwrite existing database”
- “Leave in restoring state” (for log shipping)
Pro Tip
- Test restores regularly!
- Use
CHECKSUM for backup verification
[Screenshot: Backup dialog with compression enabled]
12. Generating Scripts for Schema/Data
Walkthrough
- Right-click DB > Tasks > Generate Scripts
- Select objects (tables, SPs, etc.)
- Advanced options:
- Script Data: None/Schema only/Data only
- Include IF NOT EXISTS: Prevents errors
- Output to file/clipboard/query window
Migration Use Case
- Version control schema changes
- Deploy to production
[Screenshot: Generate Scripts wizard with options]
13. SQL Profiler & Extended Events
Walkthrough
Profiler:
- Tools > SQL Server Profiler
- New Trace > Select events:
SQL:BatchCompleted
Deadlock graph
- Run and analyze queries
Extended Events:
- Management > Extended Events > New Session
- Add events (e.g.,
sql_statement_completed)
- Watch live data or save to file
Advanced Debugging
- Filter by duration (>1000ms)
- Capture parameter values
[Screenshot: Profiler trace with duration column]
14. Database Diagrams (Visual Schema View)
Walkthrough
- Right-click Database Diagrams > New Diagram
- Add tables > Auto-arrange
- View relationships:
- PK = Gold key icon
- FK = Grey infinity symbol
- Save diagram (becomes DB object)
>Presentation Tip
- Right-click > Page Layout for printing
- Export as image for documentation
[Screenshot: ER diagram with 5 related tables]
15. Cleaning up Orphaned/Unused Objects
Walkthrough
- Find unused tables:
SELECT * FROM sys.objects
WHERE type = 'U'
AND create_date < DATEADD(month, -6, GETDATE())
AND NOT EXISTS (SELECT 1 FROM sys.dm_db_index_usage_stats
WHERE object_id = OBJECT_ID(name))
- Backup before deletion!
- Right-click > Delete (or script DROP statements)
>Safe Cleanup
- Check dependencies first
- Consider renaming before dropping
[Screenshot: Query identifying unused tables]
Final Notes
- Shortcuts: Master Ctrl+T (results to text), Ctrl+D (results to grid)
- Customization: Tools > Options > Environment > Fonts for readability
- Plugins: Consider SQL Prompt for enhanced IntelliSense
16. SQLCMD Quick Guide
Basic Connection Command
sqlcmd -S .\SQLEXPRESS -U sa -P sa123 -d test1 -C
Parameters:
-S Server name (.\SQLEXPRESS = local SQL Express instance)
-U Username (sa = system administrator)
-P Password
-d Database name
-C Encrypt connection
How to Use
- Open Command Prompt (Win+R → cmd)
- Enter the connection command
- At the
1> prompt, type SQL commands
- Execute with
GO
Common Variations:
- Windows Auth:
sqlcmd -S .\SQLEXPRESS -E
- Run script:
sqlcmd -S .\SQLEXPRESS -U sa -P sa123 -i script.sql
- Save output: Add
-o output.txt
Safety Notes:
- Avoid hardcoding passwords in scripts
- Use limited-permission accounts when possible
- Always encrypt connections (-C) in production
Type QUIT to exit SQLCMD.
17. Enabling features in SQL Server 2022 using Configuration Manager
Step 1: Open SQL Server Configuration Manager
- Press Windows + R to open the Run dialog
- Type:
- Press Enter
(This opens the Configuration Manager specifically for SQL Server 2022)
Step 2: Enable Network Protocols
-
In the left pane:
- Expand “SQL Server Network Configuration”
- Click “Protocols for [YourInstanceName]”
(Default instance shows as “MSSQLSERVER”)
-
In the right pane, right-click on each protocol you want to enable:
- TCP/IP (for remote connections)
- Named Pipes (for local network connections)
- Shared Memory (already enabled by default for local access)
-
Select Enable for each desired protocol
- Double-click TCP/IP
- Go to the IP Addresses tab
- For each active IP address:
- Set TCP Port to 1433 (or your custom port)
- Ensure Active and Enabled are both “Yes”
- Click OK to save
Step 4: Restart SQL Server Service
- In the left pane:
- Select “SQL Server Services”
- In the right pane:
- Right-click SQL Server ([YourInstanceName])
- Select Restart