Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Vu Tuyet Trinh
trinhvt-fit@mail.hut.edu.vn
Data Integrity
User-defined integrity
Defined in object definitions Enforced automatically SQL Server by using constraints, defaults, and rules
Microsoft
Outline
Types of Constraints Defining Constraints Disabling Constraints Using Defaults and Rules
Microsoft
Types of Constraints
PRIMARY KEY constraints UNIQUE constraints FOREIGN KEY constraints CHECK constraints DEFAULT constraints Cascading referential integrity
Microsoft
Only one PRIMARY KEY constraint per table Unique values Not allowed NULL values Enforced with unique index
USE Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT PK_Customers PRIMARY KEY NONCLUSTERED (CustomerID)
Microsoft
UNIQUE Constraints
Defined with one or more columns Allowing one null value Allowing multiple UNIQUE constraints on a table Enforced with a unique Index
USE Northwind ALTER TABLE dbo.Suppliers ADD CONSTRAINT U_CompanyName UNIQUE NONCLUSTERED (CompanyName)
Microsoft
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 table
USE Northwind ALTER TABLE dbo.Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (CustomerID) REFERENCES dbo.Customers(CustomerID)
Microsoft
CHECK 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 Northwind ALTER TABLE dbo.Employees ADD CONSTRAINT CK_birthdate CHECK (BirthDate > '01-01-1900' AND BirthDate <getdate())
Microsoft
DEFAULT Constraints
Applied for INSERT statements Only one DEFAULT constraint per column Cannot be used with IDENTITY property or rowversion data type Allowing some system-supplied values
USE Northwind ALTER TABLE dbo.Customers ADD CONSTRAINT DF_contactname DEFAULT 'UNKNOWN' FOR ContactName
Microsoft
Defined for DETELE or UPDATE a key to which existing foreign keys point
[ON DELETE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}] [ON UPDATE {NO ACTION|CASCADE|SET NULL|SET DEFAULT}]
Microsoft
Can be changed without recreating a table Require error-checking in applications and transactions Verify existing data?
Microsoft
Disabling Constraints
Using WITH NOCHECK option when adding a new constraint Applied to CHECK and FOREIGN KEY constraints
USE Northwind ALTER TABLE dbo.Employees WITH NOCHECK ADD CONSTRAINT FK_Employees_Employees FOREIGN KEY (ReportsTo) REFERENCES dbo.Employees(EmployeeID)
Microsoft
Disabling Constraints
Using NOCHECK option before loading new data Applied to CHECK and FOREIGN KEY Constraints
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 <default_name>, <Object_name> CREATE DEFAULT phone_no_default AS '(000)000-0000' GO EXEC sp_bindefault phone_no_default, 'Customers.Phone'
Microsoft
Rule Constraints
CREATE RULE [schema_name.] rule_name AS <condition_expression> GO EXEC sp_bindrule regioncode_rule,'Customers.Region'
CREATE RULE regioncode_rule AS @regioncode IN ('IA', 'IL', 'KS', 'MO') GO EXEC sp_bindrule regioncode_rule,'Customers.Region'
Microsoft
Note: CREATE RULE may be not supported anymore in next version of SQL Server
Low
High Low
Low
Before
Microsoft
Summary
Data integrity as means for defining the correctness and the completeness of data
Enforcing data integrity as means for maintaining complete and correct database Using data integrity, using enforcement method for data integrity are important but only when necessary
Microsoft
Microsoft
Microsoft