SQL DATATYPES
SQL DATATYPES
time hh:mm:ss[.nnnnnnn]
date YYYY-MM-DD
smalldatetime YYYY-MM-DD hh:mm:ss
datetime YYYY-MM-DD
datetime2 YYYY-MM-DD
datetimeoffset hh:mm:ss[.nnn]
YYYY-MM-DD
hh:mm:ss[.nnnnnnn]
hh:mm:ss[.nnnnnnn]
[+|-]hh:mm
Range Accuracy
3 to 5
3
4
8
6 to 8
8 to 10
Data type Length
bigint 8
int 4
smallint 2
tinyint 1
bit
decimal(preci 1 bit
sion,
money scale) 8
smallmoney 4
float(n)
Description
Integer from -2^63 (-9 223 372 036 854 775 808) to 2^63-1 (9
223 372from
Integer 036 -2^31
854 775 (-2807)
147 483 648) to 2^31-1 (2 147 483 647)
Integer from -2^15 (-32 768) to 2^15-1 (32 767)
Integer from 0 to 255
Integer
Numeric 0data
Financial or 1.type
data type with
fromfixed
-2^63precision and203
(-922 337 scale
685(accuracy
477.5808) 1- to
38, 18 by
2^63-1
Numeric default
(922
Financial data
data 337 and
203
type
type from
withscale
685 0-p,
-2^31
float 0 by default).
477.5807)
(-214 with
precision, the precision
748.3648)
where tothe
n is of one
2^31-1
number(214
ten-thousandth
748.3647)
of mantis bits unit.precision
with(1-24,
the accuracyofofone ten-thousandth
7 digits, unit.and 25-
size of 4 bytes
53, accuracy of 15 digits and size of 8 bytes).
DELETE
DML
You can use
Structure willwhere clausesame
be remains to
delete specific
, but data records
will be deleted
Performance will be slow compare to truncate
Yes, we can rollback data
STID
1
2
3
4
5
6
TRUNCATE DROP
DDL
You can not
Structure willuse
bewhere clause
remains sameto DDL
delete specific
, but data records
will be deleted It will drop the structure
Performance will be fast compare to delete
Yes, we can rollback data
NAME
AAA
BBB
CCC
DDD
EEE
FFF
EMPIDNAMEMANAGERID EMPID
1NAVEE 3 1
2YAKEN 1 2
3SRINI NULL 3
4VENKA 2 4
5KAMA 2 5
EMPIDEMPLOYEEMANAGERNAME
1NAVEEN SRINIVAS
2YAKENDERNAVEEN
3SRINIVAS NULL
4VENKATESYAKENDER
5KAMAL YAKENDER
NAME MANAGERID
NAVEEN 3
YAKENDER 1
SRINIVAS NULL
VENKATESH 2
KAMAL 2
EMPID NAME SAL DEPT
1 JOHN 50000 D002
2 JANE 30000 D003
3 JIM 40000 D002
4 JILL 70000 D001
5 JOE 60000 D003
DEPTID DEPTNAME
D001 IT
D002 HR
D003 SALES
CTE
Single query (can be reused within)
WITH clause before the query
Yes (within the query)
Memory (inline processing)
No
Readability, recursion, reusable logic
INSERT, UPDATE,DELETE AND SELECT
EMPID
1
2
3
4
5
6
7
8
Temporary Table
Session or batch-level
Created with SELECT INTO or CREATE TABLE
Yes (within the session)
Disk (in tempdb database)
Yes
Complex operations, large intermediate result sets,
multiple queries
NAME
JOHN
JANE
JIM
JILL
JOE
JACK
JERRY
JENNY
SAL DEPT
50000 D002
30000 D003
40000 D002
70000 D001
60000 D003
90000 D001
80000 D003
80000 D001
ID NAME EMAIL AGE
1AAA [email protected] 30
2BBB [email protected] 35
3CCC [email protected] 28
4DDD [email protected] 32
5EEE [email protected] 38
6FFF [email protected] 26
7GGG [email protected] 40
8HHH [email protected] 38
CITY SAL
HYD 4000
BLR 6000 4 2
HYD 8000
PUN 7000 1,7
BLR 4000
PUN 3000
HYD 5000
PUN 7000
ID Name Gender DEPT
1 SRINIVAS Male IT
2 NAVEEN Male HR
3 YAKENDER Male IT
4 REDDY Male HR
5 KAMAL Male SALES
6 RAJASHEKAR Male SALES
7 KAVITHA Female IT
8 Reenu Female HR
9 SARITHA Female SALES
D Name Gender Salary Hiredate
1 SRINIVAS Male 30000 11/5/2017
2 NAVEEN Male 50000 11/5/2017
3 YAKENDER Male 40000 12/12/2017
4 REDDY Male 45000 12/15/2017
5 KAMAL Male 50000 1/12/2018
6 RAJASHEKAR Male 25000 2/26/2018
7 KAVITHA Female 30000 2/18/2018
8 Reenu Female 40000 9/19/2017
9 SARITHA Female 25000 4/4/2018
ID Name DOB
1 AAA 8/26/1994
2 BBBB 8/19/2013
3 CCC 1/9/2016
4 DDD 11/18/1982
VIEW
SIMPLE QUERY
COMPLEX QUERY
LIMITATIONS
IT WILL NOT PARAMETERS
IT WILL TEMP TABLE
IT WILL SUPPORT PROGRAMMING
FUNCTION
IT WILL SUPPORT ONLY SELECT(READING) DATA
NO INSERT
NO UPDATE
NO DELETE
IT IS NOT GOING TO CHANGE BASE TABLES
LIMITATION
IT WILL NOT SUPPORT OUTPUT PARAMETER
NO I/U/D TABLE DATA
CAN CALL SP INSIDE FN
ADV :--
WE CAN CALL UDF AS PART SELECT/UPDATE/DELETE
STORED PROCED
DML (I/U/D/S)
DDL (C/D/A)
PROGRAMMING
INSIDED SP WE CAN CALL ANOTHER SP
CAN ALSO THE FUNCTION IN SP
USE CURSORS
LIMITATIONS
EXEC/ EXECUTE <SP NAME>
CAN NOT CALL SP AS PART OF SELECT/INSERT/UPDATE/DELETE
DATE/DELETE
EMPID EmployeeName Gender Salary DeptName
1 aaa MALE 5000 IT
2 bbb FEMALE 3000 HR
3 ccc MALE 7000 IT
4 ddd FEMALE 8000 HR
5 eee MALE 4000 IT
FK PK
EmployeeNGender Salary DEPTID DEPTIDDeptName
aaa MALE 5000 1 1 IT
bbb FEMALE 3000 2 2 HR
ccc MALE 7000 1
ddd FEMALE 8000 2
eee MALE 4000 1
PK
DeptHead DeptLocation DEPTIDDeptName
JOHN LONDON 1 IT
MIKE SYDNEY 2 HR
JOHN LONDON
MIKE SYDNEY
JOHN LONDON EMPID Employee
1 aaa
2 bbb
3 ccc
4 ddd
5 eee
Gender Salary Annual salary DeptName
MALE 5000 60000 1
FEMALE 6000 72000 2
MALE 7000 84000 1
FEMALE 8000 96000 2
MALE 4000 48000 1
DeptHead DeptLocation
JOHN LONDON
MIKE SYDNEY
DeptHead DeptLocation
JOHN LONDON
MIKE SYNDEY
FK
Gender Salary DEPTID
MALE 5000 1
FEMALE 3000 2
MALE 7000 1
FEMALE 8000 2
MALE 4000 1