SQL 5
SQL 5
Basics”
Submodule “Advanced Databases and SQL
Querying”
Yaroslav Dobrianskyi
Lead Software Engineer
3 DURATION: 3 HOURS
SCENARIO 1:
• YOU PUT CREDIT CARD INTO ATM RECEIVER TO GET 100$
• YOU HAVE ENTERED PIN AND SELECTED TO TAKE 100$ CASH
• WHEN YOU WAITING TO GET THE MONEY, THE ATM CRASHED
SCENARIO 1:
• YOU PUT CREDIT CARD INTO ATM RECEIVER TO GET 100$
• YOU HAVE ENTERED PIN AND SELECTED TO TAKE 100$ CASH
• WHEN YOU WAITING TO GET THE MONEY, THE ATM CRASHED
SCENARIO 2:
• YOU ARE MAKING AN ORDER AT ONLINE STORE MARKET
• YOU ENTERED YOU CREDIT CARD INFORMATION AND CLICK «PAY»
• SODDENLY INTERNET CONNECTION HAS BEEM LOST
TRANSACTION
IS ALL
OR NONE
• Isolation: every transaction has a well-defined boundary; • Durability: data modifications that occur within a
that is, it is isolated from another transaction. One successful transaction are kept permanently within the
transaction shouldn't affect other transactions running at system regardless of what else occurs
the same time.
Example:
Example:
Example:
Example:
BEGIN TRAN
SAVE TRAN SP1
DELETE FROM HumanResources.JobCandidate
WHERE JobCandidateID = 12
• Lost update – when one data block is simultaneously changed by different transactions, all changes are lost except for
the last one;
• "Dirty" read – reading data added or modified by a transaction, which will not be confirmed (rolled back);
• Non-repeatable read – when re-reading within one transaction, previously read data is changed;
• Phantom reads – one transaction during its execution several times selects many rows according to the same criteria.
Another transaction, in the intervals between these selections, adds rows or changes columns. As a result, it turns out
that the same samples in the first transaction give different sets of rows.
Here,
1.T1 reads the value of A.
2.T1 updates the value of A in the buffer.
3.T2 reads the value of A from the buffer.
4.T2 writes the updated the value of A.
5.T2 commits.
6.T1 fails in later stages and rolls back.
In this example,
•T2 reads the dirty value of A written by the uncommitted
transaction T1.
•T1 fails in later stages and roll backs.
•Thus, the value that T2 read now stands to be incorrect.
Here,
1.T1 reads the value of X (= 10 say).
2.T2 reads the value of X (= 10).
3.T1 updates the value of X (from 10 to 15 say) in the
buffer.
4.T2 again reads the value of X (but = 15).
In this example,
•T2 gets to read a different value of X in its second
reading.
•T2 wonders how the value of X got changed because
according to it, it is running in isolation.
Here,
1.T1 reads the value of A (= 10 say).
2.T2 updates the value to A (= 15 say) in the buffer.
3.T2 does blind write A = 25 (write without read) in the
buffer.
4.T2 commits.
5.When T1 commits, it writes A = 25 in the database.
In this example,
•T1 writes the over written value of X in the database.
•Thus, update from T1 gets lost.
Here,
1.T1 reads X.
2.T2 reads X.
3.T1 deletes X.
4.T2 tries reading X but does not find it.
In this example,
•T2 finds that there does not exist any variable X when it
tries reading X again.
•T2 wonders who deleted the variable X because
according to it, it is running in isolation.
READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.
READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.
READ COMITTED is the default isolation level for SQL Server. This prevents
Dirty Read. When this level is set, the transactions can not read the data that is
being modified by the current transaction. This will force user to wait for the
current transaction to finish up its job. But the problem with this level that is
doesn’t resolve Phantom reads or Inconsistency Analysis – as it asks User A to
wait for Read but not for update or insert.
READ UNCOMMITTED is the lowest isolation level . With this isolation level,
we allow a transaction to read the data which is being updated by other
transaction and not yet committed and resulting a dirty reads.
READ COMITTED is the default isolation level for SQL Server. This prevents
Dirty Read. When this level is set, the transactions can not read the data that is
being modified by the current transaction. This will force user to wait for the
current transaction to finish up its job. But the problem with this level that is
doesn’t resolve Phantom reads or Inconsistency Analysis – as it asks User A to
wait for Read but not for update or insert.
Read
phenomena
Non-repeatable
Dirty reads Lost updates Phantoms
reads
Isolation level
Read
- - - -
Uncommitted
Read
+ - - -
Committed
Repeatable
+ + + -
Read
Snapshot + + + +
Serializable + + + +
• Error handling in SQL Server gives us control over the Transact-SQL code
• For example, when things go wrong, we get a chance to do something about it and possibly make it right
again. SQL Server error handling can be as simple as just logging that something happened, or it could
be us trying to fix an error
• This global variable returns the error number for the last Transact-SQL statement executed
@@error = 0 – success
@@error = 1 – fail
• Generates an error message and initiates error processing for the session. RAISERROR can
either reference a user-defined message stored in the sys.messages catalog view or build a
message dynamically
• 16 – severity, 1 - state
Syntax
BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
{ sql_statement | statement_block }
END CATCH
Now, inside the CATCH statement, we can try to fix the error, report the error or even log the
error, so we know when it happened, who did it by logging the username, all the useful stuff.
We even have access to some special data only available inside the CATCH statement:
BEGIN TRY
SELECT
1 / 0 AS Error;
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_STATE() AS ErrorState,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
GO
Ranking functions return a ranking value for each row in a partition. Depending on the function that is used, some rows might receive the same value
as other rows. Ranking functions are nondeterministic.
Transact-SQL provides the following ranking functions:
• RANK
• NTILE
• DENSE_RANK
• ROW_NUMBER
Syntax:
RANKING FUNCTION ( ) OVER ( [ partition_by_clause ] order_by_clause )
• XML stands for eXtensible Markup Language. WHAT FOR? - XML Simplifies Things
• XML was designed to store and transport data. • It simplifies data sharing
• XML is a markup language much like HTML. • It simplifies data transport
• XML was designed to be self-descriptive. • It simplifies platform changes
• Many computer systems contain data in incompatible formats.
Exchanging data between incompatible systems (or upgraded systems) is
a time-consuming task for web developers. Large amounts of data must
The Difference Between XML and HTML
be converted, and incompatible data is often lost.
• XML was designed to carry data - with focus on
• XML stores data in plain text format. This provides a software- and
what data is
hardware-independent way of storing, transporting, and sharing data.
• HTML was designed to display data - with focus on
• XML also makes it easier to expand or upgrade to new operating systems,
how data looks
new applications, or new browsers, without losing data.
• XML tags are not predefined like HTML tags are
• With XML, data can be available to all kinds of "reading machines" like
people, computers, voice machines, news feeds, etc.
Microsoft SQL Server supports system stored procedures that are used for SYNTAX:
sp_xml_preparedocument
XML text management. hdoc OUTPUT
[ , xmltext ]
sp_xml_preparedocument reads the XML text provided as input, parses the
text by using the MSXML parser (Msxmlsql.dll), and provides the parsed hdoc (int) - handle to the newly created
document.
document in a state ready for consumption. This parsed document is a tree [ xmltext ] - original XML document.
representation of the various nodes in the XML document: elements,
attributes, text, comments, and so on. SYNTAX:
sp_xml_removedocument
A parsed document is stored in the internal cache of SQL Server.
hdoc
sp_xml_removedocument removes the internal representation of the XML
hdoc (int) - handle to the newly created document.
document specified by the document handle and invalidates the document
handle.
SYNTAX:
-- CREATE TABLE TO STORE XML
OPENROWSET ( { 'provider_name' , { 'datasource' ;
IF OBJECT_ID(N'dbo.TESTXMLwithOpenXML', N'U') IS null 'user_id' ; 'password' | 'provider_string' } , {
<table_or_view> | 'query' } | BULK 'data_file' , {
CREATE TABLE TESTXMLwithOpenXML ( FORMATFILE = 'format_file_path' [ <bulk_options> ] |
SINGLE_BLOB | SINGLE_CLOB | SINGLE_NCLOB } } )
Id INT IDENTITY PRIMARY KEY,
XMLData XML,
CReatedDateTime DATETIME);
SYNTAX:
OPENROWSET (BULK 'path_to_file',
-- INSERT XML DATA FROM FILE SINGLE_BLOB)
.
INSERT INTO TESTXMLwithOpenXML(XMLData, CreatedDateTime)
SELECT CONVERT(XML, BulkColumn) AS BulkColumn, GETDATE()
FROM OPENROWSET(BULK 'C:\books.xml', SINGLE_BLOB) AS x;
SELECT @x = B
--SELECT @x
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
What we need?
• Create filegroups and add files to them
• Create partition function
• Create partition scheme
What is a Filegroup?
A filegroup is a logical structure to group objects in a database.
Don’t confuse filegroups with actual files (.mdf, .ddf, .ndf, .ldf, etc.). You can have multiple filegroups per database. One filegroup will be the
primary. Then, you add additional filegroups. In a filegroup, you can have multiple files.
Function maps the rows of a table or index Scheme maps the partitions of a
in the current database into partitions partitioned table or index to filegroups in
based on the values of a specified column. current database.
SYNTAX:
SYNTAX:
CREATE PARTITION FUNCTION partition_function_name ( CREATE PARTITION SCHEME partition_scheme_name AS PARTITION
input_parameter_type ) AS RANGE [ LEFT | RIGHT ] FOR VALUES ( [ partition_function_name
boundary_value [ ,...n ] ] ) [ ; ] TO ( { file_group_name} , {…} )