T-SQL for Data
Manipulation
Vu Tuyet Trinh
Hanoi University of Technology
1
MicrosoftMicrosoft
Overview of Transact-SQL
Based on AINSI SQL 92 standard
Composing of three categories
Data Manipulation Language (DML)
Data Definition Language (DDL)
Data Control Language (DCL)
Having some Microsoft specific extensions
Beyond relational data
.net framework integration
MicrosoftMicrosoft
Outline
Selecting Data from Existing Tables
Inserting Data
Updating Data
Deleting Data
Fulltext Serach
MicrosoftMicrosoft
Select Data
The simple syntax to select the items
from the exit table is:
select <the items >
from <name of table>
where <conditions>
MicrosoftMicrosoft
Select Data
You can change the name of the items in the table:
MicrosoftMicrosoft
Select Data
You can insert some items as following:
MicrosoftMicrosoft
Select
Example a function ‘count’ to count the columns of the
table:
MicrosoftMicrosoft
Select Data
You can use some functions or some operations in the
Select Statement:
MicrosoftMicrosoft
Select Data
MicrosoftMicrosoft
Select Data
Maths
Functions
Functions
String
Functions
The Time
functions
MicrosoftMicrosoft
- ASCII()
- CHAR()
- UPPER()
- LOWER()
- LEN()
- LTRIM()
- RTRIM()
- LEFT()
- RIGHT()
- AVG()
- MIN()
- MAX()
- SUM()
- COUNT()
- SQUARE()
- SQRT()
- ROUND()
- GETDATE()
-
DATEPART(YY,g
etdate())
-
DATEDIFF(X,Y,Z)
-
DAY(),MONTH(),
YEAR()
Select Data
Maths String Time
MicrosoftMicrosoft
Select Data
Besides, there are some functions to convert the data
type:
- CAST()
- CONVERT()
MicrosoftMicrosoft
Select
Select
Order by….ect…
Join..on
Cross join
Group by
Oder by
Compute
For
Into
Having
With
Except ,
Intersect
Union
Option
MicrosoftMicrosoft
Select-With statement
Specifies a temporary named result set, known as a common
table expression (CTE).
MicrosoftMicrosoft
Select-Order by
-
You can arrange the result follow the first letter by using the
order by statement:
MicrosoftMicrosoft
Select-multiple tables
You can select the items from more than a table as
following:
With “outer join”:
MicrosoftMicrosoft
Select – multiple tables
Inner join:
MicrosoftMicrosoft
Select-multiple tables
-
When you select from more than 1 table, you can use the join…
on statement or you can use the cross join statement. With the
cross join statement, you don’t have to have to relative
condition between the tables and the result is all the Objects:
MicrosoftMicrosoft
Select-multiple tables
MicrosoftMicrosoft
Select-multiple tables
-
You can select from many tables, and create a new table to store
that result:
Now, you can refresh the sql folder, and you can see the new
table named newtbl as the following:
MicrosoftMicrosoft
Select-multiple tables
MicrosoftMicrosoft
Select-merge
A new feature in SQL2008 is merge statement. You can merge 2 or
more tables:
MicrosoftMicrosoft
Select-Group by
Specifies the groups into which output rows are to be placed. If aggregate
functions are included in the SELECT clause <select list>, GROUP BY
calculates a summary value for each group.
MicrosoftMicrosoft
Select-Having
Specifies a search condition for a group or an aggregate
HAVING is typically used in a GROUP BY clause. When
GROUP BY is not used, HAVING behaves like a WHERE
clause.
MicrosoftMicrosoft