0% found this document useful (0 votes)
7 views

Lecture1_DBP2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Lecture1_DBP2024

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

LECTURE1

Introduction SQL Server


Introduction-SQL Server
What is SQL?
 It is a non-procedural language that is used to communicate with any database such as
Oracle, SQL Server, etc.
SQL is also called Sequel it stands for Structured English Query Language,
SQL is not a case-sensitive language it means that all the commands of SQL are not
case sensitive
Every command of SQL should end with a semicolon (;) (It is optional for SQL Server)
SQL can be called NLI (Natural Language Interface). It means that all the SQL
commands are almost similar to normal English language
Introduction-SQL Server
What is SQL Commands?
SQL commands are instructions. It is used to communicate
with the database. It is also used to perform specific tasks,
functions, and queries of data.
SQL commands can perform various tasks like create a table,
add data to tables, drop the table, modify the table, set
permission for users.
Introduction-SQL Server
Types of SQL Commands There are five types of SQL commands: DDL, DML, DCL,
TCL, and DQL.

Figure1.1 Types of SQL Commands


Introduction-SQL Server
What is SQL Server Data Type?
The SQL Server Data Types are the attribute that specifies what types of data entered by
the user such as integer, character, decimal, date time, etc.
 In SQL Server Database, each column of a table, all the local variables, and
parameters must have a data type.
The SQL Server supports the following data types
 Integer data types Character data types
 Decimal data types  Binary data types
 Money / currency data types  Special data types
Date and Time data types
Introduction-SQL Server
Integer Data Types in SQL Server:
Integer Data Types are allowed only to hold integer types of values and this data type
can be applied on EmpId, ProductCode, BracnchCode columns, etc. These data types
are classified into 4 types based on their range and memory size as shown in the below
image

Table1.1 integer data types


Introduction-SQL Server
Decimal Data Types in SQL Server:
These data types are allowed decimal point values only. The Decimal Data Type
contains two types those are
1. Decimal (P, S)
2. Numeric (P, S)
But both are the same. Here P represents precision and S represents Scale and the
default value of the Decimal data type is Decimal (18, 0) and also for Numeric (18, 0)
1. Example Prices Decimal (6, 3)
1.9 OK - 23.16 OK - 1234.12 OK -- 123456.12 Wrong 12345.1 OK --
Introduction-SQL Server
SQL Server Money / Currency Data Type
These data types are used to accept currency format values into a table column. The
money data type again classified into two types

Table2.1 Money / Currency data types


Example Use a period to separate partial monetary units, like cents, from whole
monetary units. For example, “2.15 $” specifies 2 dollars and 15 cents
Introduction-SQL Server
SQL Server Date and Time data types:
Date and Time data types are used to store a particular date and time information.
These are applying on the date of joining, date of birth, hire date, order date columns,
etc. Date and time data types again classified into 3 types, such as
1. Date: This data type will accept date format information only. The default format of the
date data type is ‘YYYY/MM/DD’
2. Time: It allows time format information only. The default format of the time data type is
‘hh:mm:ss.ms’
3. DateTime: It allows date and time format information. The default format of DateTime
data type is ‘YYYY/MM/DD hh:mm: ss.ms’
Introduction-SQL Server
Character Data Types in SQL Server:
Character data types are allowed characters and integer format values.
These data types can be applied to employee names, student names,
and product name columns, etc.
Character data types again classified into two types, those are Unicode
data types and Non-Unicode data types.
Non Unicode data types: char (Size), varchar (size/max), Text
Unicode data types: nchar(size), nvarchar(size), ntext
Introduction-SQL Server
Binary data type:
These data types are used to store image files, audio files, and video files into a
database location.
Binary data types again classified into three types, such as
1. Binary(size)
2. Varbinary(size/max)
3. Image data type
Introduction-SQL Server
Special data types:
SQL Server supports the following special data types:
 XML data type:
It is used for storing XML file information (Tag-based programming
information).
SQL Server Cursor Data Type:
The cursor is an object data type that will allocate a reference memory on
the server-side for storing temporary table information.
Transact-SQL
T-SQL Server
Transact-SQL extends SQL with few additional
features:
 Local variables
 Control flow constructs (ifs, loops, etc.)
 Functions for strings, dates, math, etc.
T-SQL Server
Basic T-SQL Programming Constructs
Identifiers System Functions
Data Types Operators
Variables Expressions
Control-of-Flow Language Elements
T-SQL Server
Identifiers There are two classes of identifiers:
I. Regular identifiers
Rules for regular identifiers
1. The first character must be one of the following:
 A letter as defined by the Unicode Standard 3.2. The Unicode definition of letters includes Latin
