SQL Server - Bài 9

20 471 0
SQL Server - Bài 9

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

SQL Server - Bài

Implementing Data IntegrityVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftData IntegrityDefining the quality of the data in the database. Types:Domain Integrity(columns)Entity Integrity (rows)Referential Integrity(between tables)User-definedintegrity MicrosoftMicrosoftEnforcing Data IntegrityDeclarative data integrityDefined in object definitionsEnforced automatically SQL Server by using constraints, defaults, and rulesProcedural data integrityDefined in scriptEnforced by executing scripts, triggers and stored procedures MicrosoftMicrosoftOutline√Data IntegrityEnforcing Data IntegrityTypes of ConstraintsDefining ConstraintsDisabling ConstraintsUsing Defaults and RulesDeciding enforcement method to use MicrosoftMicrosoftTypes of ConstraintsPRIMARY KEY constraintsUNIQUE constraintsFOREIGN KEY constraintsCHECK constraintsDEFAULT constraintsCascading referential integrity MicrosoftMicrosoftPRIMARY KEY ConstraintsOnly one PRIMARY KEY constraint per tableUnique valuesNot allowed NULL valuesEnforced with unique indexUSE NorthwindALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED (CustomerID) MicrosoftMicrosoftUNIQUE ConstraintsDefined with one or more columnsAllowing one null valueAllowing multiple UNIQUE constraints on a tableEnforced with a unique IndexUSE NorthwindALTER TABLE dbo.Suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName) MicrosoftMicrosoftFOREIGN KEY ConstraintsSingle or multi-column referential integrityMust reference a PRIMARY KEY or UNIQUE constraintMust 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 ConstraintsUsed with INSERT or UPDATE StatementsDefined on one or more column(s) in the same tableCannotBe used with the rowversion data typeContain subqueries USE NorthwindALTER TABLE dbo.EmployeesADD CONSTRAINT CK_birthdateCHECK (BirthDate > '01-01-1900' AND BirthDate <getdate()) MicrosoftMicrosoftDEFAULT ConstraintsApplied for INSERT statementsOnly one DEFAULT constraint per columnCannot be used with IDENTITY propertyor rowversion data typeAllowing 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 IntegrityDeclarative data integrityDefined in object definitionsEnforced automatically SQL Server. TABLE dbo.EmployeesADD CONSTRAINT CK_birthdateCHECK (BirthDate > '0 1-0 1-1 90 0' AND BirthDate <getdate()) MicrosoftMicrosoftDEFAULT ConstraintsApplied

Ngày đăng: 15/11/2012, 10:59

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan