Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.

Similar presentations


Presentation on theme: "Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity."— Presentation transcript:

1 Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity

2 Introduction to Database System 2 How to Enforce Data Integrity When you examine data integrity, you are trying to ensure that the data in your database is correct, both of a literal standpoint (Without errors) and from a business standpoint.

3 Introduction to Database System 3 Type of Integrity Domain Integrity - is simply the set of valid values for a particular column - Data types help determine what values are valid for a particular column (example : integer, smallint, tinyint)

4 Introduction to Database System 4 Type of Integrity Entity Integrity - You can uniquely identify every row in table - You can do this with a unique index or with declarative integrity (primary-key or unique constraint)

5 Introduction to Database System 5 Type of Integrity Referential Integrity - refers to the maintenance of relationship between data rows in multiple tables. - SQL Server only provides what’s known as “delete restrict” enforcement - Another type of integrity enforcement is known as “delete cascade” integrity

6 Introduction to Database System 6 Type of Integrity Example : Referential Integrity Student Teacher

7 Introduction to Database System 7 Type of Integrity Example : Nullify Student Teacher NULL

8 Introduction to Database System 8 The IDENTITY Property o Syntax IDENTITY[(seed, increment)] - Seed is the value identifying the stating value of the identity column. - Increment is the value that identifies the amount of change (expressed as an integer) that will occur between each value. - If you don’t specify a seed or increment, they each default to 1.

9 Introduction to Database System 9 The IDENTITY Property (Cont.) You can use the IDENTITY property only if the column is an integer. Tinyint Smallint Integer Numeric Decimal – You can use numeric and decimal only if they have a scale of 0 (such as numeric (12,0)). – It must also not allow nulls. Example CREATE TABLE mytable ( col1 int not null IDENTITY(1,100), col2 char(5) not null ) result in a value of 1 for col1 when the first row is added, then 1, then 101, and so on.

10 Introduction to Database System 10 The IDENTITY Property (Cont.) SQL Server Enterprise Manager CREATE TABLE mytable ( col1 int not null IDENTITY(1,100), col2 char(5) not null )

11 Introduction to Database System 11 DEFAULT Constraints  Default constraints are used to enforce domain integrity, during inserts only.  You cannot apply default constraints to columns that are also identity columns.  Syntax Column level [CONSTRAINT constraint_name] DEFAULT {constant_expression} Table level [CONSTRAINT constraint_name] DEFAULT {constant_expression} FOR col_name

12 Introduction to Database System 12 DEFAULT Constraints there is no difference between table-level and column-level – column-level constraints used during a create table. – table-level or column-level constraints used during an alter table. Example CREATE TABLE defaulttab1 ( intcol int NOT NULL CONSTRAINT df_intcol DEFAULT 0, char5col char(5) NOT NULL DEFAULT ‘Hello’, anumber numeric(10,0) NOT NULL )

13 Introduction to Database System 13 DEFAULT Constraints Example ALTER TABLE defaulttab1 ADD moneycol money NULL CONSTRAINT df_moneycol DEFAULT $2.00, CONSTRAINT df_anumber DEFAULT 100 FOR anumber

14 Introduction to Database System 14 CHECK Constraints  Check constraints are used to enforce domain integrity  Check constraints checked during inserts and updates.  Check constraints can refer to other columns as part of their enforcement of conditions (only with table-level constraints)  Syntax Column level [CONSTRAINT constraint_name] CHECK (expression) Table level [CONSTRAINT constraint_name] CHECK (expression)

15 Introduction to Database System 15 CHECK Constraints Example CREATE TABLE checktable ( col1 int not null CONSTRAINT ck_col1 CHECK (col1 between 1 and 100), col2 char(5) null, zip_code char(5) null, col4 int not null, CONSTRAINT ck_col4 CHECK (col4 > col1), CONSTRAINT ck_zip_code CHECK (zip_code like ‘[0-9][0-9][0-9] [0-9][0-9]’))

16 Introduction to Database System 16 CHECK Constraints Example ALTER TABLE checktable ADD CONSTRAINT ck_col2 CHECK (col2 like ‘H%’)

17 Introduction to Database System 17 Primary-Key Constraints  Syntax Column level: [CONSTRAINT constraint_name] [ PRIMARY KEY [ CLUSTERED | NONCLUSTERED] [ WITH [FILLFACTOR = fillfactor] ] Table level: [CONSTRAINT constraint_name] [ PRIMARY KEY [ CLUSTERED | NONCLUSTERED] { ( column[,...n] ) } [ WITH [FILLFACTOR = fillfactor] ]

18 Introduction to Database System 18 Primary-Key Constraints Example CREATE TABLE pktable ( col1 int not null CONSTRAINT pk_col1 PRIMARY KEY, col1 char(5) null)

19 Introduction to Database System 19 Unique Constraints You can use UNIQUE constraints to ensure that no duplicate values are entered in specific columns that do not participate in a primary key.

20 Introduction to Database System 20 Unique Constraints Syntax Column level [CONSTRAINT constraint_name] [ UNIQUE [ CLUSTERED | NONCLUSTERED] [ WITH [FILLFACTOR = fillfactor] ] Table level [CONSTRAINT constraint_name] [ UNIQUE [ CLUSTERED | NONCLUSTERED] { ( column[,...n] ) } [ WITH [FILLFACTOR = fillfactor] ] UNIQUE creates the unique index.

21 Introduction to Database System 21 Unique Constraints Example CREATE TABLE myuniquetable ( col1 int not null CONSTRAINT pk_myuniquetable PRIMARY KEY, col2 char(20) NOT NULL CONSTRAINT u_myuniquetable UNIQUE ) – This creates a primary key as well as a unique constraint. – Both are unique indexes on table myuniquetable.

22 Introduction to Database System 22 Foreign Key Constraints  Syntax Column level: [CONSTRAINT constraint_name] [FOREIGN KEY] REFERENCES ref_table [ ( ref_column ) ] Table level: [CONSTRAINT constraint_name] FOREIGN KEY [(column[,...n])] REFERENCES ref_table [(ref_column[,...n])]

23 Introduction to Database System 23 Foreign-Key Constraints Example CREATE TABLE emp ( emp_id int not null CONSTRAINT pk_emp PRIMARY KEY, emp_name char(30) not null ) Go CREATE TABLE orders ( order_id int not null CONSTRAINT pk_order PRIMARY KEY, emp_id int not null CONSTRAINT fk_order FOREIGN KEY REFERENCES emp (emp_id) ) Go

24 Introduction to Database System 24 Foreign-Key Constraints Example INSERT emp VALUES (1, ‘Joe Smith’) result : …………… INSERT emp VALUES (2, ‘Ann Jones’) result : …………… INSERT orders VALUES (1,1) result :……………. INSERT orders VALUES (2,2) result :……………. INSERT orders VALUES (3,3) result : ……………. DELETE emp WHERE emp_id = 1 result : …………….

25 Introduction to Database System 25 Dropping Constraints You can drop a constraint with the ALTER TABLE statement. Example ALTER TABLE emp_manager DROP CONSTRAINT fk_emp_mgr – Success However, if you try to drop a primary-key constraint (or unique constraint) that still has foreign-key references, you will not be able to do so. Example ALTER TABLE emp_manager DROP CONSTRAINT pk_emp_mgr - Error: try to drop the primary-key constraint without having dropped the foreign key.


Download ppt "Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity."

Similar presentations


Ads by Google