Professional Documents
Culture Documents
About SQL
SQL stands for Structured Query Language.
SQL is a language that enables us to work with a database. Using SQL, we can insert records, update
records, and delete records. We can also create new database objects such as databases and tables.
And we can drop (delete the!.Although SQL is an A"S# (A!erican "ational Standards #nstitute
standard, there are !any different $ersions of SQL. %ifferent database $endors ha$e their own
$ariations of the language.
SQL Server Data Types
Character strings:
Data type Description Storage
char(n &i'ed(length character string. )a'i!u! *,+++ characters n
$archar(n ,ariable(length character string. )a'i!u! *,+++ characters
$archar(!a' ,ariable(length character string. )a'i!u! -,+./,.0-,*10 characters
te't ,ariable(length character string. )a'i!u! 123 of te't data
Unicode strings:
Data type Description Storage
nchar(n &i'ed(length Unicode data. )a'i!u! 0,+++ characters
n$archar(n ,ariable(length Unicode data. )a'i!u! 0,+++ characters
n$archar(!a' ,ariable(length Unicode data. )a'i!u! 4/5,*.+,6-1 characters
nte't ,ariable(length Unicode data. )a'i!u! 123 of te't data
Binary types:
Data type Description Storage
bit Allows +, -, or "ULL
binary(n &i'ed(length binary data. )a'i!u! *,+++ bytes
$arbinary(n ,ariable(length binary data. )a'i!u! *,+++ bytes
$arbinary(!a' ,ariable(length binary data. )a'i!u! 123
i!age ,ariable(length binary data. )a'i!u! 123
Number types:
Data type Description Storage
tinyint Allows whole nu!bers fro! + to 144 - byte
s!allint Allows whole nu!bers between (/1,.5* and /1,.5. 1 bytes
int Allows whole nu!bers between (1,-0.,0*/,50* and 1,-0.,0*/,50. 0 bytes
bigint Allows whole nu!bers between (6,11/,/.1,+/5,*40,..4,*+* and
6,11/,/.1,+/5,*40,..4,*+.
* bytes
deci!al(p,s &i'ed precision and scale nu!bers.
Allows nu!bers fro! (-+7/* 8- to -+7/* 9-.
:he p para!eter indicates the !a'i!u! total nu!ber of digits that can
be stored (both to the left and to the right of the deci!al point. p !ust
be a $alue fro! - to /*. %efault is -*.
:he s para!eter indicates the !a'i!u! nu!ber of digits stored to the
right of the deci!al point. s !ust be a $alue fro! + to p. %efault $alue
is +
4(-.
bytes
nu!eric(p,s &i'ed precision and scale nu!bers.
Allows nu!bers fro! (-+7/* 8- to -+7/* 9-.
:he p para!eter indicates the !a'i!u! total nu!ber of digits that can
be stored (both to the left and to the right of the deci!al point. p !ust
be a $alue fro! - to /*. %efault is -*.
:he s para!eter indicates the !a'i!u! nu!ber of digits stored to the
right of the deci!al point. s !ust be a $alue fro! + to p. %efault $alue
is +
4(-.
bytes
s!all!oney )onetary data fro! (1-0,.0*./50* to 1-0,.0*./50. 0 bytes
!oney )onetary data fro! (611,//.,1+/,5*4,0...4*+* to
611,//.,1+/,5*4,0...4*+.
* bytes
float(n &loating precision nu!ber data fro! (-..6; 8 /+* to -..6; 8 /+*.
:he n para!eter indicates whether the field should hold 0 or * bytes.
float(10 holds a 0(byte field and float(4/ holds an *(byte field.
%efault $alue of n is 4/.
0 or *
bytes
real &loating precision nu!ber data fro! (/.0+; 8 /* to /.0+; 8 /* 0 bytes
Date types:
Data type Description Storage
dateti!e &ro! <anuary -, -.4/ to %ece!ber /-, 6666 with an accuracy of /.//
!illiseconds
* bytes
dateti!e1 &ro! <anuary -, +++- to %ece!ber /-, 6666 with an accuracy of -++
nanoseconds
5(* bytes
s!alldateti!e &ro! <anuary -, -6++ to <une 5, 1+.6 with an accuracy of - !inute 0 bytes
date Store a date only. &ro! <anuary -, +++- to %ece!ber /-, 6666 / bytes
ti!e Store a ti!e only to an accuracy of -++ nanoseconds /(4 bytes
dateti!eoffset :he sa!e as dateti!e1 with the addition of a ti!e =one offset *(-+
bytes
ti!esta!p Stores a uni>ue nu!ber that gets updated e$ery ti!e a row gets created
or !odified. :he ti!esta!p $alue is based upon an internal clock and
does not correspond to real ti!e. ;ach table !ay ha$e only one
ti!esta!p $ariable
Other data types:
Data type Description
s>l?$ariant Stores up to *,+++ bytes of data of $arious data types, e'cept te't, nte't, and
ti!esta!p
uni>ueidentifier Stores a globally uni>ue identifier (2U#%
'!l Stores @)L for!atted data. )a'i!u! 123
cursor Stores a reference to a cursor used for database operations
table Stores a result(set for later processing
SQL Create
SQL has a nu!ber of AB;A:; co!!ands that enable you to progra!atically create database objects
including the database, its tables and !ore.
Cere are the AB;A:; co!!ands supported by SQL Ser$erD
AB;A:; AA:#E"
AB;A:; AAAC;
AB;A:; A;LL AALAULA:#E"
AB;A:; AU3;
AB;A:; %A:A3AS;
AB;A:; %;&AUL:
AB;A:; &U"A:#E"
AB;A:; #"%;@
AB;A:; );)3;B
AB;A:; )#"#"2 )E%;L
AB;A:; FBEA;%UB;
AB;A:; BUL;
AB;A:; SAC;)A
AB;A:; S;:
AB;A:; S:A:#S:#AS
AB;A:; :A3L;
AB;A:; :B#22;B
AB;A:; U"#QU; ALUS:;B;% #"%;@
AB;A:; ,#;W
SQL Create Database
Gou can create a database using the AB;A:; %A:A3AS; co!!and.
SQL syntax
CREATE DATABASE database_name
Example Code
CREATE DATABASE Payroll
Adding Arguments
:here are a nu!ber of optional argu!ents that you can supply with the AB;A:; %A:A3AS;
co!!and. Gou should check your database syste!Hs docu!entation for the specific argu!ents
supported and their usage, but hereHs an e'a!ple of supplying argu!ents when creating a database
using )icrosoftHs SQL Ser$er.
Example Code
#n this e'a!ple, we are supplying the na!e and location of the databaseHs data file and transaction
log. We are also specifying the initial si=e of these files (with the S#I; argu!ent, the !a'i!u! si=e
it can grow to (with the )A@S#I; argu!ent and the growth incre!ent of each file (using the
&#L;2BEW:C argu!ent.
USE master
GO
CREATE DATABASE Payroll
ON
( NAME = Payroll_dat,
!"ENAME = #$%&'ro(ram )*les&m*$roso)t s+l ser,er&mss+l&data&'ayrolldat-md)#,
S!.E = /0MB,
MA1S!.E = 20MB,
!"EGRO3T4 = 5MB 6
"OG ON
( NAME = #Payroll_lo(#,
!"ENAME = #$%&'ro(ram )*les&m*$roso)t s+l ser,er&mss+l&data&'ayroll-ld)#,
S!.E = 70MB,
MA1S!.E = 80MB,
!"EGRO3T4 = 5MB 6
GO
SQL Create Table
Gou create a table using the AB;A:; :A3L; co!!and.
SQL syntax
CREATE TAB"E table_name
($ol9mn_name_7 dataty'e,
$ol9mn_name_/ dataty'e,
---
6
Example
CREATE TAB"E !nd*,*d9al
(!nd*,*d9al!d *nt,
*rstName :ar$;ar(/556,
"astName :ar$;ar(/556,
UserName C;ar(706
6
Result
:his results in an e!pty table. Gou can now use an #"S;B: state!ent to add data to the table.
IndiidualId !irstName LastName UserName
SQL "lter Table
We will !odify the table using the AL:;B :A3L; co!!and.
Add a Column
SQL syntax
A"TER TAB"E table_name
ADD $ol9mn_name dataty'e
Example SQL Statement
A"TER TAB"E !nd*,*d9al
ADD a(e *nt
Change the Datatype
SQL syntax
A"TER TAB"E table_name
A"TER CO"UMN $ol9mn_name dataty'e
Example SQL Statement
A"TER TAB"E !nd*,*d9al
A"TER CO"UMN a(e n9mer*$
Drop a Column
H%roppingH a colu!n !eans re!o$ing or deleting that colu!n.
SQL syntax
A"TER TAB"E table_name
DROP CO"UMN $ol9mn_name
Example SQL Statement
A"TER TAB"E !nd*,*d9al
DROP CO"UMN a(e
SQL Constraints
Aonstraints are used to li!it the type of data that can go into a table.
Aonstraints can be specified when a table is created (with the AB;A:; :A3L; state!ent or after
the table is created (with the AL:;B :A3L; state!ent.We will focus on the following constraintsD
"E: "ULL
U"#QU;
FB#)ABG J;G
&EB;#2" J;G
AC;AJ
%;&AUL:
SQL NOT NULL Constraint
:he "E: "ULL constraint enforces a colu!n to "E: accept "ULL $alues.:he "E: "ULL
constraint enforces a field to always contain a $alue. :his !eans that you cannot insert a new record,
or update a record without adding a $alue to this field.:he following SQL enforces the KF?#dK
colu!n and the KLast"a!eK colu!n to not accept "ULL $aluesD
AB;A:; :A3L; Fersons
(
F?#d int "E: "ULL,
Last"a!e $archar(144 "E: "ULL,
&irst"a!e $archar(144,
Address $archar(144,
Aity $archar(144
:o allow na!ing of a U"#QU; constraint, and for defining a U"#QU; constraint on !ultiple
colu!ns, use the following SQL synta'D
AB;A:; :A3L; Fersons
(
F?#d int "E: "ULL,
Last"a!e $archar(144 "E: "ULL,
&irst"a!e $archar(144,
Address $archar(144,
Aity $archar(144,
AE"S:BA#": uc?Ferson#% U"#QU; (F?#d,Last"a!e
:o allow na!ing of a FB#)ABG J;G constraint, and for defining a FB#)ABG J;G constraint on
!ultiple colu!ns, use the following SQL synta'D
AB;A:; :A3L; Fersons
(
F?#d int "E: "ULL,
Last"a!e $archar(144 "E: "ULL,
&irst"a!e $archar(144,
Address $archar(144,
Aity $archar(144,
AE"S:BA#": pk?Ferson#% FB#)ABG J;G (F?#d,Last"a!e
:o allow na!ing of a &EB;#2" J;G constraint, and for defining a &EB;#2" J;G constraint on
!ultiple colu!ns, use the following SQL synta'D
AB;A:; :A3L; Erders
(
E?#d int "E: "ULL,
Erder"o int "E: "ULL,
F?#d int,
FB#)ABG J;G (E?#d,
AE"S:BA#": fk?FerErders &EB;#2" J;G (F?#d
B;&;B;"A;S Fersons(F?#d
:o allow na!ing of a AC;AJ constraint, and for defining a AC;AJ constraint on !ultiple colu!ns,
use the following SQL synta'D
AB;A:; :A3L; Fersons
(
F?#d int "E: "ULL,
Last"a!e $archar(144 "E: "ULL,
&irst"a!e $archar(144,
Address $archar(144,
Aity $archar(144,
AE"S:BA#": chk?Ferson AC;AJ (F?#dL+ A"% AityMHSandnesH
:he %;&AUL: constraint can also be used to insert syste! $alues, by using functions like
2;:%A:;(D
AB;A:; :A3L; Erders
(
E?#d int "E: "ULL,
Erder"o int "E: "ULL,
F?#d int,
Erder%ate date %;&AUL: 2;:%A:;(