T SQL Tutorial
T SQL Tutorial
Audience
This tutorial is designed for those who want to learn the basics of T-SQL.
Prerequisites
To go ahead with this tutorial, familiarity with database concepts is preferred. It is good
to have SQL Server installed on your computer, as it might assist you in executing the
examples yourself and get to know how it works.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com.
i
T-SQL
Table of Contents
About the Tutorial .....................................................................................................................................
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
1. T-SQL - OVERVIEW............................................................................................................... 1
ii
T-SQL
SAVEPOINT Command........................................................................................................................... 39
iii
1. T-SQL – Overview T-SQL
In 1970's the product called 'SEQUEL', structured English query language, developed by
IBM and later SEQUEL was renamed to 'SQL' which stands for Structured Query Language.
In 1986, SQL was approved by ANSI (American national Standards Institute) and in 1987,
it was approved by ISO (International Standards Organization).
SQL is a structure query language which is a common database language for all RDBMS
products. Different RDBMS product vendors have developed their own database language
by extending SQL for their own RDBMS products.
T-SQL stands for Transact Structure Query Language which is a Microsoft product and is
an extension of SQL Language.
Example
MS SQL Server - SQL\T-SQL
ORACLE - SQL\PL-SQL
1
2. T-SQL Server – Data Types T-SQL
SQL Server data type is an attribute that specifies types of data of any object. Each
column, variable and expression has related data type in SQL Server. These data types
can be used while creating tables. You can choose a particular data type for a table column
based on your requirement.
SQL Server offers seven categories including other category of data types for use.
Numeric and decimal are Fixed precision and scale data types and are functionally
equivalent.
Type From To
2
T-SQL
datetimeoffset (100
nanoseconds accuracy. Introduced in SQL Jan 1, 0001 Dec 31, 9999
Server 2008)
Character Strings
Type Description
Type Description
3
T-SQL
Binary Strings
Type Description
timestamp: Stores a database-wide unique number that gets updated every time
a row gets updated.
uniqueidentifier: Stores a globally unique identifier (GUID).
xml: Stores XML data. You can store XML instances in a column or a variable
(Introduced in SQL Server 2005).
4
3. T-SQL Server – Create Tables T-SQL
Creating a basic table involves naming the table and defining its columns and each
column's data type.
The SQL Server CREATE TABLE statement is used to create a new table.
Syntax
Following is the basic syntax of CREATE TABLE statement:
CREATE TABLE is the keyword telling the database system what you want to do. In this
case, you want to create a new table. The unique name or identifier for the table follows
the CREATE TABLE statement. Then in brackets comes the list defining each column in the
table and what sort of data type it is. The syntax becomes clearer to understand with the
following example.
A copy of an existing table can be created using a combination of the CREATE TABLE
statement and the SELECT statement. You can check complete details at Create Table
Using another Table.
Example
In this example, let’s create a CUSTOMERS table with ID as primary key and NOT NULL
are the constraints showing that these fields cannot be NULL while creating records in this
table:
You can verify if your table has been created successfully by looking at the message
displayed by the SQL server, otherwise you can use the following command:
5
T-SQL
You can now see that CUSTOMERS table is available in your database which you can use
to store required information related to customers.
6
4. T-SQL Server – Drop Tables T-SQL
The SQL Server DROP TABLE statement is used to remove a table definition and all data,
indexes, triggers, constraints, and permission specifications for that table.
Note: You have to be careful while using this command because once a table is deleted
then all the information available in the table would also be lost forever.
Syntax
Following is the basic syntax of DROP TABLE statement:
Example
Let us first verify CUSTOMERS table and then we will delete it from the database:
7
T-SQL
CUSTOMERS table is available in the database, so let us drop it. Following is the command
for the same.
With the above command, you will not get any rows.
8
T-SQL