characters from a through z, from A through Z, and also letter characters from other languages.
 The underscore (_), at sign (@), or number sign (#).
2. Subsequent characters can include the following:
 Letters as defined in the Unicode Standard 3.2.
 Decimal numbers from either Basic Latin or other national scripts.
 The at sign (@), dollar sign ($), number sign (#), or underscore (_).
3. The identifier must not be a Transact-SQL reserved word.
4. Embedded spaces or special characters are not allowed.
T-SQL Server
Identifiers
II. Delimited identifiers
Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with
the rules for the format of identifiers might not be delimited. For examples:
a. SELECT * FROM [High School Students]
b. SELECT "First+Last" FROM [High School Students]
T-SQL Server
Variables
A Transact-SQL local variable is an object that can hold a single data value of a
specific type. Variables in batches and scripts are typically used:
As a counter either to count the number of times a loop is performed or to control how
many times the loop is performed.
To hold a data value to be tested by a control-of-flow statement.
To save a data value to be returned by a stored procedure return code or function return
value.
T-SQL Server
Declaring a Transact-SQL Variable
 Assigning a name. The name must have a single @ as the first character.
 Assigning data type and a length.
 Setting the value to NULL.

Variables have local scope and are only visible within the batch or
procedure where they are defined.
To assign a variable a value by using the set statement, A variable can also
have a value assigned by the select statement
DECLARE @EmpIDVariable INT;
set @EmpIDVariable = 122
SELECT @EmpIDVariable = MAX (BusinessEntityID)
FROM HumanResources.Employee;
GO
T-SQL Server
Control-of-Flow Language Elements
Flow-control statements from T-SQL are rather rudimentary
compared to similar commands in other modern programming
languages such as Visual Basic and C#.
 Transact-SQL statements and programming constructs:
Comments Break
CASE Statement
Continue goto
If… Else
While
WaitFor
T-SQL Server
CASE Statement
The CASE expression has two formats:
1. The simple CASE expression compares an expression to a set of simple expressions
to determine the result. -- Simple CASE expression:
CASE input_expression
WHEN when_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END

2. The searched CASE expression evaluates a set of Boolean expressions to determine


the result
-- Searched CASE expression:
CASE
WHEN Boolean_expression THEN result_expression [ ...n ]
[ ELSE else_result_expression ]
END
T-SQL Server
-- Simple CASE expression
SELECT ProductNumber,
Category = CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
T-SQL Server
-- Searched CASE expression
SELECT ProductNumber,
Name,
"Price Range" = CASE
WHEN ListPrice = 0 THEN 'Mfg item - not for resale'
WHEN ListPrice < 50 THEN 'Under $50'
WHEN ListPrice >= 50 AND ListPrice < 250 THEN 'Under $250'
WHEN ListPrice >= 250 AND ListPrice < 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber;
GO
T-SQL Server
Where can used case statement?
Use a SELECT statement with a simple CASE expression
Use a SELECT statement with a searched CASE expression
 Use CASE in an ORDER BY clause
 Use CASE in an UPDATE statement
Use CASE in a SET statement
Use CASE in a HAVING clause
T-SQL Server
IF...ELSE (Transact-SQL)
-- Syntax
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE { sql_statement | statement_block } ]

-- example
IF DATENAME(weekday, GETDATE()) IN (N'Saturday', N'Sunday')
SELECT 'Weekend';
ELSE
SELECT 'Weekday';
T-SQL Server
WHILE (Transact-SQL)
-- Syntax for SQL Server
WHILE Boolean_expression
{ sql_statement | statement_block | BREAK | CONTINUE }

-- Example :
WHILE ( SELECT AVG(ListPrice) FROM dbo.DimProduct) < $300
BEGIN
UPDATE dbo.DimProduct SET ListPrice = ListPrice * 2;
SELECT MAX ( ListPrice) FROM dbo.DimProduct
IF ( SELECT MAX (ListPrice) FROM dbo.DimProduct) > $500
BREAK;
END
T-SQL Server
WAITFOR (Transact-SQL)
Blocks the execution of a batch, stored procedure, or transaction until
either a specified time or time interval elapses, or a specified statement
modifies or returns at least one row.
-- Syntax -- example waitfor
WAITFOR BEGIN
{ WAITFOR DELAY '02:00';
DELAY 'time_to_pass' EXECUTE sp_helpdb;
| TIME 'time_to_execute' END; GO
| [ ( receive_statement ) |
(get_conversation_group_statement ) ]
[ , TIMEOUT timeout ]
}
T-SQL Server
Operators (Transact-SQL)
An operator is a symbol specifying an action that is performed on one or more
expressions. The following table lists the operator categories that SQL Server uses.
1. Arithmetic operators
7. Comparison operators
2. Relational operators
8. String Concatenation
3. Assignment operator operator
4. Scope resolution operator 9. Compound operators
5. Bitwise operators 10. Unary operators
6. Set operators (EXCEPT and INTERSECT, UNION) 11. Logical operators

You might also like