The index and triggers in SQL

First, the index

1.1 index type

Clustered index

Physical order of rows in the table with the logical sequence of the same key

  1. Primary key index: automatically create a primary key index to define a primary key, primary key index is a special type of unique index
  2. It requires a primary key index value for each primary key is unique and can not be null

Non-clustered index

Non-clustered index specified logical order table

  1. Data stored in one location, the index is stored in another location, the index contains pointers to data storage locations
  2. There may be multiple, less than 249
  3. The only index: unique index does not allow two have the same index value

1.2 Creating an index

CREATE [UNIQUE][CLUSTERED][NONCLUSTERED] INDEX 索引名称 ON 表名称

FILLFACTOR fill factor: a specified value between 1-100, indicates the percentage of index pages filled

1.3 Delete Index

DROP INDEX 表名称,索引名

When you delete a table, all indexes of the table will also be deleted

The advantages of index 1.4

  1. Create a unique index to ensure the uniqueness of each row of data in a database table.

  2. Greatly accelerate the speed of data retrieval, which is the main reason for the index is created.

  3. Accelerating the connection between the table and the table, especially in reference to particular interest for data integrity aspects.

  4. When using clause grouping and sorting data retrieval, it can be reduced similarly grouped and sorted query time.

    5. By using the index, may be used in the optimization process hidden query, improve system performance

Shortcoming 1.5 index

  1. Creating indexes and index maintenance takes time, this time with the increase in the amount of data increases.

  2. The index needs to occupy physical space, in addition to the data table space outside the accounting data, each index also accounted for a certain physical space, if you want to build a clustered index, the space needs will be greater.

3. When the data in the table to add, delete and modify, the index also dynamically maintain, reducing maintenance speed data

Second, the flip-flop

2.1 Overview of Triggers

A trigger is a special type of stored procedures, triggers automatically effective when the data changes specified in the table. It is closely connected with the table can be seen as part of the table definition. Triggers can not be called directly by name, but not allowed to set the parameters. In SQL Server, a table can have multiple triggers. The user can use the INSERT, UPDATE or DELETE statement to set the flip-flop, flip-flops may be provided for a plurality of operations on a particular table. Triggers can include complex Transact-SQL statements. No matter how complicated operations performed by the trigger, the trigger is executed only as a separate unit, is considered a transaction. If an error occurs during the execution of a trigger, the entire transaction will automatically be rolled back.

2.2 advantage of the flip-flop

Advantage of the flip-flop in the following areas:

  1. Trigger automatically, after the data in the table to modify the trigger is activated immediately.

  2. In order to implement complex database update trigger may invoke one or more stored procedures, can be accomplished even by calling the appropriate external process (not the database management system itself)

  3. Triggers can achieve more complex than a CHECK constraint data integrity constraints. In the database, in order to achieve data integrity constraints, CHECK constraints or triggers may be used. CHECK constraints are not allowed to reference other columns in the table to complete the inspection work, and triggers can reference other columns in the table. It is more suitable for large constraint data integrity of the database management system

  4. Triggers can detect the operation within the database, thereby eliminating the unauthorized operation of the database update, make modifications to the database update operations safer, more stable operation of the database

  5. It can trigger cascading changes to related tables in the database. A trigger is based on a table is created, but can operate for more than one table in the database to achieve a cascade of changes related tables

    6. A table 3 can exist simultaneously trigger different operations (INSERT, UPDATE, and DELETE)

2.3 The role of the trigger

Its main role is to trigger enables referential integrity and data consistency complicated by the primary and foreign keys can not be guaranteed, it is possible to modify the cascade correlation table in the database, increase the CHECK constraints is more complicated than the data integrity, and custom error messages. The main role of the main triggers of the following access areas:

  1. Enforce referential integrity between databases
  2. Modify all relevant cascade database tables automatically trigger other operations associated
  3. Track changes, cancel or roll back the illegal operation to prevent illegal modification data
  4. Returns an error message custom constraint can not return information, and can trigger
  5. Triggers can call stored procedures more

