SQL Server - Bài
Implementing Data IntegrityVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftData IntegrityDefining the quality of the data in the database. Types:Domain Integrity(columns)Entity Integrity (rows)Referential Integrity(between tables)User-definedintegrity MicrosoftMicrosoftEnforcing Data IntegrityDeclarative data integrityDefined in object definitionsEnforced automatically SQL Server by using constraints, defaults, and rulesProcedural data integrityDefined in scriptEnforced by executing scripts, triggers and stored procedures MicrosoftMicrosoftOutline√Data IntegrityEnforcing Data IntegrityTypes of ConstraintsDefining ConstraintsDisabling ConstraintsUsing Defaults and RulesDeciding enforcement method to use MicrosoftMicrosoftTypes of ConstraintsPRIMARY KEY constraintsUNIQUE constraintsFOREIGN KEY constraintsCHECK constraintsDEFAULT constraintsCascading referential integrity MicrosoftMicrosoftPRIMARY KEY ConstraintsOnly one PRIMARY KEY constraint per tableUnique valuesNot allowed NULL valuesEnforced with unique indexUSE NorthwindALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED (CustomerID) MicrosoftMicrosoftUNIQUE ConstraintsDefined with one or more columnsAllowing one null valueAllowing multiple UNIQUE constraints on a tableEnforced with a unique IndexUSE NorthwindALTER TABLE dbo.Suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName) MicrosoftMicrosoftFOREIGN KEY ConstraintsSingle or multi-column referential integrityMust reference a PRIMARY KEY or UNIQUE constraintMust have SELECT or REFERENCES permissions on referenced tables Use only REFERENCES clause within same tableUSE NorthwindALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID) MicrosoftMicrosoftCHECK ConstraintsUsed with INSERT or UPDATE StatementsDefined on one or more column(s) in the same tableCannotBe used with the rowversion data typeContain subqueries USE NorthwindALTER TABLE dbo.EmployeesADD CONSTRAINT CK_birthdateCHECK (BirthDate > '01-01-1900' AND BirthDate <getdate()) MicrosoftMicrosoftDEFAULT ConstraintsApplied for INSERT statementsOnly one DEFAULT constraint per columnCannot be used with IDENTITY propertyor rowversion data typeAllowing some system-supplied valuesUSE NorthwindALTER TABLE dbo.CustomersADD CONSTRAINT DF_contactname DEFAULT 'UNKNOWN' FOR ContactName [...]... regioncode_rule,'Customers.Region' Microsoft Note: CREATE RULE may be not supported anymore in next version of SQL Server Enforcement Method to Use Data integrity components Functionality Performance costs Before or after modification Constraints Medium Low Before Defaults and rules Low Low Before Triggers High Medium-High After Data types, Null/Not Null Low Low Microsoft Before Summary Data integrity as means... FK_Employees_Employees Microsoft Default Constraints Defined as independent objects Bound to one or more columns or user-defined data types CREATE DEFAULT [schema_name.] default_name AS constant_expression [ ; ] EXEC sp_bindefault , CREATE DEFAULT phone_no_default AS '(000)00 0-0 000' GO EXEC sp_bindefault phone_no_default, 'Customers.Phone' Microsoft Rule Constraints CREATE RULE [schema_name.]... {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] [ON UPDATE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] Microsoft Considerations for using constraints Can be changed without recreating a table Require error-checking in applications and transactions Verify existing data? Microsoft If not case, disabling constraints Disabling Constraints Disabling constraint checking on existing data Using WITH NOCHECK . tables)User-definedintegrity MicrosoftMicrosoftEnforcing Data IntegrityDeclarative data integrityDefined in object definitionsEnforced automatically SQL Server. TABLE dbo.EmployeesADD CONSTRAINT CK_birthdateCHECK (BirthDate > '0 1-0 1-1 90 0' AND BirthDate <getdate()) MicrosoftMicrosoftDEFAULT ConstraintsApplied