Mastering SQL Server: Essential Commands, Queries, and Administration Tips
This comprehensive guide covers SQL Server basics such as logging in, creating and dropping databases, tables, indexes and views, advanced query techniques like UNION, outer joins and pagination, plus maintenance tasks, backup and restore, linked‑server data transfer, replication setup, and useful scripts for database administration.
1. Basic Operations
Login to the database (e.g., mysql -uroot -p123123), create and drop databases, create tables, add columns, define primary keys, create indexes and views, and perform common SELECT, INSERT, UPDATE, DELETE statements.
2. Advanced Queries
Use set operators (UNION, EXCEPT, INTERSECT), outer joins (LEFT, RIGHT, FULL), GROUP BY, TOP and pagination techniques, and conditional clauses such as WHERE 1=1, BETWEEN, IN, and NOT IN.
3. Database Maintenance
Backup and restore databases, shrink and compress data files, rebuild indexes, change object owners, and manage linked servers for cross‑server data transfer using sp_addlinkedserver, openrowset, openquery, and opendatasource.
4. SQL Server Functions and Settings
String functions (DATALENGTH, SUBSTRING, LEFT, RIGHT), NULL handling with ISNULL, custom data types via sp_addtype, and session settings such as SET NOCOUNT ON. System procedures for integrity checks and metadata retrieval are also covered.
5. Replication and Synchronization
Configure publication and subscription servers, create snapshots, set up distribution, and write stored procedures to synchronize tables between servers, handling inserts, updates, and deletions in a scheduled job.
6. Useful Scripts
Examples include dynamic SQL construction, looped inserts, scripts to list tables, columns, indexes, and user‑defined objects, as well as scripts for checking checksum equality between tables.
Illustrative images are included in the original article.
-- Example: create a database
CREATE DATABASE MyDatabase;
-- Example: create a table
CREATE TABLE dbo.Employee(
Id INT PRIMARY KEY,
Name NVARCHAR(50),
Salary MONEY
);
-- Example: add a column
ALTER TABLE dbo.Employee ADD HireDate DATE;
-- Example: create an index
CREATE UNIQUE INDEX IX_Employee_Name ON dbo.Employee(Name);
-- Example: backup a database
BACKUP DATABASE MyDatabase TO DISK = 'C:\Backups\MyDatabase.bak';
-- Example: linked server query
SELECT * FROM RemoteServer.DatabaseName.dbo.TableName;Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
