Detailed description of DDL triggers and index views in SQL Server

Source: Internet
Author: User

Index View:

Generally, all views are virtual tables, that is, the view itself does not store data and is a query. when accessing a view, SQL SERVER automatically accesses the base table data according to the view definition. Views with unique clustered Indexes,

The index view itself stores data, which can accelerate the query speed, but increases the overhead of data modification. Therefore, the index view applies to tables with fewer modifications and more queries. When creating an index view, the first index of the index View

It must be CLUSTERED and UNIQUE.

Create an index View:

 
 
  1. CREATE TABLE dbo.t1  
  2. (  
  3. USERID VARCHAR(50),   
  4. USERNAME VARCHAR(256)  
  5. );  
  6. go  
  7. CREATE TABLE dbo.t2  
  8. (  
  9. USERID VARCHAR(50),   
  10. DepartID VARCHAR(50)  
  11. );  
  12. GO  
  13. CREATE TABLE dbo.t3  
  14. (  
  15. DepartID VARCHAR(50),   
  16. DepartName VARCHAR(256)  
  17. );  
  18. GO  
  19. CREATE VIEW dbo.USERINFO  
  20. WITH SCHEMABINDING  
  21. AS 
  22. SELECT a.USERID, a.USERNAME, c.DEPARTID, c.DEPARTNAME   
  23. FROM dbo.t1 a, dbo.t2 b, dbo.t3 c   
  24. WHERE a.USERID = b.USERID   
  25. AND b.DEPARTID = C.DEPARTID  
  26. GO  
  27. CREATE UNIQUE CLUSTERED INDEX IX_USERINFO_USERIDDEPARTID ON dbo.USERINFO(USERID, DEPARTID) 

DDL triggers in SQL Server

DDL triggers can be executed when objects are defined, modified, or deleted within the whole database. You can control and review database objects at the database level. Or server-level triggers, such as user logon review.

DDL trigger event definition:

 
 
  1. <EVENT_INSTANCE> 
  2.     <EventType>type</EventType> 
  3.     <PostTime>date-time</PostTime> 
  4.     <SPID>spid</SPID> 
  5.     <ServerName>name</ServerName> 
  6.     <LoginName>name</LoginName> 
  7.     <UserName>name</UserName> 
  8.     <DatabaseName>name</DatabaseName> 
  9.     <SchemaName>name</SchemaName> 
  10.     <ObjectName>name</ObjectName> 
  11.     <ObjectType>type</ObjectType> 
  12.     <TSQLCommand>command</TSQLCommand> 
  13. </EVENT_INSTANCE> 

Create a DDL trigger:

 
 
  1. CREATE TABLE dbo.t4  
  2. (  
  3. USERNAME VARCHAR(256),   
  4. TSQL VARCHAR(MAX),  
  5. CDATE DATETIME  
  6. );  
  7. GO  
  8. CREATE TRIGGER tr_dbDDL  
  9. ON DATABASE   
  10. FOR   
  11. DROP_TABLE, ALTER_TABLE, CREATE_TABLE,   
  12. CREATE_PROCEDURE, ALTER_PROCEDURE, DROP_PROCEDURE  
  13. AS 
  14. DECLARE @xdata XML;  
  15. SELECT @xdata  = EVENTDATA();  
  16. ROLLBACK;  
  17. INSERT INTO dbo.t4(USERNAME, TSQL, CDATE)  
  18. SELECT @xdata.value('(/EVENT_INSTANCE/UserName)[1]', 'nvarchar(max)') AS dbUserName,   
  19. @xdata.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)') AS T_SQL,   
  20. GETDATE() AS CDATE;  
  21.  
  22. GO  
  23. use master  
  24. go  
  25.  
  26. CREATE TABLE dbo.t5  
  27. (  
  28. USERNAME VARCHAR(256),   
  29. TSQL VARCHAR(MAX),  
  30. CDATE DATETIME  
  31. );  
  32. GO  
  33. ALTER TRIGGER tr_svrddl  
  34. ON ALL SERVER  
  35. FOR   
  36. CREATE_DATABASE, ALTER_DATABASE, DROP_DATABASE,   
  37. DDL_LOGIN_EVENTS   
  38. AS 
  39. DECLARE @xdata XML;  
  40. SELECT @xdata  = EVENTDATA();  
  41.  
  42.  
  43. INSERT INTO dbo.t4(USERNAME, TSQL, CDATE)  
  44. SELECT @xdata.value('(/EVENT_INSTANCE/LoginName)[1]', 'nvarchar(max)') AS dbUserName,   
  45. @xdata.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(max)') AS T_SQL,   
  46. GETDATE() AS CDATE;  
  47. GO 

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.