Download Introduction to Database System

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction to Database System
Chapter 7 :
Data Integrity
Adisak Intana
Lecturer
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.
Introduction to Database System
2
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)
Introduction to Database System
3
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)
Introduction to Database System
4
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
Introduction to Database System
5
Type of Integrity
Example : Referential Integrity
Student
Teacher
Introduction to Database System
6
Type of Integrity
Example : Nullify
Student
NULL
NULL
Teacher
Introduction to Database System
7
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.
Introduction to Database System
8
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.
Introduction to Database System
9
The IDENTITY Property
(Cont.)
SQL Server Enterprise Manager
CREATE TABLE mytable (
col1 int not null IDENTITY(1,100),
col2 char(5) not null )
Introduction to Database System
10
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
Introduction to Database System
11
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 )
Introduction to Database System
12
DEFAULT Constraints
Example
ALTER TABLE defaulttab1 ADD
moneycol money NULL CONSTRAINT df_moneycol DEFAULT $2.00,
CONSTRAINT df_anumber DEFAULT 100 FOR anumber
Introduction to Database System
13
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)
Introduction to Database System
14
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]’))
Introduction to Database System
15
CHECK Constraints
Example
ALTER TABLE checktable
ADD CONSTRAINT ck_col2 CHECK (col2 like ‘H%’)
Introduction to Database System
16
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] ]
Introduction to Database System
17
Primary-Key Constraints
Example
CREATE TABLE pktable (
col1 int not null CONSTRAINT pk_col1 PRIMARY KEY,
col1 char(5) null)
Introduction to Database System
18
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.
Introduction to Database System
19
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.
Introduction to Database System
20
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.
–
Introduction to Database System
21
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])]
Introduction to Database System
22
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
Introduction to Database System
23
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
Introduction to Database System
result : …………….
24
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.
Introduction to Database System
25
Related documents