T-SQL Tutorial

SQL Server Tutorial

What is SQL Server?

SQL Server is a database management system that was developed by the company Microsoft. This system works in a relational way, and other similar systems (although they belong to the competition) are Oracle, MariaDB, and MySQL, among others.

In its beginnings, SQL Server was only available for Microsoft Windows operating systems, but in recent times this has changed, and today it is available for both GNU/Linux and Docker as well.

You can configure the SQL Server system in several instances on the same physical server. In case you choose to use it several times, it is recommended that you use the server name the first time and then opt for more specific names (you can include dates if you consider it easier that way).

Basics of SQL Server

SQL Server is widely used to store, manage, and retrieve data in various applications and systems. Understanding the basics of SQL Server and its essential SQL commands is crucial for anyone working with databases. Here, we'll cover some fundamental SQL commands and concepts in the context of SQL Server:

SQL Server Instance: SQL Server can be installed as an instance on a server. An instance is a standalone database server with its own configuration, databases, and security settings.
Databases: SQL Server organizes data into databases. Each database is an isolated container that stores tables, views, stored procedures, and other database objects. You can create, manage, and switch between databases.
Tables: Tables are the primary data storage entities in SQL Server. They consist of rows and columns, where each column has a data type defining the kind of data it can store. You use SQL commands to create, modify, and query tables.
SQL Commands:
SELECT: Used to retrieve data from one or more tables.
INSERT: Adds new records into a table.
UPDATE: Modifies existing records in a table.
DELETE: Removes records from a table.
CREATE TABLE: Creates a new table with specified columns and data types.
ALTER TABLE: Modifies an existing table, e.g., adding, modifying, or dropping columns.
DROP TABLE: Deletes an existing table and its data.
CREATE DATABASE: Creates a new database.
USE: Switches to a specific database.
CREATE INDEX: Creates an index on one or more columns to improve query performance.
Stored Procedures: SQL Server allows you to define and execute stored procedures, which are reusable SQL code blocks.
Primary Key: A primary key uniquely identifies each record in a table. It enforces data integrity and ensures that there are no duplicate values.
Foreign Key: A foreign key establishes a link between two tables, ensuring referential integrity. It typically references the primary key of another table.
Constraints: SQL Server supports various constraints like UNIQUE, NOT NULL, and CHECK to enforce data integrity rules.
Views: Views are virtual tables that display data from one or more tables. They simplify complex queries and provide a level of abstraction.
Transactions: SQL Server supports transactions to ensure data consistency. You can use BEGIN TRANSACTION, COMMIT, and ROLLBACK to manage transactions.
Security: SQL Server provides robust security features, including user accounts, roles, and permissions to control access to data and database objects.
Backups and Recovery: Regular backups are essential for data protection. SQL Server offers tools and commands to perform backups and restore data in case of failure.
Maintenance: SQL Server requires regular maintenance tasks like index rebuilding, statistics updating, and database optimization to ensure optimal performance.

Advanced topics

SQL Server examples
Nested case statements in SQL Server refer to the practice of placing one or more CASE statements within another CASE statement. This allows for conditional logic to be applied in a hierarchical or layered manner, where the outcome of one CASE statement determines the evaluation of another.
Understanding how NULL works in SQL Server is essential for accurate data retrieval and manipulation, as incorrect handling can lead to unexpected results in your queries. NULL is not the same as an empty string or a zero, as it represents the lack of data rather than a specific value. It's important to handle NULL values carefully in SQL queries by using functions like IS NULL or IS NOT NULL to filter or check for NULL values.
Bulk Insert in SQL Server allows you to quickly and efficiently insert large amounts of data from external files, such as CSV or text files, into database tables. This feature is essential for optimizing data migration and ETL (Extract, Transform, Load) processes, making it a valuable tool for database administrators and developers working with large-scale data operations.
Scheduled stored procedure in SQL Server using T-SQL involves several steps. SQL Server's Agent is a key component in this process, which allows you to schedule and automate various tasks, including the execution of stored procedures.
Table variables in SQL Server are used to store data temporarily during the execution of code within your SQL Server database.

SQL Server Query

Inner Join Left Join Right Join Self Join
Group By Having Order By Where

SQL Server Triggers

Create Trigger Syntax Create DML Trigger Example
Alter Trigger Syntax Alter DML Trigger Example
Drop Trigger Example Enable Trigger Example
Disable Trigger Example Rename Trigger Example

SQL Server View/Procedure

Create View Alter View
Modify Data View Rename View
Drop View Create Procedure
Create Function Call Stored Procedure Example

These are some of the basic SQL commands used in SQL Server. SQL is a powerful language that allows you to interact with the database, retrieve, manipulate, and manage data efficiently, making it a fundamental skill for database administrators, developers, and data analysts. Advanced SQL commands and features in SQL Server can provide more complex functionality for working with databases.