All language subtitles for [SubtitleTools.com] Foreign Keys - Learning Oracle 12c [Video]

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:01,370 --> 00:00:03,660 In this lesson, we look at foreign keys. 2 00:00:03,660 --> 00:00:06,050 So foreign keys, like primary keys, 3 00:00:06,050 --> 00:00:08,630 are used to enforce data integrity. 4 00:00:08,630 --> 00:00:11,270 They are what we call database constraints. 5 00:00:11,270 --> 00:00:14,780 And they ensure that data conforms to a business model. 6 00:00:14,780 --> 00:00:17,360 So in the parent-child relationship 7 00:00:17,360 --> 00:00:23,060 of data in an RDBMS, the foreign key is the child in that. 8 00:00:23,060 --> 00:00:26,940 So where the primary key is the parent value, 9 00:00:26,940 --> 00:00:30,410 the foreign key value is the child, if you will. 10 00:00:30,410 --> 00:00:34,220 So foreign keys are similar to primary keys in their addition 11 00:00:34,220 --> 00:00:35,450 or creation. 12 00:00:35,450 --> 00:00:39,470 They can be added out of line or after creation. 13 00:00:39,470 --> 00:00:41,670 They can't be done inline. 14 00:00:41,670 --> 00:00:44,810 So let's look at creating some tables with foreign keys that 15 00:00:44,810 --> 00:00:49,450 refer back to other tables with primary key values. 16 00:00:49,450 --> 00:00:52,590 I'm going to connect to the Scott user. 17 00:00:52,590 --> 00:00:56,650 And let's do an out of line foreign key constraint 18 00:00:56,650 --> 00:00:58,510 in a table. 19 00:00:58,510 --> 00:01:06,880 patient_ool for out of line, patient_id column, 20 00:01:06,880 --> 00:01:13,340 patient_name, and doctor_id. 21 00:01:13,340 --> 00:01:16,320 This is the doctor of the patient. 22 00:01:16,320 --> 00:01:20,050 And here we use the keyword constraint. 23 00:01:20,050 --> 00:01:21,470 And we're going to give it a name. 24 00:01:21,470 --> 00:01:23,600 We can do this with primary keys as well 25 00:01:23,600 --> 00:01:25,870 when we do them out of line. 26 00:01:25,870 --> 00:01:28,250 Call it fk for foreign key. 27 00:01:28,250 --> 00:01:31,550 And then the keyword FOREIGN KEY. 28 00:01:31,550 --> 00:01:36,350 Then the column, which is doctor_id, and the keyword 29 00:01:36,350 --> 00:01:38,940 REFERENCES. 30 00:01:38,940 --> 00:01:42,500 Table that has the primary key value. 31 00:01:42,500 --> 00:01:47,480 And the column in that table. 32 00:01:47,480 --> 00:01:47,980 All right. 33 00:01:47,980 --> 00:01:48,980 So there's a lot here. 34 00:01:48,980 --> 00:01:51,160 So let's go back through it and kind of break it 35 00:01:51,160 --> 00:01:52,450 down a little bit. 36 00:01:52,450 --> 00:01:56,860 So up to this point, this is a typical create table statement. 37 00:01:56,860 --> 00:02:01,250 However, we wanted to create a table that has a foreign key. 38 00:02:01,250 --> 00:02:04,930 So when you create a foreign key, you create the constraint. 39 00:02:04,930 --> 00:02:08,260 But you have to specify where the parent is. 40 00:02:08,260 --> 00:02:10,330 So where is the parent value? 41 00:02:10,330 --> 00:02:12,700 Now, you don't have to do this in a primary key, 42 00:02:12,700 --> 00:02:16,770 because a primary key can exist without a foreign key. 43 00:02:16,770 --> 00:02:19,330 But a foreign key needs a primary key 44 00:02:19,330 --> 00:02:20,980 to reference back to. 45 00:02:20,980 --> 00:02:22,780 So that's the same as saying, you 46 00:02:22,780 --> 00:02:24,970 can have parents without children, 47 00:02:24,970 --> 00:02:27,460 but not children without parents. 48 00:02:27,460 --> 00:02:29,410 So we use the keyword constraint. 49 00:02:29,410 --> 00:02:31,240 And then we've given it a name here. 50 00:02:31,240 --> 00:02:33,220 If we don't give it a name, it just 51 00:02:33,220 --> 00:02:35,740 gets a system-generated name. 52 00:02:35,740 --> 00:02:37,810 The keyword FOREIGN KEY. 53 00:02:37,810 --> 00:02:41,880 And this is the column in the patient table, 54 00:02:41,880 --> 00:02:44,280 so it's doctor_id. 55 00:02:44,280 --> 00:02:49,380 And then it references the doctor out of line table 56 00:02:49,380 --> 00:02:51,540 that we've already created. 57 00:02:51,540 --> 00:02:56,240 And the column that it has a primary key on is doctor_id. 58 00:02:56,240 --> 00:02:58,100 So you can see they're named the same. 59 00:02:58,100 --> 00:03:00,330 We've named the columns the same. 60 00:03:00,330 --> 00:03:03,350 And this is a very common way to construct 61 00:03:03,350 --> 00:03:07,070 the names of your primary key, foreign key relationships, 62 00:03:07,070 --> 00:03:10,610 because it makes it more evident when you look at a data model 63 00:03:10,610 --> 00:03:13,970 itself to say, OK, these are common columns, 64 00:03:13,970 --> 00:03:16,910 because that's what we join on in an RDBMS, 65 00:03:16,910 --> 00:03:19,910 common columns that have common values. 66 00:03:19,910 --> 00:03:22,280 In this relationship, let's say a doctor 67 00:03:22,280 --> 00:03:23,840 can have many patients. 68 00:03:23,840 --> 00:03:25,910 But a patient in our case for this example 69 00:03:25,910 --> 00:03:27,740 would only have one doctor. 70 00:03:27,740 --> 00:03:32,930 And so that doctor_id is back in the doctor out of line table. 71 00:03:32,930 --> 00:03:37,080 So that's creating an out of line foreign key constraint. 72 00:03:37,080 --> 00:03:39,590 So let's try one after creation. 73 00:03:39,590 --> 00:03:42,250 Look at the syntax for that. 74 00:03:42,250 --> 00:03:48,310 Grab all of this for the sake of not retyping. 75 00:03:48,310 --> 00:03:51,210 Change this to patient_ac for after creation. 76 00:03:54,030 --> 00:03:56,520 So now we just have a table that doesn't 77 00:03:56,520 --> 00:03:58,920 have a relationship to any other table, 78 00:03:58,920 --> 00:04:00,270 no foreign key constraint. 79 00:04:00,270 --> 00:04:02,550 But let's say we want to add one. 80 00:04:02,550 --> 00:04:08,700 So let's use the alter table patient_ac, 81 00:04:08,700 --> 00:04:14,810 add constraint, patient_ac_fk. 82 00:04:14,810 --> 00:04:19,660 foreign key, then the column in the patient_ac table 83 00:04:19,660 --> 00:04:21,720 that is the foreign key. 84 00:04:21,720 --> 00:04:27,430 And again, uses references table called doctor_ac 85 00:04:27,430 --> 00:04:33,230 and the primary key column in that table. 86 00:04:33,230 --> 00:04:35,980 So let's look at a little bit about how this works when 87 00:04:35,980 --> 00:04:38,300 we insert data into a table. 88 00:04:38,300 --> 00:04:42,060 So let's insert a value into the patient_ac table. 89 00:04:46,470 --> 00:04:46,970 All right. 90 00:04:46,970 --> 00:04:50,240 So the patient_ac table has patient_id, patient_name, 91 00:04:50,240 --> 00:04:52,140 and doctor_id. 92 00:04:52,140 --> 00:04:59,150 We're going to attempt to insert into the patient_ac table. 93 00:04:59,150 --> 00:05:03,290 So notice the error that we get here, integrity constraint-- 94 00:05:03,290 --> 00:05:05,100 and there's the name that we gave it-- 95 00:05:05,100 --> 00:05:05,910 is violated. 96 00:05:05,910 --> 00:05:08,130 The parent key is not found. 97 00:05:08,130 --> 00:05:09,240 So what happened? 98 00:05:09,240 --> 00:05:13,710 We attempted to put a row into the patient_ac table. 99 00:05:13,710 --> 00:05:16,890 The patient_ac table has a foreign key, primary key 100 00:05:16,890 --> 00:05:20,010 relationship between the doctor_ac. 101 00:05:20,010 --> 00:05:22,770 And so when we attempted to put a 2 102 00:05:22,770 --> 00:05:27,240 into the doctor_id column, a part of the primary key, 103 00:05:27,240 --> 00:05:29,010 foreign key relationship is for Oracle 104 00:05:29,010 --> 00:05:32,850 to check that that exists in the other table, 105 00:05:32,850 --> 00:05:34,860 in the doctor_ac table. 106 00:05:34,860 --> 00:05:37,740 It did not, so we receive an error. 107 00:05:37,740 --> 00:05:41,380 And it does not allow us to put that value into the table. 108 00:05:41,380 --> 00:05:49,280 So let's fix this by inserting a doctor into doctor_ac. 109 00:05:49,280 --> 00:05:53,600 So his doctor_id is 2, which matches what we're attempting 110 00:05:53,600 --> 00:05:54,830 in the patient table. 111 00:05:58,840 --> 00:06:00,980 One row inserted. 112 00:06:00,980 --> 00:06:02,700 We commit. 113 00:06:02,700 --> 00:06:05,710 And now I'm going to clear this for clarity. 114 00:06:05,710 --> 00:06:08,260 And then we're going to attempt to run 115 00:06:08,260 --> 00:06:12,880 our insert into the table with the foreign key. 116 00:06:12,880 --> 00:06:16,530 And the row was inserted. 117 00:06:16,530 --> 00:06:17,840 And we commit. 118 00:06:17,840 --> 00:06:21,410 select star from patient_ac. 119 00:06:21,410 --> 00:06:23,030 And our values are put in there. 120 00:06:23,030 --> 00:06:25,220 And this works because the doctor_id number 121 00:06:25,220 --> 00:06:30,330 2 in this table matches the doctor_id value of 2 122 00:06:30,330 --> 00:06:32,490 in the doctor_ac table. 123 00:06:32,490 --> 00:06:34,220 So this is just a little bit about how 124 00:06:34,220 --> 00:06:39,650 that foreign key and primary key relationship works in Oracle. 9679

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.