When to use Transactions in SQL Server

后端 未结 4 1851
南笙
南笙 2021-01-30 11:11

There are lots and lots of questions on HOW to use Transactions. What I want to know is WHEN? Under what circumstances? What types of queries? Can Try-Catch blo

4条回答
  •  醉酒成梦
    2021-01-30 11:29

    Before concluding this section on Data Manipulation Language commands there are two further commands, which are very useful.

    Changes made to the database by INSERT, UPDATE and DELETE commands are temporary until explicitly committed. This is performed by the command:

    COMMIT;

    On execution of this command all changes to the database made by you are made permanent and cannot be undone.

    A COMMIT is automatically executed when you exit normally from SQL*Plus. However, it does no harm to occasionally issue a COMMIT command.

    A COMMIT does not apply to any SELECT commands as there is nothing to commit.

    A COMMIT does not apply to any DDL commands (eg CREATE TABLE, CREATE INDEX, etc). These are automatically committed and cannot be rolled back.

    If you wished to rollback (ie undo) any changes made to the database since the last commit, you can issue the command:

    ROLLBACK;

    A group of related SQL commands that all have to complete successfully or otherwise be rolled back, is called a transaction. Part of your research for Outcome 3 includes investigating transaction processing and the implications of rollback and commit.

    Furthermore, during the process of inserts, updates and deletes, the RDBMS needs to preserve the integrity of the database and allow multi-user access (ie concurrency). A transaction that has not yet been committed needs to be transparent to users. For example, an uncommitted insert should not be accessible to another user. Also, two users trying to update the same record should not impede each other. This needs to be managed. Concurrency is managed by the RDBMS using locking strategies. Part of your research for Outcome 3 includes investigating locking strategies covering column, row field and table locks.

提交回复
热议问题