HOTSPOT
Note: This question is part of a series of questions that use the same scenario. For your convenience, the
scenario is repeated in each question. Each question presents a different goal and answer choices, but the text
of the scenario is exactly the same in each question in this series.
You have a database that contains the following tables: BlogCategory, BlogEntry, ProductReview, Product, and
SalesPerson. The tables were created using the following Transact SQL statements:
You must modify the ProductReview Table to meet the following requirements:
1. The table must reference the ProductID column in the Product table
2. Existing records in the ProductReview table must not be validated with the Product table.
3. Deleting records in the Product table must not be allowed if records are referenced by the ProductReview
table.
4. Changes to records in the Product table must propagate to the ProductReview table.
You also have the following databse tables: Order, ProductTypes, and SalesHistory, The transact-SQL
statements for these tables are not available.
You must modify the Orders table to meet the following requirements:
1. Create new rows in the table without granting INSERT permissions to the table.
2. Notify the sales person who places an order whether or not the order was completed.
You must add the following constraints to the SalesHistory table:
– a constraint on the SaleID column that allows the field to be used as a record identifier
– a constant that uses the ProductID column to reference the Product column of the ProductTypes table
– a constraint on the CategoryID column that allows one row with a null value in the column
– a constraint that limits the SalePrice column to values greater than four
Finance department users must be able to retrieve data from the SalesHistory table for sales persons wherethe value of the SalesYTD column is above a certain threshold.
You plan to create a memory-optimized table named SalesOrder. The table must meet the following
requirments:
– The table must hold 10 million unique sales orders.
– The table must use checkpoints to minimize I/O operations and must not use transaction logging.
– Data loss is acceptable.
Performance for queries against the SalesOrder table that use Where clauses with exact equality operations
must be optimized.
You need to create a stored procedure named spDeleteCategory to delete records in the database. The stored
procedure must meet the following requirments:
1. Delete records in both the BlogEntry and BlogCategory tables where CategoryId equals parameter
@CategoryId.
2. Avoid locking the entire table when deleting records from the BlogCategory table.
3. If an error occurs during a delete operation on either table, all changes must be rolled back, otherwise all
changes should be committed.
How should you complete the procedure? To answer, select the appropriate Transact-SQL segments in the
answer area.
Hot Area:
Explanation:
Box 1: SET TRANSACTION ISOLATION LEVEL READ COMMITTED
You can minimize locking contention while protecting transactions from dirty reads of uncommitted data
modifications by using either of the following:
* The READ COMMITTED isolation level with the READ_COMMITTED_SNAPSHOT database option set ON.
* The SNAPSHOT isolation level.
With ROWLOCK we should use READ COMMITTEED
Box 2: ROWLOCK
Requirement: Avoid locking the entire table when deleting records from the BlogCategory table
ROWLOCK specifies that row locks are taken when page or table locks are ordinarily taken. When specified intransactions operating at the SNAPSHOT isolation level, row locks are not taken unless ROWLOCK is
combined with other table hints that require locks, such as UPDLOCK and HOLDLOCK.
Incorrect: Not TABLOCKX
TABLOCKX specifies that an exclusive lock is taken on the table.
Box 3: COMMIT
Box 4: ROLLBACKhttps://msdn.microsoft.com/en-us/library/ms187373.aspx
https://msdn.microsoft.com/en-us/library/ms187967.aspx
Correct
set implicit_transactions on
rowlock
commit
rollback
I am concerned about the 3rd requirement, that either all changes must be committed or all rolled back. There is no ‘BEGIN TRANSACTION’ statement in the answer as it stands, meaning that if the first DELETE succeeds it will automatically commit, even if there is an error in the second DELETE. This makes me think the first answer should be SET IMPLICIT_TRANSACTIONS ON.
I have set a test enviroment for this question and put a ‘delete from product;’ between delete on blogEntry and blogCategory to control when will raise error ( check constraint in the ProductReview table ).
with SET IMPLICIT_TRANSACTIONS ON … procedure is atomic… all is done or none
with SET IMPLICIT_TRANSACTIONS OFF .. procedure delete from blogEntry always, but on error in ‘delete from products;’ do not delete from blogCategory… i think this do not in compliance with item 3.
with SET TRANSACTION ISOLATION LEVAL READ COMMITTED.. works equals SET IMPLICIT TRANSACTIONS OFF
with SET TRANSACTION ISOLATION LEVEL SNAPSHOT.. works equals SET IMPLICIT TRANSACTIONS OFF
DROP PROCEDURE SpDeleteCategory;
GO
CREATE PROCEDURE SpDeleteCategory (@CategoryID int)
AS
BEGIN
SET NOCOUNT ON;
–SET IMPLICIT_TRANSACTIONS ON;
–SET IMPLICIT_TRANSACTIONS OFF; –Works but auto commit each statement
–SET TRANSACTION ISOLATION LEVEL READ COMMITTED; –Works but auto commit each statement
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRY
DELETE FROM BlogEntry WHERE Category = @CategoryID;
DELETE FROM Product;
DELETE FROM BlogCategory
WITH (
ROWLOCK
–TABLOCK
)
WHERE CategoryID = @CategoryID;
PRINT @@TRANCOUNT;
IF @@TRANCOUNT > 0
BEGIN
–BEGIN TRANSACTION;
COMMIT TRANSACTION;
END;
END TRY
BEGIN CATCH
PRINT ‘Catch’
PRINT @@TRANCOUNT;
IF @@TRANCOUNT > 0
BEGIN
–BEGIN TRANSACTION
–COMMIT TRANSACTION
ROLLBACK TRANSACTION;
END
END CATCH;
END;
is this correct!?
First Box :
SET IMPLICIT_TRANSACTIONS ON.