Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,440 --> 00:00:04,260
In this lesson, we're going
to look at check constraints.
2
00:00:04,260 --> 00:00:07,950
So a check constraint is another
type of database constraint
3
00:00:07,950 --> 00:00:10,980
that's used to enforce
data integrity, which
4
00:00:10,980 --> 00:00:14,140
ensures that data conforms
to a business model.
5
00:00:14,140 --> 00:00:16,290
So there is a lot
of different ways
6
00:00:16,290 --> 00:00:20,100
that data integrity can
be done or performed.
7
00:00:20,100 --> 00:00:22,170
There's considerable
argument, and has
8
00:00:22,170 --> 00:00:24,510
been for many years,
about whether or 0
9
00:00:24,510 --> 00:00:27,810
it's better to check data
integrity at the application
10
00:00:27,810 --> 00:00:29,980
layer or at the database layer.
11
00:00:29,980 --> 00:00:32,850
And so usually, a
balance is struck
12
00:00:32,850 --> 00:00:36,420
and things like primary key,
foreign key relationships
13
00:00:36,420 --> 00:00:38,040
are done in the database.
14
00:00:38,040 --> 00:00:42,150
And then certain data checking
occurs in an application.
15
00:00:42,150 --> 00:00:44,670
But that doesn't have to
be the case because we
16
00:00:44,670 --> 00:00:47,250
can use check
constraints in a database
17
00:00:47,250 --> 00:00:52,210
to check data before it
goes into a row and a table.
18
00:00:52,210 --> 00:00:54,930
So let's take a form
entry for example.
19
00:00:54,930 --> 00:00:57,750
So this is just a
typical internet form
20
00:00:57,750 --> 00:01:00,540
where you've gone to a website
and you want to sign up.
21
00:01:00,540 --> 00:01:05,040
And let's say they put a
field in there for your age,
22
00:01:05,040 --> 00:01:06,960
for you to enter your age.
23
00:01:06,960 --> 00:01:08,820
Now some degree
of checking should
24
00:01:08,820 --> 00:01:13,410
occur to ensure that the
value that's put in for age
25
00:01:13,410 --> 00:01:16,830
is greater than zero,
not a negative number,
26
00:01:16,830 --> 00:01:19,780
is not a letter, or
something like that.
27
00:01:19,780 --> 00:01:22,380
And so that can be done
with a check constraint.
28
00:01:22,380 --> 00:01:25,290
So when that data
is put into the form
29
00:01:25,290 --> 00:01:30,390
and it's passed back to usually
a PL/SQL package or procedure
30
00:01:30,390 --> 00:01:34,140
to insert the data, then we
attempt to put that data in.
31
00:01:34,140 --> 00:01:37,050
And if it doesn't conform
to the check constraint,
32
00:01:37,050 --> 00:01:40,410
then an error is returned
and the row is not inserted.
33
00:01:40,410 --> 00:01:43,590
So a check constraint is going
to allow a check on the data
34
00:01:43,590 --> 00:01:45,090
before it goes in.
35
00:01:45,090 --> 00:01:46,680
Like other types
of constraints, it
36
00:01:46,680 --> 00:01:51,360
can be done in line, out
of line, or after creation.
37
00:01:51,360 --> 00:01:57,190
So let's take a look at
adding check constraints.
38
00:01:57,190 --> 00:02:00,560
So I'm going to connect
to Scott and I'm
39
00:02:00,560 --> 00:02:03,560
going to create
a hospital table.
40
00:02:03,560 --> 00:02:05,290
Call it hospital_il
because we're
41
00:02:05,290 --> 00:02:08,980
going to use an inline check
constraint in the example.
42
00:02:08,980 --> 00:02:15,350
Hospital_id,
hospital_description,
43
00:02:15,350 --> 00:02:16,730
hospital_code.
44
00:02:19,260 --> 00:02:23,430
And this code is going to be
basically a location code.
45
00:02:23,430 --> 00:02:26,850
So we can add an inline
check constraint here
46
00:02:26,850 --> 00:02:30,570
that checks and makes sure that
that value that's entered in
47
00:02:30,570 --> 00:02:32,520
is within a certain set.
48
00:02:32,520 --> 00:02:37,830
So we could say, check
hospital_code in IL
49
00:02:37,830 --> 00:02:42,730
for Illinois, MO for
Missouri, or KS for Kansas.
50
00:02:42,730 --> 00:02:45,450
And watch your parentheses
here, because we
51
00:02:45,450 --> 00:02:48,390
have an outer one on
the check constraint
52
00:02:48,390 --> 00:02:50,940
and then another
set of parentheses
53
00:02:50,940 --> 00:02:54,420
to define the set of values.
54
00:02:54,420 --> 00:02:58,380
Then there's the final one
that defines the create table
55
00:02:58,380 --> 00:03:00,070
statement itself.
56
00:03:00,070 --> 00:03:02,850
Notice that SQL Developer kind
of helps us out with this.
57
00:03:02,850 --> 00:03:05,250
Whenever we highlight
a parentheses
58
00:03:05,250 --> 00:03:07,200
it shows us the pairing.
59
00:03:07,200 --> 00:03:09,120
Let's try it again.
60
00:03:09,120 --> 00:03:11,240
And hospital_il is created.
61
00:03:13,940 --> 00:03:17,150
So that's an example of an
inline check constraint.
62
00:03:17,150 --> 00:03:20,800
So it is defined as a
part of the column itself.
63
00:03:20,800 --> 00:03:22,730
So let's look at an
out of line constraint
64
00:03:22,730 --> 00:03:25,550
and how the syntax
for that works.
65
00:03:25,550 --> 00:03:33,950
Just grab all of this,
my table definition.
66
00:03:33,950 --> 00:03:36,470
Add a comma here.
67
00:03:36,470 --> 00:03:41,250
Change this to ool
for out of line.
68
00:03:41,250 --> 00:03:45,540
And then we use the keyword
constraint [INAUDIBLE] code
69
00:03:45,540 --> 00:03:50,410
for hospital code,
ool_ck for check,
70
00:03:50,410 --> 00:03:52,680
and the keyword for
the constraint check.
71
00:03:52,680 --> 00:03:56,010
Now again, this code_ool_ck
is just the name
72
00:03:56,010 --> 00:03:59,430
that we're giving
to the constraint.
73
00:03:59,430 --> 00:03:59,930
In.
74
00:04:06,640 --> 00:04:08,980
So this is an out
of line constraint.
75
00:04:08,980 --> 00:04:11,090
So we have given it a name here.
76
00:04:11,090 --> 00:04:14,200
And notice that we can't
give a name to a constraint
77
00:04:14,200 --> 00:04:15,520
when it's used in line.
78
00:04:15,520 --> 00:04:18,220
So that's one of the advantages
of either doing it out
79
00:04:18,220 --> 00:04:20,770
of line or after its creation.
80
00:04:20,770 --> 00:04:23,020
But again, it is checking
for hospital_code
81
00:04:23,020 --> 00:04:26,350
to be in one of
those three values.
82
00:04:26,350 --> 00:04:29,150
And it created successfully.
83
00:04:29,150 --> 00:04:31,190
The last type of check
constraint that we can add
84
00:04:31,190 --> 00:04:33,620
would be after creation.
85
00:04:33,620 --> 00:04:37,150
So let's go ahead and
create this table,
86
00:04:37,150 --> 00:04:41,310
call it ac for after creation.
87
00:04:41,310 --> 00:04:44,590
And now we've created it
without the check constraint.
88
00:04:44,590 --> 00:04:54,290
So to check, we put altar table,
ac, add constraint, code_ac_ck.
89
00:04:56,900 --> 00:05:04,820
Check that hospital_code
is in this set of values.
90
00:05:07,850 --> 00:05:12,030
And table is altered, so that
check constraint is put in.
91
00:05:12,030 --> 00:05:14,190
So let's try inserting
data here and let's
92
00:05:14,190 --> 00:05:17,120
see if we can violate
the constraint.
93
00:05:17,120 --> 00:05:21,850
Insert into hospital_ac values.
94
00:05:21,850 --> 00:05:29,450
We have a hospital ID, hospital
description, and the code.
95
00:05:29,450 --> 00:05:32,140
So let's attempt to put in KY.
96
00:05:35,270 --> 00:05:37,330
It gives us an error,
check constraint,
97
00:05:37,330 --> 00:05:39,400
and gives us the name, violated.
98
00:05:39,400 --> 00:05:43,470
The values being inserted do
not satisfy the named check.
99
00:05:43,470 --> 00:05:45,700
And then tells us,
do not insert values
100
00:05:45,700 --> 00:05:47,110
that violate the constraint.
101
00:05:47,110 --> 00:05:49,660
So let's say this
was a mistake, it
102
00:05:49,660 --> 00:05:52,060
was meant to be KS for Kansas.
103
00:05:52,060 --> 00:05:55,130
Clear this out for clarity.
104
00:05:55,130 --> 00:05:56,330
And the row inserts.
105
00:05:56,330 --> 00:05:57,050
Why?
106
00:05:57,050 --> 00:05:58,850
Because it meets the
check constraint.
107
00:05:58,850 --> 00:06:03,240
It can be any of these three
values, but no other value.
108
00:06:03,240 --> 00:06:04,790
So that's another
example of how we
109
00:06:04,790 --> 00:06:07,670
can use a constraint, in
this case a check constraint,
110
00:06:07,670 --> 00:06:12,130
to enforce business rules and
to enforce data integrity.
8833
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.