2.4 Classification trigger

SqlServer include three general types of triggers: DML triggers, DDL triggers, and logon triggers.

1.DML (Data Manipulation Language, Data Manipulation Language) triggers

DML triggers are attached on a particular table or view of the operation code, to perform these operations when the event data manipulation language database server. SqlServer in DML triggers, there are three:

  1. Trigger insert: is activated when data is inserted into the table;
  2. delete trigger: triggered when data is deleted from the table;
  3. update trigger: is triggered when you modify data in a table.

When you encounter the following should be considered DML triggers:

  1. Cascading changes through related tables in the database
  2. To prevent malicious or erroneous insert, update and delete operations, and enforce other restrictions more complex check constraints defined limits.
  3. Before and after evaluation of the data changes to the table state, and before going to the measures based on this difference.

2.DDL (Data Definition Language, Data Definition Language) triggers

DDL triggers a data definition language occurs when the server or database (mainly create, drop, alter the beginning of the sentence) when an event is activated using, use DDL triggers to prevent certain changes or record data in the data schema change or event action.

3. logon trigger

Login LOGIN event trigger will respond to stimulate the stored procedure. This event is raised when a user session is established with the SQL Server instance. And the user logon trigger will actually stimulate session prior to the establishment after login authentication phase is completed. Therefore, all messages (such as error messages and messages from the PRINT statement) from the internal triggers and generally will arrive at the user's will be sent to the SQL Server error log. If the authentication fails, login will not fire the trigger.

Works 2.5 Trigger

When the trigger fires:

  1. The system automatically deleted or inserted tables are created in memory;
  2. Read-only, can not be modified after the trigger execution is completed, automatically deleted.

inserted表:

  1. Temporarily save the rows after the insert or update;
  2. Can be inserted to check whether the data from the inserted table business needs;
  3. If not, an error message report is sent to the user, and rolls back insert.

deleted table:

  1. Temporarily keeps a record of deleted or updated row before;
  2. You can check data is deleted from the deleted table meets business needs;
  3. If not, the error message is reported to the user, and roll back the insert operation.

inserted and deleted Control Table:

Modify the operating record inserted表 deleted Table
Increase (insert) Record The new record store
Delete (deleted) Record Store deleted records
Modify (update) record After the update record storage Storing records before the update

Here Insert Picture Description

2.6 Creating DML Triggers

If the user through data manipulation language (DML) event edit data, perform DML triggers. DML events are INSERT against a table or view, UPDATE, or DELETE statement

CREATE TRIGGER Cou_Trig
ON Course AFTER INSERT AS
INSERT INTO Teacher VALUES('08','zhangsan')

The meaning of this is that when the trigger table Course INSERT making changes, automatically activates the trigger, automatically execute the statement following the block AS

CREATE TRIGGER Cou_TrigDele ON Course
AFTER DELETE AS
DELETE Teacher WHERE Tname='zhangsan'

. 1 .DML triggers are attached on a particular table or view of the operation code, to perform these operations when the event data manipulation language database server.

SqlServer in DML triggers, there are three:

  1. Trigger insert: is activated when data is inserted into the table;

  2. delete trigger: triggered when data is deleted from the table;

  3. update trigger: is triggered when you modify data in a table.

2. DDL (Data Definition Language Language) Triggers

DDL triggers a data definition language occurs when the server or database (mainly create, drop, alter the beginning of the sentence) when an event is activated using, use DDL triggers to prevent certain changes or record data in the data schema change or event action.

3. logon trigger

Login LOGIN event trigger will respond to stimulate the stored procedure. This event is raised when a user session is established with the SQL Server instance. And the user logon trigger will actually stimulate session prior to the establishment after login authentication phase is completed. Therefore, all messages (such as error messages and messages from the PRINT statement) from the internal triggers and generally will arrive at the user's will be sent to the SQL Server error log. If the authentication fails, the login trigger will not fire

Published 125 original articles · won praise 267 · views 20000 +

Guess you like

Origin blog.csdn.net/chonbi/article/details/104856078