Você está na página 1de 10

SQL Integrity Constraints

Integrity Constraints are used to apply business rules for the database tables.
The constraints available in SQL are Foreign Key, Not Null, ni!ue, Chec", pri#ary "ey.
Constraints can be defined in two ways
$% The constraints can be specified i##ediately after the colu#n definition. This is called
colu#n&level definition.
'% The constraints can be specified after all the colu#ns are defined. This is called table&level
definition.
$% SQL Primary key:
This constraint defines a colu#n or co#bination of colu#ns (hich uni!uely identifies each ro(
in the table.
Syntax to define a Primary key at column level:
colu#n na#e datatype )C*NST+,INT constraint-na#e. /+I0,+1 K21
Syntax to define a Primary key at table level:
)C*NST+,INT constraint-na#e. /+I0,+1 K21 3colu#n-na#e$,colu#n-na#e',..%
colu#n-na#e$, colu#n-na#e' are the na#es of the colu#ns (hich define the pri#ary Key.
The synta4 (ithin the brac"et i.e. )C*NST+,INT constraint-na#e. is optional.
For 24a#ple5 To create an e#ployee table (ith /ri#ary Key constraint, the !uery (ould be li"e.
/ri#ary Key at colu#n level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%
%9
or
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% C*NST+,INT e#p-id-p" /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%
%9
/ri#ary Key at colu#n level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37%,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%,
C*NST+,INT e#p-id-p" /+I0,+1 K21 3id%
%9
/ri#ary Key at table level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37%, N*T NLL,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%,
,LT2+ T,6L2 e#ployee ,:: C*NST+,INT /K-20/L*122-I: /+I0,+1 K21 3id%
%9
'% SQL Foreign "ey or +eferential Integrity 5
This constraint identifies any colu#n referencing the /+I0,+1 K21 in another table. It
establishes a relationship bet(een t(o colu#ns in the sa#e table or bet(een different tables. For
a colu#n to be defined as a Foreign Key, it should be a defined as a /ri#ary Key in the table
(hich it is referring. *ne or #ore colu#ns can be defined as Foreign "ey.
Synta4 to define a Foreign "ey at colu#n level5
)C*NST+,INT constraint-na#e. +2F2+2NC2S +eferenced-Table-na#e3colu#n-na#e%
Synta4 to define a Foreign "ey at table level5
)C*NST+,INT constraint-na#e. F*+2I;N K213colu#n-na#e% +2F2+2NC2S
referenced-table-na#e3colu#n-na#e%9
For 24a#ple5
$% Lets use the <product< table and <order-ite#s<.
Foreign Key at colu#n level5
C+2,T2 T,6L2 product
3 product-id nu#ber37% C*NST+,INT pd-id-p" /+I0,+1 K21,
product-na#e char3'8%,
supplier-na#e char3'8%,
unit-price nu#ber3$8%
%9
C+2,T2 T,6L2 order-ite#s
3 order-id nu#ber37% C*NST+,INT od-id-p" /+I0,+1 K21,
product-id nu#ber37% C*NST+,INT pd-id-f" +2F2+2NC2S, product3product-id%,
product-na#e char3'8%,
supplier-na#e char3'8%,
unit-price nu#ber3$8%
%9
Foreign Key at table level5
C+2,T2 T,6L2 order-ite#s
3 order-id nu#ber37% ,
product-id nu#ber37%,
product-na#e char3'8%,
supplier-na#e char3'8%,
unit-price nu#ber3$8%
C*NST+,INT od-id-p" /+I0,+1 K213order-id%,
C*NST+,INT pd-id-f" F*+2I;N K213product-id% +2F2+2NC2S product3product-id%
%9
'% If the e#ployee table has a =#gr-id= i.e, #anager id as a foreign "ey (hich references pri#ary
"ey =id= (ithin the sa#e table, the !uery (ould be li"e,
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
#gr-id nu#ber37% +2F2+2NC2S e#ployee3id%,
salary nu#ber3$8%,
location char3$8%
%9
>% SQL Not Null Constraint 5
This constraint ensures all ro(s in the table contain a definite value for the colu#n (hich is
specified as not null. ?hich #eans a null value is not allo(ed.
Synta4 to define a Not Null constraint5
)C*NST+,INT constraint na#e. N*T NLL
For 24a#ple5 To create a e#ployee table (ith Null value, the !uery (ould be li"e
C+2,T2 T,6L2 e#ployee
3 id nu#ber37%,
na#e char3'8% C*NST+,INT n#-nn N*T NLL,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%
%9
@% SQL ni!ue Key5
This constraint ensures that a colu#n or a group of colu#ns in each ro( have a distinct value. ,
colu#n3s% can have a null value but the values cannot be duplicated.
Synta4 to define a ni!ue "ey at colu#n level5
)C*NST+,INT constraint-na#e. NIQ2
Synta4 to define a ni!ue "ey at table level5
)C*NST+,INT constraint-na#e. NIQ23colu#n-na#e%
For 24a#ple5 To create an e#ployee table (ith ni!ue "ey, the !uery (ould be li"e,
ni!ue Key at colu#n level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8% NIQ2
%9
or
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8% C*NST+,INT loc-un NIQ2
%9
ni!ue Key at table level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
salary nu#ber3$8%,
location char3$8%,
C*NST+,INT loc-un NIQ23location%
%9
7% SQL Chec" Constraint 5
This constraint defines a business rule on a colu#n. ,ll the ro(s #ust satisfy this rule. The
constraint can be applied for a single colu#n or a group of colu#ns.
Synta4 to define a Chec" constraint5
)C*NST+,INT constraint-na#e. CA2CK 3condition%
For 24a#ple5 In the e#ployee table to select the gender of a person, the !uery (ould be li"e
Chec" Constraint at colu#n level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
gender char3$% CA2CK 3gender in 3=0=,=F=%%,
salary nu#ber3$8%,
location char3$8%
%9
Chec" Constraint at table level5
C+2,T2 T,6L2 e#ployee
3 id nu#ber37% /+I0,+1 K21,
na#e char3'8%,
dept char3$8%,
age nu#ber3'%,
gender char3$%,
salary nu#ber3$8%,
location char3$8%,
C*NST+,INT gender-c" CA2CK 3gender in 3=0=,=F=%%
%9

Você também pode gostar