Lecture 05 DMS
Lecture 05 DMS
1
11/7/2023
Lecture Agenda
2
11/7/2023
What is Transact-SQL?
…
…
…
…
…
…
…
Microsoft relational database services
• SQL is declarative, not procedural
• Describe what you want, don’t specify steps
3
11/7/2023
SalesOrderHeader Product
OrderID OrderDate CustomerID ProductID Name ListPrice
1 1/1/2015 1 1 Widget 2.99
2 1/1/2015 3 2 Gizmo 1.79
3 1/2/2015 1 3 Thingybob 3.49
Sales Production
Sales.Order Order Product
Production.Product
Production.Order
4
11/7/2023
10
5
11/7/2023
11
Data Manipulation Language Data Definition Language (DDL) Data Control Language (DCL) Transaction Control Language
(DML) (TCL)
Statements for querying Statements for defining Statements for assigning Statements for managing
and modifying data: database objects: security permissions: transaction:
Module
12
6
11/7/2023
13
Data Types
String Types
Why Data types are important?
🡪 Further practices with data types will
Date - Time
be introduce in another lecture.
14
7
11/7/2023
15
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 308. 4 or 8 bytes
16
8
11/7/2023
datetime2 From 1/1/0001 to 31/12/9999 with an accuracy of 100 nanoseconds 6-8 bytes
datetimeoffset The same as datetime2 with the addition of a time zone offset 8-10 bytes
17
NULL Values
• Useful functions:
• ISNULL(column/variable, value): Returns value if the column or variable is NULL
• NULLIF(column/variable, value): Returns NULL if the column or variable is value
• COALESCE(column/variable1, column/variable2, ...): Returns the value of the first non-NULL column or variable in the list
18
9
11/7/2023
Module Review
You must return the Name and Price columns from a table named Product in the Production schema. In the resulting rowset,
you want the Name column to be named ProductName. Which of the following Transact-SQL statements should you use?
❑ SELECT * FROM Product AS Production.Product;
❑ SELECT Name AS ProductName, Price FROM Production.Product;
❑ SELECT ProductName, Price FROM Production.Product;
You must retrieve data from a column that is defined as char(1). If the value in the column is a digit between 0 and 9, the
query should return it as an integer value. Otherwise, the query should return NULL.
Which function should you use?
❑ CAST
❑ NULLIF
❑ TRY_CONVERT
You must return the Cellphone column from the Sales.Customer table. Cellphone is a varchar column that permits NULL
values. For rows where the Cellphone value is NULL, your query should return the text 'None'. What query should you use?
❑ SELECT ISNULL(Cellphone, 'None') AS Cellphone FROM Sales.Customer;
❑ SELECT NULLIF(Cellphone, 'None') AS Cellphone FROM Sales.Customer;
❑ SELECT CONVERT(varchar, Cellphone) AS None FROM Sales.Customer;
19
20
10
11/7/2023
Operators
Group Operator Description
= Equal
<> Or != Not equal
Comparison operators > Greater than
< Less than
>= Greater than or equal
<= Less than or equal
AND Return records that meet all the conditions separated by AND in WHERE clause.
Logical operators OR Return records that meet any of the conditions separated by OR in WHERE clause.
NOT Return records that do not satisfy any of the conditions in WHERE clause.
[NOT] BETWEEN Returns values [NOT] within a given range
[NOT] IN Specify multiple values in a WHERE clause (a shorthand for multiple OR conditions)
SQL operators IS [NOT] NULL Returns records having [NOT] NULL values in the given fileds.
[NOT] LIKE Returns records that [DO NOT] match a specified pattern in a column.
21
Expressions
22
11
11/7/2023
Database Design
23
THANK YOU !
24
12