Download presentation
Presentation is loading. Please wait.
Published byMitchell McDaniel Modified over 8 years ago
1
Constraints Lesson 8
2
Skills Matrix
3
Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules, and defaults, constraints, triggers, and XML schema as applied to a column. Entity Integrity: Entity integrity applies to rows and includes rules, NULLs, defaults, constraints, and triggers. Referential Integrity: Referential integrity applies between tables or columns of the same table and includes constraints and triggers.
4
Check Constraints Use a check constraint in a Transact-SQL statement linked to a field. Check constraints restrict the data acceptable in the field even if the data belongs to the correct data type.
5
Check Constraints You can’t use default constraints in a few places, though: –Defaults can’t be used on columns with the timestamp data type. –Defaults can’t be used on IDENTITY columns. IDENTITY columns contain a number automatically incremented with each new record. –Defaults can’t be used on columns with the ROWGUIDCOL property set. ROWGUIDCOL indicates that the column includes a globally unique identifier (GUID) column for the table.
6
Default Constraints If users leave fields blank by not including them in the INSERT or UPDATE statement that they use to add or modify a record, default constraints fill in those fields. This can be a big timesaver in a data-entry department—if you use it correctly.
7
Unique Constraints You should use a unique constraint when you need to ensure that no duplicate values can be added to a field.
8
Rules Rules (CREATE RULE) store your defined constraints which can then be applied to multiple tables. Create it once and use it many times. Microsoft promises to remove support for rules in an upcoming version of SQL Server.
9
Primary Key Constraint Primary key constraint uses one or more columns in a table. –When using two or more columns, it’s called a composite primary key. –The primary key uniquely identifies a row in a table. Some considerations include:
10
Primary Key Constraint You may have but one primary key per table. The value of the column, or composite of two or more columns, must be unique. A duplicate value will be denied by the RDBMS. Null values are not allowed. Defining a primary key also automatically generates an index. SQL Server uses the index to enforce uniqueness.
11
Foreign Key Constraint A foreign key constraint relates to a column or combination of columns used to establish and enforce a link between the data in one or more tables or one or more columns in one table. Some considerations include: –The foreign key must reference a primary key or unique constraint. –A user must have REFERENCES permission on a referenced table.
12
Foreign Key Constraint –A foreign key constraint that uses only the REFERENCES clause without the FOREIGN KEY clause refers to a column in the same table. –Foreign keys do not create indexes automatically. –The data type of the foreign key and the data type of the column to which it points must match. The names do not need to match. –You may declare multiple foreign keys in a single table.
13
Summary You learned that tables are wide open to just about any kind of data when they’re first created. Users can’t violate the data type of an attribute; other than that, the table accepts anything, including the illogical. Guard against a data-entry person making an appointment for a date in the past, for example.
14
Summary To restrict the data your users can enter in a text box in the client application, you learned to create default, check, and unique constraints. –You learned rules are available but discouraged. Primary key and unique constraints guard against duplicate data entry.
15
Summary for Certification Examination Know your constraints. Understand the constraints discussed in this lesson. –Check constraints restrict the data a user may to enter in a column, even through the data type does not restrict the data. –Default constraints fill in data for you automatically when you do not specify a value while inserting a new record. –Unique constraints prevent users from accidentally inserting repetitive values. Know that SQL Server still supports rules.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.