During project development, we should be familiar with database transactions, but how can we use transactions well?
I would like to share some of the following notes with you:
1. For the business to be processed, the transaction operation time should be as short as possible. Keeping the transaction open will lock the resource for a long period of time, which will block other users from performing operatio
I have read many articles about transactions before this article. Most of them are profound and difficult for beginners to understand. Here we will use a very simple example to explain the application of transactions.
First, create a temporary table and fill in some data.
Create Table # tab (ID int, country nvarchar (10), popu INT) insert into # tab values (1, 'China', '123 ') insert into # tab values (2, 'America ', '000000') insert into # tab values (3, 'Canada', '000000') insert into # tab v
Translated from: http://blog.chinaunix.net/uid-7953959-id-2543262.htmlTransaction log files Transaction log file is a file that is used to record database updates, with an extension of ldf.In SQL Server 7.0 and SQL
Label:--LockNote:locks in SQL Server are used to control the concurrency of one transaction with another. The system automatically sets or releases the lock for the resource being accessed . If a transaction locks a resource and another transaction accesses the resource,
Transaction Log (Transaction logs) isDatabaseStructure that is very important but often ignored. Since it is not as active as the schema in the database, few people are concerned about transaction logs.Transaction logs are records of database changes. They can record any operations on the database and save the record r
Provide a sophisticated method of compressing the log and database files as follows:
1. Clear the Log
DUMP TRANSACTION Library name with NO_LOG
2. Truncate the transaction log:
BACKUP LOG
We generally think that truncate is an operation that cannot be rolled back, it deletes all the data in the table and resets the identity column.
If you do a truncate operation in a transaction, you can roll back. Conversely, it does not recover data from the log file file. It does not record the deleted data in the log file, it only records the cell allocation
insert an additional information record to the appendix table. You can also modify the data in the report and part tables on the page. The table relationship is shown in figure
The code in the Save button is as follows:
/// /// Upload an attachment storage event/// Modify by dlw 2010-07-01/// /// /// Protected void lbtnsave_click (Object sender, eventargs E){Try{Upladfile (); // This method uploads files to the server. You do not need to c
Deleting a message will cascade the reply information. At this time, we need to use the transaction, as shown in the following SQL
Copy codeThe Code is as follows: alter procedure [dbo]. [proc_tb_leaveword_delete]
(
@ LeavewordID INT,
@ Record TINYINT OUTPUT
)
AS
BEGIN
BEGIN TRY
BEGIN TRANSACTION
Delete from tb_leavewordID WHERE leavewordID = @ leavewordID
Delete
SQL Server log clearing method.Perform the following three steps in sequence in the query Analyzer. databasename is the name of your database tutorial file.1. Clear logs: dump transaction databasename WITH NO_LOG2. Truncate transaction
Copy Code code as follows:
DECLARE @name VARCHAR (25)
DECLARE @SQL VARCHAR (1000)
DECLARE @logid INT
DECLARE sysdatabase_name CURSOR for SELECT name from master.dbo.sysdatabases
OPEN Sysdatabase_name
FETCH NEXT from Sysdatabase_name into @name
While @ @FETCH_STATUS = 0
BEGIN
IF (@name not in (' xxx ')-database name that does not require log shrinkage
BEGIN
Tags: base upd src inf paid erro values CLU pointOriginal: SQL Server log file structure and error operation data retrievalI. Overview In SQL Server, there are data files. mdf and log files. ldf, the
Tags: style blog color ar sp div on log bsSelect * fromBankInsert intoBankValues(10000)Insert intoBankValues(20000)--Open a transactionbegin Tran--Defining temporary variablesDeclare @num int = 0;UpdateBankSetBalance=Balance-15000 whereId=1Set @num = @num + @ @error;--Previous statement error countUpdateBankSetBalance=Balance+15000 whereId=2Set @num = @num + @ @error;if @num 0 begin--rolling back a transactionrollback TranEndElse begin--Commit a
References:
Http://msdn.microsoft.com/en-us/library/ms187103.aspxOverview
SQL Server uses log transmission, you can automatically back up transaction logs in the "primary database" on the "primary server" instance to one or more "secondary databases" on the separate "seconda
In the query analyzer, perform the following three steps in sequence, where databasename is your database file name1. Clear logs: Dump transaction databasename with no_log
2. truncate transaction logs: backup log databasename with no_log
3. shrink the database: DBCC shrinkdatabase (databasename)
--/////////////////////
In the query analyzer, perform the following three steps in sequence, where databasename is your database file name
1. Clear logs: dump transaction databasename WITH NO_LOG
2. truncate transaction logs: backup log databasename WITH NO_LOG
3. shrink the database: dbcc shrinkdatabase (databasename)
--///////////////////
1. Delete Log
2. Clear the Log
3. Shrinking database files
4. truncating the transaction log
The performance of the database is the DBA need to focus on, the increase of log files seriously affect the performance
I have read many articles about transactions before this article. Most of them are profound and difficult for beginners to understand. Here we will use a very simple example to explain the application of transactions.
First, create a temporary table and fill in some data.
Create Table # tab (ID int, country nvarchar (10), popu INT) insert into # tab values (1, 'China', '123 ') insert into # tab values (2, 'America ', '000000') insert into # tab values (3, 'Canada', '000000') insert into # tab v
Deleting a message will cascade the reply information. At this time, we need to use the transaction, as shown in the following SQLCopy codeThe Code is as follows:Alter procedure [dbo]. [proc_tb_leaveword_delete](@ LeavewordID INT,@ Record TINYINT OUTPUT)ASBEGINBEGIN TRYBEGIN TRANSACTIONDelete from tb_leavewordID WHERE leavewordID = @ leavewordIDDelete from tb_reply WHERE leavewordID = @ leavewordIDSET @ record = 0 -- SuccessCOMMIT TRANSACTIONEND TRYBE
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.