Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.

Similar presentations


Presentation on theme: "Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh."— Presentation transcript:

1 Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh

2 Constraints  Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules, and defaults, constraints, and triggers.  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.

3 Check Constraints  Check constraints restrict the data acceptable in the field even if the data belongs to the correct data type.  Use a check constraint in a Transact-SQL statement linked to a field.

4 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.

5 Default 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 Unique Constraints  You should use a unique constraint when you need to ensure that no duplicate values can be added to a field.

7 Example - Constraints  Adds a new column with a CHECKconstraint ALTER TABLE Staff ADD gender char(1) CONSTRAINT g_const check (gender in ('M','F'));  Adding a DEFAULT constraint to an existing column ALTER TABLE Staff ADD CONSTRAINT g_def DEFAULT ‘M’ FOR gender; Slide 28 (of 82)

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.

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.

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  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.

14 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.

15


Download ppt "Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh."

Similar presentations


Ads by Google