Professional Documents
Culture Documents
INSERT Fundamentals
The INSERT statement adds one or more new rows to a table INSERT inserts data_values as one or more rows into the specified table_or_view column_list is a list of column names used to specify the columns for which data is supplied
INSERT Syntax:
INSERT [INTO] table_or_view [(column_list)] data_values
Syntax:
INSERT SomeTable OUTPUT dml_select_list INTO (@table_variable | output_table) (column_list)
Example Query:
DECLARE @MyTableVar table( ScrapReasonID smallint, Name varchar(50), ModifiedDate datetime); INSERT Production.ScrapReason OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate INTO @MyTableVar VALUES (N'Operator error', GETDATE());
DELETE Fundamentals
The DELETE statement removes one or more rows in a table or view DELETE removes rows from the table_or_view parameter that meet the search condition table_sources can be used to specify additional tables or views that can be used by the WHERE clause
DELETE FROM Sales.SalesPersonQuotaHistory WHERE SalesPersonID IN (SELECT SalesPersonID FROM Sales.SalesPerson WHERE SalesYTD > 2500000.00);
DELETE TOP (2.5) PERCENT FROM Production.ProductInventory;
You cannot use TRUNCATE TABLE on tables that are referenced by a FOREIGN KEY constraint
Syntax:
DELETE SomeTable OUTPUT column_list
Example Query:
DELETE Production.Culture OUTPUT DELETED.*; CultureID Name ModifiedDate --------------------------------------Ar Arabic 1998-06-01 En English 1998-06-01
Truncate a Table
Delete Rows Based on Other Tables Use the OUTPUT Clause with the DELETE Statement
UPDATE Fundamentals
The UPDATE statement changes data values in one, many, or all rows of a table An UPDATE statement referencing a table or view can change the data in only one base table at a time UPDATE has three major clauses:
SET comma-separated list of columns to be updated FROM supplies values for the SET clause WHERE specifies a search condition for the SET clause
UPDATE Syntax: UPDATE table_or_view SET column_name = expression FROM table_sources WHERE search_condition
Before
SalesYTD -------------677558.4653 4557045.0459
After
SalesYTD -------------721382.488 4593234.5123
Syntax:
UPDATE SomeTable OUTPUT dml_select_list FROM table_source WHERE search_condition DECLARE @NewTableVar table ( Dollars money ); UPDATE Sales.SalesPerson SET Bonus = 10000 OUTPUT INSERTED.Bonus INTO @NewTableVar; SELECT Dollars FROM @NewTableVar; Dollars -------10000.00 10000.00 ... (17 row(s) affected)
Transaction Fundamentals
A Transaction:
Is a sequence of operations performed as a single logical unit of work Exhibits the four ACID Properties
Atomicity must be an atomic unit of work Consistency - must leave all data in a consistent state Isolation - must be isolated from the modifications made by any other concurrent transactions Durability persists even after system failure
COMMIT TRANSACTION
COMMIT { TRAN | TRANSACTION } [ transaction_name | @tran_name_variable ]
COMMIT TRAN T1;
ROLLBACK TRANSACTION
ROLLBACK { TRAN | TRANSACTION } [ transaction_name | @tran_name_variable | savepoint_name | @savepoint_variable ]
Choosing a transaction isolation level does not affect the locks acquired to protect data modifications
The levels are READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SNAPSHOT, and SERIALIZABLE
Syntax
SET TRANSACTION ISOLATION LEVEL <level>;
bb
Logon information
Lab Scenario
You are a database developer at Adventure Works. You have
been asked by the senior database administrator to add, delete, and change rows on several tables in the AdventureWorks2008 database. You must be sure to follow the specifications that the senior database administrator has provided you closely so you do not insert incorrect data into the tables, change data unnecessarily, or delete vital company data.
Lab Review
Why are values for column names listed in the same order
as columns on the table when using the INSERT statement? the DELETE statement?
How can we verify that rows have been deleted after using
How can we write an UPDATE statement to ensure that all