SQLDEV320A WEEK8-1
SQLDEV320A WEEK8-1
TODAY
• ASSIGNMENT 7 REVIEW
VIEWS
TRIGGERS
TRANSACTIONS AND
CONCURRENCY
LOCK OBJECTS
Trigger Types
• FOR | AFTER trigger
Database
• INSTEAD OF trigger
DMLTriggers
Triggers INSERT, UPDATE or DELETE statement to a table or
view
DDL Triggers
CREATE, ALTER, DROP, GRANT DENY, REVOKE,
UPDATE
What are
triggers
A trigger is a special
kind of stored
procedure that
automatically executes
when a table event
occurs in the database
server.
Enforce Enforce business logic
DML trigger
DML trigger
Overrides the standard action of the
Trigger Types triggering statement: an INSERT,
UPDATE or DELETE
-- 3 Test trigger
INSERT Table1 ([Description]) VALUES ('Test 1');
-- 4 Show results
SELECT *
FROM Table1
AFTER Trigger Example:
USE
[AdventureWorks2019]
GO
CREATE TRIGGER
trg_OrderDetailNotDiscontinued
AS ON [Sales].
-- Prevent for discontinued
[SalesOrderDetail] AFTER
orders IF UPDATE
INSERT, items
EXISTS (
SELECT 1
FROM Production.Product
JOIN INSERTED p
i i.productid = p.productid
ON
WHERE p.DiscontinuedDate < GETDATE()
)
BEGI
N
RAISERROR('Order Item is discontinued. Transaction
Failed.',16,1);
ROLLBACK TRAN;
INSERTED • Inserted and Deleted virtual tables are
and created for the trigger
• Each trigger has its own Inserted and
DELETED Deleted tables
• For every new set of values in a row,
virtual there's a row in the
tables • Inserted virtual table
• For every old set of values in a row,
there's a row in the Deleted virtual
table
• An update generates an Inserted and
Deleted row for every single update.
More on virt
ual tables
Using INSERTED and DELETED virtual
tables
USE
[test]
GO
DDLTrigger:
Logon Trigger:
1
Databases have Data files
(.mdf
minimum two or .ndf)
operating system Log files
files (.ldf)
SQL Server
File Data files contain data and
objects such as tables, indexes,
Architectur stored procedures, and views.
e
Log files contain the
information that is required to
recover all transactions in the
database.
SQL Server Data File
Architecture
A data file A data page SQL Server All pages Uniform Mixed
(.mdf is 8KB in reads data are stored Extent – extent –
or .ndf) is size pages in in extents owned by a shared by
logically groups of 8 single up to 8
divided in pages, or object objects
pages extents
https://technet.microsoft.com/en-us/library/ms190969(v=sql.105).aspx
SQL Server Data File Architecture
Page Type Contents
Data Data rows with all data except for large
objects
Index Index entries
Text/Image Large objects – text, image, varchar(max)
Global Allocation Map Information on whether extents are allocated
Page Free Space Information on page allocation and free space
Index Allocation Map Info on extents used by a table or index
Bulk Changed Map Info on extents modified since last BACKUP
LOG
Differential Changed Info on extents that have changed since
Map last BACKUPDATABASE
SQL Server Table Architecture
A table is contained in one or more partitions
28
• Dirty reads: These occur while a transaction is updating
a row, and a second transaction reads the row before the
first transaction is committed. If the original update rolls
back, the uncommitted changes will be read by the
Transaction
second transaction, even though they are never
committed to the database.
Concurrenc
• Phantom reads: These occur when a transaction
issues two reads, and between the two reads the
underlying data is updated with data being
inserted or deleted. This causes the results of
y: Issues
each query to differ. Rows returned in one query
that do not appear in the other are called
phantom-rows.
Levels
locks are used during a query, and data
READCOMMITTED cannot be modified by other processes
(default) while the query is retrieving thedata.
controls how and when REPEATABLE READ When enabled, dirty and non-repeatable reads are not
the changes made by allowed. This is achieved by placing
shared locks on all read resources.
one transaction are
visible to other SERIALIZABLE Most restrictive setting. Range locks are placed on the
Key Lock(KEY)
Locks nodes on an index
35
Query Hints
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SELECT *
FROM Sales.SalesOrderDetail sod
WITH (NOLOCK)
JOIN
Sales.SalesOrderHeader soh
ON sod.SalesOrderID =
soh.SalesOrderID
ORDER BY sod.ModifiedDate;
SQL server uses locking and row
versioning to prevent users from
reading uncommitted data and
prevent multiple users from
attempting to change the same
data at the same time.
COMMIT TRANSACTION
Collect Deadlock Information
sp_lock
sp_who2
@@Error returns 1205 SQLProfiler
Locks\Lock:Deadlock
Locks\Lock:Deadlock chain
QUIZ 8 CODING
ASSIGNMENT