All language subtitles for English_en_0_SQL Tutorial - Full Database Course for Beginners(720P_HD)
Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:01,000
SQL Tutorial - Full Database Course for Beginners
2
00:00:01,000 --> 00:00:04,800
In this course I’m going to teach you everything you
need to know to get started using SQL.
3
00:00:04,800 --> 00:00:10,900
Now SQL is a language which is used to interact
with relational database management systems.
4
00:00:10,900 --> 00:00:13,300
And a relational database management system
5
00:00:13,300 --> 00:00:19,400
is basically just a software application which we can
use to create and manage different databases.
6
00:00:19,400 --> 00:00:22,400
And so, we're going to talk about
all of this stuff in this course.
7
00:00:22,400 --> 00:00:24,400
We’re going to start off with the basics.
8
00:00:24,400 --> 00:00:25,900
So, we'll just look at what is a database.
9
00:00:25,900 --> 00:00:27,500
We’ll talk about the different types of databases.
10
00:00:27,500 --> 00:00:34,400
We'll talk about what SQL is and what it actually
means and how you can use it to create databases.
11
00:00:34,400 --> 00:00:37,400
And then we're going to go ahead
and we're going to install something
12
00:00:37,400 --> 00:00:40,000
called a relational database management system.
13
00:00:40,000 --> 00:00:44,100
Which like I said, is just software
that we can use to manage a database.
14
00:00:44,100 --> 00:00:48,900
We're going to install a relational database
management system called MySQL.
15
00:00:48,900 --> 00:00:53,600
And MySQL is one of the most popular database
management systems for beginners.
16
00:00:53,600 --> 00:00:55,200
And also, just in general.
17
00:00:55,200 --> 00:00:58,400
So, MySQL is a great first system to learn.
18
00:00:58,400 --> 00:01:02,100
And so, once we have that all install,
then we'll start writing SQL.
19
00:01:02,100 --> 00:01:05,400
So, we can write out little SQL code,
little queries
20
00:01:05,400 --> 00:01:09,600
in order to create databases
and create database tables
21
00:01:09,600 --> 00:01:13,500
and, you know, input information,
retrieve information.
22
00:01:13,500 --> 00:01:15,800
And then we're going to get into writing SQL queries.
23
00:01:15,800 --> 00:01:18,600
And queries are used to query a database.
24
00:01:18,600 --> 00:01:20,000
So, we'll create a database.
25
00:01:20,000 --> 00:01:21,400
We’ll populate it with information.
26
00:01:21,400 --> 00:01:26,500
And I’ll show you guys how you can write these little
SQL queries to get specific pieces of information.
27
00:01:26,500 --> 00:01:30,100
So, we'll start off with the basics
and we'll just learn all of the fundamentals.
28
00:01:30,100 --> 00:01:33,200
And then I’m going to show you guys
some more advanced techniques
29
00:01:33,200 --> 00:01:36,600
to getting information out of a database.
30
00:01:36,600 --> 00:01:41,200
And finally, I’m going to show you guys
how you can actually design database schemas.
31
00:01:41,200 --> 00:01:46,300
So, a database schema is basically just like all of the
different tables and all the different relations
32
00:01:46,300 --> 00:01:48,900
that the database is going to store.
33
00:01:48,900 --> 00:01:52,600
And if you don’t understand what any of that means,
don’t worry, we're going to cover all of it.
34
00:01:52,600 --> 00:01:58,000
But this course will cover basically everything
about SQL, all of the fundamentals,
35
00:01:58,000 --> 00:02:00,900
all of the things that you need to get started.
36
00:02:00,900 --> 00:02:04,700
And we'll also look at database
design and schema design.
37
00:02:04,700 --> 00:02:06,700
So, it’s going to be a pretty awesome course.
38
00:02:06,700 --> 00:02:09,000
I’m excited to be bringing you guys this stuff.
39
00:02:09,000 --> 00:02:12,200
And just know that all of the code,
all the stuff that’s in this course
40
00:02:12,200 --> 00:02:15,000
is going to be available
on the Giraffe Academy website.
41
00:02:15,000 --> 00:02:16,700
GiraffeAcademy.com.
42
00:02:16,700 --> 00:02:19,700
And there’s also going to be some
additional things on the website
43
00:02:19,700 --> 00:02:23,700
that isn’t going to be in course just because
I didn’t have time to cover all of it.
44
00:02:23,700 --> 00:02:26,000
So, check out the website for more information.
45
00:02:26,000 --> 00:02:28,600
But without further ado, lets get into it.
46
00:02:28,600 --> 00:02:34,100
Let’s learn SQL which is one of the most popular
languages for not only jobs,
47
00:02:34,100 --> 00:02:36,100
but just for developers in general.
48
00:02:36,100 --> 00:02:39,700
[What is a database?]
49
00:02:39,700 --> 00:02:43,800
In this video I’m going to give you guys a full
introduction into databases.
50
00:02:43,800 --> 00:02:46,600
So, we're not going to get too specific
on any one point.
51
00:02:46,600 --> 00:02:50,900
Basically, I want to show you guys
just what databases are in general.
52
00:02:50,900 --> 00:02:55,100
We're going to talk about how databases are used,
why we use databases.
53
00:02:55,100 --> 00:02:59,600
And the different types of databases
that you’ll find out in the world.
54
00:02:59,600 --> 00:03:03,600
So, this should basically just give
you an introduction to databases in general.
55
00:03:03,600 --> 00:03:07,900
If you don’t know anything about databases,
then this video will kind of walk you through
56
00:03:07,900 --> 00:03:09,700
and get you up to speed
with what you need to know
57
00:03:09,700 --> 00:03:15,000
so you can then go off and start learning
how to use an actual database.
58
00:03:15,000 --> 00:03:19,500
So, I just have a little PowerPoint slide here
that I want to kind of walk you guys through.
59
00:03:19,500 --> 00:03:21,700
And we'll look at some different
features of databases.
60
00:03:21,700 --> 00:03:24,400
So, the first question is,
“What is a database?”
61
00:03:24,400 --> 00:03:28,000
You’ll often see databases
abbreviated as DB.
62
00:03:28,000 --> 00:03:29,600
So DB would stand for database.
63
00:03:29,600 --> 00:03:34,500
And, a database is any collection
of related information.
64
00:03:34,500 --> 00:03:39,900
What I’m sharing with you guys here is the absolute
most general definition of this word.
65
00:03:39,900 --> 00:03:44,800
A database really, if you want to get down to it,
is just any collection of related information.
66
00:03:44,800 --> 00:03:46,800
So, that could be something like a phonebook,
67
00:03:46,800 --> 00:03:52,100
a shopping lists, a to-do list,
your five best friends, Facebook’s userbase.
68
00:03:52,100 --> 00:03:54,900
All of these are examples of databases.
69
00:03:54,900 --> 00:04:00,600
All of these are collections of information
that store related stuff, right?
70
00:04:00,600 --> 00:04:02,400
So, the phone book, right?
71
00:04:02,400 --> 00:04:04,600
This stores people’s phone numbers.
72
00:04:04,600 --> 00:04:06,200
Their names and their phone numbers.
73
00:04:06,200 --> 00:04:08,300
It’s a collection of related information.
74
00:04:08,300 --> 00:04:14,100
A to-do list – this stores a list of things
that you want to do during the day, right?
75
00:04:14,100 --> 00:04:15,100
It’s related information.
76
00:04:15,100 --> 00:04:16,800
Your five best friends.
77
00:04:16,800 --> 00:04:18,200
That’s information.
78
00:04:18,200 --> 00:04:20,800
It’s information that you’re storing somewhere.
79
00:04:20,800 --> 00:04:24,500
You have – you know, if I came up to you
and asked you who your five best friends were,
80
00:04:24,500 --> 00:04:25,300
I’m sure you could list them off.
81
00:04:25,300 --> 00:04:26,900
That, in essence, is a database.
82
00:04:26,900 --> 00:04:28,700
It’s a collection of related information.
83
00:04:28,700 --> 00:04:31,000
And then Facebook’s userbase, right?
84
00:04:31,000 --> 00:04:34,800
So, all of the users that Facebook
keeps track of and stores.
85
00:04:34,800 --> 00:04:36,700
That’s another example of a database.
86
00:04:36,700 --> 00:04:40,500
So, databases can be stored in different ways.
87
00:04:40,500 --> 00:04:42,500
So, you could store a database on paper.
88
00:04:42,500 --> 00:04:47,400
If I had a shopping list or a to-do list, I might
just scribble that down on a piece of paper.
89
00:04:47,400 --> 00:04:49,400
You could store a database in your mind.
90
00:04:49,400 --> 00:04:52,500
So, your five best friends, for example.
91
00:04:52,500 --> 00:04:55,500
If I came up to you and I asked you,
“List off your five best friends.”
92
00:04:55,500 --> 00:04:57,700
And you probably don’t have it
written down somewhere.
93
00:04:57,700 --> 00:05:01,400
You don’t have an app on your phone
that tells you who all your friends are, right?
94
00:05:01,400 --> 00:05:03,400
You just know that information in your mind naturally.
95
00:05:03,400 --> 00:05:06,400
And so, that’s another way
that you can store a database.
96
00:05:06,400 --> 00:05:08,200
You can just store the information in your mind.
97
00:05:08,200 --> 00:05:10,300
You can store information on a computer.
98
00:05:10,300 --> 00:05:15,400
And this is probably the most common use case
is people will create a database
99
00:05:15,400 --> 00:05:17,600
and they’ll store all the information
on their computer.
100
00:05:17,600 --> 00:05:20,500
This PowerPoint, the PowerPoint
that we're looking at right now.
101
00:05:20,500 --> 00:05:22,300
This is an example of a database.
102
00:05:22,300 --> 00:05:24,200
It has related information on it.
103
00:05:24,200 --> 00:05:27,000
And I’m using it to teach this lesson.
104
00:05:27,000 --> 00:05:29,400
And then finally like a comment section.
105
00:05:29,400 --> 00:05:33,100
So, if you go down to the comment section
of this video, that’s a database, right?
106
00:05:33,100 --> 00:05:37,700
It’s a general database that’s storing
comments for the video.
107
00:05:37,700 --> 00:05:43,200
So, that really, in essence, is everything you need
to know about databases to get started.
108
00:05:43,200 --> 00:05:47,400
It’s a collection of related information
that can be stored in different ways.
109
00:05:47,400 --> 00:05:50,300
So, now that we understand
the very general definition,
110
00:05:50,300 --> 00:05:54,700
I want to kind of walk you guys through more
specifically what we can do with databases.
111
00:05:54,700 --> 00:05:56,900
So, over here I have another slide.
112
00:05:56,900 --> 00:05:59,000
It’s Computers + Databases equals heart.
113
00:05:59,000 --> 00:06:03,200
Now, the point I’m trying to drive home
with this slide is that storing a database,
114
00:06:03,200 --> 00:06:09,100
storing a collection of related information
on a computer is extremely useful.
115
00:06:09,100 --> 00:06:12,500
And computers are actually great
for storing databases.
116
00:06:12,500 --> 00:06:14,500
So, I have here two things.
117
00:06:14,500 --> 00:06:18,000
We have over here on the left
Amazon vs a Shopping List.
118
00:06:18,000 --> 00:06:21,900
So, these are two examples of situations
where we have a database.
119
00:06:21,900 --> 00:06:24,900
We kind of talked about how
a shopping list is a database.
120
00:06:24,900 --> 00:06:27,200
It’s a collection of related information, right?
121
00:06:27,200 --> 00:06:29,600
A collection of products that you
want to buy from the store.
122
00:06:29,600 --> 00:06:32,000
Amazon.com is also a database.
123
00:06:32,000 --> 00:06:35,700
Amazon is storing all this product information,
all this user information.
124
00:06:35,700 --> 00:06:38,700
They’re storing reviews on the products,
the prices of the products.
125
00:06:38,700 --> 00:06:40,700
They’re storing all of this information.
126
00:06:40,700 --> 00:06:45,400
So, let’s take a look and we'll compare
and contrast these two databases
127
00:06:45,400 --> 00:06:49,600
and we'll see why computers
are the preferred medium for storing data.
128
00:06:49,600 --> 00:06:55,300
So, Amazon.com keeps track of products, reviews,
purchase orders, credit cards, users, media, etc.
129
00:06:55,300 --> 00:06:59,300
So, Amazon is keeping track of so much information.
130
00:06:59,300 --> 00:07:04,200
Trillions of pieces of information
need to be stored and readily available.
131
00:07:04,200 --> 00:07:05,600
Over here we have a shopping list.
132
00:07:05,600 --> 00:07:09,300
A shopping list keeps track of consumer products
that need to be purchased, right?
133
00:07:09,300 --> 00:07:14,900
We're talking 10 to 20 pieces of information
need to be stored and readily available.
134
00:07:14,900 --> 00:07:18,800
So, Amazon, we need to store
trillions of pieces of information.
135
00:07:18,800 --> 00:07:21,100
With the shopping list,
we need to store 10 or 20.
136
00:07:21,100 --> 00:07:25,900
Both of these are databases,
but the one we have trillions of stuff.
137
00:07:25,900 --> 00:07:28,100
The other one we have 10 to 20 things.
138
00:07:28,100 --> 00:07:33,000
And so, over here on Amazon, the information
on Amazon is extremely valuable
139
00:07:33,000 --> 00:07:36,100
and it’s critical to Amazon.com’s functioning, right?
140
00:07:36,100 --> 00:07:40,200
So, the information that Amazon is storing
it’s database is absolutely essential.
141
00:07:40,200 --> 00:07:44,800
And another thing is that security is essential, right?
142
00:07:44,800 --> 00:07:49,700
Amazon stores people’s personal information, like
social security number or credit card, address, phone.
143
00:07:49,700 --> 00:07:53,300
Like that information needs to be
locked down and secure.
144
00:07:53,300 --> 00:07:58,200
A shopping list on the other hand, the information
is for convenience’s sake only, right?
145
00:07:58,200 --> 00:08:00,800
It’s not absolutely necessary for shopping.
146
00:08:00,800 --> 00:08:05,700
Like if you didn’t have a shopping list, you could still
go to the store and, for the most part,
147
00:08:05,700 --> 00:08:07,500
you’d be able to find everything that you need.
148
00:08:07,500 --> 00:08:11,600
Also, with the shopping list,
security is just not important at all, right?
149
00:08:11,600 --> 00:08:15,800
If you drop your shopping list on the ground –
I mean if somebody else was to pick it up
150
00:08:15,800 --> 00:08:17,000
and look at it, it’s like not a big deal.
151
00:08:17,000 --> 00:08:17,800
It’s a shopping list.
152
00:08:17,800 --> 00:08:23,900
Finally, over here on Amazon,
the information is stored on a computer.
153
00:08:23,900 --> 00:08:28,400
Whereas with the shopping list, the information
is stored maybe on a piece of paper,
154
00:08:28,400 --> 00:08:31,600
or it could even just be stored
in someone’s memory, right?
155
00:08:31,600 --> 00:08:33,400
So, maybe you just memorized your shopping list
156
00:08:33,400 --> 00:08:36,600
and that’s where you’re storing
your shopping list database.
157
00:08:36,600 --> 00:08:41,100
So, what I’m trying to show you guys is that
a database doesn’t just have to be on a computer
158
00:08:41,100 --> 00:08:44,900
and it doesn’t just have to be like in your memory
or on a piece of paper, right?
159
00:08:44,900 --> 00:08:48,600
Databases are in all of these different environments.
160
00:08:48,600 --> 00:08:54,900
But here’s the thing, for an application like Amazon,
storing information on something like a computer
161
00:08:54,900 --> 00:08:58,700
makes it really easy to store
trillions of pieces of information.
162
00:08:58,700 --> 00:09:04,000
And really easy to secure the data
and make sure that security is taken care of.
163
00:09:04,000 --> 00:09:06,800
And it also makes it really easy
to back up that information
164
00:09:06,800 --> 00:09:11,900
and duplicate that information and store
that information on different computers.
165
00:09:11,900 --> 00:09:18,100
So, the main point of this slide is that computers are
great at keeping track of large amounts of information.
166
00:09:18,100 --> 00:09:24,600
And so, going forward, we're going to be talking about
how can we create databases on the computers
167
00:09:24,600 --> 00:09:28,200
because computers are so good
at storing that information.
168
00:09:28,200 --> 00:09:34,100
So, let’s talk about how we can go about
creating databases on a computer.
169
00:09:34,100 --> 00:09:40,200
Now, a database could be as simple
as like a text file where you store information.
170
00:09:40,200 --> 00:09:42,600
Or it could be like an Excel file, right?
171
00:09:42,600 --> 00:09:44,200
Microsoft Excel file.
172
00:09:44,200 --> 00:09:48,900
But generally, if you’re going to be using
a database with an application
173
00:09:48,900 --> 00:09:52,300
or you’re going to be using a database
to store huge amounts of information,
174
00:09:52,300 --> 00:09:56,000
a lot of times what people will do is
they’ll use special software
175
00:09:56,000 --> 00:09:59,900
which is designed to help you create
and maintain the database.
176
00:09:59,900 --> 00:10:02,900
This is called database management systems.
177
00:10:02,900 --> 00:10:06,700
So a database management system
is a special software program
178
00:10:06,700 --> 00:10:11,000
that helps users create and maintain a database
on a computer.
179
00:10:11,000 --> 00:10:15,000
So, it makes it really easy for us
to manage large amounts of information.
180
00:10:15,000 --> 00:10:19,300
So, if you’re a company like Amazon
and you have trillions of pieces of information
181
00:10:19,300 --> 00:10:21,900
that you need to keep track of, well,
the database management system
182
00:10:21,900 --> 00:10:25,800
can make it pretty easy for you to store
trillions of pieces of information, right?
183
00:10:25,800 --> 00:10:29,200
It’s not like all that information
is just in like some single text file.
184
00:10:29,200 --> 00:10:33,200
The database management system
will make it really easy for you to store that.
185
00:10:33,200 --> 00:10:36,100
Database management systems
can also handle security.
186
00:10:36,100 --> 00:10:41,600
So, they can make it so only certain people with the
usernames and passwords can access the data.
187
00:10:41,600 --> 00:10:47,400
It’ll also help you to backup your data and import
and export data from other sources.
188
00:10:47,400 --> 00:10:50,700
So, if you have a bunch of information
and you want to back it up,
189
00:10:50,700 --> 00:10:53,500
a database management system can help you do that.
190
00:10:53,500 --> 00:10:57,400
Database management systems
can also interact with software applications.
191
00:10:57,400 --> 00:11:00,200
So, you know, Amazon.com is a website.
192
00:11:00,200 --> 00:11:03,000
And it’s interacting with the Amazon database
193
00:11:03,000 --> 00:11:07,500
which is stored mostly likely
using a database management system.
194
00:11:07,500 --> 00:11:11,900
So, you could write a program that could interact
with the database management system.
195
00:11:11,900 --> 00:11:15,800
All right, so let’s take a look at
this quick little diagram that I have here.
196
00:11:15,800 --> 00:11:17,400
So, we have Amazon over here.
197
00:11:17,400 --> 00:11:19,200
And this would be like Amazon.com.
198
00:11:19,200 --> 00:11:22,700
And Amazon is communicating
with a database management system.
199
00:11:22,700 --> 00:11:26,900
And I don’t work for Amazon so I’m not 100% sure
exactly how they're doing this.
200
00:11:26,900 --> 00:11:29,500
But for the most part, this is the general use case.
201
00:11:29,500 --> 00:11:33,000
So, we have our database management system
which is this little box here.
202
00:11:33,000 --> 00:11:39,900
And the database management system is creating
and storing and keeping track of a database.
203
00:11:39,900 --> 00:11:42,900
So, the database management system
isn’t the actual database.
204
00:11:42,900 --> 00:11:46,100
The database management system
is the software application
205
00:11:46,100 --> 00:11:52,800
that is creating, maintaining, updating, deleting
information from the actual database.
206
00:11:52,800 --> 00:11:56,200
So, Amazon.com will interact with
the database management system
207
00:11:56,200 --> 00:12:00,700
in order to create, read, update,
and delete information.
208
00:12:00,700 --> 00:12:05,800
So, Amazon isn’t creating or reading
or updating this information directly.
209
00:12:05,800 --> 00:12:10,600
Amazon is telling the database management system
to do that for it.
210
00:12:10,600 --> 00:12:13,400
And by going through
the database management system,
211
00:12:13,400 --> 00:12:16,600
we can be sure that all the data
is getting stored correctly
212
00:12:16,600 --> 00:12:20,000
and there’s going to be no problems with the data.
213
00:12:20,000 --> 00:12:22,700
So, let’s talk about C.R.U.D.
214
00:12:22,700 --> 00:12:24,700
And this is an acronym that you’re going to hear a lot.
215
00:12:24,700 --> 00:12:26,100
C-R-U-D.
216
00:12:26,100 --> 00:12:29,500
It stands for Create, Read, Update, and Delete.
217
00:12:29,500 --> 00:12:33,400
You’ll also hear people call this
Create, Retrieve, Update, and Delete.
218
00:12:33,400 --> 00:12:39,500
Now, CRUD represents the four main operations
that we're going to be doing with the database.
219
00:12:39,500 --> 00:12:42,300
So, you’re going to be creating information
in the database.
220
00:12:42,300 --> 00:12:44,700
So, creating new database entries.
221
00:12:44,700 --> 00:12:46,700
You’re going to be reading information
from the database.
222
00:12:46,700 --> 00:12:50,800
So, you know, retrieving or getting
the information that you already stored in there.
223
00:12:50,800 --> 00:12:53,400
You’re going to be updating the existing information.
224
00:12:53,400 --> 00:12:56,500
And you're going to be deleting the information
that’s already in there.
225
00:12:56,500 --> 00:13:02,300
And these are the core four operations that we want
the database management system to perform for us.
226
00:13:02,300 --> 00:13:07,600
So, any good database management system
is going to be able to do all four of these things.
227
00:13:07,600 --> 00:13:11,000
So, now that we've talked a little bit about
database management systems,
228
00:13:11,000 --> 00:13:17,500
I want to talk to you guys about the two main types
of databases that you'll find in computing.
229
00:13:17,500 --> 00:13:20,500
So, the first is called a relational database.
230
00:13:20,500 --> 00:13:26,000
You’ll hear people refer to these as SQL[Sequel]
databases or S-Q-L databases.
231
00:13:26,000 --> 00:13:29,600
And then we also have what are called
non-relational databases.
232
00:13:29,600 --> 00:13:36,100
And you’ll hear people refer to non-relational
databases as no-SQL or not just SQL databases.
233
00:13:36,100 --> 00:13:40,200
We're going to talk about what SQL is in a second.
234
00:13:40,200 --> 00:13:42,400
But I want to show you guys these two types.
235
00:13:42,400 --> 00:13:48,100
So, a relational database which we have over here
on the left, organizes data into one or more tables.
236
00:13:48,100 --> 00:13:54,100
So, each table has columns and rows
and a unique key identifies each row.
237
00:13:54,100 --> 00:13:59,200
Now, relational databases are by far
the most popular types of databases.
238
00:13:59,200 --> 00:14:03,200
And relational databases are going to store
everything inside of these tables.
239
00:14:03,200 --> 00:14:07,900
So, we'll organize all the data that we want
to store inside of these predefined tables
240
00:14:07,900 --> 00:14:10,500
and then we can just insert information into there.
241
00:14:10,500 --> 00:14:13,500
A relational database is a lot like an Excel spreadsheet.
242
00:14:13,500 --> 00:14:17,000
So, if you’ve ever used a spreadsheet
where we have columns and rows
243
00:14:17,000 --> 00:14:20,800
and we're storing information,
that’s essentially what a relational database is.
244
00:14:20,800 --> 00:14:23,700
And then over here on the right
we have non-relational databases.
245
00:14:23,700 --> 00:14:30,100
And a non-relational database is basically just any
type of database that’s not a relational database.
246
00:14:30,100 --> 00:14:34,000
So, like I said, relational databases
are by far the most popular type.
247
00:14:34,000 --> 00:14:38,800
And so, because they’re so popular,
any other type of database
248
00:14:38,800 --> 00:14:43,100
that’s not technically relational is just referred
to as non-relational.
249
00:14:43,100 --> 00:14:45,900
And non-relational databases, you know,
it’s very general.
250
00:14:45,900 --> 00:14:51,400
Because any database that’s not relational
is getting put into this category.
251
00:14:51,400 --> 00:14:56,200
So, non-relational database is organized database
in anything but a traditional table.
252
00:14:56,200 --> 00:14:59,500
So, you’ll have things like key-value stores documents
253
00:14:59,500 --> 00:15:02,300
which would be kind of like
JavaScript Object Notation.
254
00:15:02,300 --> 00:15:03,300
Or like XMl.
255
00:15:03,300 --> 00:15:05,300
They’ll store data in graphs.
256
00:15:05,300 --> 00:15:07,600
Or even flexible tables.
257
00:15:07,600 --> 00:15:11,400
So, really non-relational database
is a very general category.
258
00:15:11,400 --> 00:15:13,900
It’s just anything that’s not relational.
259
00:15:13,900 --> 00:15:16,500
So, let’s take a look real quick at a relational database.
260
00:15:16,500 --> 00:15:20,300
Or you’ll hear people, like I said,
refer to this as a SQL database.
261
00:15:20,300 --> 00:15:23,400
So, over here, we have two examples of tables.
262
00:15:23,400 --> 00:15:26,800
So, this would be basically how we would store
information in a relational database.
263
00:15:26,800 --> 00:15:29,000
So, over here I have a student table.
264
00:15:29,000 --> 00:15:32,100
And this student table might store individual students.
265
00:15:32,100 --> 00:15:35,500
So, you’ll see over here we have
an ID, a name, and a major.
266
00:15:35,500 --> 00:15:39,900
So, for every single one of my students,
I have an entry just like this.
267
00:15:39,900 --> 00:15:43,800
And you'll notice over here,
I’m giving each of these entries an ID.
268
00:15:43,800 --> 00:15:48,400
And that ID will uniquely identify that row in the table.
269
00:15:48,400 --> 00:15:50,500
And over here, we have a users table.
270
00:15:50,500 --> 00:15:53,300
So, maybe you're creating users for your application.
271
00:15:53,300 --> 00:15:57,400
So, they have a username,
a password, and then an email, right?
272
00:15:57,400 --> 00:16:00,200
So, the username is something
that’s going to be unique.
273
00:16:00,200 --> 00:16:03,400
It’s something that uniquely identifies
each row in the table.
274
00:16:03,400 --> 00:16:06,500
And then we're also storing like password and email.
275
00:16:06,500 --> 00:16:10,300
So, we kind of talked about how
relational databases store data in tables.
276
00:16:10,300 --> 00:16:14,100
And when we want to create a relational database,
277
00:16:14,100 --> 00:16:18,600
we can use a
relational database management system.
278
00:16:18,600 --> 00:16:22,200
And a relational database management system,
or an RDBMS
279
00:16:22,200 --> 00:16:27,500
is just a database management system that helps you
create and maintain a relational database.
280
00:16:27,500 --> 00:16:34,000
And some of the most popular are my MySQL,
Oracle, PostgreSQL, and MariaDB.
281
00:16:34,000 --> 00:16:36,600
There’s a bunch of these that are,
you know, extremely popular.
282
00:16:36,600 --> 00:16:42,700
And relational database management systems use
something called Structured Query Language, or SQL.
283
00:16:42,700 --> 00:16:50,200
And SQL is a standardized language for interacting
with relational database management systems.
284
00:16:50,200 --> 00:16:53,000
So, remember,
a relational database management system
285
00:16:53,000 --> 00:16:59,300
is just a software application that we can use
in order to create, maintain
286
00:16:59,300 --> 00:17:02,700
and do different things to our relational database.
287
00:17:02,700 --> 00:17:08,200
And SQL or Structured Query Language
is the language that we can use
288
00:17:08,200 --> 00:17:11,800
to interact with those relational
database management systems.
289
00:17:11,800 --> 00:17:17,500
So, we can use SQL to perform CRUD operations
as well as other administrative tasks
290
00:17:17,500 --> 00:17:20,500
like user management, security, backup, etc.
291
00:17:20,500 --> 00:17:25,000
And we can use SQL to define tables and structures.
292
00:17:25,000 --> 00:17:29,000
So, a relational database uses tables
in order to organize its information.
293
00:17:29,000 --> 00:17:32,300
And we can use SQL to define those tables.
294
00:17:32,300 --> 00:17:34,700
And then insert information into those tables.
295
00:17:34,700 --> 00:17:39,300
And SQL is a standardized language
which means it’s pretty much used
296
00:17:39,300 --> 00:17:41,800
on every relational database management system.
297
00:17:41,800 --> 00:17:48,400
However, different relational database management
systems will implement SQL just a little bit differently.
298
00:17:48,400 --> 00:17:54,400
So, not all SQL code that you use on one
relational database management system
299
00:17:54,400 --> 00:17:58,500
will port over to another one
without slight modification.
300
00:17:58,500 --> 00:18:01,700
So, now let’s talk a little bit
about non-relational databases.
301
00:18:01,700 --> 00:18:06,000
And in non-relational databases
is just anything that’s not relational.
302
00:18:06,000 --> 00:18:12,000
So, a non-relational database stores data
in anything but a, you know, a static table.
303
00:18:12,000 --> 00:18:16,700
So, I’m showing you guys a couple different
examples of non-relational databases
304
00:18:16,700 --> 00:18:18,500
and how they would store data.
305
00:18:18,500 --> 00:18:20,700
So, over here we have a document database.
306
00:18:20,700 --> 00:18:25,800
And this would store information inside of like
little objects or documents.
307
00:18:25,800 --> 00:18:29,200
So, we're talking about things like JSON or XML.
308
00:18:29,200 --> 00:18:34,700
Basically, I think JSON is kind of a very popular format
and that’s kind of what I have up here.
309
00:18:34,700 --> 00:18:37,100
So, JSON is JavaScript Object Notation.
310
00:18:37,100 --> 00:18:43,200
So, there’s a lot of non-relational databases
that will store data in documents just like this.
311
00:18:43,200 --> 00:18:45,600
They’ll also store data in graphs.
312
00:18:45,600 --> 00:18:47,900
So, over here we have graphs.
313
00:18:47,900 --> 00:18:49,300
And there’s like these little nodes.
314
00:18:49,300 --> 00:18:53,300
And then there’s, you know, connecting lines between
the nodes which would represent like relationships.
315
00:18:53,300 --> 00:18:56,200
And then over here we have a key value hash.
316
00:18:56,200 --> 00:18:58,800
So, we would have a key
and it gets mapped to a value.
317
00:18:58,800 --> 00:19:00,400
And that could really be anything.
318
00:19:00,400 --> 00:19:00,700
It could be a string.
319
00:19:00,700 --> 00:19:01,400
It could be JSON.
320
00:19:01,400 --> 00:19:03,000
It could be a graph.
321
00:19:03,000 --> 00:19:04,400
It could be anything like that.
322
00:19:04,400 --> 00:19:07,600
So, these are just three common examples
323
00:19:07,600 --> 00:19:12,700
of what SQL or non-relational databases
might look like or how they might store their data.
324
00:19:12,700 --> 00:19:19,200
And just like with relational databases we can have
non-relational database management systems.
325
00:19:19,200 --> 00:19:24,200
And these help users to create
and maintain non-relational databases.
326
00:19:24,200 --> 00:19:30,100
So, some examples would be mongoDB,
DynamoDB, apache cassandra, firebase.
327
00:19:30,100 --> 00:19:33,500
There’s a bunch of these popular noSQL databases
328
00:19:33,500 --> 00:19:36,200
that have been sprouting up
over the last decade or so.
329
00:19:36,200 --> 00:19:38,600
And these are all implementation specific.
330
00:19:38,600 --> 00:19:43,000
So, unlike relational database management systems
where we have SQL
331
00:19:43,000 --> 00:19:46,500
which is just a standard language
that can interact with all of them,
332
00:19:46,500 --> 00:19:52,500
there isn’t a standard language for interacting with
non-relational database management system.
333
00:19:52,500 --> 00:19:55,400
So, generally, the non-relational
database management system
334
00:19:55,400 --> 00:20:02,100
will implement their own language for performing
CRUD and administrative operations on the database.
335
00:20:02,100 --> 00:20:04,500
So, now that we've looked at the different types
of databases,
336
00:20:04,500 --> 00:20:07,900
I want to talk to you guys about
some core concepts within databases.
337
00:20:07,900 --> 00:20:12,200
So, one of the most common things
that you’re going to be doing are queries.
338
00:20:12,200 --> 00:20:17,400
So, a database query is a request that’s made
to the database management system
339
00:20:17,400 --> 00:20:19,100
for specific information.
340
00:20:19,100 --> 00:20:21,700
So, you’ll hear the word query thrown around a lot.
341
00:20:21,700 --> 00:20:25,900
And generally, a query is you’re just asking
the database management system
342
00:20:25,900 --> 00:20:27,100
to give you some information.
343
00:20:27,100 --> 00:20:30,200
So, as a databases structure becomes
more and more complex,
344
00:20:30,200 --> 00:20:36,000
it becomes more difficult to get
the specific pieces of information that we want.
345
00:20:36,000 --> 00:20:40,500
And if you have a very complex
database layout or schema,
346
00:20:40,500 --> 00:20:45,400
then getting a specific piece of information
can be a little bit tricky.
347
00:20:45,400 --> 00:20:48,200
And that’s why we write database queries.
348
00:20:48,200 --> 00:20:53,900
So, can write a very complex database query
which in a lot of ways is like a program,
349
00:20:53,900 --> 00:20:56,500
if you’ve ever used a programming language before.
350
00:20:56,500 --> 00:21:00,900
And that query will then instruct the
relational database management system
351
00:21:00,900 --> 00:21:06,400
to grab a specific piece or specific pieces
of information from the database.
352
00:21:06,400 --> 00:21:11,300
So, a good way to think of a query,
is it’s kind of like a Google search, right?
353
00:21:11,300 --> 00:21:16,300
So, if I go on Google and I would generally
type in the specific information that I want.
354
00:21:16,300 --> 00:21:20,600
And then Google would give me the information
that matches that search query.
355
00:21:20,600 --> 00:21:22,600
That’s a lot like a database query.
356
00:21:22,600 --> 00:21:26,800
Except with a database query we're
not going to be writing the query in English
357
00:21:26,800 --> 00:21:28,200
like we would with Google.
358
00:21:28,200 --> 00:21:30,900
So, I can, you know, go up to my Google search bar
359
00:21:30,900 --> 00:21:34,500
and I can type something in in English or Spanish
or whatever language you speak.
360
00:21:34,500 --> 00:21:40,200
But with a relational database management system
we have to write our queries using either SQL
361
00:21:40,200 --> 00:21:45,000
or a specific language that’s meant for that
database management system.
362
00:21:45,000 --> 00:21:46,900
So, let’s wrap this up real quick.
363
00:21:46,900 --> 00:21:50,100
We kind of covered a lot of
the main points with databases.
364
00:21:50,100 --> 00:21:52,900
So, a database is any collection
of related information.
365
00:21:52,900 --> 00:21:54,100
It’s a very general term.
366
00:21:54,100 --> 00:21:59,600
And computers are great for storing databases
because computers are really fast.
367
00:21:59,600 --> 00:22:01,400
They can store lots of pieces of information.
368
00:22:01,400 --> 00:22:05,100
And they can handle things like security really easily.
369
00:22:05,100 --> 00:22:09,500
And database management system
make it easy to create, maintain,
370
00:22:09,500 --> 00:22:12,400
and secure a database on a computer.
371
00:22:12,400 --> 00:22:16,400
Database management systems will allow you
to perform the CRUD operations
372
00:22:16,400 --> 00:22:18,800
and other administrative tasks on the database.
373
00:22:18,800 --> 00:22:23,300
There’s two main types of databases, relational
and non-relational databases.
374
00:22:23,300 --> 00:22:28,700
Relational databases use SQL and they store data
in tables with rows and columns.
375
00:22:28,700 --> 00:22:33,200
Non-relational databases store data
using other data structures.
376
00:22:33,200 --> 00:22:37,200
So, things that aren’t relational database tables.
377
00:22:37,200 --> 00:22:39,500
And then we talked a little bit about querying.
378
00:22:39,500 --> 00:22:44,700
A query is just a require that you would make
to the database management system
379
00:22:44,700 --> 00:22:47,200
for a specific piece of information.
380
00:22:47,200 --> 00:22:52,600
So, that is kind of the basics,
the overall high-level basics of databases.
381
00:22:52,600 --> 00:22:55,300
And really, if you understand everything
in this PowerPoint,
382
00:22:55,300 --> 00:22:58,500
then you have a good grasp of database fundamentals.
383
00:22:58,500 --> 00:23:04,200
And as we go forward, we're going to learn more and
more things about databases like how to create them,
384
00:23:04,200 --> 00:23:09,600
how to store data, and how to organize our data
so that it’s easy to retrieve when we want it.
385
00:23:09,600 --> 00:23:13,300
[Tables and Keys]
386
00:23:13,300 --> 00:23:18,100
In this tutorial, we're going to look at some
of the core concepts in relational databases.
387
00:23:18,100 --> 00:23:22,600
So, we're going to talk about how we store
information in relational databases.
388
00:23:22,600 --> 00:23:23,800
Namely, in tables.
389
00:23:23,800 --> 00:23:26,600
Let’s talk about the different things
about those tables.
390
00:23:26,600 --> 00:23:28,400
We're going to talk about things called keys.
391
00:23:28,400 --> 00:23:30,300
We're also going to look at rows and columns.
392
00:23:30,300 --> 00:23:34,100
We'll talk about different tables can be related
to each other
393
00:23:34,100 --> 00:23:36,700
and how we can define relationships between tables.
394
00:23:36,700 --> 00:23:42,200
So, this will really just give you guys a full introduction
into some of the core concepts of relational databases
395
00:23:42,200 --> 00:23:46,700
which you can then apply when
creating your own relational database
396
00:23:46,700 --> 00:23:49,700
using a relational database management system.
397
00:23:49,700 --> 00:23:50,900
So, let’s get started.
398
00:23:50,900 --> 00:23:53,500
Over here I have an example of a table.
399
00:23:53,500 --> 00:23:55,800
And this is a student table.
400
00:23:55,800 --> 00:24:00,800
And so, the student table defines
specific information about a student.
401
00:24:00,800 --> 00:24:03,200
So, up here we have the column names.
402
00:24:03,200 --> 00:24:07,600
So, the student ID, the same of the student,
and then the major.
403
00:24:07,600 --> 00:24:11,900
So, I’m storing three pieces of information
about each student, right?
404
00:24:11,900 --> 00:24:14,700
They have their ID, their name, and their major.
405
00:24:14,700 --> 00:24:16,300
So, over here we have Jack.
406
00:24:16,300 --> 00:24:17,700
His student ID is 1.
407
00:24:17,700 --> 00:24:19,200
And he’s majoring in biology.
408
00:24:19,200 --> 00:24:20,800
Down here, we have Claire.
409
00:24:20,800 --> 00:24:24,200
Her student ID is 3 and she is majoring in English.
410
00:24:24,200 --> 00:24:27,400
So, all tables in relational databases
are going to have two things.
411
00:24:27,400 --> 00:24:30,100
They’re going to have columns
and they're going to have rows.
412
00:24:30,100 --> 00:24:33,200
Now, columns are these vertical sections right here.
413
00:24:33,200 --> 00:24:36,500
So, a column would define a single attribute, right?
414
00:24:36,500 --> 00:24:37,700
So, we have our name column.
415
00:24:37,700 --> 00:24:40,700
And inside the name column
we have the names of all the students.
416
00:24:40,700 --> 00:24:42,100
We have the major column.
417
00:24:42,100 --> 00:24:44,800
Inside the major column we have
the majors of all the students, right?
418
00:24:44,800 --> 00:24:45,800
Makes sense.
419
00:24:45,800 --> 00:24:47,200
And then we have rows.
420
00:24:47,200 --> 00:24:51,400
And a row is an individual entry
in the student table.
421
00:24:51,400 --> 00:24:56,800
So, a row or a horizontal entry
would represent a single student, right?
422
00:24:56,800 --> 00:25:01,100
So, in a single row we're storing
the student ID, the name, and the major.
423
00:25:01,100 --> 00:25:08,400
So, the columns represent a single attribute and
the row represents an entry or an actual student.
424
00:25:08,400 --> 00:25:12,200
Whenever we're creating a table
in a relational database
425
00:25:12,200 --> 00:25:17,100
we always want to have one very special column
which is called the primary key.
426
00:25:17,100 --> 00:25:24,400
And a primary key is basically an attribute which
uniquely defines the row in the database.
427
00:25:24,400 --> 00:25:28,800
So, it’s an attribute about a specific entry
which will uniquely define it.
428
00:25:28,800 --> 00:25:31,200
So, over here, we have student ID.
429
00:25:31,200 --> 00:25:34,900
And notice that I colored this in red
and I also underlined it.
430
00:25:34,900 --> 00:25:41,000
And this underline is basically going to specify
that this column or this attribute of the student
431
00:25:41,000 --> 00:25:43,200
is going to be the primary key.
432
00:25:43,200 --> 00:25:47,700
In other words, we can use this attribute
to uniquely identify a specific row.
433
00:25:47,700 --> 00:25:53,600
So, over here we have Kate
and she’s a sociology major and her ID is 1.
434
00:25:53,600 --> 00:25:57,800
So, we would say that Kate’s primary key is 1.
435
00:25:57,800 --> 00:26:00,500
Now, let me show you guys why primary keys
can come in handy.
436
00:26:00,500 --> 00:26:04,800
So, down here, inside of these gold squares
I have two entries in this database.
437
00:26:04,800 --> 00:26:07,800
So, we have two students, both of which
are named Jack.
438
00:26:07,800 --> 00:26:10,400
And both of which are biology majors.
439
00:26:10,400 --> 00:26:13,000
Now, this isn’t that, you know, special of a case, right?
440
00:26:13,000 --> 00:26:16,900
You can imagine that in a school you might have
two students with the same name
441
00:26:16,900 --> 00:26:18,300
who are the same major.
442
00:26:18,300 --> 00:26:24,200
But in this case, how can we differentiate
between this guy and this guy?
443
00:26:24,200 --> 00:26:27,000
Well, we can use this primary key.
444
00:26:27,000 --> 00:26:32,300
So, even though the name and the major
of this row in the table
445
00:26:32,300 --> 00:26:36,900
are the same as the name and the major of this row,
the student ID is different.
446
00:26:36,900 --> 00:26:39,200
The primary key is different.
447
00:26:39,200 --> 00:26:41,200
And that’s the whole point of a primary key,
448
00:26:41,200 --> 00:26:45,600
is that the primary key is always
going to be unique for each row in the table.
449
00:26:45,600 --> 00:26:50,500
So, even if all of the attributes of the row
are the same, the primary key won’t be.
450
00:26:50,500 --> 00:26:54,700
And therefore we can differentiate
between the two rows.
451
00:26:54,700 --> 00:27:00,800
So, I can differentiate between this Jack and this Jack
by referring to their student IDs.
452
00:27:00,800 --> 00:27:03,800
And so, generally, whenever we have a table
in a relational database,
453
00:27:03,800 --> 00:27:06,600
you always want to define a primary key.
454
00:27:06,600 --> 00:27:08,400
And a primary key can be anything.
455
00:27:08,400 --> 00:27:09,200
It can be a number.
456
00:27:09,200 --> 00:27:10,900
It could be a string of text.
457
00:27:10,900 --> 00:27:13,100
It could really be anything that you want.
458
00:27:13,100 --> 00:27:16,700
But it has to uniquely identify the specific row.
459
00:27:16,700 --> 00:27:21,600
So, in other words, another row in this student table
couldn't have a primary key of 2.
460
00:27:21,600 --> 00:27:24,200
So, down here I have another example
of a database table.
461
00:27:24,200 --> 00:27:26,000
This is a user table.
462
00:27:26,000 --> 00:27:30,500
So, just like that student table,
except it’s storing information about users.
463
00:27:30,500 --> 00:27:34,100
So, over here we have email, password,
date created and type.
464
00:27:34,100 --> 00:27:36,600
So, email is going to be our primary key.
465
00:27:36,600 --> 00:27:39,400
You can see I put that in red and I also underlined it.
466
00:27:39,400 --> 00:27:44,000
And these emails are unique to each entry
in the table, right?
467
00:27:44,000 --> 00:27:46,300
So, fakemail@fake.co.
468
00:27:46,300 --> 00:27:49,700
No, other rows in this table could have
that exact email.
469
00:27:49,700 --> 00:27:51,700
And then over here we're storing the password.
470
00:27:51,700 --> 00:27:53,300
We're also storing a date.
471
00:27:53,300 --> 00:27:57,000
So, a lot of times in databases
you can store date values or time values.
472
00:27:57,000 --> 00:27:59,400
And then over here we're storing a type.
473
00:27:59,400 --> 00:28:03,900
And so, basically whenever you want to store data,
what you’re going to do is define a table.
474
00:28:03,900 --> 00:28:07,900
So, you’re going to define, you know,
all this information up here
475
00:28:07,900 --> 00:28:12,000
and then you'll just insert specific pieces
of information into that table.
476
00:28:12,000 --> 00:28:16,600
And so, over here we have one more example
of a database table.
477
00:28:16,600 --> 00:28:19,800
And we're actually going to be looking
at this example as we go forward.
478
00:28:19,800 --> 00:28:22,900
So, this is an example of an employee.
479
00:28:22,900 --> 00:28:27,100
So, imagine that we have like a company database
and we were storing information about employees.
480
00:28:27,100 --> 00:28:29,800
So, we have the employee ID which is the primary key.
481
00:28:29,800 --> 00:28:33,400
We're storing first name,
last name, birthdate, sex, and salary.
482
00:28:33,400 --> 00:28:37,000
And we're storing all of this specific information
about an employee.
483
00:28:37,000 --> 00:28:39,900
Now, I want you guys to notice
the primary key over here.
484
00:28:39,900 --> 00:28:44,100
So, employee ID, this is just a, you know,
some sort of random number
485
00:28:44,100 --> 00:28:46,500
that we’ve assigned to each employee, right?
486
00:28:46,500 --> 00:28:48,500
This employee’s ID is 100.
487
00:28:48,500 --> 00:28:50,900
This employee ID is 101, etc.
488
00:28:50,900 --> 00:28:56,800
And this over here, this employee ID,
this is what we would call a surrogate key.
489
00:28:56,800 --> 00:29:01,500
And a surrogate key is basically a key that has no
mapping to anything in the real world, right?
490
00:29:01,500 --> 00:29:05,700
It’s essentially just, you know, like in this case a
random number that we assign to an employee, right?
491
00:29:05,700 --> 00:29:09,200
So this, employee Jan has an employee ID of 100.
492
00:29:09,200 --> 00:29:10,800
That doesn’t necessarily mean anything.
493
00:29:10,800 --> 00:29:16,200
100 is just a value that we're using to represent
Jan inside of the database.
494
00:29:16,200 --> 00:29:21,300
And so, we would refer to that as a surrogate key,
which is just a type of primary key.
495
00:29:21,300 --> 00:29:24,400
Surrogate key is a key that has no mapping to
anything in the real world.
496
00:29:24,400 --> 00:29:26,800
We can also have something called a natural key.
497
00:29:26,800 --> 00:29:30,100
And over here you'll see that
I have the same exact table
498
00:29:30,100 --> 00:29:33,900
except instead of having employee ID,
I have employee SSN.
499
00:29:33,900 --> 00:29:36,300
And SSN stands for Social Security Number.
500
00:29:36,300 --> 00:29:40,800
So, Social Security Number is a number
that we use here in the United States
501
00:29:40,800 --> 00:29:43,600
in order to uniquely identify each citizen.
502
00:29:43,600 --> 00:29:51,500
So, in this case we're using the Social Security Number
in order to uniquely identify each row in the table.
503
00:29:51,500 --> 00:29:57,200
In other words, we're using the Social Security Number
as the primary key of the table.
504
00:29:57,200 --> 00:30:00,800
And this is an example of
what we would call a natural key.
505
00:30:00,800 --> 00:30:08,500
And this is a key that has a mapping or has a purpose
in the real world, not just in the database.
506
00:30:08,500 --> 00:30:14,000
So, a lot of times you'll hear people refer to surrogate
keys or natural keys and that’s the difference.
507
00:30:14,000 --> 00:30:17,800
A surrogate key is a primary key
that has no mapping to the real world.
508
00:30:17,800 --> 00:30:25,100
And a natural key is a key that has a mapping to the
real world, just like Social Security Number.
509
00:30:25,100 --> 00:30:29,500
So, those are kind of the two different types of
primary keys that you might see being stored.
510
00:30:29,500 --> 00:30:33,600
Another thing I want to talk to you guys
about are what are called foreign keys.
511
00:30:33,600 --> 00:30:39,200
And a foreign key is basically an attribute
that we can store on a database table
512
00:30:39,200 --> 00:30:43,900
that will link us to another database table.
513
00:30:43,900 --> 00:30:46,900
So, over here I have this same exact employee table.
514
00:30:46,900 --> 00:30:51,600
And then I also have this
other attribute over here, branch_id.
515
00:30:51,600 --> 00:30:54,000
And you notice that I colored this in green.
516
00:30:54,000 --> 00:30:57,300
And this is what we would call a foreign key.
517
00:30:57,300 --> 00:31:04,900
And a foreign key stores the primary key
of a row in another database table.
518
00:31:04,900 --> 00:31:09,400
So, here we have our employee table
and I’m defining information about the employee.
519
00:31:09,400 --> 00:31:14,100
But let’s say that an employee belongs
to a specific branch in our company, right?
520
00:31:14,100 --> 00:31:16,100
So, a company might have different branches.
521
00:31:16,100 --> 00:31:23,600
And we can store the information about what branch
the employee belongs to inside of a foreign key.
522
00:31:23,600 --> 00:31:29,200
So, the foreign key over here is actually
a primary key inside of another table.
523
00:31:29,200 --> 00:31:30,600
In our case, a branch table.
524
00:31:30,600 --> 00:31:32,300
So, let me show you guys that.
525
00:31:32,300 --> 00:31:35,900
So, down here, we have this branch table
in our database, right?
526
00:31:35,900 --> 00:31:38,300
So, the branch is it’s own separate table.
527
00:31:38,300 --> 00:31:42,400
And we have – the branch has a primary key
over here which is 2, 3, 1.
528
00:31:42,400 --> 00:31:45,600
And the branch names are Scranton, Stamford,
and Corporate.
529
00:31:45,600 --> 00:31:49,300
So this in itself is its own table, right?
530
00:31:49,300 --> 00:31:49,900
It’s the branch table.
531
00:31:49,900 --> 00:31:54,800
And over here we can define which branch
a specific employee belongs to,
532
00:31:54,800 --> 00:32:00,200
by referring to the ID, the primary key of the branch.
533
00:32:00,200 --> 00:32:07,700
So, here, Jan Levinson, her branch ID is 1, which
means she is in the corporate branch, right?
534
00:32:07,700 --> 00:32:10,800
Because branch ID number 1 is corporate.
535
00:32:10,800 --> 00:32:16,800
Michael Scott, his branch ID is 2,
which means he’s in the Scranton branch.
536
00:32:16,800 --> 00:32:23,100
So this number is mapping this row over here
into this other table.
537
00:32:23,100 --> 00:32:24,900
And that’s what a foreign key does.
538
00:32:24,900 --> 00:32:31,200
A foreign key is essentially just a way that we can
define relationships between the two tables.
539
00:32:31,200 --> 00:32:35,100
So, a foreign key is just a primary key of another table.
540
00:32:35,100 --> 00:32:37,300
So, Andy Bernard, right?
541
00:32:37,300 --> 00:32:41,700
His branch ID is 3 which means he belongs
to the Stamford branch.
542
00:32:41,700 --> 00:32:44,000
And so, that’s what a foreign key does.
543
00:32:44,000 --> 00:32:49,000
A foreign key allows us to link up or define
relationships between tables.
544
00:32:49,000 --> 00:32:56,100
I can say that Andy belongs to the Stamford branch
and I can define that using the foreign key.
545
00:32:56,100 --> 00:33:02,000
And over here you'll see on the branch table, I also
defined another foreign key which is manager ID.
546
00:33:02,000 --> 00:33:04,800
mgr_id, that would stand for manager ID.
547
00:33:04,800 --> 00:33:10,500
And now this is actually going to be a foreign key
which connects branch to the employee table.
548
00:33:10,500 --> 00:33:16,700
So, manager ID is going to be the ID of a particular
employee who is the manager of the branch.
549
00:33:16,700 --> 00:33:19,000
So, let’s take a look at the Scranton branch.
550
00:33:19,000 --> 00:33:20,400
So, the branch ID is 2.
551
00:33:20,400 --> 00:33:24,000
The name of the branch is Scranton,
and the manager ID is 101.
552
00:33:24,000 --> 00:33:25,600
So, let’s go check it out.
553
00:33:25,600 --> 00:33:29,800
So, over here in the employee table,
employee 101 is Michael Scott.
554
00:33:29,800 --> 00:33:36,000
What that means is that Michael Scott
is the manager of the Scranton branch, right?
555
00:33:36,000 --> 00:33:40,300
We were able to define that relational
by using these foreign keys.
556
00:33:40,300 --> 00:33:44,300
So, down here, the Stamford branch,
the manager is 102.
557
00:33:44,300 --> 00:33:48,200
If we come up here, we can see
employee 102 is Josh Porter.
558
00:33:48,200 --> 00:33:51,200
So, Josh Porter is the manager
of the Stamford branch.
559
00:33:51,200 --> 00:33:54,200
Corporate over here, the manager ID is 108.
560
00:33:54,200 --> 00:33:57,900
So, over here we didn’t actually include
108 over there.
561
00:33:57,900 --> 00:34:01,500
So, obviously, this isn’t like, you know,
a huge table of employees.
562
00:34:01,500 --> 00:34:07,000
But the manager for the corporate branch
would be the employee with the ID of 108,
563
00:34:07,000 --> 00:34:09,600
whatever that would be inside of this table.
564
00:34:09,600 --> 00:34:11,600
So, that’s what a foreign key does.
565
00:34:11,600 --> 00:34:16,100
A foreign key is able to help us to define relationships
between the tables, right?
566
00:34:16,100 --> 00:34:20,500
I can define which branch
a specific employee belongs to
567
00:34:20,500 --> 00:34:26,600
by including the branch’s ID as a foreign key
in the employee table.
568
00:34:26,600 --> 00:34:30,900
Similarly, I can define which employee
is the manager of a branch
569
00:34:30,900 --> 00:34:36,700
by including that employee’s ID as a foreign key
in the branch table.
570
00:34:36,700 --> 00:34:40,200
And so, that’s really why foreign keys
are going to come in handy.
571
00:34:40,200 --> 00:34:45,200
And it’s also important to note that a particular table
can have more than one foreign key on it.
572
00:34:45,200 --> 00:34:47,600
So, ever here I have my employee table, right?
573
00:34:47,600 --> 00:34:50,300
And it’s the same exact thing as we have before.
574
00:34:50,300 --> 00:34:55,100
But I also added another attribute
or other column over here which is super_id.
575
00:34:55,100 --> 00:34:58,200
And that stands for supervisor ID.
576
00:34:58,200 --> 00:35:04,400
And so, what this is going to do is it’s going to define
who is the supervisor of a particular employee.
577
00:35:04,400 --> 00:35:09,500
And what’s cool about this foreign key
is it’s actually relating back to the same table.
578
00:35:09,500 --> 00:35:13,900
So, an employee can be a supervisor
of another employee.
579
00:35:13,900 --> 00:35:15,800
So, over here we have Angela Martin, right?
580
00:35:15,800 --> 00:35:17,800
Her employee ID is 103.
581
00:35:17,800 --> 00:35:20,800
And her supervisor ID is 101.
582
00:35:20,800 --> 00:35:26,100
And that means her supervisor
is the employee with the ID 101.
583
00:35:26,100 --> 00:35:28,100
So, if we come over here we can look.
584
00:35:28,100 --> 00:35:32,800
Angela Martin’s supervisor is Michael Scott because
Michael Scott has an employee ID of 101.
585
00:35:32,800 --> 00:35:34,400
So, Michael Scott.
586
00:35:34,400 --> 00:35:36,800
Michael Scott’s super ID is 100.
587
00:35:36,800 --> 00:35:43,500
That means Michael Scott’s supervisor is the
employee with ID 100, which is Jan Levison.
588
00:35:43,500 --> 00:35:47,500
So, we can these this foreign key over here
on the employee table
589
00:35:47,500 --> 00:35:50,000
to define relationships between employees.
590
00:35:50,000 --> 00:35:54,800
So, before we define the relationship between
the employee table and the branch table,
591
00:35:54,800 --> 00:36:00,100
and now we're defining a relationship between the
employee table and the employee table, right?
592
00:36:00,100 --> 00:36:03,700
And so, what’s cool about this is
then basically inside of this table,
593
00:36:03,700 --> 00:36:08,800
we can tell what the different supervisor
or supervisee relationships are, right?
594
00:36:08,800 --> 00:36:12,400
I can tell that Josh Porter’s supervisor is Jan Levinson.
595
00:36:12,400 --> 00:36:15,300
And I can tell that Angela Martin’s supervisor
is Michael Scott.
596
00:36:15,300 --> 00:36:19,100
Defining that information by using a foreign key.
597
00:36:19,100 --> 00:36:21,500
All right, so let’s take a look at another table.
598
00:36:21,500 --> 00:36:23,400
I actually added in another table down here.
599
00:36:23,400 --> 00:36:25,800
So, over here we have employee,
just like we had before.
600
00:36:25,800 --> 00:36:26,600
We have branch.
601
00:36:26,600 --> 00:36:28,900
And then we also have branch supplier.
602
00:36:28,900 --> 00:36:33,300
And branch supplier is another table
that we can look at.
603
00:36:33,300 --> 00:36:39,000
So, a branch supplier would basically define
who the suppliers are for specific branches.
604
00:36:39,000 --> 00:36:41,000
So, we have these branches.
605
00:36:41,000 --> 00:36:43,000
Maybe they're selling a certain product.
606
00:36:43,000 --> 00:36:44,900
Maybe they're selling like paper products.
607
00:36:44,900 --> 00:36:48,700
And the branch suppliers would define
who are the suppliers for that branch.
608
00:36:48,700 --> 00:36:54,200
And you'll notice up here, the primary key actually
consists of two columns.
609
00:36:54,200 --> 00:36:58,200
And this is what we would call
a composite key or a composite key.
610
00:36:58,200 --> 00:37:03,900
And a composite key is basically a key
that needs two attributes.
611
00:37:03,900 --> 00:37:07,700
So, this key is made up of two columns, right?
612
00:37:07,700 --> 00:37:10,800
It’s made up of branch_id and supplier_name.
613
00:37:10,800 --> 00:37:15,200
So, branch_id is actually going to refer
to the specific branch.
614
00:37:15,200 --> 00:37:20,300
And supplier_name is going to refer
to the specific supplier, right?
615
00:37:20,300 --> 00:37:24,900
So, we can say over here that Hammer Mill
supplies paper to branch number 2.
616
00:37:24,900 --> 00:37:28,600
So Hammer Mill supplies paper to Scranton.
617
00:37:28,600 --> 00:37:33,500
Uni-ball supplies writing utensils to
branch ID number 2, which is Scranton.
618
00:37:33,500 --> 00:37:38,500
Patriot Paper supplies paper to
branch ID number 3 which is Stamford.
619
00:37:38,500 --> 00:37:40,600
So, inside of this branch supplier table,
620
00:37:40,600 --> 00:37:46,800
I’m able to define which different suppliers are
supplying what to which different branches.
621
00:37:46,800 --> 00:37:51,900
Now, the reason that I need this composite key
or this composite key,
622
00:37:51,900 --> 00:37:56,900
is because the supplier name doesn’t
uniquely identify each row.
623
00:37:56,900 --> 00:38:00,000
And the branch_id doesn’t uniquely identify each row.
624
00:38:00,000 --> 00:38:04,400
Only together can they
uniquely identify each row, right?
625
00:38:04,400 --> 00:38:07,100
So, for example, supplier name, Hammer Mill.
626
00:38:07,100 --> 00:38:10,300
You’ll notice that Hammer Mill shows up here
and it also shows up here.
627
00:38:10,300 --> 00:38:12,500
So, Hammer Mill supplies to branch number 2.
628
00:38:12,500 --> 00:38:14,800
And they also supply to branch number 3.
629
00:38:14,800 --> 00:38:19,200
Uni-ball supplies to branch number 3
and Uni-ball supplies to branch number 2.
630
00:38:19,200 --> 00:38:22,800
So, this column has repeated values, right?
631
00:38:22,800 --> 00:38:25,500
This column has Hammer Mill repeated.
632
00:38:25,500 --> 00:38:26,900
It has Uni-ball repeated.
633
00:38:26,900 --> 00:38:30,100
branch_id also has those things repeated, right?
634
00:38:30,100 --> 00:38:32,800
So, branch_id 2 shows up here
a bunch of times.
635
00:38:32,800 --> 00:38:35,500
branch_id 3 shows up here a bunch of times, right?
636
00:38:35,500 --> 00:38:40,500
And so, the branch_id column
can’t uniquely identify the table.
637
00:38:40,500 --> 00:38:44,200
And the supplier name column
can’t uniquely identify the table.
638
00:38:44,200 --> 00:38:47,800
Only together can they uniquely identify it, right?
639
00:38:47,800 --> 00:38:50,400
So, we would say Hammer Mill supplies branch 2.
640
00:38:50,400 --> 00:38:53,100
That combination only shows up once.
641
00:38:53,100 --> 00:38:57,900
We wouldn't put that combination again
because we’re already defining it.
642
00:38:57,900 --> 00:38:59,900
So, Hammer Mill supplies branch 2.
643
00:38:59,900 --> 00:39:01,600
Hammer Mill supplies branch 3.
644
00:39:01,600 --> 00:39:07,200
These two rows are uniquely identified by the
identified by the supplier name and the branch ID.
645
00:39:07,200 --> 00:39:13,600
And so, over here in these employee and the branch
table, we only find one column as the primary key.
646
00:39:13,600 --> 00:39:18,800
But over here, we define two columns as
the primary key, which would be a composite key.
647
00:39:18,800 --> 00:39:23,700
And that’s actually pretty common, where we're going
to have two columns uniquely identifying each row.
648
00:39:23,700 --> 00:39:26,700
All right, so down here I want
to show you guys one more example.
649
00:39:26,700 --> 00:39:32,200
And this is going to show you one other way that we
can define like different relationships with these tables.
650
00:39:32,200 --> 00:39:34,600
So, I actually added in two other tables.
651
00:39:34,600 --> 00:39:38,200
We have a client table
and we have this Works With table.
652
00:39:38,200 --> 00:39:40,300
So, let me kind of walk you guys
through this a little bit.
653
00:39:40,300 --> 00:39:42,100
We still have employee.
We still have branch.
654
00:39:42,100 --> 00:39:43,700
But over here, we're defining clients.
655
00:39:43,700 --> 00:39:46,100
And so, a client might be like a customer, right?
656
00:39:46,100 --> 00:39:53,000
So, a client would buy paper products or, you know,
whatever products from the branch and employee.
657
00:39:53,000 --> 00:39:54,800
So, the client has a client ID.
658
00:39:54,800 --> 00:39:56,600
They have a client name.
659
00:39:56,600 --> 00:39:59,700
And then they also have a foreign key
which is a branch ID.
660
00:39:59,700 --> 00:40:03,900
So, a client is going to be associated
with a specific branch, right?
661
00:40:03,900 --> 00:40:07,600
So, we would say Lackawana County
is associated with branch 2.
662
00:40:07,600 --> 00:40:12,800
Which means the Lackawana County
is going to buy products from the Scranton branch.
663
00:40:12,800 --> 00:40:18,700
The John Daly Law, LLC is going to
buy products from branch ID number 3.
664
00:40:18,700 --> 00:40:21,700
So, they're going to buy products
from the Stamford branch, right?
665
00:40:21,700 --> 00:40:23,400
So, this is a client table.
666
00:40:23,400 --> 00:40:27,000
And down here, we have this Works_With table.
667
00:40:27,000 --> 00:40:31,200
And what the Works_With table is doing
is it’s defining the relationships
668
00:40:31,200 --> 00:40:33,700
between the employees and the clients.
669
00:40:33,700 --> 00:40:38,700
Namely, how much paper
an employee sells to a specific client.
670
00:40:38,700 --> 00:40:41,200
So over here we have employee ID.
671
00:40:41,200 --> 00:40:42,400
We have client ID.
672
00:40:42,400 --> 00:40:43,800
And we have total sales.
673
00:40:43,800 --> 00:40:48,000
So, the employee ID is going to refer
to an employee in the employee table.
674
00:40:48,000 --> 00:40:51,500
The client ID is going to refer
to a client in the client table.
675
00:40:51,500 --> 00:40:58,000
And then we're going to define how much in product
the client has bought from the employee.
676
00:40:58,000 --> 00:41:01,600
So, employee 101, for example, that is Michael Scott.
677
00:41:01,600 --> 00:41:05,700
Michael Scott has sold client ID 401.
678
00:41:05,700 --> 00:41:12,100
Michael Scott has sold Lackawana County
$267,000 worth of product, right?
679
00:41:12,100 --> 00:41:13,700
How about this one, 104.
680
00:41:13,700 --> 00:41:23,100
So, Andy Bernard has sold client number 403 –
has sold John Daly Law $5000 in paper products.
681
00:41:23,100 --> 00:41:30,200
And so, this Works_With table is able to define how
much an employee has sold to a client.
682
00:41:30,200 --> 00:41:33,200
And you'll notice over here
we have a composite key.
683
00:41:33,200 --> 00:41:36,900
And the composite key
is employee ID and client ID.
684
00:41:36,900 --> 00:41:40,400
And this is actually a special type of composite key
685
00:41:40,400 --> 00:41:45,300
because both of these columns
are actually foreign keys.
686
00:41:45,300 --> 00:41:47,700
So, employee ID is a foreign key, right?
687
00:41:47,700 --> 00:41:49,300
It relates to the employee table.
688
00:41:49,300 --> 00:41:51,800
Client ID is also a foreign key.
689
00:41:51,800 --> 00:41:53,800
It relates to the client table.
690
00:41:53,800 --> 00:42:00,700
And both of those foreign keys together
makes up the primary key of the table.
691
00:42:00,700 --> 00:42:05,700
And that is actually a very useful way
to define a primary key.
692
00:42:05,700 --> 00:42:09,000
Because when we do something like this,
we can define a relationship, right?
693
00:42:09,000 --> 00:42:14,000
So, I can define how much product
the client has bought from the employee.
694
00:42:14,000 --> 00:42:17,700
And that is something that can
because were useful to keep track of.
695
00:42:17,700 --> 00:42:22,900
So, as you can see, like these tables can either
be very simple or they can be very complex.
696
00:42:22,900 --> 00:42:28,600
And the more complex your database design
or your database schema is,
697
00:42:28,600 --> 00:42:32,800
the more complex these tables
and these keys are going to have to be, right?
698
00:42:32,800 --> 00:42:35,700
So, this employee table has a primary key
and two foreign keys.
699
00:42:35,700 --> 00:42:38,500
The client table has a primary key and a foreign key.
700
00:42:38,500 --> 00:42:42,400
This Works_With table has a composite key.
701
00:42:42,400 --> 00:42:43,200
You know what I mean?
702
00:42:43,200 --> 00:42:44,500
Both of which are foreign keys.
703
00:42:44,500 --> 00:42:49,800
Like it can get very complex, but we can use
the primary keys and the foreign keys
704
00:42:49,800 --> 00:42:52,200
in order to define different relationships.
705
00:42:52,200 --> 00:42:58,100
But really, that is just a, you know, I think a pretty
good introduction into kind of how tables work.
706
00:42:58,100 --> 00:42:59,500
We talked about rows and columns.
707
00:42:59,500 --> 00:43:01,300
We talked about primary keys.
708
00:43:01,300 --> 00:43:04,500
We talked about surrogate keys and natural keys.
709
00:43:04,500 --> 00:43:08,800
You know, the different types of keys
that might map to the real world or not.
710
00:43:08,800 --> 00:43:10,200
We talked about foreign keys.
711
00:43:10,200 --> 00:43:11,400
We talked about composite keys.
712
00:43:11,400 --> 00:43:16,700
And I kind of showed you guys a few different
examples of ways that things might work.
713
00:43:16,700 --> 00:43:19,500
So, this would be like our company database.
714
00:43:19,500 --> 00:43:22,300
And obviously, we would have
a lot more information in here.
715
00:43:22,300 --> 00:43:26,600
But this is kind of a good example of
how we might define different tables
716
00:43:26,600 --> 00:43:30,800
and then define the relationships between
those tables in a relational database.
717
00:43:30,800 --> 00:43:34,900
[SQL Basics]
718
00:43:34,900 --> 00:43:41,000
In this tutorial I’m going to walk you guys through SQL
which stands for Structured Query Language.
719
00:43:41,000 --> 00:43:45,200
So, SQL is a term that you're going to hear
thrown around all the time
720
00:43:45,200 --> 00:43:46,800
when you're talking about databases.
721
00:43:46,800 --> 00:43:49,000
And SQL actually is a language.
722
00:43:49,000 --> 00:43:55,700
And it’s a language that’s used for interacting with
relational database management systems.
723
00:43:55,700 --> 00:44:00,000
So, SQL is – it’s similar to –
kind of like a programming language.
724
00:44:00,000 --> 00:44:03,600
A lot of times you’ll hear people refer to SQL
as a programming language.
725
00:44:03,600 --> 00:44:07,900
It’s not technically a programming language
in the tradition sense.
726
00:44:07,900 --> 00:44:13,500
Although, you can use SQL to provide instructions
to a relational database management system.
727
00:44:13,500 --> 00:44:16,800
So, I guess if you want, you could call it
a programming language.
728
00:44:16,800 --> 00:44:19,800
So, like I said, SQL, it’s a language that’s used
729
00:44:19,800 --> 00:44:22,200
for interacting with relational database
management systems.
730
00:44:22,200 --> 00:44:25,500
A relational database management system
is a piece of software.
731
00:44:25,500 --> 00:44:32,600
It’s a software application that you can use to create
and maintain a relational database.
732
00:44:32,600 --> 00:44:35,600
You might use a relational database
management system
733
00:44:35,600 --> 00:44:39,600
to create a database for an application
that you're building.
734
00:44:39,600 --> 00:44:42,300
And then that relational database
management system
735
00:44:42,300 --> 00:44:45,600
can make sure that the database
is structured correctly
736
00:44:45,600 --> 00:44:48,800
and store everything the way
that it needs to be stored.
737
00:44:48,800 --> 00:44:53,900
So, in order to interact with a relational
database management system,
738
00:44:53,900 --> 00:44:55,900
we need to use a special language.
739
00:44:55,900 --> 00:44:58,300
So, I can’t just talk to it in English.
740
00:44:58,300 --> 00:45:01,600
I can’t just be like, “Hey, relational database
management system,
741
00:45:01,600 --> 00:45:03,600
get me this piece of information.”
742
00:45:03,600 --> 00:45:06,800
Relational database management systems
don’t speak English.
743
00:45:06,800 --> 00:45:09,900
But what they do speak is a language called SQL.
744
00:45:09,900 --> 00:45:12,100
They speak Structured Query Language.
745
00:45:12,100 --> 00:45:17,100
And so, if we want to ask a relational database
management system to do something for us,
746
00:45:17,100 --> 00:45:23,200
for example, like store a piece of information
or create a table, update a piece of information.
747
00:45:23,200 --> 00:45:28,800
We can ask the relational database management
system to do that using SQL.
748
00:45:28,800 --> 00:45:35,400
So, SQL is the language that we can use to
communicate with the database management system.
749
00:45:35,400 --> 00:45:41,100
And, you know, we can use SQL to get the relational
database management system to do stuff for us,
750
00:45:41,100 --> 00:45:43,300
like create, retrieve, update, and delete data.
751
00:45:43,300 --> 00:45:45,700
Create and manage different databases.
752
00:45:45,700 --> 00:45:49,000
Design and create database tables.
753
00:45:49,000 --> 00:45:54,200
So, we can define like a database schema
which would just be like the overall like table design.
754
00:45:54,200 --> 00:45:56,800
And perform administrative tasks.
755
00:45:56,800 --> 00:45:58,700
So, things like security.
756
00:45:58,700 --> 00:46:02,500
We could do like user management, importing,
exporting, backup, all that stuff.
757
00:46:02,500 --> 00:46:09,300
So, SQL can be used to tell the relational database
management system to do all of that stuff for us.
758
00:46:09,300 --> 00:46:13,600
Now, here’s the thing, SQL implementation
can vary between the systems.
759
00:46:13,600 --> 00:46:18,800
So, SQL, the actual language actually does
have a formal specification.
760
00:46:18,800 --> 00:46:25,700
So, there is like an overall formal specification
which defines, you know, how SQL needs to be used
761
00:46:25,700 --> 00:46:27,700
and all the different commands that can be used.
762
00:46:27,700 --> 00:46:32,400
But the problem is that there’s a bunch of these
relational database management system.
763
00:46:32,400 --> 00:46:39,000
Some of the popular ones we hear about, like
Postegres, MySQL, Oracle, Microsoft SQL Server.
764
00:46:39,000 --> 00:46:42,800
Like all of these relational database
management systems
765
00:46:42,800 --> 00:46:46,500
are going implement SQL just a little bit differently.
766
00:46:46,500 --> 00:46:52,100
So, you could write SQL code that would work on
one relational database management system,
767
00:46:52,100 --> 00:46:56,400
but then if you tried to use it on another one,
it might not work 100%.
768
00:46:56,400 --> 00:46:59,000
Now, for the most part, everything
should be the same,
769
00:46:59,000 --> 00:47:01,400
but you might just need to tweak
a couple little things.
770
00:47:01,400 --> 00:47:03,900
So, one thing you want to keep in mind with SQL
771
00:47:03,900 --> 00:47:09,300
is that, you know, SQL is used on all of the major
relationship database management systems,
772
00:47:09,300 --> 00:47:12,600
but it’s used slightly differently.
773
00:47:12,600 --> 00:47:16,200
So, certain instructions might work
on one database management system
774
00:47:16,200 --> 00:47:18,400
and they might not work on another, or vice versa.
775
00:47:18,400 --> 00:47:21,900
Or you might do things just
a little bit differently, depending.
776
00:47:21,900 --> 00:47:26,700
But for the most part, they’re all implementing SQL
which means it’s all basically the same.
777
00:47:26,700 --> 00:47:31,600
So, the concepts are the same
but the implementation may vary just a little bit.
778
00:47:31,600 --> 00:47:33,600
So, let’s talk a little bit more about SQL.
779
00:47:33,600 --> 00:47:35,600
It’s actually a hybrid language.
780
00:47:35,600 --> 00:47:39,700
So, it’s basically four types of languages in one.
781
00:47:39,700 --> 00:47:43,900
And you know, you don’t need to
worry too much about all this stuff,
782
00:47:43,900 --> 00:47:48,000
but you are going to hear people talking
about these different aspects of SQL.
783
00:47:48,000 --> 00:47:50,800
And so, I want to introduce them to you
so you understand what they are.
784
00:47:50,800 --> 00:47:56,700
So, SQL, you know like I said, it’s four types of
languages all mashed into one single language.
785
00:47:56,700 --> 00:48:04,600
So, SQL is a data query language, which means it can
be used to query the database for information.
786
00:48:04,600 --> 00:48:10,400
So, you can write queries in SQL which tell the
relational database management system
787
00:48:10,400 --> 00:48:14,100
what pieces of information
that you want to get from the database.
788
00:48:14,100 --> 00:48:19,700
And so, a data query language is used to get data
that’s already stored in the database.
789
00:48:19,700 --> 00:48:22,400
SQL is also a data definition language.
790
00:48:22,400 --> 00:48:27,400
And what that means is you can use
SQL to define database schemas.
791
00:48:27,400 --> 00:48:31,300
Now, a schema is basically just like the
overall layout of the database.
792
00:48:31,300 --> 00:48:37,100
Like what tables are going to be in the database,
what columns those tables are going to have,
793
00:48:37,100 --> 00:48:39,800
and the data types that those columns
are going to be able to store.
794
00:48:39,800 --> 00:48:47,100
So, we can use SQL to define data in the different,
like I said, database schemas.
795
00:48:47,100 --> 00:48:50,500
SQL is also a data control language which means
796
00:48:50,500 --> 00:48:53,900
it’s used for controlling access
to the data in the database.
797
00:48:53,900 --> 00:48:57,600
Basically, you can use it to configure
like users and permissions.
798
00:48:57,600 --> 00:49:01,600
So, I could define a bunch of different users
for the database.
799
00:49:01,600 --> 00:49:04,900
And I can say like, okay,
this user can write to this table.
800
00:49:04,900 --> 00:49:07,900
Or this user can read information from this table.
801
00:49:07,900 --> 00:49:11,500
Or this user can update
and delete information from this table.
802
00:49:11,500 --> 00:49:16,800
So, you can use SQL to control the access to the data.
803
00:49:16,800 --> 00:49:20,200
And SQL is also a data manipulation language.
804
00:49:20,200 --> 00:49:26,500
So, it’s used for inserting, updating,
and deleting data from the database.
805
00:49:26,500 --> 00:49:30,500
So, these are the four different types
of things that you can do with SQL.
806
00:49:30,500 --> 00:49:34,500
And you'll hear people using these terms kind of a lot.
807
00:49:34,500 --> 00:49:39,400
And even like database management systems
might throw error messages or certain things.
808
00:49:39,400 --> 00:49:42,600
And they'll say like data query language
or data definition language.
809
00:49:42,600 --> 00:49:48,100
So, it is good to just understand these
different types of things that SQL can do
810
00:49:48,100 --> 00:49:52,600
and how they're broken up into these four
like broad types of languages.
811
00:49:52,600 --> 00:49:56,400
But basically, the whole point of this slide
is that SQL can do a bunch of stuff.
812
00:49:56,400 --> 00:49:58,200
And it’s super powerful.
813
00:49:58,200 --> 00:50:03,700
And that’s one of the reasons why it’s used in
all these relational database management systems.
814
00:50:03,700 --> 00:50:05,300
So, now let’s talk about queries.
815
00:50:05,300 --> 00:50:09,200
And one of the things that we're going to be
doing a done of in this course,
816
00:50:09,200 --> 00:50:12,200
and you know, if you’re working with databases,
you’re going to be doing this all the time,
817
00:50:12,200 --> 00:50:13,200
which is querying.
818
00:50:13,200 --> 00:50:17,200
So, query is a set of instructions given to the
relational database management system
819
00:50:17,200 --> 00:50:24,900
generally written in SQL, that tell the RDBMS
what information you want it to retrieve for you.
820
00:50:24,900 --> 00:50:31,200
So, if you have a database that has like millions
or billions of records in it, like there’s tons of data.
821
00:50:31,200 --> 00:50:36,600
And a lot of times that data is, you know,
spread out across different tables, right?
822
00:50:36,600 --> 00:50:39,200
It’s sort of hidden in this complex schema.
823
00:50:39,200 --> 00:50:40,200
You know what I mean?
824
00:50:40,200 --> 00:50:42,700
Like you have one piece of information
stored over here
825
00:50:42,700 --> 00:50:45,900
and then another piece over here,
another piece over here in different tables.
826
00:50:45,900 --> 00:50:49,500
And you want to kind of grab all that information
and organize it in a specific way.
827
00:50:49,500 --> 00:50:55,000
We can use queries in order to tell
the RDBMS to do that for us.
828
00:50:55,000 --> 00:51:01,900
So, the goal of writing a query is that we only want
to get the information that we need, right?
829
00:51:01,900 --> 00:51:04,900
Imagine that if you, you know,
every time you wanted a piece of information
830
00:51:04,900 --> 00:51:09,200
from the database you had to grab all of the
information in the database, parse through it,
831
00:51:09,200 --> 00:51:11,400
and then find the information that you want.
832
00:51:11,400 --> 00:51:12,800
We don’t have to do that.
833
00:51:12,800 --> 00:51:19,700
Instead, you can write a query which will tell the
RDBMS exactly what piece of information you want
834
00:51:19,700 --> 00:51:23,300
and it’ll be able to grab just that information
and send it back to you.
835
00:51:23,300 --> 00:51:26,200
So, over here I just have a little example of a query.
836
00:51:26,200 --> 00:51:32,000
So, this would be like maybe we're trying to get –
this would be SELECT employee.name, employee.age.
837
00:51:32,000 --> 00:51:36,300
So, select the employee’s name and the age
from the employee table
838
00:51:36,300 --> 00:51:39,500
where the employee’s salary is greater than $30,000.
839
00:51:39,500 --> 00:51:43,000
So, what this will do is it’ll give us every employee
in an employee table
840
00:51:43,000 --> 00:51:45,600
who makes more than $30,000, right?
841
00:51:45,600 --> 00:51:46,800
That’s kind of like a general query.
842
00:51:46,800 --> 00:51:50,200
Don’t worry too much about specifically
what all this stuff means.
843
00:51:50,200 --> 00:51:52,300
We're going to get into that in this course.
844
00:51:52,300 --> 00:51:54,500
But that’s kind of like what a query would look like.
845
00:51:54,500 --> 00:52:01,200
We're telling the RDBMS what information we want
and then it only gives us back that information.
846
00:52:01,200 --> 00:52:03,800
Not like all the information in the database.
847
00:52:03,800 --> 00:52:06,600
All right, so that is kind of an overview of SQL.
848
00:52:06,600 --> 00:52:08,900
I mean obviously I didn’t get into everything.
849
00:52:08,900 --> 00:52:11,700
We're going to look at all of these, you know,
little specific things
850
00:52:11,700 --> 00:52:14,700
that we can do with SQL as we go forward.
851
00:52:14,700 --> 00:52:19,000
But for now, that should kind of give you
an idea of what SQL is, why it’s useful,
852
00:52:19,000 --> 00:52:25,000
and you know, sort of what it’s doing for us
and how we can use it to work with our databases.
853
00:52:25,000 --> 00:52:29,500
[MySQL Windows Installation]
854
00:52:29,500 --> 00:52:31,900
In this tutorial I’m going to show you guys
how to download
855
00:52:31,900 --> 00:52:34,800
and install two things that we're going
to need going forward in this course.
856
00:52:34,800 --> 00:52:38,200
The first thing and the most important
is called MySQL.
857
00:52:38,200 --> 00:52:41,600
And MySQL is a RDBMS.
858
00:52:41,600 --> 00:52:44,900
Which basically means it’s a software application
859
00:52:44,900 --> 00:52:50,300
which we can use to maintain and create
and do all that sort of stuff with databases.
860
00:52:50,300 --> 00:52:54,200
And so, when we have MySQL
installed on our computer,
861
00:52:54,200 --> 00:52:58,200
we can actually set up what’s called
a MySQL database server.
862
00:52:58,200 --> 00:53:02,100
And that would basically be a server
where MySQL is running.
863
00:53:02,100 --> 00:53:05,700
And then we can write SQL in
all sorts of queries and instructions
864
00:53:05,700 --> 00:53:09,400
in order to create
and do all sorts of stuff with databases.
865
00:53:09,400 --> 00:53:12,700
So, the first thing we'll do is download
and install MySQL.
866
00:53:12,700 --> 00:53:16,100
And the second thing we're going to do
is we're going to install another program
867
00:53:16,100 --> 00:53:19,100
which is called PopSQL, or PopSQL.
868
00:53:19,100 --> 00:53:22,000
And it’s basically a program
that I’m going to be using in this course
869
00:53:22,000 --> 00:53:24,900
in order to kind of like write all of my queries.
870
00:53:24,900 --> 00:53:26,100
It’s essentially a text editor.
871
00:53:26,100 --> 00:53:30,600
But it’s a text editor that can hook up
with our MySQL database.
872
00:53:30,600 --> 00:53:34,900
And it’ll just be a really easy interface
and a really easy way for us to write queries
873
00:53:34,900 --> 00:53:36,900
and get the information back.
874
00:53:36,900 --> 00:53:39,800
So, I’m going to show you guys how to download
and install both of those things.
875
00:53:39,800 --> 00:53:42,800
The first thing we want to do is open our browser
and we're going to install MySQL.
876
00:53:42,800 --> 00:53:44,200
So, you just want to come up here
877
00:53:44,200 --> 00:53:49,100
and you're just going to search in Google
for MySQL community server.
878
00:53:49,100 --> 00:53:52,700
So MySQL community server is this first link
right here.
879
00:53:52,700 --> 00:53:54,700
It’s just dev.MySQL.com.
880
00:53:54,700 --> 00:53:59,000
And this is actually a free
and opensource piece of software.
881
00:53:59,000 --> 00:54:01,400
It’s basically like the most basic version of MySQL.
882
00:54:01,400 --> 00:54:03,400
And it’s the version that we're going to be using.
883
00:54:03,400 --> 00:54:07,100
So, if you’re a beginner,
this is probably what you want to go for.
884
00:54:07,100 --> 00:54:09,500
It’s kind of just like a great environment
for you to learn in.
885
00:54:09,500 --> 00:54:10,300
It’s pretty simple.
886
00:54:10,300 --> 00:54:12,700
So, down here you have a couple different options.
887
00:54:12,700 --> 00:54:16,800
You can download just like a zip
or you can download the actual installer.
888
00:54:16,800 --> 00:54:20,700
So, you’ll see over here there’s
this option for the MySQL installer.
889
00:54:20,700 --> 00:54:22,100
So, you’re going to go ahead and click on that.
890
00:54:22,100 --> 00:54:24,900
And then that’ll bring you to this menu over here.
891
00:54:24,900 --> 00:54:29,200
And you can see here’s the MSI installer.
892
00:54:29,200 --> 00:54:31,400
So, we're just going to go ahead and download that.
893
00:54:31,400 --> 00:54:32,800
And it brings you to this page.
894
00:54:32,800 --> 00:54:36,100
It’s asking you to like log in or sign up,
but you can just click No Thanks down here.
895
00:54:36,100 --> 00:54:38,100
And it’ll go ahead and start the download.
896
00:54:38,100 --> 00:54:41,700
So, this installer is really great
because it will kind of configure everything for us
897
00:54:41,700 --> 00:54:46,600
and it’ll allow us to pick and choose what MySQL
products that we want to actually download.
898
00:54:46,600 --> 00:54:52,200
So, let’s go ahead and accept the license
and I’m going to click Next now.
899
00:54:52,200 --> 00:54:54,900
Here we have a couple options
for how we want to set this up.
900
00:54:54,900 --> 00:54:59,600
You can see you can just do like Developer,
Default, Server only, Client only.
901
00:54:59,600 --> 00:55:02,300
What we want to do is –
actually, we're going to do a custom install.
902
00:55:02,300 --> 00:55:05,500
Now, here’s the thing, if you want,
you can just do the developer default.
903
00:55:05,500 --> 00:55:09,400
But that’s going to install a bunch of stuff
that we're not actually going to be using.
904
00:55:09,400 --> 00:55:12,200
Or at least that I’m not going to be using
in this course.
905
00:55:12,200 --> 00:55:15,600
So, I’m going to ahead and click custom
because I only need to install a couple things.
906
00:55:15,600 --> 00:55:19,500
So, the first thing we’ll do is come down here
to MySQL Servers
907
00:55:19,500 --> 00:55:23,500
and I’m just going to click MySQL server 5,700 right here.
908
00:55:23,500 --> 00:55:27,600
And I’m going to go ahead and add that over here
to the right side.
909
00:55:27,600 --> 00:55:30,800
And then I’m going to come down here
to applications.
910
00:55:30,800 --> 00:55:34,100
And I’m going to come down here to MySQL Shell.
911
00:55:34,100 --> 00:55:35,300
And I’m going to do the same thing.
912
00:55:35,300 --> 00:55:36,900
So, I’m just going to click down here to MySQL shell.
913
00:55:36,900 --> 00:55:38,500
I’m doing the 64 bit version.
914
00:55:38,500 --> 00:55:39,900
I’m going to add that over here.
915
00:55:39,900 --> 00:55:42,800
So, these are the only two things
that I’m really going to be using for this course.
916
00:55:42,800 --> 00:55:45,000
But if you want, you can download everything.
917
00:55:45,000 --> 00:55:46,400
That way, you just have it.
918
00:55:46,400 --> 00:55:47,200
So, I’m going to click Next.
919
00:55:47,200 --> 00:55:48,200
I’m going to click Execute.
920
00:55:48,200 --> 00:55:51,300
And this is going to go ahead
and install both of those things for us.
921
00:55:51,300 --> 00:55:52,900
So, now you can see they’re downloading.
922
00:55:52,900 --> 00:55:54,300
And this shouldn't take too long.
923
00:55:54,300 --> 00:55:55,500
They should be downloaded pretty quickly.
924
00:55:55,500 --> 00:55:57,100
And then we'll be able to move on.
925
00:55:57,100 --> 00:56:00,600
All right, once those two things are finished
downloading, then we can just click next.
926
00:56:00,600 --> 00:56:03,200
And we can also just click next through here.
927
00:56:03,200 --> 00:56:05,600
And you’ll see it’s asking us what we want.
928
00:56:05,600 --> 00:56:09,100
I’m just going to click standalone,
MySQL Server, Classic, MySQL Replication.
929
00:56:09,100 --> 00:56:10,100
That’s all we need.
930
00:56:10,100 --> 00:56:14,700
And you can just leave all of this stuff,
all this Type and Networking stuff as the default.
931
00:56:14,700 --> 00:56:18,000
And then over here, it’s asking us to create
a root password.
932
00:56:18,000 --> 00:56:24,600
So, whenever we're using this MySQL database server,
we're going to have to log into it using an account.
933
00:56:24,600 --> 00:56:29,300
So, there’s already a default admin account
set up for us which is called Root.
934
00:56:29,300 --> 00:56:30,800
But we're going to have to give this a password.
935
00:56:30,800 --> 00:56:32,600
So, I’m just going to type in password
as the password.
936
00:56:32,600 --> 00:56:34,400
And again, down here.
937
00:56:34,400 --> 00:56:39,500
So, make sure that you remember what this password
is because we're actually going to need it later.
938
00:56:39,500 --> 00:56:42,100
And if you want, you can add additional users.
939
00:56:42,100 --> 00:56:43,100
I’m not going to do that.
940
00:56:43,100 --> 00:56:43,900
So, then we can just click Next.
941
00:56:43,900 --> 00:56:45,600
And you'll see there’s an option here.
942
00:56:45,600 --> 00:56:48,000
It says Start the MySQL Server at system start-up.
943
00:56:48,000 --> 00:56:51,400
And if you want, MySQL to start whenever
you start-up the system
944
00:56:51,400 --> 00:56:53,000
you can go ahead and keep that checked.
945
00:56:53,000 --> 00:56:54,700
And then I’m just going to click Next.
946
00:56:54,700 --> 00:56:55,900
And click Next.
947
00:56:55,900 --> 00:56:57,700
And just click Execute.
948
00:56:57,700 --> 00:57:01,300
So, this is going to go through and do a bunch of stuff
that we need to do to get this set up.
949
00:57:01,300 --> 00:57:05,400
So, you'll notice that it’s actually, you know,
not super trivial for us to set this up.
950
00:57:05,400 --> 00:57:07,600
I mean obviously the installer makes it really easy.
951
00:57:07,600 --> 00:57:09,800
But MySQL is a complex program.
952
00:57:09,800 --> 00:57:13,100
And that’s why there’s all these little things
that we have to keep configuring.
953
00:57:13,100 --> 00:57:16,500
But we're almost done
and then we'll be able to move on.
954
00:57:16,500 --> 00:57:21,200
So, now that everything is finished configuring,
we can just click finish and I’m going to click Next.
955
00:57:21,200 --> 00:57:23,400
And we can just click Finish.
956
00:57:23,400 --> 00:57:27,000
All right, so once we've finished installing everything,
957
00:57:27,000 --> 00:57:30,300
now what we need to do is we're going to actually
create a database that we can use.
958
00:57:30,300 --> 00:57:34,500
So, I’m going to come over here and we're just going
to come over here into the Start Menu.
959
00:57:34,500 --> 00:57:38,200
And you’ll see there’s this option here,
MySQL 5,700 command line client.
960
00:57:38,200 --> 00:57:40,600
So, we're going to actually go ahead and use that.
961
00:57:40,600 --> 00:57:44,500
And what this will do when I open it up,
you’ll see it says Enter Password.
962
00:57:44,500 --> 00:57:47,100
So, I’m just going to enter in that password
that I created.
963
00:57:47,100 --> 00:57:48,500
In my case, it was just password.
964
00:57:48,500 --> 00:57:51,200
And you'll see here we're actually logged in.
965
00:57:51,200 --> 00:57:56,600
So, now we're connected to that MySQL server
that is running on our computer.
966
00:57:56,600 --> 00:58:00,500
So, from in here, what we can actually do
is create a database.
967
00:58:00,500 --> 00:58:03,900
So, I’m just going to say create database.
968
00:58:03,900 --> 00:58:06,600
And I’m going to give it a name.
969
00:58:06,600 --> 00:58:07,400
So, I’m going to call it giraffe.
970
00:58:07,400 --> 00:58:09,400
And then I’m going to type a semicolon.
971
00:58:09,400 --> 00:58:12,000
So, what this will do, is it’ll create a database
for us called giraffe.
972
00:58:12,000 --> 00:58:15,100
Now remember, MySQL is a database
management system
973
00:58:15,100 --> 00:58:20,700
which means it’s an application or a software program
that’s designed to help us manage various databases.
974
00:58:20,700 --> 00:58:24,200
So, in order to start building a database,
we need to create one.
975
00:58:24,200 --> 00:58:26,200
So, I’m just going to say create database giraffe.
976
00:58:26,200 --> 00:58:27,200
I’ll hit enter.
977
00:58:27,200 --> 00:58:30,200
And this is going to go ahead and create
that database for us.
978
00:58:30,200 --> 00:58:33,500
So, now we've created this database,
we're kind of ready to go.
979
00:58:33,500 --> 00:58:37,300
So, for the rest of the course, what I’m going to be
doing is teaching you guys like SQL commands.
980
00:58:37,300 --> 00:58:40,100
So, we're going to be learning
all about this SQL language
981
00:58:40,100 --> 00:58:43,000
which is used to interact with MySQL
and other database management systems.
982
00:58:43,000 --> 00:58:47,200
Now, if you want, you can do everything
that I’m doing for the rest of the course
983
00:58:47,200 --> 00:58:48,600
here inside of this terminal.
984
00:58:48,600 --> 00:58:53,100
So, inside of this little terminal window you can
basically do everything that I’m doing.
985
00:58:53,100 --> 00:58:55,500
You can type in SQL like this up here.
986
00:58:55,500 --> 00:58:56,500
Create database.
987
00:58:56,500 --> 00:58:58,200
That’s all valid SQL.
988
00:58:58,200 --> 00:59:03,200
But I’m actually going to be using a different program
which is called PopSQL.
989
00:59:03,200 --> 00:59:07,300
And that program just makes it a lot easier
for us to visualize what’s going on.
990
00:59:07,300 --> 00:59:10,900
It’s not like some kind of boring terminal window.
991
00:59:10,900 --> 00:59:15,600
So, that’s basically a text editor
which will connect to our database server
992
00:59:15,600 --> 00:59:18,200
and we'll be able to write our SQL
from inside of there.
993
00:59:18,200 --> 00:59:20,900
So, I’m going to show you guys real quick
how to download that.
994
00:59:20,900 --> 00:59:22,500
You’re just going to come up here.
995
00:59:22,500 --> 00:59:23,500
I’ll make a new tab.
996
00:59:23,500 --> 00:59:28,600
And we're going to come up to the search bar
and just type in P-O-P S-Q-L, PopSQL.
997
00:59:28,600 --> 00:59:33,600
And you'll see this link here, PopSQL.io.
998
00:59:33,600 --> 00:59:34,900
So, we're just going to click that.
999
00:59:34,900 --> 00:59:37,500
So, here there should be an option
to download for Windows
1000
00:59:37,500 --> 00:59:40,600
so we can just click on that
and that’ll start downloading it for us.
1001
00:59:40,600 --> 00:59:42,100
So, PopSQL is cool.
1002
00:59:42,100 --> 00:59:46,900
Actually, it’s kind of like Google Docs
but for writing SQL queries.
1003
00:59:46,900 --> 00:59:49,700
Now, we're not going to be using that functionality.
1004
00:59:49,700 --> 00:59:51,800
We're just going to be using it as a text editor.
1005
00:59:51,800 --> 00:59:56,600
But it’s a great way for us to visualize
like the SQL queries that we're writing
1006
00:59:56,600 --> 00:59:59,700
and then also like what gets returned
from those queries and stuff like that.
1007
00:59:59,700 --> 01:00:04,500
So, once that’s done downloading, we can just click on
it and we're going to go ahead and run the installer.
1008
01:00:04,500 --> 01:00:07,500
When that’s done installing,
it should open up right away.
1009
01:00:07,500 --> 01:00:09,200
And you’ll see we have to sign in.
1010
01:00:09,200 --> 01:00:11,600
So, you can either create
an account just with Google
1011
01:00:11,600 --> 01:00:15,200
or you can create an account with your email,
but you have to sign in in order to use it.
1012
01:00:15,200 --> 01:00:16,400
So, I’m going to go ahead and sign in.
1013
01:00:16,400 --> 01:00:18,000
And you’ll see it says, “Welcome, Mike.”
1014
01:00:18,000 --> 01:00:19,400
So, we're able to sign in.
1015
01:00:19,400 --> 01:00:20,800
And I’ll open the app.
1016
01:00:20,800 --> 01:00:25,500
So, now that we have the app open,
what it’s asking us to do is connect to a database.
1017
01:00:25,500 --> 01:00:31,200
So, remember, we set up our MySQL database server
and we created a database.
1018
01:00:31,200 --> 01:00:36,000
So, what we can do now is we can connect
to that database from inside of PopSQL.
1019
01:00:36,000 --> 01:00:41,100
So, over here, it’s just asking us to type in our
nickname, so I’m just going to type in Giraffe.
1020
01:00:41,100 --> 01:00:44,900
And the type, if you click down,
you’ll see we have MySQL right there.
1021
01:00:44,900 --> 01:00:49,000
Host name – so the host name
is actually going to be localhost.
1022
01:00:49,000 --> 01:00:54,600
And that just refers to like the local address
of the computer that you're currently on.
1023
01:00:54,600 --> 01:00:56,500
Port is going to be 3306.
1024
01:00:56,500 --> 01:01:00,500
Assuming that you used all the defaults
when you're installing MySQL,
1025
01:01:00,500 --> 01:01:01,800
then that should be the port number.
1026
01:01:01,800 --> 01:01:06,000
And then finally down here, the database
we want to connect to was called giraffe.
1027
01:01:06,000 --> 01:01:07,600
So, that’s what I named it.
1028
01:01:07,600 --> 01:01:09,900
If you named it different from giraffe,
but in the name there.
1029
01:01:09,900 --> 01:01:14,700
And then username is going to be root and the
password is going to be the password that you set up.
1030
01:01:14,700 --> 01:01:16,300
So, in my case it was just password.
1031
01:01:16,300 --> 01:01:18,800
So, once all that information is in,
we can just click connect,
1032
01:01:18,800 --> 01:01:21,000
and you'll see it’s connected us to our database.
1033
01:01:21,000 --> 01:01:24,000
So, now we basically have a text editor
that we can use,
1034
01:01:24,000 --> 01:01:31,000
which is hooked up to our database so we can write
all of our SQL code, all of our SQL queries in here.
1035
01:01:31,000 --> 01:01:37,700
And it’ll actually get run
on our MySQL database server.
1036
01:01:37,700 --> 01:01:39,000
So, now everything is set up.
1037
01:01:39,000 --> 01:01:41,500
And we're ready to go off
and learn some awesome SQL commands.
1038
01:01:41,500 --> 01:01:44,400
Now, I realize that this was kind of
like a little bit of a complex setup.
1039
01:01:44,400 --> 01:01:46,200
Unfortunately, that’s just unavoidable
1040
01:01:46,200 --> 01:01:51,200
because database management systems are,
by nature, you know, complex programs.
1041
01:01:51,200 --> 01:01:55,700
I mean this isn’t – they’re not designed to be like
extremely user friendly, so.
1042
01:01:55,700 --> 01:01:58,600
But now we're ready to go and everything is set up.
1043
01:01:58,600 --> 01:02:02,300
[MySQL Mac Installation]
1044
01:02:02,300 --> 01:02:05,400
In this tutorial I’m going to show you guys
how to download and install two things
1045
01:02:05,400 --> 01:02:07,400
that we're going to need going forward in this course.
1046
01:02:07,400 --> 01:02:12,000
Now, the first thing we're going to download
and install and the most important is MySQL.
1047
01:02:12,000 --> 01:02:15,500
And MySQL is a relational database
management system.
1048
01:02:15,500 --> 01:02:19,600
And it’s actually one of the most popular relational
database management systems around.
1049
01:02:19,600 --> 01:02:24,800
And a lot of people use MySQL
as they're first sort of dip
1050
01:02:24,800 --> 01:02:27,700
or they're first dive into
a database management system.
1051
01:02:27,700 --> 01:02:32,800
So, for that reason, I’ve picked MySQL as the
relational database management system
1052
01:02:32,800 --> 01:02:35,700
that we're going to use in order to learn SQL.
1053
01:02:35,700 --> 01:02:41,700
Now it’s important to note that the focus of this
course isn’t so much on MySQL as it is on SQL.
1054
01:02:41,700 --> 01:02:44,400
So, SQL is Structured Query Language.
1055
01:02:44,400 --> 01:02:45,400
It’s an actual language.
1056
01:02:45,400 --> 01:02:50,200
And MySQL is a relationship database
management system.
1057
01:02:50,200 --> 01:02:54,500
So, we're going to be using MySQL
in order to kind of learn about
1058
01:02:54,500 --> 01:02:57,600
and use and learn all the most
common stuff with SQL.
1059
01:02:57,600 --> 01:03:02,400
The second thing we're going to install in addition
to MySQL is actually going to be optional.
1060
01:03:02,400 --> 01:03:05,700
You don’t have to install it,
but it’s going to be a text editor
1061
01:03:05,700 --> 01:03:09,700
that we can use in order to
write our SQL commands.
1062
01:03:09,700 --> 01:03:13,300
So, it’s called PopSQL.
1063
01:03:13,300 --> 01:03:16,800
And it’s an awesome program which basically allows
you to write out SQL statements.
1064
01:03:16,800 --> 01:03:20,300
And then you can execute them
from inside the program
1065
01:03:20,300 --> 01:03:22,300
and it’ll show you all the results that you get back.
1066
01:03:22,300 --> 01:03:26,200
So, I’m going to be using PopSQL in order to kind of
visualize everything that we're doing
1067
01:03:26,200 --> 01:03:28,700
and just make it a lot easier for you guys
to follow along.
1068
01:03:28,700 --> 01:03:31,800
So, I’m going to show you guys how to download
both those things so you can follow along with me.
1069
01:03:31,800 --> 01:03:36,300
First thing we're going to do is head over to our
browser and we're going to install MySQL.
1070
01:03:36,300 --> 01:03:40,300
So, you just want to type in MySQL community server.
1071
01:03:40,300 --> 01:03:41,900
And we're just going to type that in to Google.
1072
01:03:41,900 --> 01:03:44,200
And then down here, this should pop up.
1073
01:03:44,200 --> 01:03:47,900
It’s just download MySQL Community Server
from dev.MySQL.com.
1074
01:03:47,900 --> 01:03:54,700
So, MySQL Community Server is a free and
opensource version of MySQL that anybody can use.
1075
01:03:54,700 --> 01:03:58,600
And if you’re just starting off and learning about
SQL and learning about databases,
1076
01:03:58,600 --> 01:04:00,300
then this is a great starting point.
1077
01:04:00,300 --> 01:04:03,200
And then all we have to do down here is scroll down
1078
01:04:03,200 --> 01:04:05,800
and you’re going to see that
there’s a couple different options here.
1079
01:04:05,800 --> 01:04:09,500
Basically, what we want to download
is the DMG Archive.
1080
01:04:09,500 --> 01:04:12,000
So, it’s just this first one right here.
1081
01:04:12,000 --> 01:04:13,900
So, I'm just going to go ahead and click Download.
1082
01:04:13,900 --> 01:04:16,400
And that’s going to start downloading for us.
1083
01:04:16,400 --> 01:04:18,200
Now, it brings you to this page.
1084
01:04:18,200 --> 01:04:20,200
It’s kind of asking you to log in or sign up,
but you don’t have to.
1085
01:04:20,200 --> 01:04:23,100
You can just click No thanks, just start my download.
1086
01:04:23,100 --> 01:04:25,300
And then it’ll start the download for you.
1087
01:04:25,300 --> 01:04:28,000
So, this is – it’s probably around like 400 megabytes.
1088
01:04:28,000 --> 01:04:32,300
It’s not too too big of a file,
but it’s also not like a super small file.
1089
01:04:32,300 --> 01:04:36,200
You’re probably going to need at least over
a gigabyte of storage on your computer
1090
01:04:36,200 --> 01:04:38,700
to hold everything that SQL is going to need.
1091
01:04:38,700 --> 01:04:42,200
All right, once that is finished downloading,
then we can just click on it
1092
01:04:42,200 --> 01:04:45,700
and we're going to go ahead and install MySQL
onto the computer.
1093
01:04:45,700 --> 01:04:48,000
All right, so here we just have this like PKG.
1094
01:04:48,000 --> 01:04:49,600
So, I'm just going to double click on this.
1095
01:04:49,600 --> 01:04:51,100
And it’ll open up this installer.
1096
01:04:51,100 --> 01:04:52,500
So, we're just going to click through.
1097
01:04:52,500 --> 01:04:55,800
And you can basically just leave everything
as the default.
1098
01:04:55,800 --> 01:04:59,500
Just keep it in the default install location
and it’s asking me for my password.
1099
01:04:59,500 --> 01:05:00,600
All right.
1100
01:05:00,600 --> 01:05:05,300
So once that is done running the installer,
this window right here is going to pop up.
1101
01:05:05,300 --> 01:05:07,000
It says, MySQL Installer.
1102
01:05:07,000 --> 01:05:08,400
It’s giving me this date.
1103
01:05:08,400 --> 01:05:13,200
And it says “A temporary password
is generated for root@localhost.”
1104
01:05:13,200 --> 01:05:15,500
Now, when we're using MySQL
1105
01:05:15,500 --> 01:05:18,800
we’re actually going to have to
log into the SQL server
1106
01:05:18,800 --> 01:05:21,200
using a username and password.
1107
01:05:21,200 --> 01:05:25,600
And in order to log in, you’re actually going
to need this temporary password
1108
01:05:25,600 --> 01:05:27,600
that they provide for you right now.
1109
01:05:27,600 --> 01:05:30,100
Now, it’s root@localhost.
1110
01:05:30,100 --> 01:05:33,200
So, root is the actual username
that we're going to use to login.
1111
01:05:33,200 --> 01:05:37,300
Localhost is going to be address of the SQL server
that’s running on our computer.
1112
01:05:37,300 --> 01:05:40,400
And then this is going to be
like a temporary password.
1113
01:05:40,400 --> 01:05:42,500
So, what you want to do is just copy this.
1114
01:05:42,500 --> 01:05:42,600
You want to make sure that you keep this.
1115
01:05:42,600 --> 01:05:46,100
So, what you could do, is you could
just click Command+Shift+4.
1116
01:05:46,100 --> 01:05:49,800
And you'll see this little like thing comes up.
1117
01:05:49,800 --> 01:05:52,600
If you just drag around here,
that’s going to screenshot this for you.
1118
01:05:52,600 --> 01:05:55,000
And so, now we'll have a picture of this
on your desktop.
1119
01:05:55,000 --> 01:05:57,100
Point is, is you don’t want to lose this.
1120
01:05:57,100 --> 01:05:59,600
So you want to make sure that you have this
temporary password accessible.
1121
01:05:59,600 --> 01:06:04,100
If you don’t, then it’s going to be a problem because
you're not going to be able to get into the account.
1122
01:06:04,100 --> 01:06:07,200
So, make sure that you record that.
1123
01:06:07,200 --> 01:06:09,900
And then I’m just going to click Okay
and then we're done here, so we can close it.
1124
01:06:09,900 --> 01:06:11,300
And I’ll move it to trash.
1125
01:06:11,300 --> 01:06:17,100
All right, so once we've installed MySQL,
now what I want to do is set up our MySQL server
1126
01:06:17,100 --> 01:06:21,700
which basically means that we're going to be running
MySQL on our computer.
1127
01:06:21,700 --> 01:06:25,600
Now the way this works is MySQL is a relational
database management system.
1128
01:06:25,600 --> 01:06:30,700
And it’s basically going to act as like
a little database server for us.
1129
01:06:30,700 --> 01:06:33,000
And so, we can connect to MySQL.
1130
01:06:33,000 --> 01:06:36,700
We can login and then we can
manage the database from there.
1131
01:06:36,700 --> 01:06:40,800
So, we need to make sure that the MySQL server
is started on our computer.
1132
01:06:40,800 --> 01:06:43,100
So, I’m just going to come up here to the search bar.
1133
01:06:43,100 --> 01:06:45,600
And I’m just going to search for System Preferences.
1134
01:06:45,600 --> 01:06:48,900
And then down here there should be
this little icon for MySQL.
1135
01:06:48,900 --> 01:06:50,500
So, we're just going to click on that.
1136
01:06:50,500 --> 01:06:53,000
And you’ll see it’s going to open up
this window over here.
1137
01:06:53,000 --> 01:06:54,600
So, it says, “MySQL Server Status.
1138
01:06:54,600 --> 01:06:57,300
The MySQL Database Server is currently stopped.”
1139
01:06:57,300 --> 01:06:59,400
So, what we want to do is start it.
1140
01:06:59,400 --> 01:07:01,200
And I’m going to put my password in.
1141
01:07:01,200 --> 01:07:01,200
And then if you want, you can have it automatically
start when you open your computer.
1142
01:07:01,200 --> 01:07:08,500
It says, “Automatically start MySQL Server
on Startup.”
1143
01:07:08,500 --> 01:07:10,500
If you want to do that, you can.
1144
01:07:10,500 --> 01:07:11,400
You don’t have to.
1145
01:07:11,400 --> 01:07:15,100
So, basically, now a MySQL database server
is running on our computer.
1146
01:07:15,100 --> 01:07:20,200
And what we can do now is we can connect
to that database server
1147
01:07:20,200 --> 01:07:23,300
and we can log in and then we can
start creating our databases
1148
01:07:23,300 --> 01:07:26,000
and creating all of our tables and stuff like that.
1149
01:07:26,000 --> 01:07:29,100
So, now that this is set up,
there’s actually a couple things that we have to do.
1150
01:07:29,100 --> 01:07:33,000
The first thing I want to do is update our password.
1151
01:07:33,000 --> 01:07:35,100
So, we were given that temporary password.
1152
01:07:35,100 --> 01:07:36,100
So, I'm going to open up the terminal.
1153
01:07:36,100 --> 01:07:39,000
And you can just go over here to the search bar
and type in terminal.
1154
01:07:39,000 --> 01:07:40,600
And that’ll open this guy up right here.
1155
01:07:40,600 --> 01:07:43,700
So, from in here, we need to start configuring
a couple different things.
1156
01:07:43,700 --> 01:07:48,300
First thing we need to do is get this terminal
to recognize the MySQL command.
1157
01:07:48,300 --> 01:07:54,300
So, if I come over here and I try to type in MySQL,
you’ll notice that it says command not found.
1158
01:07:54,300 --> 01:08:00,200
Basically, our Mac doesn’t know about this MySQL
command because we haven’t told it where it is.
1159
01:08:00,200 --> 01:08:03,300
So, there’s a couple things that we have to do
in order to get this MySQL command to work.
1160
01:08:03,300 --> 01:08:09,100
So, all we have to do is basically just tell
our computer where we installed MySQL.
1161
01:08:09,100 --> 01:08:11,800
So, what I’m going to do,
is I’m just going to type this over here
1162
01:08:11,800 --> 01:08:13,200
and you guys can type exactly what I’m typing.
1163
01:08:13,200 --> 01:08:27,900
Echo single-quote, export PATH in all caps
is equal to /usr/local/mysql/bin.
1164
01:08:27,900 --> 01:08:31,400
And then you’re going to type a colon$PATH
in all caps.
1165
01:08:31,400 --> 01:08:32,400
Just like that.
1166
01:08:32,400 --> 01:08:34,900
And then you're going to end off the quotation mark.
1167
01:08:34,900 --> 01:08:36,300
So, you’re going to do single-quote.
1168
01:08:36,300 --> 01:08:38,600
And then two greater than signs.
1169
01:08:38,600 --> 01:08:42,500
And we're just going to say ~
which is this little tilde character.
1170
01:08:42,500 --> 01:08:46,200
/.bash_profile.
1171
01:08:46,200 --> 01:08:53,200
So, what this is doing is it’s actually adding this line
inside of this file called .bash_profile.
1172
01:08:53,200 --> 01:08:59,000
Basically, what this will do is it’ll make it so we can use
that MySQL whenever we want inside the terminal.
1173
01:08:59,000 --> 01:09:01,300
So, I'm just going to hit Enter.
1174
01:09:01,300 --> 01:09:06,600
And then I’m just going to .tilde/.bash_profile.
1175
01:09:06,600 --> 01:09:08,500
This will kind of reload that file.
1176
01:09:08,500 --> 01:09:11,300
And now what you want to do is type in MySQL.
1177
01:09:11,300 --> 01:09:13,000
And we'll see if this works.
1178
01:09:13,000 --> 01:09:17,700
So, you notice now when I type in MySQL,
instead of saying the command wasn’t found,
1179
01:09:17,700 --> 01:09:19,600
we got this error message that says,
1180
01:09:19,600 --> 01:09:23,300
“Access denied for user ‘giraffeacademy’@’localhost’
(using password: NO).
1181
01:09:23,300 --> 01:09:26,400
What this means is that
the MySQL command is now working.
1182
01:09:26,400 --> 01:09:31,100
So, basically we're able to give commands to MySQL.
1183
01:09:31,100 --> 01:09:32,600
We're able to do stuff like that.
1184
01:09:32,600 --> 01:09:34,200
So, I’m just going to clear this out.
1185
01:09:34,200 --> 01:09:37,500
And now what we want to do is we want to
actually connect to the MySQL server
1186
01:09:37,500 --> 01:09:39,100
that’s running and we want to log in.
1187
01:09:39,100 --> 01:09:41,200
So, I can just type MySQL just like that.
1188
01:09:41,200 --> 01:09:44,000
And you're going to type hyphen u.
1189
01:09:44,000 --> 01:09:46,300
And you're just going to type root.
1190
01:09:46,300 --> 01:09:49,200
And then you’re going to type hyphen p
and your going to hit enter.
1191
01:09:49,200 --> 01:09:51,200
And this is going to ask you to enter a password.
1192
01:09:51,200 --> 01:09:54,500
And so, now is the time where we want to
enter in that temporary password.
1193
01:09:54,500 --> 01:09:57,600
So, in my case, I took a screenshot of it.
1194
01:09:57,600 --> 01:10:00,900
So, that screenshot is now going to be stored
on my desktop.
1195
01:10:00,900 --> 01:10:01,800
It’s over here.
1196
01:10:01,800 --> 01:10:06,300
And so, what I can do is I can just look at this
screenshot now and I can type in the password.
1197
01:10:06,300 --> 01:10:08,400
So, I'm going to go ahead and type in that password.
1198
01:10:08,400 --> 01:10:11,900
All right, so now that I’ve typed in that password,
I was able to log in.
1199
01:10:11,900 --> 01:10:15,800
So, basically now we are logged into the SQL server,
1200
01:10:15,800 --> 01:10:19,900
or to the MySQL server that is running
on our local computer.
1201
01:10:19,900 --> 01:10:24,000
So, what we can do now is we can actually
change that temporary password.
1202
01:10:24,000 --> 01:10:26,100
So, I’m just going to type in the following command.
1203
01:10:26,100 --> 01:10:30,000
ALTER.
So ALTER USER.
1204
01:10:30,000 --> 01:10:35,200
And then I’m just going to type in ‘root’@’localhost’.
1205
01:10:35,200 --> 01:10:39,100
And you'll notice that I have single quotes around
root and single quotes around local host.
1206
01:10:39,100 --> 01:10:44,700
So, then I’m going to type in identified by –
so I’m just going to type in a new password.
1207
01:10:44,700 --> 01:10:47,600
I’m just going to set it as password just
so it’s easy to remember.
1208
01:10:47,600 --> 01:10:48,900
And then you're going to type a semicolon.
1209
01:10:48,900 --> 01:10:50,100
And now you want to hit enter.
1210
01:10:50,100 --> 01:10:53,600
And so, what that’s going to do,
is its going to update your user information.
1211
01:10:53,600 --> 01:10:58,400
So, now you can actually login using something
other than that temporary password.
1212
01:10:58,400 --> 01:11:01,300
So, you'll be able to login using the password
that we set over here.
1213
01:11:01,300 --> 01:11:03,700
So, now what we want to do is just make sure
that everything works.
1214
01:11:03,700 --> 01:11:04,900
So, I’m going to type in exit.
1215
01:11:04,900 --> 01:11:06,000
Just like that.
1216
01:11:06,000 --> 01:11:07,000
And I’m going to clear this out.
1217
01:11:07,000 --> 01:11:09,500
And now what we want to do is try to log in again.
1218
01:11:09,500 --> 01:11:12,400
So, I’m going to say MySQL-the root -p.
1219
01:11:12,400 --> 01:11:16,900
And this time I'm going to enter in the new password
which was password.
1220
01:11:16,900 --> 01:11:18,100
And you’ll see that I’m able to login.
1221
01:11:18,100 --> 01:11:20,000
So, as long as that worked and you’re able to log in,
1222
01:11:20,000 --> 01:11:25,000
then you updated your password,
so now you're going to be able to get in, no problem.
1223
01:11:25,000 --> 01:11:29,100
So, the next thing I want to do really quick
is we're actually going to create a database.
1224
01:11:29,100 --> 01:11:32,200
So, MySQL is a database management system.
1225
01:11:32,200 --> 01:11:35,900
So, what it allows you to do is manage
and keep track of different databases
1226
01:11:35,900 --> 01:11:37,900
and the information that are in those databases.
1227
01:11:37,900 --> 01:11:43,500
So, what we want to do before we can actually get
started is we want to create an actual database, right?
1228
01:11:43,500 --> 01:11:48,300
So, what we can do is we can just type
out create database.
1229
01:11:48,300 --> 01:11:52,200
And then I’m just going to
name this database giraffe, just like that.
1230
01:11:52,200 --> 01:11:54,200
So, I’m creating a database named giraffe.
1231
01:11:54,200 --> 01:11:56,700
And you can name your database whatever you want.
1232
01:11:56,700 --> 01:11:59,600
Generally, it’s going to correspond
to the type of data that you're storing in it.
1233
01:11:59,600 --> 01:12:03,900
I’m just going to be using this as a general purpose
database to go throughout this course.
1234
01:12:03,900 --> 01:12:06,800
But we can just say create database giraffe.
1235
01:12:06,800 --> 01:12:08,700
And this will create a database for us called giraffe.
1236
01:12:08,700 --> 01:12:11,100
And then you’re going to type a semicolon after that.
1237
01:12:11,100 --> 01:12:12,600
And then you're going to click Enter.
1238
01:12:12,600 --> 01:12:16,700
So, now we actually have a database
called giraffe that we can use.
1239
01:12:16,700 --> 01:12:20,600
All right, so once you’ve done all that,
we're actually done here inside of the command line.
1240
01:12:20,600 --> 01:12:22,500
Now, here’s the thing.
1241
01:12:22,500 --> 01:12:25,800
If you want, you can actually write out
all of your SQL commands.
1242
01:12:25,800 --> 01:12:30,300
You can do everything that I’m going to be doing
in this course here inside of this terminal.
1243
01:12:30,300 --> 01:12:36,100
So, you can interact with the MySQL database server
just from here inside the terminal.
1244
01:12:36,100 --> 01:12:37,100
You can write SQL.
1245
01:12:37,100 --> 01:12:38,200
You can give it SQL commands.
1246
01:12:38,200 --> 01:12:40,400
You can do all sorts of stuff like that.
1247
01:12:40,400 --> 01:12:45,400
In our case though, I’m actually going to be using
another separate program in order to do that.
1248
01:12:45,400 --> 01:12:49,700
And this environment, this terminal environment
is not a very good environment
1249
01:12:49,700 --> 01:12:52,400
for like visualizing things and seeing things.
1250
01:12:52,400 --> 01:12:56,300
So, there’s actually another program called PopSQL
which I’m going to use.
1251
01:12:56,300 --> 01:12:58,200
I’m going to show you guys how to download
that right now.
1252
01:12:58,200 --> 01:13:00,700
But just know that this part is optional.
1253
01:13:00,700 --> 01:13:03,900
So, if you want, you can do everything
from inside of the terminal.
1254
01:13:03,900 --> 01:13:06,400
So, I’m going to open up my web browser again.
1255
01:13:06,400 --> 01:13:07,900
We're going to come back up here.
1256
01:13:07,900 --> 01:13:10,600
And I’m just going to do a Google search for PopSQL.
1257
01:13:10,600 --> 01:13:14,700
And this page should pop up It’s PopSQL.io.
1258
01:13:14,700 --> 01:13:22,300
So, basically what this is, is it’s a program that
we can use to write SQL queries and write SQL code
1259
01:13:22,300 --> 01:13:24,000
in order to do different things.
1260
01:13:24,000 --> 01:13:26,000
And PopSQL actually has some really cool features.
1261
01:13:26,000 --> 01:13:29,700
It’s kind of like Google Docs
but for writing SQL queries.
1262
01:13:29,700 --> 01:13:32,600
In our case though, we're just going to use it
to write out our SQL.
1263
01:13:32,600 --> 01:13:35,500
So, over here there should be a button
to download it for MAC.
1264
01:13:35,500 --> 01:13:36,700
So, I’m just going to click that.
1265
01:13:36,700 --> 01:13:39,400
And then it’s going to go ahead
and start downloading for us.
1266
01:13:39,400 --> 01:13:44,200
Once that’s downloaded, then I’m just going to
open it up And we'll be able to run the installer.
1267
01:13:44,200 --> 01:13:48,300
See, over here all we have to do is just drag
this over here to the applications folder
1268
01:13:48,300 --> 01:13:50,600
and then it’s going to be on our computer.
1269
01:13:50,600 --> 01:13:53,000
So, now what we want to do
is we're going to open that up.
1270
01:13:53,000 --> 01:13:56,500
So, I’m just going to go over here to applications.
1271
01:13:56,500 --> 01:13:59,000
And we'll scroll down to PopSQL.
1272
01:13:59,000 --> 01:14:01,700
And we're going to open this program up.
1273
01:14:01,700 --> 01:14:04,400
So, in order to use PopSQL
you're actually going to have sign in.
1274
01:14:04,400 --> 01:14:07,900
So, you can just sign in with a Google account
or with an email address.
1275
01:14:07,900 --> 01:14:09,300
I’m going to go ahead and sign in.
1276
01:14:09,300 --> 01:14:11,200
So, you can see it logged me in.
1277
01:14:11,200 --> 01:14:14,500
It says, “Welcome, Mike!”
So, now we'll go ahead and return to the app.
1278
01:14:14,500 --> 01:14:19,600
So, now that we're logged in, what it’s going to ask us
to do right away is connect to a database.
1279
01:14:19,600 --> 01:14:22,500
So, basically what this application is going to do
1280
01:14:22,500 --> 01:14:28,300
is it’s going to connect to that
MySQL database server that we have set up.
1281
01:14:28,300 --> 01:14:30,800
So, what we can do over here
is just give this a nickname.
1282
01:14:30,800 --> 01:14:32,400
So, I’m just going to call this Draft.
1283
01:14:32,400 --> 01:14:34,900
And it’s asking us for the type.
1284
01:14:34,900 --> 01:14:38,000
So, over here if you scroll down
you'll see the first option is my MySQL.
1285
01:14:38,000 --> 01:14:39,200
So, we'll just click MySQL.
1286
01:14:39,200 --> 01:14:41,100
Now, it’s asking us for a host name.
1287
01:14:41,100 --> 01:14:47,700
And the host name is basically the address
where this database server is located.
1288
01:14:47,700 --> 01:14:49,500
So, in our case it’s just at localhost.
1289
01:14:49,500 --> 01:14:52,000
Just like that.
1290
01:14:52,000 --> 01:14:53,500
So, just type in localhost.
1291
01:14:53,500 --> 01:14:55,300
Leave the port number the same.
1292
01:14:55,300 --> 01:14:57,800
3306 is going to be the default port number.
1293
01:14:57,800 --> 01:15:01,500
And then the database we're going to connect to
is named giraffe.
1294
01:15:01,500 --> 01:15:05,200
So, if you remember, like a minute ago
we created that giraffe database.
1295
01:15:05,200 --> 01:15:06,700
That’s what you want to put in here.
1296
01:15:06,700 --> 01:15:09,300
So, if you named it something other than giraffe,
put that name in here.
1297
01:15:09,300 --> 01:15:11,200
And then we need a username.
1298
01:15:11,200 --> 01:15:12,900
So, the username is going to be root.
1299
01:15:12,900 --> 01:15:18,800
And then the password which they're asking for down
here is going to be the password that you set up.
1300
01:15:18,800 --> 01:15:20,900
So, in my case, it was just password like that.
1301
01:15:20,900 --> 01:15:23,600
And then I’m just going to click Connect.
1302
01:15:23,600 --> 01:15:26,900
And this is going to go ahead
and connect us to the database.
1303
01:15:26,900 --> 01:15:29,800
So, at this point we installed MySQL.
1304
01:15:29,800 --> 01:15:31,200
We set up our first database.
1305
01:15:31,200 --> 01:15:33,100
And we connected everything with PopSQL.
1306
01:15:33,100 --> 01:15:36,600
And so, now we are able to start using PopSQL.
1307
01:15:36,600 --> 01:15:38,800
We can start learning all sorts of SQL stuff.
1308
01:15:38,800 --> 01:15:40,500
And it’s going to be awesome.
1309
01:15:40,500 --> 01:15:44,400
But keep in mind, if you don’t want to use PopSQL,
which I honestly would recommend using it.
1310
01:15:44,400 --> 01:15:48,500
You can do everything from inside the terminal here.
1311
01:15:48,500 --> 01:15:52,700
[Creating Tables]
1312
01:15:52,700 --> 01:15:55,600
In this tutorial I’m going to
talk to you guys about datatypes.
1313
01:15:55,600 --> 01:15:57,200
We're going to talk about creating tables.
1314
01:15:57,200 --> 01:16:00,300
We're also going to talk about
altering and deleting tables.
1315
01:16:00,300 --> 01:16:02,800
So, in the last tutorial we set everything up.
1316
01:16:02,800 --> 01:16:04,400
So, we got MySQL set up.
1317
01:16:04,400 --> 01:16:06,700
We also downloaded this program PopSQL
1318
01:16:06,700 --> 01:16:12,300
which is basically just a text editor that we can use
to write all of our SQL code and all that stuff.
1319
01:16:12,300 --> 01:16:17,800
So, what I want to do in this tutorial
is show you guys how to create tables, right?
1320
01:16:17,800 --> 01:16:19,100
So, we created a database.
1321
01:16:19,100 --> 01:16:22,800
In our case, we created a database
named giraffe in the last tutorial.
1322
01:16:22,800 --> 01:16:27,300
And now what I want to do is start populating
that database with different tables.
1323
01:16:27,300 --> 01:16:30,600
In other words, I want to start defining
our database schema.
1324
01:16:30,600 --> 01:16:34,100
Now whenever you're working with
a relational database management system,
1325
01:16:34,100 --> 01:16:37,000
your first step is always to create tables.
1326
01:16:37,000 --> 01:16:42,400
So, before we can start inserting any information,
before we can start querying for information,
1327
01:16:42,400 --> 01:16:46,700
we actually have to create the physical tables
inside of our database.
1328
01:16:46,700 --> 01:16:49,600
And we can do that by defining the tables layout.
1329
01:16:49,600 --> 01:16:55,600
And so, what we want to do is we basically want to
use a SQL command which is called Create Table.
1330
01:16:55,600 --> 01:17:00,100
And then inside of that command
we can pass in all the different attributes
1331
01:17:00,100 --> 01:17:03,600
or all the different columns
that we want our table to have.
1332
01:17:03,600 --> 01:17:07,700
And so, the first thing I want to show you guys
is the different types of data
1333
01:17:07,700 --> 01:17:10,200
that we can store inside of our database.
1334
01:17:10,200 --> 01:17:13,100
So, over here I actually just have a little list.
1335
01:17:13,100 --> 01:17:15,800
And I’m just going to paste it in here.
1336
01:17:15,800 --> 01:17:19,900
So, these are all of the basic data types
that we're going to be using in SQL.
1337
01:17:19,900 --> 01:17:22,400
Now, these aren’t all of the datatypes.
1338
01:17:22,400 --> 01:17:24,200
There are actually tons of datatypes.
1339
01:17:24,200 --> 01:17:27,900
And depending on the relational database
management system that you're using,
1340
01:17:27,900 --> 01:17:31,200
a lot of them have different datatypes
to do different things.
1341
01:17:31,200 --> 01:17:38,100
I would say that these six datatypes
right here make up like the core SQL datatypes.
1342
01:17:38,100 --> 01:17:41,300
Like these are probably the most
common datatypes that you're going to see.
1343
01:17:41,300 --> 01:17:43,600
But just keep in mind that there are a few others.
1344
01:17:43,600 --> 01:17:45,700
Now, we're using the MySQL database.
1345
01:17:45,700 --> 01:17:50,800
And all of these datatypes are going to be able to be
used in the MySQL database management system.
1346
01:17:50,800 --> 01:17:54,600
And all of these are going to be used, for the most
part, in any relational database management system.
1347
01:17:54,600 --> 01:18:02,200
But like I said, specific database management systems
will allow you to use different datatypes
1348
01:18:02,200 --> 01:18:04,500
depending on how they want to implement things.
1349
01:18:04,500 --> 01:18:08,600
So, let’s go through these different datatypes
and I’ll kind of talk you guys through how to use them.
1350
01:18:08,600 --> 01:18:09,800
So, INT.
1351
01:18:09,800 --> 01:18:11,900
This is actually going to refer to an integer.
1352
01:18:11,900 --> 01:18:16,600
So, anytime you see INT just like that, I-N-T,
that basically means any whole number, right?
1353
01:18:16,600 --> 01:18:18,500
So, this is any sort of whole number.
1354
01:18:18,500 --> 01:18:20,500
But it can’t have decimal places after it.
1355
01:18:20,500 --> 01:18:23,400
If you want to be able to store decimals,
you can use this decimal datatype.
1356
01:18:23,400 --> 01:18:26,700
And the decimal datatype
will allow you to store decimals.
1357
01:18:26,700 --> 01:18:30,200
And you'll see, we have
this parenthesis here after decimal.
1358
01:18:30,200 --> 01:18:32,500
And I have M and N in here.
1359
01:18:32,500 --> 01:18:35,200
Now these are actually both going to
end up being numbers.
1360
01:18:35,200 --> 01:18:39,900
So, M is going to be the total number of digits
that you want to store for this number.
1361
01:18:39,900 --> 01:18:44,100
And N is going to be the number of digits
that you want to store after the decimal point.
1362
01:18:44,100 --> 01:18:50,400
So, when we're working with databases you have to
be very specific about the different information.
1363
01:18:50,400 --> 01:18:54,000
And specifically, the amount of digits
that you want to store for a number.
1364
01:18:54,000 --> 01:18:58,400
So, what we could do is I could say like 10 here
and I could put a 4 over here.
1365
01:18:58,400 --> 01:19:02,400
And what this means is we want to store
a decimal number with 10 total digits
1366
01:19:02,400 --> 01:19:06,100
and 4 of those digits coming after the decimal place.
1367
01:19:06,100 --> 01:19:12,300
So, you can specify, you know, depending on how
accurate you want these numbers to be stored,
1368
01:19:12,300 --> 01:19:13,900
you can modify those numbers.
1369
01:19:13,900 --> 01:19:15,400
Down here we have VARCHAR.
1370
01:19:15,400 --> 01:19:19,100
And this actually stands for Variable Char
or Variable Character.
1371
01:19:19,100 --> 01:19:22,000
This is basically a way that we can store
a string of text.
1372
01:19:22,000 --> 01:19:25,700
So, VARCHAR, we have these parenthesis after here.
1373
01:19:25,700 --> 01:19:26,900
And you can put a number in here.
1374
01:19:26,900 --> 01:19:32,300
So, if I put a 1 in here, then this is going to
store a string of text, length 1.
1375
01:19:32,300 --> 01:19:38,300
If I put (100) in here, this is going to
store a string of text with 100 characters.
1376
01:19:38,300 --> 01:19:40,900
So, that means that the maximum amount
of characters
1377
01:19:40,900 --> 01:19:45,200
that you can store inside of a VARCHAR(100)
would be 100.
1378
01:19:45,200 --> 01:19:49,600
Down here we have BLOB
which stands for Binary Large Object.
1379
01:19:49,600 --> 01:19:55,100
And this is basically just a structure
that can store large amounts of binary data.
1380
01:19:55,100 --> 01:19:58,900
So, if you have – a lot of people will use these
for like images or files.
1381
01:19:58,900 --> 01:20:01,800
Like you can store those inside of a BLOB.
1382
01:20:01,800 --> 01:20:04,900
And they'll be able to be stored in the database.
1383
01:20:04,900 --> 01:20:06,100
We also have DATE.
1384
01:20:06,100 --> 01:20:08,400
So, DATE will be like a specific date and time.
1385
01:20:08,400 --> 01:20:13,700
And you can see we format a date like YYYY-MM-DD.
1386
01:20:13,700 --> 01:20:17,200
So, this would be the year, the 2-digit month,
and then the 2-digit day.
1387
01:20:17,200 --> 01:20:20,800
You can also have a TIMESTAMP,
which is similar to date,
1388
01:20:20,800 --> 01:20:23,800
but it’s generally used for recording
like when things happen.
1389
01:20:23,800 --> 01:20:28,600
So, you can record like when an item got inserted
into the database or something like that.
1390
01:20:28,600 --> 01:20:36,000
So, over here it’s just YYYY-MM-DD
and then we have the hours, minutes, and the seconds.
1391
01:20:36,000 --> 01:20:39,700
So, those are all, like I said, the main datatypes
that you're going to be using.
1392
01:20:39,700 --> 01:20:41,000
But these aren’t all the datatypes.
1393
01:20:41,000 --> 01:20:43,600
So, depending on the database management system
that you're using,
1394
01:20:43,600 --> 01:20:47,700
you want to check to see
what specific datatypes they offer.
1395
01:20:47,700 --> 01:20:50,800
But like I said, for the most part,
these should work in just about any system.
1396
01:20:50,800 --> 01:20:54,100
So, now I want to talk to you guys
about creating database tables.
1397
01:20:54,100 --> 01:20:57,000
So, what we can actually do is we can create tables
1398
01:20:57,000 --> 01:21:02,000
and we're going to have use those datatypes to tell
the relational database management system
1399
01:21:02,000 --> 01:21:05,500
what types of data we want to store
in each column in the table.
1400
01:21:05,500 --> 01:21:09,800
So, in order to create a table,
we're actually going to type out some SQL.
1401
01:21:09,800 --> 01:21:13,500
Now, I’m just going to type out CREATE TABLE.
1402
01:21:13,500 --> 01:21:16,600
And these are two SQL reserve words.
1403
01:21:16,600 --> 01:21:18,500
CREATE and TABLE.
1404
01:21:18,500 --> 01:21:21,600
And you'll notice that I typed them
in all capital letters.
1405
01:21:21,600 --> 01:21:26,200
Now, this is actually a pretty common convention
when writing SQL queries.
1406
01:21:26,200 --> 01:21:31,100
A lot of people will write these reserve words
in SQL in all capitals.
1407
01:21:31,100 --> 01:21:32,100
But you don’t have to.
1408
01:21:32,100 --> 01:21:35,200
If I wanted, I could write create table just like that.
1409
01:21:35,200 --> 01:21:37,500
And you’ll see it’s getting highlighted the same way.
1410
01:21:37,500 --> 01:21:40,300
The reason that people write these in all caps is
1411
01:21:40,300 --> 01:21:46,700
because then it’s easy to distinguish the SQL
from any other text that we might be writing.
1412
01:21:46,700 --> 01:21:51,900
So, I would say for the most part, just stick with
making these all uppercase and you should be fine.
1413
01:21:51,900 --> 01:21:53,300
So, I’m going to say CREATE TABLE.
1414
01:21:53,300 --> 01:21:56,000
And then I want to type in the name of the table
that I want to create.
1415
01:21:56,000 --> 01:21:59,500
So, what we're going to do is we're going to
actually create a database table.
1416
01:21:59,500 --> 01:22:01,800
So, I actually have a database table set up over here.
1417
01:22:01,800 --> 01:22:04,100
You'll see it’s a student table.
1418
01:22:04,100 --> 01:22:09,600
And it’s storing just a list of students
for like maybe in a college or university.
1419
01:22:09,600 --> 01:22:12,500
So, there’s three things that we're storing
about this student.
1420
01:22:12,500 --> 01:22:15,600
The student’s ID, the student’s name,
and the student’s major.
1421
01:22:15,600 --> 01:22:19,300
So, we're storing all of this information
about this student.
1422
01:22:19,300 --> 01:22:25,300
So, what I can do now is I can actually create
this exact table inside of my database.
1423
01:22:25,300 --> 01:22:26,500
So, I can say CREATE TABLE.
1424
01:22:26,500 --> 01:22:28,000
We'll call it student.
1425
01:22:28,000 --> 01:22:30,900
And you'll notice that I made this lowercase.
1426
01:22:30,900 --> 01:22:35,000
So, this isn’t an SQL reserve word,
so I’m not going to make it uppercase.
1427
01:22:35,000 --> 01:22:39,500
And then what I can do is I can just make an open
and closes parenthesis and a semicolon.
1428
01:22:39,500 --> 01:22:44,300
Now, any command that you write in SQL
is always going to end with a semicolon.
1429
01:22:44,300 --> 01:22:48,200
And if you're just starting out,
you might forget to put that semicolon in there,
1430
01:22:48,200 --> 01:22:51,100
but you want to make sure that you always
put it in there otherwise it’s not valid SQL.
1431
01:22:51,100 --> 01:22:52,500
So, I’m just going to click Enter.
1432
01:22:52,500 --> 01:22:54,800
And I’m going to down here
in-between these parenthesis.
1433
01:22:54,800 --> 01:22:58,700
And what I’m going to is I’m going to start defining
the different columns
1434
01:22:58,700 --> 01:23:01,400
or the different attributes on this table.
1435
01:23:01,400 --> 01:23:06,300
So, what we can do is we can define a column name
and then a column datatype.
1436
01:23:06,300 --> 01:23:10,500
So, I can come over here
and I can say the first attribute.
1437
01:23:10,500 --> 01:23:14,600
So, in my case, the first attribute for this table
is going to be student ID.
1438
01:23:14,600 --> 01:23:18,100
And you'll also notice that student ID is a primary key.
1439
01:23:18,100 --> 01:23:21,600
So, on this table, student ID is the primary key.
1440
01:23:21,600 --> 01:23:26,800
Meaning it’s the column on the table
which will uniquely identify the specific row.
1441
01:23:26,800 --> 01:23:29,500
So, I'm just going to call this student_id.
1442
01:23:29,500 --> 01:23:32,500
And now I need to give this a datatype.
1443
01:23:32,500 --> 01:23:38,500
So, I’m going to give this a datatype of INT because all
of these ID’s as you can see, are integers, right?
1444
01:23:38,500 --> 01:23:40,000
They’re just integer numbers.
1445
01:23:40,000 --> 01:23:46,600
Now because this is going to be the primary key
for this table, I need to denote that.
1446
01:23:46,600 --> 01:23:50,200
So, over here I can just say PRIMARY KEY,
just like that.
1447
01:23:50,200 --> 01:23:54,700
And what that’s going to do is it’s going to tell MySQL
that this is going to be the primary key for the table.
1448
01:23:54,700 --> 01:23:57,100
The next thing we want to store is the student’s name.
1449
01:23:57,100 --> 01:23:59,000
So, I’m just going to say name.
1450
01:23:59,000 --> 01:24:00,800
And the name is actually going to be a string.
1451
01:24:00,800 --> 01:24:04,300
So, this could be like Jack or Tommy or Kara, right?
1452
01:24:04,300 --> 01:24:07,000
It’s a name that we're storing about this table.
1453
01:24:07,000 --> 01:24:10,100
So, over here I’m going to make this a VARCHAR.
1454
01:24:10,100 --> 01:24:12,700
And then I’m going to make an open
and closed parenthesis.
1455
01:24:12,700 --> 01:24:16,500
Now, remember, with the VARCHAR datatype
we have to tell MySQL
1456
01:24:16,500 --> 01:24:19,400
how many characters we want this to be able to store.
1457
01:24:19,400 --> 01:24:22,200
So, with someone’s name, what you want to do
1458
01:24:22,200 --> 01:24:25,700
is you basically just want to think like how many
characters do we really want to allocate
1459
01:24:25,700 --> 01:24:28,200
to storing someone’s name?
1460
01:24:28,200 --> 01:24:33,600
Because here’s the thing, if I allocated like 1000
characters for someone’s name,
1461
01:24:33,600 --> 01:24:38,100
will in reality, normal people don’t have a name
with 1000 characters, right?
1462
01:24:38,100 --> 01:24:42,700
I mean maybe your name would be 20 characters
or 30 if you're really pushing it,
1463
01:24:42,700 --> 01:24:46,200
but 1000 is just totally out of the bounds of reality.
1464
01:24:46,200 --> 01:24:49,300
And you have to think if you're storing
like millions of names,
1465
01:24:49,300 --> 01:24:55,200
potentially, you know, allocating 1000 characters
to each name when you only need 20
1466
01:24:55,200 --> 01:24:57,700
is going to take up a lot of extra space
in your database.
1467
01:24:57,700 --> 01:25:00,400
So, I think what we should do is maybe say like 20.
1468
01:25:00,400 --> 01:25:05,400
So, let’s say that the maximum length of someone’s
name we want to store is going to be 20 characters.
1469
01:25:05,400 --> 01:25:09,900
And really, you know, depending on the domain
of the application that you're building,
1470
01:25:09,900 --> 01:25:11,500
that’s going to be different.
1471
01:25:11,500 --> 01:25:13,000
But in my case, let’s just say it’s 20.
1472
01:25:13,000 --> 01:25:15,700
And then finally we wanted to store
the student’s major.
1473
01:25:15,700 --> 01:25:17,100
So, I can say over here, major.
1474
01:25:17,100 --> 01:25:20,000
And this is also going to be a VARCHAR.
1475
01:25:20,000 --> 01:25:23,500
So, why don’t we also allocate 20 characters
to their major.
1476
01:25:23,500 --> 01:25:24,900
And that should be enough.
1477
01:25:24,900 --> 01:25:29,100
So, you'll notice that I’m defining each of the
attributes, each of the columns on the table.
1478
01:25:29,100 --> 01:25:30,900
And then I’m putting a comma.
1479
01:25:30,900 --> 01:25:33,200
And then I’m defining the next column.
1480
01:25:33,200 --> 01:25:35,300
And then I’m putting a comma.
1481
01:25:35,300 --> 01:25:36,500
And then the final column.
1482
01:25:36,500 --> 01:25:39,000
So, this right here, this CREATE TABLE statement is
1483
01:25:39,000 --> 01:25:42,300
basically going to create
this table over here for us, right?
1484
01:25:42,300 --> 01:25:44,100
We have the primary key, which is an INT.
1485
01:25:44,100 --> 01:25:47,400
And we have a name and a major which are strings.
1486
01:25:47,400 --> 01:25:49,900
So, that’s going to go ahead and do that for us.
1487
01:25:49,900 --> 01:25:54,800
Now, from inside PopSQL, what’s really cool
is we can just click on this query right here.
1488
01:25:54,800 --> 01:25:56,700
Click on this SQL statement.
1489
01:25:56,700 --> 01:25:59,000
And I can come over here and click Run.
1490
01:25:59,000 --> 01:26:03,800
And what this will do is it’ll automatically
run this SQL on our MySQL server.
1491
01:26:03,800 --> 01:26:05,100
So, I'm going to click Run.
1492
01:26:05,100 --> 01:26:09,800
And you'll see over here we get a success message
that says Rows affected: 0.
1493
01:26:09,800 --> 01:26:15,600
So, that actually was able to create
that new table in our database.
1494
01:26:15,600 --> 01:26:18,900
So, that is basically how we could create a table.
1495
01:26:18,900 --> 01:26:21,200
And now we have this table stored in our database.
1496
01:26:21,200 --> 01:26:22,800
I want to show you guys another thing we can do.
1497
01:26:22,800 --> 01:26:25,700
So, over here I’m defining the primary key.
1498
01:26:25,700 --> 01:26:28,400
So, student_id was the primary key, right?
1499
01:26:28,400 --> 01:26:31,900
It’s the one column that’s going to uniquely
identify each row.
1500
01:26:31,900 --> 01:26:36,800
And if you want, you can put primary key over here
after like student_id.
1501
01:26:36,800 --> 01:26:41,200
Or what you can do is you can get rid of this
and you can define this down below.
1502
01:26:41,200 --> 01:26:45,100
So, instead of defining the primary key up here
next to student_id,
1503
01:26:45,100 --> 01:26:47,100
I can come down here and I could change it.
1504
01:26:47,100 --> 01:26:52,500
So, I could say now PRIMARY KEY,
and then I can make an open and closed parenthesis.
1505
01:26:52,500 --> 01:26:55,800
And in here, I could put the name of the column
that I want to be the primary key.
1506
01:26:55,800 --> 01:26:57,900
So, in my case, I’ll just say student_id.
1507
01:26:57,900 --> 01:27:02,400
So, now what this is system is that the primary key
is going to be student_id.
1508
01:27:02,400 --> 01:27:04,900
So, this is a equivalent to what I was doing up here.
1509
01:27:04,900 --> 01:27:08,200
It’s just another way that you can do it.
1510
01:27:08,200 --> 01:27:12,500
And so, now we already have our table created,
so I’m not going to go ahead and create it again.
1511
01:27:12,500 --> 01:27:14,800
All right, so now that we have
our student table created,
1512
01:27:14,800 --> 01:27:16,800
let’s go ahead and make sure they created correctly.
1513
01:27:16,800 --> 01:27:19,100
So, what I’m going to do
is I’m going to come down here
1514
01:27:19,100 --> 01:27:21,000
and I’m going to type out another MySQL command.
1515
01:27:21,000 --> 01:27:23,200
So, I’m just going to type out DESCRIBE.
1516
01:27:23,200 --> 01:27:25,100
And then I’m going to type in the name of the table.
1517
01:27:25,100 --> 01:27:27,100
So, in our case it’s going to be student.
1518
01:27:27,100 --> 01:27:33,500
And you'll notice with PopSQL, if I click over here on
this query, it gets highlighted in that blue color.
1519
01:27:33,500 --> 01:27:36,000
And if I click down here on this query,
it gets highlighted.
1520
01:27:36,000 --> 01:27:39,500
So, what you can do with PopSQL
is you can have like a query up here.
1521
01:27:39,500 --> 01:27:43,800
And you can run that query or you can click
on this query down here and run it.
1522
01:27:43,800 --> 01:27:45,300
So, I’m going to click on DESCRIBE student.
1523
01:27:45,300 --> 01:27:46,500
And then I’m going click Run.
1524
01:27:46,500 --> 01:27:49,400
So, you'll see that it’s actually describing
the table that we just created.
1525
01:27:49,400 --> 01:27:51,200
So, there’s a list of all these different fields.
1526
01:27:51,200 --> 01:27:53,300
We have student_id, name, and major.
1527
01:27:53,300 --> 01:27:55,600
It’s telling us the datatypes that we're storing.
1528
01:27:55,600 --> 01:27:57,600
So, like varchar(20).
1529
01:27:57,600 --> 01:28:01,800
And then it’s telling us a bunch of other information which we're going to get into later.
1530
01:28:01,800 --> 01:28:05,500
So, what this is, it basically described
everything about this table.
1531
01:28:05,500 --> 01:28:09,600
So, now what I want to show you guys
how to do is delete and modify a table.
1532
01:28:09,600 --> 01:28:13,500
So, now that we've created a table
and we saw that it got created correctly,
1533
01:28:13,500 --> 01:28:15,600
if you wanted, you could delete the table.
1534
01:28:15,600 --> 01:28:19,300
So, what I could do is I can come down here
and type out DROP TABLE
1535
01:28:19,300 --> 01:28:20,900
and I can just type the name of the table.
1536
01:28:20,900 --> 01:28:23,400
So, in our case, it’s the student table, and a semicolon.
1537
01:28:23,400 --> 01:28:25,700
And now I’m going to click on here and I’ll click run.
1538
01:28:25,700 --> 01:28:28,400
And what this is going to do
is it’s going to drop that table.
1539
01:28:28,400 --> 01:28:31,300
So, now if I came up here
and I click on describe student,
1540
01:28:31,300 --> 01:28:33,700
in other words, I’m saying
I want to execute this command.
1541
01:28:33,700 --> 01:28:36,800
And I click run, you'll see that it says
“NO_SUCH_TABLE”.
1542
01:28:36,800 --> 01:28:42,600
Table ‘giraffe.student’ doesn’t exist
because remember, we just dropped it.
1543
01:28:42,600 --> 01:28:45,700
So, what we can do, is we can actually create again.
1544
01:28:45,700 --> 01:28:48,600
So, I’m going to click on this CREATE TABLE query
and I’ll click Run.
1545
01:28:48,600 --> 01:28:50,600
And that will go ahead and create it.
1546
01:28:50,600 --> 01:28:55,000
So, now if we click DESCRIBE student, you’ll see
we get the table back because we created it again.
1547
01:28:55,000 --> 01:28:56,400
So, you can drop the table.
1548
01:28:56,400 --> 01:28:57,700
You can also modify the table.
1549
01:28:57,700 --> 01:29:02,200
So, let’s say that after the table was created,
you wanted to give it another column.
1550
01:29:02,200 --> 01:29:05,300
What I could do is I could add another column.
1551
01:29:05,300 --> 01:29:07,400
So, I could say ALTER TABLE.
1552
01:29:07,400 --> 01:29:11,800
And then say student,
because that’s the name of the table.
1553
01:29:11,800 --> 01:29:13,000
And then I could say ADD.
1554
01:29:13,000 --> 01:29:16,100
So, what this is going to do
is it’s going to add an extra column onto the table.
1555
01:29:16,100 --> 01:29:17,900
So, why don’t we add a column for GPA.
1556
01:29:17,900 --> 01:29:22,300
So we could store a student’s GPA
and that’s going to be a decimal.
1557
01:29:22,300 --> 01:29:24,900
So, I’m just going to click a semicolon.
1558
01:29:24,900 --> 01:29:28,700
And this is going to go ahead
and end off this command.
1559
01:29:28,700 --> 01:29:34,000
Now, with decimal, remember I showed you
that you could put numbers after here, like 1 and 2.
1560
01:29:34,000 --> 01:29:39,800
So, what we're going to do is generally a GPA
would be like 3. something something.
1561
01:29:39,800 --> 01:29:41,000
Or 4. something something.
1562
01:29:41,000 --> 01:29:47,000
So, we're going to have this be three total digits with
two of the digits occurring after the decimal point.
1563
01:29:47,000 --> 01:29:51,800
So, now I can go ahead and run this command
and you'll see it says success down here.
1564
01:29:51,800 --> 01:29:54,700
So, if we were to describe the student table again,
1565
01:29:54,700 --> 01:30:00,600
if I just click on this and click Run, now you'll see
we have that extra column in here, GPA.
1566
01:30:00,600 --> 01:30:02,700
And it’s a decimal (3,2).
1567
01:30:02,700 --> 01:30:08,900
So, that is how we can drop a table
and that’s also how we can alter a specific column.
1568
01:30:08,900 --> 01:30:11,600
If you wanted, you could also drop a specific column.
1569
01:30:11,600 --> 01:30:14,200
So, I could say ALTER TABLE student.
1570
01:30:14,200 --> 01:30:20,000
And over here, I can just say DROP COLUMN
and just type in the name of the column.
1571
01:30:20,000 --> 01:30:23,100
So, what this will do is it’ll drop that GPA column
from the table.
1572
01:30:23,100 --> 01:30:24,800
So, let’s go ahead and run.
1573
01:30:24,800 --> 01:30:25,800
We get this success message.
1574
01:30:25,800 --> 01:30:32,000
Now, if I describe the table and I clicked run,
you'll see that the GPA field went away.
1575
01:30:32,000 --> 01:30:33,400
So, I actually dropped that field.
1576
01:30:33,400 --> 01:30:34,900
So, you can create tables.
1577
01:30:34,900 --> 01:30:35,900
We can add columns onto tables.
1578
01:30:35,900 --> 01:30:38,400
We can remove columns from tables.
1579
01:30:38,400 --> 01:30:40,600
And then we can just remove the table altogether.
1580
01:30:40,600 --> 01:30:46,000
So, hopefully that show you guys some of the basics
of doing those operations on tables.
1581
01:30:46,000 --> 01:30:48,300
Now, obviously whenever
you're creating your database,
1582
01:30:48,300 --> 01:30:51,200
the first thing that you want to do
is define your database schema.
1583
01:30:51,200 --> 01:30:54,000
In other words, you want to create
all the different tables
1584
01:30:54,000 --> 01:30:57,800
and then you can start inserting data
into the tables as such.
1585
01:30:57,800 --> 01:31:01,900
So, in the next tutorial I'm going to talk to you guys
about inserting data into tables,
1586
01:31:01,900 --> 01:31:04,600
so we'll actually insert some student information
into our student table.
1587
01:31:04,600 --> 01:31:09,100
[Inserting Data]
1588
01:31:09,100 --> 01:31:13,200
In this tutorial, I’m going to talk to you guys
about inserting data into a database tables.
1589
01:31:13,200 --> 01:31:17,600
So, in the last tutorial we set up our first table
which is this student table.
1590
01:31:17,600 --> 01:31:18,800
And you can see it up here.
1591
01:31:18,800 --> 01:31:21,900
And then down here we described the student table.
1592
01:31:21,900 --> 01:31:24,800
So, it has student_id, name, major, etc.
1593
01:31:24,800 --> 01:31:30,100
So, now that we have our table set up, we want to
actually start inputting information, right?
1594
01:31:30,100 --> 01:31:32,100
We want to start storing some different data.
1595
01:31:32,100 --> 01:31:33,600
So, I’m going to show you guys how to do that.
1596
01:31:33,600 --> 01:31:35,900
I’m going to leave this
CREATE TABLE statement up here
1597
01:31:35,900 --> 01:31:37,900
because we're actually going to
come back to that in a sec.
1598
01:31:37,900 --> 01:31:40,800
But down here, I’m going to
show you guys how to insert values.
1599
01:31:40,800 --> 01:31:46,400
So, in order to insert a piece of information into
a table, we just want to type out INSERT INTO.
1600
01:31:46,400 --> 01:31:48,700
And now I want to type out the name of the table.
1601
01:31:48,700 --> 01:31:51,000
So, in our case, we have this student table.
1602
01:31:51,000 --> 01:31:54,100
And then I just want to type out VALUES.
1603
01:31:54,100 --> 01:31:58,600
So, basically, we're saying insert
into the student table the values.
1604
01:31:58,600 --> 01:32:02,600
And then over here I’m going to make an open
and closed parenthesis and a semicolon.
1605
01:32:02,600 --> 01:32:05,300
So, now this is actually a full statement.
1606
01:32:05,300 --> 01:32:07,800
So, remember, we need to
end this off with a semicolon.
1607
01:32:07,800 --> 01:32:12,900
Inside of these parenthesis I can actually
put the information that I want to store.
1608
01:32:12,900 --> 01:32:20,800
So, the way that the table is set up, we have the
student_id first, then the name, then the major.
1609
01:32:20,800 --> 01:32:24,500
And that's the order that we want to
put this information into the table.
1610
01:32:24,500 --> 01:32:28,800
So, I need to put this student_id first,
the name, and then the major.
1611
01:32:28,800 --> 01:32:31,700
So, over here I’m just going to type in the student ID.
1612
01:32:31,700 --> 01:32:35,800
So, we're going to be actually entering in the same
information that we have over here.
1613
01:32:35,800 --> 01:32:38,100
So, this is kind of like our little template.
1614
01:32:38,100 --> 01:32:42,800
So, we have the first student, Jack,
who’s a biology major and his student ID is 1.
1615
01:32:42,800 --> 01:32:44,900
And you can see we just keep going from there.
1616
01:32:44,900 --> 01:32:47,400
So, I’m going to insert, you know,
roughly the same information.
1617
01:32:47,400 --> 01:32:51,700
So, the first thing we want to do is put the student ID,
so that’s going to be 1.
1618
01:32:51,700 --> 01:32:55,400
And you'll notice that with an integer
I can just type out the number 1.
1619
01:32:55,400 --> 01:32:58,300
And then over here we're going
to put in a VARCHAR(20).
1620
01:32:58,300 --> 01:33:00,800
So, basically this is going to be a string.
1621
01:33:00,800 --> 01:33:05,700
Anytime we're typing out strings in SQL,
we're going to make these double quotation marks.
1622
01:33:05,700 --> 01:33:08,000
And then in here we can type out the string.
1623
01:33:08,000 --> 01:33:10,300
So, the student’s name is going to be Jack.
1624
01:33:10,300 --> 01:33:15,600
And then another comma
and we're going to put in his major which is biology.
1625
01:33:15,600 --> 01:33:18,500
So, we have Jack with the student ID of 1.
1626
01:33:18,500 --> 01:33:19,900
And his major is Biology.
1627
01:33:19,900 --> 01:33:24,500
So, insert into student values 1, Jack, Biology,
1628
01:33:24,500 --> 01:33:29,000
this is going to go ahead
and insert this value into the database table.
1629
01:33:29,000 --> 01:33:30,700
So, now when I click Run –
1630
01:33:30,700 --> 01:33:34,400
and you'll notice all I have to do is
just click on this query and then click Run.
1631
01:33:34,400 --> 01:33:36,900
It says down here Success.
1632
01:33:36,900 --> 01:33:38,300
Rows affected 1.
1633
01:33:38,300 --> 01:33:41,600
So, we affected one row in the student table.
1634
01:33:41,600 --> 01:33:46,600
So, now what we can do is we can actually see
how this got inserted into the table.
1635
01:33:46,600 --> 01:33:54,400
So, what I’m going to do is actually below here,
I’m just going to type out select asterisk from student.
1636
01:33:54,400 --> 01:33:57,900
And this is actually a very simple SQL command.
1637
01:33:57,900 --> 01:34:01,000
What this is going to do is it’s going to
grab all the information from the student table.
1638
01:34:01,000 --> 01:34:05,500
And we're actually going to spend
a whole tutorial just talking about using SELECT.
1639
01:34:05,500 --> 01:34:08,900
But for now, just know
you can type SELECT * FROM student.
1640
01:34:08,900 --> 01:34:12,200
And this is going to go ahead and give us
all the information from the student table.
1641
01:34:12,200 --> 01:34:14,800
So, I’m just going to click on this query here
and I’ll click Run.
1642
01:34:14,800 --> 01:34:18,500
And you'll see down here we get this table.
1643
01:34:18,500 --> 01:34:20,400
So, it says student_id 1.
1644
01:34:20,400 --> 01:34:21,200
Name, Jack.
1645
01:34:21,200 --> 01:34:22,200
Major, Biology.
1646
01:34:22,200 --> 01:34:29,200
So, so far in the student table, we have 1 student
which is Jack and he’s a biology major.
1647
01:34:29,200 --> 01:34:31,100
So, why don’t we actually insert another student.
1648
01:34:31,100 --> 01:34:35,200
So, I’m going to keep this same line of code except
now I’m going to insert a student with ID 2.
1649
01:34:35,200 --> 01:34:38,300
And this student’s name is going to be Kate.
1650
01:34:38,300 --> 01:34:42,800
And she is going to be a sociology major.
1651
01:34:42,800 --> 01:34:45,900
So, we have Kate who’s ID is 2, sociology.
1652
01:34:45,900 --> 01:34:48,800
So, if I click on this command here and I click Run,
1653
01:34:48,800 --> 01:34:52,300
this is going to go ahead and insert Kate
into the database.
1654
01:34:52,300 --> 01:34:57,500
So, now I’m going to click on Select All
from students, or Select * from students.
1655
01:34:57,500 --> 01:34:58,700
And we'll click Run.
1656
01:34:58,700 --> 01:35:01,900
And now you can see we're getting
all the information from the student table.
1657
01:35:01,900 --> 01:35:04,400
So, we have Jack and then we also have Kate.
1658
01:35:04,400 --> 01:35:09,300
So, we added in – so far,
we've inserted in 2 people into there.
1659
01:35:09,300 --> 01:35:13,000
So, what you can do is you can basically
use this same format
1660
01:35:13,000 --> 01:35:15,500
in order to keep inserting students
into the student table, right?
1661
01:35:15,500 --> 01:35:20,900
As long as you have their ID and you have their name
and their major, then everything is good.
1662
01:35:20,900 --> 01:35:23,500
But I want to show you guys another thing we can do.
1663
01:35:23,500 --> 01:35:27,200
So, let’s say there’s a situation where
we had a student who didn’t have a major.
1664
01:35:27,200 --> 01:35:31,800
Maybe they just had no major or we didn’t know
what their major was
1665
01:35:31,800 --> 01:35:33,600
so we couldn't insert it into the database.
1666
01:35:33,600 --> 01:35:37,600
Well, in a situation like that we could
actually modify this statement a little bit.
1667
01:35:37,600 --> 01:35:41,900
So, I could say INSERT INTO student
and then after student here
1668
01:35:41,900 --> 01:35:44,200
I can make an open and closed parenthesis.
1669
01:35:44,200 --> 01:35:48,900
And I can type out the names of the columns
or the names of the attributes that I have
1670
01:35:48,900 --> 01:35:50,600
or that I want to insert.
1671
01:35:50,600 --> 01:35:54,700
So, I can say like student_id and then name.
1672
01:35:54,700 --> 01:35:57,200
So, you'll notice that I’m not including major in here.
1673
01:35:57,200 --> 01:36:00,700
And what that means is that over here
in this value section,
1674
01:36:00,700 --> 01:36:04,000
we can include the student ID and the name.
1675
01:36:04,000 --> 01:36:06,000
But we don’t have to include the major.
1676
01:36:06,000 --> 01:36:10,800
So, like if I don’t know what the major is,
like I can’t obviously put anything there.
1677
01:36:10,800 --> 01:36:16,500
So, by saying student open and closed parenthesis
and then specifying the 2 attributes that I do have,
1678
01:36:16,500 --> 01:36:20,700
I can then just put them over here
and I won’t get an error by not including it.
1679
01:36:20,700 --> 01:36:22,700
So, now we can change these values.
1680
01:36:22,700 --> 01:36:24,000
So, we'll change the primary key to three.
1681
01:36:24,000 --> 01:36:27,300
And then let’s see who the next student
in our database is.
1682
01:36:27,300 --> 01:36:28,300
So, it looks like it’s Claire.
1683
01:36:28,300 --> 01:36:31,000
So, we'll have Claire.
1684
01:36:31,000 --> 01:36:33,700
And then we can go ahead and run this query.
1685
01:36:33,700 --> 01:36:35,500
And you'll see rows affected, 1.
1686
01:36:35,500 --> 01:36:37,200
So, we added in another row.
1687
01:36:37,200 --> 01:36:41,500
But if I was to select all this data – so I’m going to click
Select again and I’m just going to click Run.
1688
01:36:41,500 --> 01:36:44,800
Now you'll see that Claire’s student ID is 3.
1689
01:36:44,800 --> 01:36:47,000
Her name is Claire, but here major is NULL.
1690
01:36:47,000 --> 01:36:50,300
So, we didn’t actually enter in a major for Claire.
1691
01:36:50,300 --> 01:36:55,500
And therefore, inside the major field,
she’s getting a value of NULL.
1692
01:36:55,500 --> 01:36:57,100
And that’s actually pretty common.
1693
01:36:57,100 --> 01:37:01,500
So, that’s basically going to be what happens
when we don’t enter in something.
1694
01:37:01,500 --> 01:37:07,000
And so, what you can do is you can specify what
pieces of information you want to insert into the table
1695
01:37:07,000 --> 01:37:08,700
by specifying them over here.
1696
01:37:08,700 --> 01:37:13,400
And then here in the value section,
you just have to add those pieces of information.
1697
01:37:13,400 --> 01:37:17,100
So, that’s two ways that you can insert something
into a database table.
1698
01:37:17,100 --> 01:37:21,100
And it’s important to notice
that you can’t insert duplicate entries.
1699
01:37:21,100 --> 01:37:25,800
So, if I was to try to insert this again,
you'll see that the primary key is still 3
1700
01:37:25,800 --> 01:37:30,100
which means I’m not going to be able to
enter this record in
1701
01:37:30,100 --> 01:37:33,600
because a record with that primary key
already exists inside the table.
1702
01:37:33,600 --> 01:37:37,300
So, if I was to click Run again,
you'll see I'm getting this error.
1703
01:37:37,300 --> 01:37:40,600
It says Duplicate Entry ‘3’ for key ‘PRIMARY’.
1704
01:37:40,600 --> 01:37:45,400
Basically, what that means is we have a duplicate key
and it’s not going to work.
1705
01:37:45,400 --> 01:37:47,500
But if I was to change this to 4.
1706
01:37:47,500 --> 01:37:50,700
So, instead of having an ID of 3, it’s 4.
1707
01:37:50,700 --> 01:37:52,000
Now, this is going to work.
1708
01:37:52,000 --> 01:37:56,500
And we'll be able to see we have
two entries with Claire,
1709
01:37:56,500 --> 01:37:59,800
but they have different student ID’s
so we're all good to go.
1710
01:37:59,800 --> 01:38:02,900
So, that is the basics of inserting stuff into a table.
1711
01:38:02,900 --> 01:38:05,800
And really, those two techniques are really useful.
1712
01:38:05,800 --> 01:38:08,100
So, that’s kind of the basics of inserting into a table.
1713
01:38:08,100 --> 01:38:10,600
Now, there’s a lot of more things
we can get into with this.
1714
01:38:10,600 --> 01:38:13,900
And in the next tutorial, I’m going to show
you guys some other stuff that we can do
1715
01:38:13,900 --> 01:38:16,700
in order to make inserting into the tables a lot easier.
1716
01:38:16,700 --> 01:38:20,500
[C]
1717
01:38:20,500 --> 01:38:25,200
In this tutorial I’m going to talk to you guys some
more about inserting information into a database.
1718
01:38:25,200 --> 01:38:29,100
So, in the last tutorial we kind of looked at
the basics of inserting information.
1719
01:38:29,100 --> 01:38:30,400
So, we have our table here.
1720
01:38:30,400 --> 01:38:31,600
It’s the student table.
1721
01:38:31,600 --> 01:38:35,300
And down here we kind of looked at
how we can insert information.
1722
01:38:35,300 --> 01:38:38,800
So, we can insert into the name of the table,
and then values.
1723
01:38:38,800 --> 01:38:40,300
And we can pass in the values.
1724
01:38:40,300 --> 01:38:43,600
We can also say insert into the name of the table.
1725
01:38:43,600 --> 01:38:47,800
And then over here we could specify
what information we want to insert.
1726
01:38:47,800 --> 01:38:51,300
And then here in the values
we just insert that information.
1727
01:38:51,300 --> 01:38:56,400
So, we've kind of been using this photo over here of
this like, you know, student database.
1728
01:38:56,400 --> 01:38:59,500
And I actually just went ahead
and wrote out all the code
1729
01:38:59,500 --> 01:39:02,800
for inserting all of these different students
into the database.
1730
01:39:02,800 --> 01:39:06,100
So, you'll see we're inserting Jack
who’s a biology major.
1731
01:39:06,100 --> 01:39:07,600
Kate, sociology.
1732
01:39:07,600 --> 01:39:09,300
Claire, who doesn’t have a major.
1733
01:39:09,300 --> 01:39:12,600
There’s another one name Jack
who’s also a biology major.
1734
01:39:12,600 --> 01:39:16,500
But you'll notice that it has a
different primary key than this guy up here.
1735
01:39:16,500 --> 01:39:19,000
And then there’s Mike who’s computer science.
1736
01:39:19,000 --> 01:39:25,400
So, these instructions will actually insert
each of those students into our database.
1737
01:39:25,400 --> 01:39:30,500
And you'll see over here
when I run this Select * FROM command.
1738
01:39:30,500 --> 01:39:33,600
So, with PopSQL, if you just click on the SQL statement
1739
01:39:33,600 --> 01:39:36,300
and then you click Run, it’ll go ahead
and run it for you.
1740
01:39:36,300 --> 01:39:38,400
So, you'll see down here we get all that information.
1741
01:39:38,400 --> 01:39:39,800
So, there database is set up.
1742
01:39:39,800 --> 01:39:42,100
And that’s sort of like the general use case, right?
1743
01:39:42,100 --> 01:39:46,800
That’s like we set up our database table
and we're just straight-up inserting information into it.
1744
01:39:46,800 --> 01:39:48,500
But there’s some other stuff we can do.
1745
01:39:48,500 --> 01:39:51,400
Namely, we can actually set up our database table
1746
01:39:51,400 --> 01:40:00,000
in order to make it easier for us to insert elements or
to control the type of information or the type of rows
1747
01:40:00,000 --> 01:40:02,500
that we can insert into the database table.
1748
01:40:02,500 --> 01:40:06,200
So, what I’m actually going to do
is I’m actually just going to drop this table.
1749
01:40:06,200 --> 01:40:09,100
So, I’m just going to say DROP TABLE student.
1750
01:40:09,100 --> 01:40:11,000
And we're actually just going to start over.
1751
01:40:11,000 --> 01:40:15,300
So, again, with PopSQL all you have to do
is click on the SQL command.
1752
01:40:15,300 --> 01:40:17,800
Like I can click on DROP TABLE right here.
1753
01:40:17,800 --> 01:40:20,900
And then when I click Run, it’ll go ahead and drop it.
1754
01:40:20,900 --> 01:40:26,400
So, now if I tried to SELECT ALL from the student table,
you'll see it says there’s no such table.
1755
01:40:26,400 --> 01:40:31,200
So, let’s go ahead and I’m going to show you guys
how we can create this student table
1756
01:40:31,200 --> 01:40:35,100
in order to make it a little bit easier
for us to insert stuff.
1757
01:40:35,100 --> 01:40:39,000
So, the first thing I’m going to show you guys
is something called NOT NULL.
1758
01:40:39,000 --> 01:40:44,400
And NOT NULL will basically allow us to define
that a particular column in the table cannot be NULL.
1759
01:40:44,400 --> 01:40:47,100
So, let’s say that when we're storing our students,
1760
01:40:47,100 --> 01:40:49,800
we don’t want the student
to be able to have a NULL name.
1761
01:40:49,800 --> 01:40:54,500
And NULL is basically just a value
that represents no value.
1762
01:40:54,500 --> 01:40:57,400
So, if something is NULL it means
that it doesn’t have a value.
1763
01:40:57,400 --> 01:41:00,700
So, let’s say that we always want the student
to have a name no matter what.
1764
01:41:00,700 --> 01:41:06,100
Well, after I same name VARCHAR(20)
what I could do is I can say NOT NULL.
1765
01:41:06,100 --> 01:41:08,300
And I'm just putting that right after here.
1766
01:41:08,300 --> 01:41:10,400
What that means is this column can’t be null.
1767
01:41:10,400 --> 01:41:14,700
So, if you insert a row into the table,
you can’t insert NULL for a name.
1768
01:41:14,700 --> 01:41:19,500
I could also do something else
which would be UNIQUE.
1769
01:41:19,500 --> 01:41:21,900
So, UNIQUE is another keyword which we can use.
1770
01:41:21,900 --> 01:41:27,900
And basically, what this is going to mean is that the
major field has to be unique for each row in this table.
1771
01:41:27,900 --> 01:41:34,300
So, if somebody else or another row in the table
has the same major as another entry,
1772
01:41:34,300 --> 01:41:36,400
then that will get rejected.
1773
01:41:36,400 --> 01:41:39,300
So, NOT NULL and UNIQUE
are actually pretty useful.
1774
01:41:39,300 --> 01:41:41,900
So, now what we can do
is we can actually create this table.
1775
01:41:41,900 --> 01:41:42,700
So, I’m just going to click on this query.
1776
01:41:42,700 --> 01:41:44,100
And I’m going to click Run.
1777
01:41:44,100 --> 01:41:46,400
And you'll see that we get a success message.
1778
01:41:46,400 --> 01:41:47,400
So, we were able to create the table.
1779
01:41:47,400 --> 01:41:50,300
And so, now why don’t we start populating
this table with information?
1780
01:41:50,300 --> 01:41:54,400
So, I’ll come over here and I’ll click insert
into student – Jack, biology.
1781
01:41:54,400 --> 01:41:56,300
So, we're going to insert Jack.
1782
01:41:56,300 --> 01:42:00,800
And then we'll select the table and you'll see
that Jack got inserted just fine.
1783
01:42:00,800 --> 01:42:02,500
Let’s click on this second one.
1784
01:42:02,500 --> 01:42:03,500
Kate, sociology.
1785
01:42:03,500 --> 01:42:04,600
So, we'll run this.
1786
01:42:04,600 --> 01:42:07,200
And again, we'll just select all from the table.
1787
01:42:07,200 --> 01:42:09,300
You'll see both of those got inserted.
1788
01:42:09,300 --> 01:42:11,400
So, now we get down here into this next one.
1789
01:42:11,400 --> 01:42:13,000
So, insert into student.
1790
01:42:13,000 --> 01:42:16,300
So, for Claire, we're inserting in
student_id and name.
1791
01:42:16,300 --> 01:42:18,400
But let’s actually switch this up.
1792
01:42:18,400 --> 01:42:23,900
So, why don’t we get rid of this and instead
of saying this, we'll give her a major.
1793
01:42:23,900 --> 01:42:26,200
So, we're going to say that Claire’s a chemistry major.
1794
01:42:26,200 --> 01:42:27,900
But we're actually going to get rid of this name.
1795
01:42:27,900 --> 01:42:31,200
So, instead of Claire,
I’m just going to say NULL here.
1796
01:42:31,200 --> 01:42:35,100
And you'll notice up here,
I said that the name cannot be NULL.
1797
01:42:35,100 --> 01:42:40,400
So, when I created this table,
I specified that the name field cannot be NULL.
1798
01:42:40,400 --> 01:42:44,800
So, if I come down here and I try to
insert a student with no name,
1799
01:42:44,800 --> 01:42:49,100
with a name that’s set to NULL and click Run,
you'll see that we get this error.
1800
01:42:49,100 --> 01:42:51,200
And it says, “You have an error in your SQL.”
1801
01:42:51,200 --> 01:42:57,600
And basically, what that means
is we can’t insert in a value here that’s NULL
1802
01:42:57,600 --> 01:42:59,800
because we specified that it can’t be NULL up here.
1803
01:42:59,800 --> 01:43:02,100
So, you’re actually not going to be able to do that.
1804
01:43:02,100 --> 01:43:07,700
Now, we also, over here on this major field,
we defined that it has to be unique.
1805
01:43:07,700 --> 01:43:09,800
So, if I come down here
and I try to execute this line,
1806
01:43:09,800 --> 01:43:13,500
so you’ll see, we're trying to insert
this person’s name as Jack
1807
01:43:13,500 --> 01:43:15,000
and they're a biology major.
1808
01:43:15,000 --> 01:43:19,500
But we already defined one person
that was a biology major up here, right?
1809
01:43:19,500 --> 01:43:20,700
It was this first one.
1810
01:43:20,700 --> 01:43:25,100
So, if I try to enter in another biology major,
it’s going to give me another error.
1811
01:43:25,100 --> 01:43:26,500
So, I’m going to click run.
1812
01:43:26,500 --> 01:43:31,700
And you'll see down here it says
Duplicate entry ‘Biology’ for key ‘major’.
1813
01:43:31,700 --> 01:43:36,600
So, it yelled at us because we entered
in a duplicate entry.
1814
01:43:36,600 --> 01:43:40,800
So, NOT NULL and UNIQUE are really
good ways for you
1815
01:43:40,800 --> 01:43:44,300
to kind of control the data
that gets stored on the table.
1816
01:43:44,300 --> 01:43:47,400
And actually, funny enough,
if you have a primary key –
1817
01:43:47,400 --> 01:43:54,400
a primary key is actually just an attribute or a column
on the table that is both NOT NULL and UNIQUE.
1818
01:43:54,400 --> 01:43:58,300
So, a primary key is basically just something
that’s NOT NULL and UNIQUE.
1819
01:43:58,300 --> 01:43:59,900
So, there’s a couple other ones.
1820
01:43:59,900 --> 01:44:01,600
We're actually going to drop this table again.
1821
01:44:01,600 --> 01:44:03,600
So, I’m just going to click DROP TABLE
and we'll go ahead and drop the table.
1822
01:44:03,600 --> 01:44:08,900
And over here, I’m going to create the table again,
but we're going to give this some other stuff.
1823
01:44:08,900 --> 01:44:10,300
So, these are actually what we would call constraints.
1824
01:44:10,300 --> 01:44:15,100
So, I would say like a constraint would be
that it’s NOT NULL or that it’s UNIQUE.
1825
01:44:15,100 --> 01:44:18,300
But there’s a couple other constraints
that we can add that I want to show you guys.
1826
01:44:18,300 --> 01:44:21,800
So, let’s say that we wanted to set a default value.
1827
01:44:21,800 --> 01:44:27,600
So, let’s say that if somebody didn’t enter in a major,
we wanted to be able to give them a default major.
1828
01:44:27,600 --> 01:44:31,100
Well, I can actually say that something has a default.
1829
01:44:31,100 --> 01:44:34,600
So, I can come down here
and I can say after major, DEFAULT.
1830
01:44:34,600 --> 01:44:40,200
And then inside of single quotes I can just type in
what I want the default to be.
1831
01:44:40,200 --> 01:44:45,800
So, in our case, if somebody doesn’t provide us with a
major, why don’t we just say that they’re ‘undecided’.
1832
01:44:45,800 --> 01:44:50,900
So, basically if the major field is left blank,
we'll say that the person is undecided.
1833
01:44:50,900 --> 01:44:55,000
So, then down here, why don’t we go ahead
and insert a student into the table
1834
01:44:55,000 --> 01:44:56,500
that doesn’t have a major.
1835
01:44:56,500 --> 01:44:58,100
So, here we can just say INSERT INTO student
1836
01:44:58,100 --> 01:45:01,900
and we'll go ahead and specify
that we're going to enter in the student_id.
1837
01:45:01,900 --> 01:45:03,300
And also the name.
1838
01:45:03,300 --> 01:45:06,000
But you'll notice that I’m not putting major in here.
1839
01:45:06,000 --> 01:45:08,000
Which means that we don’t have to give this a major.
1840
01:45:08,000 --> 01:45:09,500
So, now we'll get rid of Jack’s major.
1841
01:45:09,500 --> 01:45:15,500
And when I go ahead and run this,
you'll see that we got a success message.
1842
01:45:15,500 --> 01:45:19,800
And if I was to select all the entries from the student,
it says student_id is 1.
1843
01:45:19,800 --> 01:45:22,300
His name is Jack and his major is undecided.
1844
01:45:22,300 --> 01:45:24,500
So, because I didn’t insert a major,
1845
01:45:24,500 --> 01:45:28,500
it’s going ahead and populating that major
for us with the value undecided
1846
01:45:28,500 --> 01:45:32,200
because that was the default value
that I placed over here.
1847
01:45:32,200 --> 01:45:35,700
So, that’s another really useful constraint
that we can put on this.
1848
01:45:35,700 --> 01:45:38,800
All right, so there’s one more of these
little constraints that I want to show you guys.
1849
01:45:38,800 --> 01:45:42,300
And it’s actually really useful for working
with primary keys.
1850
01:45:42,300 --> 01:45:50,900
So, you'll notice that the primary keys
for all of these guys were like 1, 2, 3, 4, 5, etc., right?
1851
01:45:50,900 --> 01:45:54,400
I like manually went in and incremented
all those primary keys.
1852
01:45:54,400 --> 01:45:57,900
But what you can actually do
you can have the database do that for you.
1853
01:45:57,900 --> 01:46:04,500
So, I can come over here right here after students,
so remember, student_id is the primary key, right?
1854
01:46:04,500 --> 01:46:05,800
We defined that down here.
1855
01:46:05,800 --> 01:46:10,500
I can come ever here and say AUTO_INCREMENT.
1856
01:46:10,500 --> 01:46:15,100
And AUTO_INCREMENT is basically going to specify
that the data that gets inserted into here
1857
01:46:15,100 --> 01:46:18,400
is going to automatically incremented
every time we add one in.
1858
01:46:18,400 --> 01:46:23,500
So, I could actually come down here and instead of
inserting a student ID, I can just leave it out.
1859
01:46:23,500 --> 01:46:27,700
So, I can just insert the name and the major.
1860
01:46:27,700 --> 01:46:31,200
And then I can add that information in here.
1861
01:46:31,200 --> 01:46:35,700
So, Jack is going to go ahead and study biology.
1862
01:46:35,700 --> 01:46:37,800
And actually, let me get rid of these.
1863
01:46:37,800 --> 01:46:39,200
So, we can kind of see how this works.
1864
01:46:39,200 --> 01:46:43,300
So, I can copy this for another one.
1865
01:46:43,300 --> 01:46:47,700
And we'll make this Kate and she’s studying sociology.
1866
01:46:47,700 --> 01:46:51,400
So, notice that I’m not actually
inserting the student_id, right?
1867
01:46:51,400 --> 01:46:53,400
All I’m inserting is the name and the major.
1868
01:46:53,400 --> 01:46:56,900
But because we said that this table
was going to be auto incremented.
1869
01:46:56,900 --> 01:47:00,400
In other words because we said that the student_id
was going to be auto incremented
1870
01:47:00,400 --> 01:47:02,700
we don’t actually have to do that.
1871
01:47:02,700 --> 01:47:04,200
So, I can come down here and I can run this.
1872
01:47:04,200 --> 01:47:05,600
And actually, whoops.
1873
01:47:05,600 --> 01:47:06,400
I have to create the table again.
1874
01:47:06,400 --> 01:47:07,200
My bad.
1875
01:47:07,200 --> 01:47:08,100
So, we're going to create the table.
1876
01:47:08,100 --> 01:47:11,200
And I can insert in this first entry, Jack,
who’s a biology major.
1877
01:47:11,200 --> 01:47:13,200
And I’ll run this.
1878
01:47:13,200 --> 01:47:16,300
And you'll see it says 1 row affected.
1879
01:47:16,300 --> 01:47:19,800
And then I’m also going to insert this next one
which is Kate who is studying sociology,
1880
01:47:19,800 --> 01:47:21,100
so I’m just going to click Run.
1881
01:47:21,100 --> 01:47:23,500
And so, now I’ve inserted
both of these students into the table.
1882
01:47:23,500 --> 01:47:28,100
And if I select all from the student table, you'll see
down here that we get both of these entries.
1883
01:47:28,100 --> 01:47:33,400
But Jack has an ID of 1 and Kate as an ID of 2 even
though I didn’t rigorously specify that, right?
1884
01:47:33,400 --> 01:47:37,400
So, I didn’t actually add in the ID’s
for either of these guys,
1885
01:47:37,400 --> 01:47:42,700
and yet they still showed up down here in the table
which was pretty useful.
1886
01:47:42,700 --> 01:47:47,400
So, that’s a great way for you to just like automatically
increment like something like a primary key
1887
01:47:47,400 --> 01:47:50,100
or really any other row in the table
by using AUTO_INCREMENT.
1888
01:47:50,100 --> 01:47:54,800
So, those are just a couple little things
that you can do when you’re creating the table.
1889
01:47:54,800 --> 01:47:56,700
We would call those constraints.
1890
01:47:56,700 --> 01:48:02,000
So, you can add constraints onto the specific columns
of the table and that can control
1891
01:48:02,000 --> 01:48:04,500
like what types of information
can be put inside of there.
1892
01:48:04,500 --> 01:48:07,800
So, that’s – there’s a little bit more
of an advanced way
1893
01:48:07,800 --> 01:48:10,500
for you guys to insert information into the database.
1894
01:48:10,500 --> 01:48:14,600
[Update & Delete]
1895
01:48:14,600 --> 01:48:19,100
In this tutorial I’m going to talk to you guys about
updating and deleting rows inside of a database table.
1896
01:48:19,100 --> 01:48:24,900
So, in the last few tutorials we've looked at creating
database tables and inserting information into them.
1897
01:48:24,900 --> 01:48:28,000
But now once we have that information
inside those tables,
1898
01:48:28,000 --> 01:48:32,100
I’m going to show you guys how to update it
and delete it whenever you want.
1899
01:48:32,100 --> 01:48:35,000
So, we have here our student database.
1900
01:48:35,000 --> 01:48:39,100
And this is – or the student table and it’s the table
that we've been using throughout the course.
1901
01:48:39,100 --> 01:48:42,000
And so, down here you can see
it’s just a bunch of students.
1902
01:48:42,000 --> 01:48:44,500
And they have names, majors, and ID's.
1903
01:48:44,500 --> 01:48:48,200
So, right now, these are all the students
that are stored inside of our database.
1904
01:48:48,200 --> 01:48:51,500
So, these are the database entries
that we can work with.
1905
01:48:51,500 --> 01:48:54,600
The first thing I’ll show you guys show to do
is update some of these entries.
1906
01:48:54,600 --> 01:48:58,500
And so, you know, let’s say that we wanted,
you know, update something.
1907
01:48:58,500 --> 01:49:02,500
So, let’s say that we wanted to change the name of
one of the majors in our school, right?
1908
01:49:02,500 --> 01:49:06,600
So, let’s say I am the database
administrator for my school.
1909
01:49:06,600 --> 01:49:12,100
And we decided that instead of calling the major
biology, we wanted to call it bio.
1910
01:49:12,100 --> 01:49:15,700
So, the official name for the major
is no longer biology.
1911
01:49:15,700 --> 01:49:17,100
It’s just going to be bio.
1912
01:49:17,100 --> 01:49:20,400
Well, we could actually make that change
inside the database.
1913
01:49:20,400 --> 01:49:22,800
So, I can use the special keyword called UPDATE.
1914
01:49:22,800 --> 01:49:26,300
And I can just type out the name of the table
where I want to do the update.
1915
01:49:26,300 --> 01:49:27,600
So, in our case, it’s student.
1916
01:49:27,600 --> 01:49:27,600
And over here I can say UPDATE student
I want to set major equal to ‘Bio’, right?
1917
01:49:27,600 --> 01:49:37,900
But here’s the thing.
1918
01:49:37,900 --> 01:49:42,000
I don’t want to set the major equal to bio
for all the students.
1919
01:49:42,000 --> 01:49:46,800
I only want to set the major equal to bio for the
students who have their major as biology.
1920
01:49:46,800 --> 01:49:48,600
So, over here we can specify that.
1921
01:49:48,600 --> 01:49:50,100
I can say WHERE.
1922
01:49:50,100 --> 01:49:51,700
And here I can put a condition.
1923
01:49:51,700 --> 01:49:54,400
So, I can say major is equal to ‘Biology’.
1924
01:49:54,400 --> 01:49:56,000
And I’m going to be semicolon.
1925
01:49:56,000 --> 01:50:00,400
So, this whole thing now is
one SQL statement or query.
1926
01:50:00,400 --> 01:50:03,700
And so, what I'm doing is
I’m updating the student table
1927
01:50:03,700 --> 01:50:10,500
and I’m setting the major column equal to ‘Bio’
where the major column is already equal to ‘Biology’.
1928
01:50:10,500 --> 01:50:14,800
So, now if I was to go ahead and run this,
you'll see it says Rows affected: 2.
1929
01:50:14,800 --> 01:50:17,700
So, we updated to of the rows in this database.
1930
01:50:17,700 --> 01:50:20,600
And if I was to select all of them
and I’m going to click Run,
1931
01:50:20,600 --> 01:50:23,900
you'll see down here now for the two students
who are biology majors,
1932
01:50:23,900 --> 01:50:27,200
it’s now saying that they’re bio majors, right?
1933
01:50:27,200 --> 01:50:33,800
So, it updated the database only in those places
where the student’s major was equal to ‘Biology’.
1934
01:50:33,800 --> 01:50:35,200
And that’s what we can do here.
1935
01:50:35,200 --> 01:50:40,400
So, this is just a very basic update statement,
but we can use this in order to all sorts of stuff.
1936
01:50:40,400 --> 01:50:41,800
So, let’s do another one.
1937
01:50:41,800 --> 01:50:44,300
We can do the same for computer science.
1938
01:50:44,300 --> 01:50:45,700
So, I could say UPDATE student.
1939
01:50:45,700 --> 01:50:52,800
SET major equal to ‘Comp sci’
WHERE major = ‘Computer Science’.
1940
01:50:52,800 --> 01:50:54,800
So, I’m basically abbreviating this major.
1941
01:50:54,800 --> 01:50:56,300
So, let’s go ahead and run this.
1942
01:50:56,300 --> 01:50:58,900
And again, I’m just going to
SELECT ALL from the table.
1943
01:50:58,900 --> 01:51:04,500
You'll see down here, Mike, who used to be a
‘Computer Science’ major is now a ‘Comp Sci’ major.
1944
01:51:04,500 --> 01:51:06,200
So, I was able to update that.
1945
01:51:06,200 --> 01:51:08,600
Another thing you can do
is you can update specific things.
1946
01:51:08,600 --> 01:51:10,300
So, I can say like UPDATE student.
1947
01:51:10,300 --> 01:51:13,000
SET major equal to ‘Comp Sci’, WHERE.
1948
01:51:13,000 --> 01:51:15,200
And then over here I can do a different condition.
1949
01:51:15,200 --> 01:51:19,800
So, I can say like student_id is equal to 4.
1950
01:51:19,800 --> 01:51:24,500
So, now I’m updating the student’s major
who has an ID of 4.
1951
01:51:24,500 --> 01:51:26,400
So, this is like a different type of query.
1952
01:51:26,400 --> 01:51:28,400
So, down here I’m running that.
1953
01:51:28,400 --> 01:51:30,100
And then I’m going to SELECT ALL.
1954
01:51:30,100 --> 01:51:34,600
And so, now you can see that Jack
who used to be a biology major
1955
01:51:34,600 --> 01:51:37,700
is now a comp sci major
because his student ID was 4.
1956
01:51:37,700 --> 01:51:41,400
So, you can get kind of creative, you know,
playing around with setting different things
1957
01:51:41,400 --> 01:51:43,300
and then checking different conditions.
1958
01:51:43,300 --> 01:51:47,000
You can also use more complex conditions down here.
1959
01:51:47,000 --> 01:51:50,100
So, down here we have bio majors
and we have chemistry majors, right?
1960
01:51:50,100 --> 01:51:54,600
Well, let’s say that any student
who is either a biology or a chemistry major
1961
01:51:54,600 --> 01:51:56,900
is now going to be a biochemistry major.
1962
01:51:56,900 --> 01:51:59,800
So, we're combining those two majors together.
1963
01:51:59,800 --> 01:52:01,600
Well, I could update that here.
1964
01:52:01,600 --> 01:52:03,900
So, I could say UPDATE student.
1965
01:52:03,900 --> 01:52:06,200
SET major equal to ‘Biochemistry’.
1966
01:52:06,200 --> 01:52:11,500
WHERE major is equal to ‘Bio’.
1967
01:52:11,500 --> 01:52:14,000
And over here we could say OR.
1968
01:52:14,000 --> 01:52:18,100
And then we could also say major
is equal to ‘Chemistry’.
1969
01:52:18,100 --> 01:52:20,000
And this is going to give us some OR logic.
1970
01:52:20,000 --> 01:52:24,700
So, now if the student has a major of bio
or if they have a major of chemistry,
1971
01:52:24,700 --> 01:52:26,600
we're going to set it equal to biochemistry.
1972
01:52:26,600 --> 01:52:29,500
So, we're basically combining those two majors together.
1973
01:52:29,500 --> 01:52:33,200
So, now I’ll click Run
and you can see rows affected is 2.
1974
01:52:33,200 --> 01:52:35,300
And I’ll SELECT ALL from students.
1975
01:52:35,300 --> 01:52:39,600
And you can see now Jack and Claire –
Jack used to be a bio major.
1976
01:52:39,600 --> 01:52:41,200
Claire used to be a chem major.
1977
01:52:41,200 --> 01:52:43,300
Are both now biochemistry majors.
1978
01:52:43,300 --> 01:52:47,400
So, you can use OR in order to
check multiple things like that.
1979
01:52:47,400 --> 01:52:49,900
You can also set multiple things.
1980
01:52:49,900 --> 01:52:53,000
So, over here I’m actually going to
get rid of this condition.
1981
01:52:53,000 --> 01:52:57,700
And I can say SET name equal to ‘Tom’.
1982
01:52:57,700 --> 01:53:06,000
And major equal to ‘undecided’.
1983
01:53:06,000 --> 01:53:11,800
WHERE student_id is equal to 1.
1984
01:53:11,800 --> 01:53:16,300
So, whoever has the student equal to 1
is going to have their name set to Tom
1985
01:53:16,300 --> 01:53:18,800
and their major set to undecided.
1986
01:53:18,800 --> 01:53:20,000
So, let’s go ahead and run this.
1987
01:53:20,000 --> 01:53:22,500
And you'll see it says 1 row affected.
1988
01:53:22,500 --> 01:53:24,600
So, let’s SELECT ALL from the student’s table.
1989
01:53:24,600 --> 01:53:29,700
And you'll see down here, the student with the ID of 1
is now named Tom and their major is undecided.
1990
01:53:29,700 --> 01:53:34,500
So, you can change multiple columns
within the same query like I did up here.
1991
01:53:34,500 --> 01:53:39,200
All right, so there’s one more thing we can do,
which we can actually get rid of this WHERE.
1992
01:53:39,200 --> 01:53:40,400
So, this is actually optional.
1993
01:53:40,400 --> 01:53:45,600
So, if I got rid of WHERE, I can then set something.
1994
01:53:45,600 --> 01:53:47,000
So, why don’t we set major equal to undecided.
1995
01:53:47,000 --> 01:53:51,200
So, if I get rid of that WHERE statement, this is just
going to apply to every single row in the table.
1996
01:53:51,200 --> 01:53:53,800
So, now when we run this,
you'll see rows affected was 5.
1997
01:53:53,800 --> 01:53:58,800
And now when we SELECT ALL from the students
table, all of their majors are now undecided.
1998
01:53:58,800 --> 01:54:03,100
So, you can either select specific rows
or groups of rows with that WHERE
1999
01:54:03,100 --> 01:54:06,800
or you can just do it to all of them
by dropping the WHERE.
2000
01:54:06,800 --> 01:54:10,100
So, now I’m going to show you guys
how we can delete rows from the table.
2001
01:54:10,100 --> 01:54:12,400
And it’s actually pretty similar
to how we would update rows.
2002
01:54:12,400 --> 01:54:15,100
So, instead of updating a specific row
or a group of rows,
2003
01:54:15,100 --> 01:54:18,400
I can actually delete a specific row
or a group of rows.
2004
01:54:18,400 --> 01:54:21,700
So, it’s like I said, pretty similar to UPDATE.
2005
01:54:21,700 --> 01:54:25,200
I’m just going to type in DELETE FROM
and then the name of the table.
2006
01:54:25,200 --> 01:54:27,300
So in our case it’s going to be student.
2007
01:54:27,300 --> 01:54:30,600
And you can actually just end this off right here.
2008
01:54:30,600 --> 01:54:31,800
I can put a semicolon here.
2009
01:54:31,800 --> 01:54:35,500
And this will go ahead and delete
all of the rows inside of the table.
2010
01:54:35,500 --> 01:54:37,600
But why don’t we try to get more specific?
2011
01:54:37,600 --> 01:54:39,600
So, I’m going to try to delete a specific row.
2012
01:54:39,600 --> 01:54:41,300
I can say DELETE FROM student.
2013
01:54:41,300 --> 01:54:43,500
WHERE – and now I can specify a condition.
2014
01:54:43,500 --> 01:54:48,800
So, I can say like WHERE student_id is equal to 5.
2015
01:54:48,800 --> 01:54:55,200
So, what this will do is it’ll delete any students or any
rows from the table that have a student ID of 5.
2016
01:54:55,200 --> 01:54:58,100
So, down here we just have one like that.
2017
01:54:58,100 --> 01:55:01,600
So, Mike, who is an undecided major has an ID of 5.
2018
01:55:01,600 --> 01:55:03,200
So, if I was to run this.
2019
01:55:03,200 --> 01:55:06,300
Now you'll see it says rows affected, 1.
2020
01:55:06,300 --> 01:55:10,800
And we'll select all from students
and Mike is no longer inside the table.
2021
01:55:10,800 --> 01:55:13,100
So, we actually deleted a specific row.
2022
01:55:13,100 --> 01:55:16,000
And just like update,
you can get more fancy with these queries.
2023
01:55:16,000 --> 01:55:21,300
So, I could say like WHERE name is equal to Tom.
2024
01:55:21,300 --> 01:55:23,800
And you can also do an AND.
2025
01:55:23,800 --> 01:55:26,300
So, instead doing OR, we're doing an AND here.
2026
01:55:26,300 --> 01:55:30,800
And I can say like major is equal to ‘undecided’.
2027
01:55:30,800 --> 01:55:34,700
So, this will delete any students from the table
who have the name of Tom
2028
01:55:34,700 --> 01:55:37,400
and who are an undecided major.
2029
01:55:37,400 --> 01:55:41,100
So, now when I run this query, you'll see –
and I select all the students again –
2030
01:55:41,100 --> 01:55:44,100
that Tom got deleted from the table.
2031
01:55:44,100 --> 01:55:45,800
So, he’s no longer there.
2032
01:55:45,800 --> 01:55:48,700
So, just like we used WHERE
in the UPDATE statements,
2033
01:55:48,700 --> 01:55:51,500
we can use WHERE with the DELETE statements
to do all of that.
2034
01:55:51,500 --> 01:55:54,000
But if you wanted,
you can just delete everything from the table.
2035
01:55:54,000 --> 01:55:56,100
So, now I’m just going to run this command.
2036
01:55:56,100 --> 01:56:00,200
And if I query the table,
you'll see that nothing is there.
2037
01:56:00,200 --> 01:56:01,700
So, we deleted everything.
2038
01:56:01,700 --> 01:56:03,700
So, updating and deleting is extremely useful.
2039
01:56:03,700 --> 01:56:09,000
And you can use that WHERE statement in order to
specify what specific row you want to update
2040
01:56:09,000 --> 01:56:10,600
and what specific row you want to delete.
2041
01:56:10,600 --> 01:56:14,400
[Basic Queries]
2042
01:56:14,400 --> 01:56:18,500
In this tutorial I’m going to talk to you guys about
getting information from the database.
2043
01:56:18,500 --> 01:56:21,600
More specifically we're going to
look at the SELECT keyword
2044
01:56:21,600 --> 01:56:26,300
and we're going to look at the different ways
that we can ask the database management system
2045
01:56:26,300 --> 01:56:28,200
to give us some information back.
2046
01:56:28,200 --> 01:56:31,500
So, one of the core tenets of interacting
with the database management system
2047
01:56:31,500 --> 01:56:34,600
and using SQL is writing these little queries.
2048
01:56:34,600 --> 01:56:41,600
And a query is essentially just a block of SQL that’s
designed to ask the database management system
2049
01:56:41,600 --> 01:56:43,600
for a particular piece of information.
2050
01:56:43,600 --> 01:56:48,800
And so, one of the things that we need to be aware of
is that when you’re keeping track of, you know,
2051
01:56:48,800 --> 01:56:50,500
huge amounts of information in a database,
2052
01:56:50,500 --> 01:56:53,500
you want to be able to grab
specific information easily.
2053
01:56:53,500 --> 01:56:59,700
So, let’s say that I’m trying to, you know, grab a bunch
of students from our little student table right here.
2054
01:56:59,700 --> 01:57:04,500
Well, imagine that we had like a million students
stored inside of that table, right?
2055
01:57:04,500 --> 01:57:07,400
I might not want to just grab every single student.
2056
01:57:07,400 --> 01:57:11,100
I might want to just grab students
who meet a certain condition
2057
01:57:11,100 --> 01:57:14,800
or students who have a certain major
or students with a certain name.
2058
01:57:14,800 --> 01:57:18,900
And we can use SQL queries in order to
specify those things.
2059
01:57:18,900 --> 01:57:24,500
So, instead of the relational database management
system giving us back all the entries in a specific table,
2060
01:57:24,500 --> 01:57:28,600
instead it can just give us back very specific entries
which meet a certain condition.
2061
01:57:28,600 --> 01:57:32,300
So, we're going to talk a little bit
about the basics of doing that stuff.
2062
01:57:32,300 --> 01:57:34,000
Now, this is actually a huge topic
2063
01:57:34,000 --> 01:57:36,900
and it’s the topic that we're going to be talking about
for most of the rest of course.
2064
01:57:36,900 --> 01:57:41,800
So, this is going to kind of give you guys an
introduction into writing all of these little queries.
2065
01:57:41,800 --> 01:57:44,500
So, over here, you’ll see that I have this query here.
2066
01:57:44,500 --> 01:57:48,400
And it’s just SELECT * FROM student.
2067
01:57:48,400 --> 01:57:50,600
And actually, if you want,
you can put this on two different lines.
2068
01:57:50,600 --> 01:57:51,900
A lot of people will do that.
2069
01:57:51,900 --> 01:57:56,000
So, this SELECT keyword is a very special word.
2070
01:57:56,000 --> 01:58:00,300
And this SELECT keyword is basically going to tell
the relational database management system
2071
01:58:00,300 --> 01:58:02,800
that we want to get some information from it.
2072
01:58:02,800 --> 01:58:04,200
So, I can say SELECT.
2073
01:58:04,200 --> 01:58:07,500
And then right next to it I can specify
what information I want to get.
2074
01:58:07,500 --> 01:58:12,100
And so far in this course we've
been using this star or this asterisk.
2075
01:58:12,100 --> 01:58:15,900
And basically, the asterisk means
that we want to grab all of the information.
2076
01:58:15,900 --> 01:58:19,700
But if we wanted, we could specify a specific column.
2077
01:58:19,700 --> 01:58:23,900
So, we can select specific columns of information
2078
01:58:23,900 --> 01:58:26,700
that we want to get back
from the database management system
2079
01:58:26,700 --> 01:58:29,100
and then we can say FROM whichever table.
2080
01:58:29,100 --> 01:58:36,600
So, SELECT * FROM table could also be read
as select every column from the student table.
2081
01:58:36,600 --> 01:58:40,900
So, over here you'll see when I run this,
we get this information down here.
2082
01:58:40,900 --> 01:58:43,000
So, we're getting all of the students in the table.
2083
01:58:43,000 --> 01:58:47,500
We're getting their student ID’s
and their names and their majors.
2084
01:58:47,500 --> 01:58:49,600
If I wanted, I could change the columns up here.
2085
01:58:49,600 --> 01:58:52,000
So, I could say like name.
2086
01:58:52,000 --> 01:58:56,200
And now what this will do is
it’ll select all of the names from the student table.
2087
01:58:56,200 --> 01:59:00,100
So, if I was to run this query,
you'll see down here we get just the names.
2088
01:59:00,100 --> 01:59:04,600
So, we have the name
and then it’s Jake, Kate, Claire, Jack, and Mike.
2089
01:59:04,600 --> 01:59:07,900
So, these are all of the names
that were stored inside of the database.
2090
01:59:07,900 --> 01:59:10,400
I could also include something else.
2091
01:59:10,400 --> 01:59:13,500
So, I could say name, major FROM student.
2092
01:59:13,500 --> 01:59:15,500
And then down here when I run this query,
2093
01:59:15,500 --> 01:59:18,400
you'll see we're getting the
students, names, and the majors.
2094
01:59:18,400 --> 01:59:20,700
But we're not getting the student’s ID.
2095
01:59:20,700 --> 01:59:24,200
So, we're able to specify
which specific columns we want to get back.
2096
01:59:24,200 --> 01:59:27,300
You can also pre-pen these
with the name of the table.
2097
01:59:27,300 --> 01:59:30,000
So, I could say like student.name.
2098
01:59:30,000 --> 01:59:31,600
And student.major.
2099
01:59:31,600 --> 01:59:34,100
And sometimes people will do this
just because student.name,
2100
01:59:34,100 --> 01:59:37,400
it’s clear which table the name is coming from.
2101
01:59:37,400 --> 01:59:40,700
And as we write more and more complex queries,
that can come in handy more.
2102
01:59:40,700 --> 01:59:46,500
So, for the most part, I’m probably going to be writing
them both ways, just depending on the situation.
2103
01:59:46,500 --> 01:59:48,300
But you could do something like this.
2104
01:59:48,300 --> 01:59:50,800
We can also order the information that we get back.
2105
01:59:50,800 --> 01:59:55,900
So, here I’m getting the student’s name
and the student’s major from student.
2106
01:59:55,900 --> 01:59:58,600
And if I wanted, I could order that information.
2107
01:59:58,600 --> 02:00:02,100
So, I could use another command which is ORDER BY.
2108
02:00:02,100 --> 02:00:07,000
And then over here I can just put the name
of the column that I want to order these by.
2109
02:00:07,000 --> 02:00:08,900
So, I can say ORDER BY name.
2110
02:00:08,900 --> 02:00:12,200
And now when I run this,
you'll see we get the same results,
2111
02:00:12,200 --> 02:00:15,100
but they're in alphabetical order based off the name.
2112
02:00:15,100 --> 02:00:17,700
So, we get Claire, Jack, Jack, Kate, and Mike.
2113
02:00:17,700 --> 02:00:20,000
So, these are now ordered in alphabetical order.
2114
02:00:20,000 --> 02:00:23,300
And by default, these are going to be
in ascending order.
2115
02:00:23,300 --> 02:00:26,000
But if you wanted, you could put them
in descending order.
2116
02:00:26,000 --> 02:00:29,500
So, you could say DESC and this stands for descending.
2117
02:00:29,500 --> 02:00:33,800
So, now if I run this, you'll see that all the names
get returned in the opposite order.
2118
02:00:33,800 --> 02:00:36,300
So, Claire is all the way at the bottom.
2119
02:00:36,300 --> 02:00:39,000
And then we go all the way back up to Mike.
2120
02:00:39,000 --> 02:00:40,600
So, you can order by anything.
2121
02:00:40,600 --> 02:00:43,100
So, I can even order by like student_id.
2122
02:00:43,100 --> 02:00:46,000
So, I’m not returning the student_id.
2123
02:00:46,000 --> 02:00:49,900
In other words, I’m not getting the student ID up here,
but I can still order by it.
2124
02:00:49,900 --> 02:00:53,600
And so, now these are going to be in descending
order of student ID’s.
2125
02:00:53,600 --> 02:00:56,500
So, actually why don’t we just get all of these now.
2126
02:00:56,500 --> 02:00:58,600
So, I’ll just say SELECT ALL from student.
2127
02:00:58,600 --> 02:01:01,700
And you'll see now it’s ordered in descending order
by student ID.
2128
02:01:01,700 --> 02:01:03,700
It’s a little bit clearer, 5, 4, 3, 2, 1.
2129
02:01:03,700 --> 02:01:08,100
But I can also get rid of it or I can just say ASC
which stands for ascending.
2130
02:01:08,100 --> 02:01:10,800
And now it will order them in ascending order.
2131
02:01:10,800 --> 02:01:13,800
So, you can order by a bunch of different stuff.
2132
02:01:13,800 --> 02:01:15,900
You can also order by different sub columns.
2133
02:01:15,900 --> 02:01:20,200
So, I could say like ORDER BY –
and we'll start with major.
2134
02:01:20,200 --> 02:01:23,500
And then after that we'll do student_id.
2135
02:01:23,500 --> 02:01:26,600
So, SELECT ALL from student.
2136
02:01:26,600 --> 02:01:27,900
ORDER BY major.
2137
02:01:27,900 --> 02:01:29,100
And then student_id.
2138
02:01:29,100 --> 02:01:31,000
So, it’s going to order them by major first.
2139
02:01:31,000 --> 02:01:33,400
And then if there’s any of them
that have the same major,
2140
02:01:33,400 --> 02:01:35,500
it’ll order them by student ID further.
2141
02:01:35,500 --> 02:01:38,200
So, I’m going to run this.
2142
02:01:38,200 --> 02:01:40,200
And you'll see down here,
it’s ordering everybody by major.
2143
02:01:40,200 --> 02:01:43,700
So, we're getting biology, biology, chemistry,
computer science, sociology.
2144
02:01:43,700 --> 02:01:46,600
In this case, these two students
have the same major.
2145
02:01:46,600 --> 02:01:48,500
They also have the same name too.
2146
02:01:48,500 --> 02:01:50,500
But their student ID’s are different.
2147
02:01:50,500 --> 02:01:53,600
So, the student ID’s are now order
in ascending order.
2148
02:01:53,600 --> 02:01:57,900
But I said descending right here.
2149
02:01:57,900 --> 02:01:59,200
And I run this query.
2150
02:01:59,200 --> 02:02:04,500
Now you'll notice that the biology major
with student ID 4 came first and then 1.
2151
02:02:04,500 --> 02:02:10,300
So, it ordered it first by major and then within that,
if they have the same major,
2152
02:02:10,300 --> 02:02:11,800
it ordered them by student ID.
2153
02:02:11,800 --> 02:02:13,800
And you can specify as many of those as you want.
2154
02:02:13,800 --> 02:02:16,500
You can also limit the amount
and results you're getting.
2155
02:02:16,500 --> 02:02:19,400
So, I can say like SELECT ALL from student.
2156
02:02:19,400 --> 02:02:20,600
And then I can say LIMIT.
2157
02:02:20,600 --> 02:02:21,900
And here I can put like 2.
2158
02:02:21,900 --> 02:02:25,000
And now what this will do,
is it’ll limit the results I get back to 2.
2159
02:02:25,000 --> 02:02:28,100
So, now instead of getting all the students back,
we only got 2.
2160
02:02:28,100 --> 02:02:33,800
So, if you only want like a specify number of rows
back from the table, then you’ll only get that.
2161
02:02:33,800 --> 02:02:35,500
And you can also combine these.
2162
02:02:35,500 --> 02:02:36,900
So, I could also like order them.
2163
02:02:36,900 --> 02:02:41,700
So, I could say ORDER BY student_id, descending.
2164
02:02:41,700 --> 02:02:44,600
So, now this is going to select all the students.
2165
02:02:44,600 --> 02:02:47,800
Order them by student ID
only give us 2 of them back.
2166
02:02:47,800 --> 02:02:50,200
So, now when I run this
you'll see we're getting 2 back.
2167
02:02:50,200 --> 02:02:53,700
And it’s ordering them in
descending order by student ID.
2168
02:02:53,700 --> 02:02:55,600
So, that can be pretty useful.
2169
02:02:55,600 --> 02:02:58,300
And that’s just another way
that you can make these more complex.
2170
02:02:58,300 --> 02:03:01,800
The final thing that we can do that I’m going to show
you guys in this tutorial is filtering.
2171
02:03:01,800 --> 02:03:04,100
So, I can say WHERE.
2172
02:03:04,100 --> 02:03:07,600
So, if you remember in a previous tutorial
we were updating and deleting students.
2173
02:03:07,600 --> 02:03:12,700
And we wanted to only update or delete specific
students where certain conditions were true.
2174
02:03:12,700 --> 02:03:14,600
And we used this WHERE condition.
2175
02:03:14,600 --> 02:03:16,000
You can do the same thing for SELECT.
2176
02:03:16,000 --> 02:03:22,800
So, I could say like SELECT ALL FROM students
WHERE major is equal to ‘Biology’.
2177
02:03:22,800 --> 02:03:26,300
And so, now this is only going to give us
the students who are biology majors.
2178
02:03:26,300 --> 02:03:31,100
And you can see down here we get back the
two Jacks who are both biology majors.
2179
02:03:31,100 --> 02:03:34,200
You can do the same thing for chemistry.
2180
02:03:34,200 --> 02:03:36,000
So, let’s see if we have any chemistry majors.
2181
02:03:36,000 --> 02:03:37,300
We do.
2182
02:03:37,300 --> 02:03:38,500
Looks like Claire is a chemistry major.
2183
02:03:38,500 --> 02:03:42,800
And if you wanted, we can only return
specific columns, like I said.
2184
02:03:42,800 --> 02:03:46,800
So, we can say SELECT, you know,
the name and major from student.
2185
02:03:46,800 --> 02:03:48,200
WHERE major is ‘Chemistry’.
2186
02:03:48,200 --> 02:03:51,700
And now we're only getting the name
and the major back.
2187
02:03:51,700 --> 02:03:53,800
And you can make these more complex.
2188
02:03:53,800 --> 02:03:59,500
So, I could say like WHERE major is equal to
‘Chemistry’ OR major is equal to ‘Biology’.
2189
02:03:59,500 --> 02:04:03,500
And so, now this will give us all of the chemistry
and the biology majors
2190
02:04:03,500 --> 02:04:05,700
so we get the two Jacks and Claire.
2191
02:04:05,700 --> 02:04:08,600
And we can also do different things.
2192
02:04:08,600 --> 02:04:13,600
So, like I said, like WHERE major is equal to
‘Chemistry’ OR name is equal to ‘Kate’.
2193
02:04:13,600 --> 02:04:16,300
And so, now we'll get back any of those students.
2194
02:04:16,300 --> 02:04:20,000
So, we get back Kate and Claire
and they have different majors.
2195
02:04:20,000 --> 02:04:22,800
So, you can play around
with those WHERE statements
2196
02:04:22,800 --> 02:04:26,500
to get specific entries from the individual table.
2197
02:04:26,500 --> 02:04:31,100
So, I want to talk to you guys about how we can make
these where’s a little bit more complex.
2198
02:04:31,100 --> 02:04:35,600
So, obviously over here we're using equals,
but there’s a bunch of other stuff you can use too.
2199
02:04:35,600 --> 02:04:38,100
So, this is actually comment in SQL.
2200
02:04:38,100 --> 02:04:42,800
So, if you put two dashes, then anything
after it is going to be a comment.
2201
02:04:42,800 --> 02:04:46,100
But here I have all the different comparison operators.
2202
02:04:46,100 --> 02:04:47,100
So, we have equals.
2203
02:04:47,100 --> 02:04:53,700
We also have less than, greater than, less than or
equal to, greater than equal to, equal to, not equal to
2204
02:04:53,700 --> 02:04:56,000
– which is these less than/greater than signs.
2205
02:04:56,000 --> 02:04:57,000
And then AND and OR.
2206
02:04:57,000 --> 02:04:59,500
So, you guys have seen some of these,
but we can use these.
2207
02:04:59,500 --> 02:05:02,400
So, I can say like major not equal to chemistry.
2208
02:05:02,400 --> 02:05:06,300
So, this is going to select all the students
where the major is not equal to chemistry.
2209
02:05:06,300 --> 02:05:11,600
So, if I run this, now we get all the students except
Claire because Claire is a chemistry major.
2210
02:05:11,600 --> 02:05:13,700
Or we can do the same thing for numbers.
2211
02:05:13,700 --> 02:05:16,000
So, I can say like student_id.
2212
02:05:16,000 --> 02:05:19,500
So SELECT ALL the students where student_id
is less than 3.
2213
02:05:19,500 --> 02:05:21,700
And we need to get rid of this.
2214
02:05:21,700 --> 02:05:26,100
And so, now we'll get all the students
who have student ID’s.
2215
02:05:26,100 --> 02:05:28,500
Let me get all of these.
2216
02:05:28,500 --> 02:05:32,500
So, we're only getting students 1 and 2.
2217
02:05:32,500 --> 02:05:35,700
We're not getting anything –
or we can do less than equal to 3.
2218
02:05:35,700 --> 02:05:39,000
And now this will also give us that student ID
equal to 3.
2219
02:05:39,000 --> 02:05:40,500
And you can combine these.
2220
02:05:40,500 --> 02:05:46,700
So, like where student_id is less than 3
and name is not equal to Jack.
2221
02:05:46,700 --> 02:05:50,400
So, if we run this now, you'll see we get Kate
and Claire,
2222
02:05:50,400 --> 02:05:54,100
but we don’t get Jack who is student ID number 1
because it didn’t fit the condition.
2223
02:05:54,100 --> 02:05:58,800
So, you can these all these comparison operators
to compare, you know, different things.
2224
02:05:58,800 --> 02:06:01,700
And by using them inside of this
WHERE statement over here
2225
02:06:01,700 --> 02:06:06,500
you can seriously filter the results down to
only like the few that you need.
2226
02:06:06,500 --> 02:06:10,800
All right, so I want to show you guys one more cool
thing we can do which is using the IN keyword.
2227
02:06:10,800 --> 02:06:16,400
So, instead of just like putting a little condition
like this, we could say WHERE.
2228
02:06:16,400 --> 02:06:19,100
And then we could say like
the name of one of the columns.
2229
02:06:19,100 --> 02:06:20,100
So, like name.
2230
02:06:20,100 --> 02:06:21,600
And then I can say IN.
2231
02:06:21,600 --> 02:06:24,900
And then over here I can put some parenthesis
and I can put some values in here.
2232
02:06:24,900 --> 02:06:31,500
So, if the name is Claire, Kate, or Mike.
2233
02:06:31,500 --> 02:06:38,100
So, basically what this is saying is SELECT ALL
from student WHERE the name is IN these
2234
02:06:38,100 --> 02:06:39,300
like this group of values, right?
2235
02:06:39,300 --> 02:06:42,600
So, if the name is Claire, Kate, or Mike,
then it’s going to select that.
2236
02:06:42,600 --> 02:06:48,400
So, now I can click Run and you'll see down here,
we get all the entries, Kate, Claire, and Mike.
2237
02:06:48,400 --> 02:06:52,700
So, this is a really easy way to compare like
one column to a bunch of different values.
2238
02:06:52,700 --> 02:06:55,400
So, like we could check to see if the name is in there.
2239
02:06:55,400 --> 02:06:57,500
We could also do the same thing for like major.
2240
02:06:57,500 --> 02:07:03,000
So, like WHERE major IN ‘Biology’ or ‘Chemistry’.
2241
02:07:03,000 --> 02:07:09,400
And now if we run this, you'll see that we get Jack
and Claire because they’re both biology.
2242
02:07:09,400 --> 02:07:11,300
So, we can use IN.
2243
02:07:11,300 --> 02:07:13,800
And you can also combine all these things together.
2244
02:07:13,800 --> 02:07:25,300
So, you could say like WHERE major IN ‘Biology’ and
‘Chemistry’ AND student_id is greater than 2, right?
2245
02:07:25,300 --> 02:07:30,100
And now I'll click Run and you'll see
that we get these two students, right?
2246
02:07:30,100 --> 02:07:34,600
So, you can combine the SELECTs
and the WHEREs and even like the LIMITs
2247
02:07:34,600 --> 02:07:38,100
and all that other stuff to make these
kind of complex queries.
2248
02:07:38,100 --> 02:07:41,200
But that’s sort of the basics of doing these queries.
2249
02:07:41,200 --> 02:07:43,700
I mean obviously these are very simple queries.
2250
02:07:43,700 --> 02:07:47,200
And as the database schemas get more complex,
2251
02:07:47,200 --> 02:07:52,300
the queries that you need to select specific pieces
of information are also going to get more complex.
2252
02:07:52,300 --> 02:07:54,000
And we're just using one table here, right?
2253
02:07:54,000 --> 02:07:56,900
We're just getting information from the student table.
2254
02:07:56,900 --> 02:07:59,100
But if we had multiple different tables,
2255
02:07:59,100 --> 02:08:02,900
you know, maybe certain tables
have like foreign keys to other tables,
2256
02:08:02,900 --> 02:08:05,300
like getting information can get pretty complex.
2257
02:08:05,300 --> 02:08:10,500
And as we go forward in the course, we're actually
going to design a more complex database schema.
2258
02:08:10,500 --> 02:08:16,600
And using that database schema, we're going to learn
more about using these select commands.
2259
02:08:16,600 --> 02:08:19,500
But for now, that kind of shows you guys the basics.
2260
02:08:19,500 --> 02:08:21,800
And so, what you want to do is
just kind of play around with these, right?
2261
02:08:21,800 --> 02:08:22,700
I mean we have our student table.
2262
02:08:22,700 --> 02:08:24,400
It’s very simple.
2263
02:08:24,400 --> 02:08:25,600
It has three columns.
2264
02:08:25,600 --> 02:08:28,700
And you know, just play around with getting
specific pieces of information
2265
02:08:28,700 --> 02:08:32,800
using these WHEREs and INs
and all of these different keywords
2266
02:08:32,800 --> 02:08:36,100
in order to select the information that you want.
2267
02:08:36,100 --> 02:08:40,100
[Company Database Into]
2268
02:08:40,100 --> 02:08:45,400
In this tutorial I’m going to introduce you guys
to a more complex database query
2269
02:08:45,400 --> 02:08:47,500
that we're going to be using
for the rest of the course.
2270
02:08:47,500 --> 02:08:51,000
And so, up to this point in the course
we've learned a lot of stuff with SQL.
2271
02:08:51,000 --> 02:08:54,500
We learned how to create tables,
insert data into those tables.
2272
02:08:54,500 --> 02:08:56,600
We learned about the different datatypes.
2273
02:08:56,600 --> 02:08:59,900
We also learned how to update and delete data.
2274
02:08:59,900 --> 02:09:04,200
And we learned how to query
and select data from the database, right?
2275
02:09:04,200 --> 02:09:07,100
So, we learned a lot of the core concepts in SQL.
2276
02:09:07,100 --> 02:09:09,800
And up to this point, if you guys
have been following along,
2277
02:09:09,800 --> 02:09:12,200
then you have a pretty good
fundamental understanding
2278
02:09:12,200 --> 02:09:14,500
of the different things
that we can do in SQL.
2279
02:09:14,500 --> 02:09:17,800
Now, obviously we didn’t cover everything,
but we covered a lot of the basics, right?
2280
02:09:17,800 --> 02:09:21,500
And we've been using this student table
which just has three columns.
2281
02:09:21,500 --> 02:09:23,100
One primary key.
2282
02:09:23,100 --> 02:09:26,000
And, you know, we learned how to create this table.
2283
02:09:26,000 --> 02:09:27,600
We learned how to insert all this information.
2284
02:09:27,600 --> 02:09:30,300
We learned how to update and delete.
2285
02:09:30,300 --> 02:09:33,400
And then also just, you know,
query for the specific language.
2286
02:09:33,400 --> 02:09:37,400
Here’s a problem though,
is this is a very simple example, right?
2287
02:09:37,400 --> 02:09:42,500
I mean it’s one database table
and it only has three different columns.
2288
02:09:42,500 --> 02:09:47,600
But in reality database schemas are going to be
a lot more complex than this, right?
2289
02:09:47,600 --> 02:09:52,100
The chances sort of your database schema
just having one table is going to be pretty slim.
2290
02:09:52,100 --> 02:09:58,200
And so, you know, to really master SQL
and to really learn about all the different features.
2291
02:09:58,200 --> 02:10:02,000
And there are certain features that we haven’t
covered yet that I want to cover.
2292
02:10:02,000 --> 02:10:04,900
We're going to need
a more complex database schema.
2293
02:10:04,900 --> 02:10:09,600
There’s certain things that I just can’t show you guys
on this student table
2294
02:10:09,600 --> 02:10:12,100
because it’s just not complex enough, right?
2295
02:10:12,100 --> 02:10:17,200
So, what I actually did is I went ahead
and designed another database schema.
2296
02:10:17,200 --> 02:10:21,100
So, I actually designed a database
that could be used for a company.
2297
02:10:21,100 --> 02:10:23,300
So, I’m going to go ahead and pull that up.
2298
02:10:23,300 --> 02:10:26,200
And right here we have our company database.
2299
02:10:26,200 --> 02:10:28,900
And you can find this .pdf
in the description below.
2300
02:10:28,900 --> 02:10:29,900
I’ll put a link to it.
2301
02:10:29,900 --> 02:10:35,300
But basically this is the database schema that we're
going to be using for the remainder of the course.
2302
02:10:35,300 --> 02:10:37,700
So, this is a more complex database schema.
2303
02:10:37,700 --> 02:10:44,500
But because it’s complex, it’s going to give us some
awesome opportunities to query it, right?
2304
02:10:44,500 --> 02:10:47,100
In other words, the more complex
the database schema,
2305
02:10:47,100 --> 02:10:50,800
the more complex the queries we're going to be able
to write and play around with.
2306
02:10:50,800 --> 02:10:55,700
Because there’s only so many types of queries
that we can write for that student table.
2307
02:10:55,700 --> 02:11:01,500
But this is going to be a better example for us to learn
about different types of queries and stuff like that.
2308
02:11:01,500 --> 02:11:04,900
So, I’m going to go ahead and walk you guys through
this schema in this tutorial.
2309
02:11:04,900 --> 02:11:07,000
That way you guys understand it.
2310
02:11:07,000 --> 02:11:09,000
Because if you don’t –
if you can’t wrap your head around it,
2311
02:11:09,000 --> 02:11:12,500
then you’re not going to be able to follow along
with the rest of the course.
2312
02:11:12,500 --> 02:11:15,000
So, I’m just going to give you guys
an explanation of this.
2313
02:11:15,000 --> 02:11:17,400
And that way we have it going forward.
2314
02:11:17,400 --> 02:11:22,500
So, this database schema maps out
the database for a company, right?
2315
02:11:22,500 --> 02:11:26,300
So, this would be like the information
that we might want to store about a company.
2316
02:11:26,300 --> 02:11:29,200
So, up here we have our first table
which is the Employee table.
2317
02:11:29,200 --> 02:11:32,200
And so, this is just going to store
information about employees.
2318
02:11:32,200 --> 02:11:36,500
So, we're storing like the employee ID,
the first name, the last name, birth date.
2319
02:11:36,500 --> 02:11:38,600
So, you'll see here, this is actually a date.
2320
02:11:38,600 --> 02:11:40,400
Sex which is going to be male or female.
2321
02:11:40,400 --> 02:11:43,100
Salary, which will be like how much they make.
2322
02:11:43,100 --> 02:11:45,600
And then over here we have two foreign keys.
2323
02:11:45,600 --> 02:11:51,100
So, the primary key of this table is the employee ID
over here which we have in red.
2324
02:11:51,100 --> 02:11:55,600
The foreign keys are going to be
these keys over here in green.
2325
02:11:55,600 --> 02:12:03,600
And basically, a foreign key is just – it’s going to store
the primary key of an entry in a different table, right?
2326
02:12:03,600 --> 02:12:05,000
So, we have two foreign keys.
2327
02:12:05,000 --> 02:12:06,400
The first here is super_id.
2328
02:12:06,400 --> 02:12:08,700
That stands for supervisor ID.
2329
02:12:08,700 --> 02:12:13,600
So, an employee in our company
is going to be able to have a supervisor.
2330
02:12:13,600 --> 02:12:16,500
And a supervisor is actually going
to be another employee.
2331
02:12:16,500 --> 02:12:21,800
So, super_id is going to point to
another employee in the employee table.
2332
02:12:21,800 --> 02:12:23,600
We also have branch_id.
2333
02:12:23,600 --> 02:12:28,500
So, different employees in the company are going to
be able to work for different branches.
2334
02:12:28,500 --> 02:12:31,600
And you'll see down here we have this branch table.
2335
02:12:31,600 --> 02:12:35,900
And it just has a branch ID, a name, and then also
some other stuff which we'll talk about.
2336
02:12:35,900 --> 02:12:39,200
So, an employee can work at a branch, right?
2337
02:12:39,200 --> 02:12:41,800
And that’s kind of what we're storing over here.
2338
02:12:41,800 --> 02:12:43,100
So, let’s take a look at this.
2339
02:12:43,100 --> 02:12:47,800
We would say that the employee Michael Scott,
his super_id is 100.
2340
02:12:47,800 --> 02:12:52,500
That means Michael Scott’s supervisor
has an ID of 100.
2341
02:12:52,500 --> 02:12:56,600
So, Michael Scott’s supervisor
is going to be David Wallace, right?
2342
02:12:56,600 --> 02:12:59,000
Because David Wallace has an employee ID of 100.
2343
02:12:59,000 --> 02:12:59,000
Kelly Kapoor has a supervisor ID of 102.
2344
02:12:59,000 --> 02:13:08,000
That means Kelly Kapoor’s supervisor
is going to be employee 102.
2345
02:13:08,000 --> 02:13:09,700
So, it’s going to be Michael Scott, right?
2346
02:13:09,700 --> 02:13:11,100
So, hopefully that makes sense, right?
2347
02:13:11,100 --> 02:13:13,000
An employee can have a supervisor.
2348
02:13:13,000 --> 02:13:18,900
And super_id is a foreign key which points
to the employee ID of another employee.
2349
02:13:18,900 --> 02:13:21,700
And then we also have branch_id over here again.
2350
02:13:21,700 --> 02:13:24,400
And this will point to the branch.
2351
02:13:24,400 --> 02:13:25,600
So, branch_id.
2352
02:13:25,600 --> 02:13:28,300
Angela Martin has a branch ID of 2.
2353
02:13:28,300 --> 02:13:32,600
That means Angela Martin works
at the Scranton branch.
2354
02:13:32,600 --> 02:13:36,300
Andy Bernard has a branch ID of 3.
2355
02:13:36,300 --> 02:13:38,900
That means Andy Bernard works
at the Stamford branch.
2356
02:13:38,900 --> 02:13:41,000
So, hopefully that kind of makes sense.
2357
02:13:41,000 --> 02:13:43,700
And then down here, like I said,
we have the Branch table.
2358
02:13:43,700 --> 02:13:46,300
And the Branch table just has an ID, a name.
2359
02:13:46,300 --> 02:13:47,900
And also has a manager ID.
2360
02:13:47,900 --> 02:13:54,500
So, on the Branch table we're actually storing
the ID of an employee who is the manager.
2361
02:13:54,500 --> 02:13:56,700
So, this is actually a foreign key.
2362
02:13:56,700 --> 02:14:01,100
So, manager ID is going to point to
one of the employees up here.
2363
02:14:01,100 --> 02:14:07,200
So, we would say that the manager
of the Scranton branch has an ID of 102.
2364
02:14:07,200 --> 02:14:11,900
So, the manager of the Scranton branch
is going to be Michael Scott because he has a 102.
2365
02:14:11,900 --> 02:14:14,600
The manager of the Stamford branch has an ID of 106.
2366
02:14:14,600 --> 02:14:19,900
So, the manager of the Stamford branch
is Josh Porter because he has an ID of 106.
2367
02:14:19,900 --> 02:14:20,900
Hopefully that makes sense.
2368
02:14:20,900 --> 02:14:24,200
And then we're storing the date
that the manager started being the manager.
2369
02:14:24,200 --> 02:14:26,200
So, now we'll check out the client table.
2370
02:14:26,200 --> 02:14:32,400
So, the client table has a client ID, client name,
and also has a foreign key branch ID.
2371
02:14:32,400 --> 02:14:39,100
So, we would say that the client Dunmore High
School, you know, works with branch number 2.
2372
02:14:39,100 --> 02:14:43,000
Or the client Times Newspaper works
with branch number 3.
2373
02:14:43,000 --> 02:14:47,300
So, Dunmore Highschool
would be a client of branch number 2
2374
02:14:47,300 --> 02:14:51,800
which is the Scranton branch over here, right?
2375
02:14:51,800 --> 02:14:53,900
Hopefully that makes sense.
2376
02:14:53,900 --> 02:14:59,200
The Times Newspaper is a client of branch number 3
which is the Stamford branch.
2377
02:14:59,200 --> 02:15:01,200
So, that’s kind of how those are connected.
2378
02:15:01,200 --> 02:15:05,100
And then over here we have another one
which is branch supplier.
2379
02:15:05,100 --> 02:15:10,000
So, this has a compound primary key,
or a composite key.
2380
02:15:10,000 --> 02:15:13,100
And the first part of it is the branch_id.
2381
02:15:13,100 --> 02:15:16,400
So, a Branch Supplier is obviously going to
store the branch_id.
2382
02:15:16,400 --> 02:15:19,400
And it’s also going to store a supplier name.
2383
02:15:19,400 --> 02:15:23,100
So, it’s important to notice
that we need a composite key here
2384
02:15:23,100 --> 02:15:27,800
because the branch_id doesn’t
uniquely identify each row.
2385
02:15:27,800 --> 02:15:30,300
And the supplier_name doesn’t
uniquely identify each row.
2386
02:15:30,300 --> 02:15:33,800
Only together can they uniquely identify each row.
2387
02:15:33,800 --> 02:15:37,000
And then the last table down here
is the Works_With table.
2388
02:15:37,000 --> 02:15:41,800
So, this basically defines the relationship
between employees and clients.
2389
02:15:41,800 --> 02:15:43,600
So, we're going to go ahead and assume
2390
02:15:43,600 --> 02:15:48,300
that an employee can work with a client
and sell the client different products.
2391
02:15:48,300 --> 02:15:54,600
So, employee 105 sold $55,000
worth of paper to client 400.
2392
02:15:54,600 --> 02:16:02,800
Employee number 108 sold $22,500
worth of product to client_id 402, etc.
2393
02:16:02,800 --> 02:16:07,100
So, this is kind of mapping the relationships
between employees and clients
2394
02:16:07,100 --> 02:16:11,000
so telling us how much
a certain employee sold to a certain client.
2395
02:16:11,000 --> 02:16:13,100
And you'll see this is also a composite key.
2396
02:16:13,100 --> 02:16:18,600
So, this is the database schema that we're going to be
working with for the rest of the course.
2397
02:16:18,600 --> 02:16:23,300
And like I said, in order to kind of show you guys
some more advanced SQL queries,
2398
02:16:23,300 --> 02:16:28,600
we're going to need a database schema that is,
you know, complex just like this one.
2399
02:16:28,600 --> 02:16:32,700
If this isn’t super clear to you, what you might
want to do is just kind of look over the
2400
02:16:32,700 --> 02:16:35,200
pdf that I'm going to include in the description below.
2401
02:16:35,200 --> 02:16:37,900
And really, what you want to do
is just trace the relationships.
2402
02:16:37,900 --> 02:16:40,100
So, you know, like really make sure
2403
02:16:40,100 --> 02:16:43,300
that you understand how this
database schema is put together,
2404
02:16:43,300 --> 02:16:45,400
how everything relates to each other.
2405
02:16:45,400 --> 02:16:50,100
And then going forward in the course,
we're going to be using this database schema.
2406
02:16:50,100 --> 02:16:52,900
So, in the next tutorial I’m going to
show you guys how to create this.
2407
02:16:52,900 --> 02:16:57,400
So, I’m going to actually give you all the code
for creating this entire database.
2408
02:16:57,400 --> 02:17:02,700
And then from there we'll go ahead and we'll start
learning some more advanced queries
2409
02:17:02,700 --> 02:17:04,800
and some more advanced stuff that we can do.
2410
02:17:04,800 --> 02:17:08,000
[Creating Company Database]
2411
02:17:08,000 --> 02:17:14,400
In this tutorial I’m going to show you guys how we can
create a complex database schema in SQL.
2412
02:17:14,400 --> 02:17:18,100
So, in the last tutorial I showed you guys
this database over here.
2413
02:17:18,100 --> 02:17:19,500
It’s this company database.
2414
02:17:19,500 --> 02:17:22,600
This is basically just an example database
that I created.
2415
02:17:22,600 --> 02:17:25,900
And it’s a database that we're going to be using
for the remainder of the course
2416
02:17:25,900 --> 02:17:30,100
in order to learn a little bit more advanced SQL
querying and stuff like that.
2417
02:17:30,100 --> 02:17:31,600
So, this is a company database.
2418
02:17:31,600 --> 02:17:35,900
And we have a bunch of tables like this employee
table, branch table, works with table, etc.
2419
02:17:35,900 --> 02:17:39,200
In the last video I kind of talked about
what each of those did.
2420
02:17:39,200 --> 02:17:42,000
We looked at some of the different relationships
between the tables.
2421
02:17:42,000 --> 02:17:45,900
So, in this video I’m going to show you guys
how we can actually implement this database.
2422
02:17:45,900 --> 02:17:50,700
So, how can we take this database
and actually create it in MySQL?
2423
02:17:50,700 --> 02:17:54,100
So, not only are we going
to create all of these tables
2424
02:17:54,100 --> 02:17:58,800
and we're going to define all of these relationships,
like the foreign keys and all that stuff.
2425
02:17:58,800 --> 02:18:01,700
We're also going to populate all of that information.
2426
02:18:01,700 --> 02:18:05,400
So, I’m going to populate these database tables
with all this information.
2427
02:18:05,400 --> 02:18:09,100
That way we can use that information going forward
for the examples.
2428
02:18:09,100 --> 02:18:11,900
So, this tutorial is going to be pretty cool
2429
02:18:11,900 --> 02:18:15,600
because I’m going to show you guys how to build
an advanced database schema just like this.
2430
02:18:15,600 --> 02:18:19,700
It’s a little bit more complex than the student table
we had looked at before.
2431
02:18:19,700 --> 02:18:24,800
And before we get started, I just want to say all of
the code that I'm going to be using in this tutorial
2432
02:18:24,800 --> 02:18:27,300
is going to be available in the description below.
2433
02:18:27,300 --> 02:18:30,400
So, there’ll be a link that you can click on
and I’ll have all of this.
2434
02:18:30,400 --> 02:18:33,800
So, you know, you don’t have to type out
everything that I'm typing out.
2435
02:18:33,800 --> 02:18:38,300
And in fact, I’m just going to be pasting in a bunch of
stuff so that way you don’t have to type anything.
2436
02:18:38,300 --> 02:18:42,600
So, the first thing we want to do is
we're going to drop that student table.
2437
02:18:42,600 --> 02:18:47,400
So, I’m just going to go ahead
and DROP TABLE student.
2438
02:18:47,400 --> 02:18:49,600
And we'll go ahead and drop that table.
2439
02:18:49,600 --> 02:18:51,600
That way we don’t have to worry about it.
2440
02:18:51,600 --> 02:18:53,900
And so, once we've gone ahead
and dropped the student table,
2441
02:18:53,900 --> 02:18:57,400
now we can start creating all of these tables
for our company database.
2442
02:18:57,400 --> 02:19:01,500
So, I’m just basically going to paste in the code
for creating each of these tables.
2443
02:19:01,500 --> 02:19:02,900
I already have it all written out.
2444
02:19:02,900 --> 02:19:06,000
And I’ll kind of walk you guys through what it is
and what we're doing.
2445
02:19:06,000 --> 02:19:08,200
So, here we have CREATE TABLE employee.
2446
02:19:08,200 --> 02:19:09,500
So, we're going to create this Employee table.
2447
02:19:09,500 --> 02:19:11,900
We have our employee ID which is an integer.
2448
02:19:11,900 --> 02:19:15,000
And this is going to be the primary key of the table.
2449
02:19:15,000 --> 02:19:17,700
And then we also have
first_name, last_name, birth_day.
2450
02:19:17,700 --> 02:19:20,700
So, birthday is actually a DATE
as you can see over here.
2451
02:19:20,700 --> 02:19:23,200
We haven’t used the DATE datatype yet,
but we're using it now.
2452
02:19:23,200 --> 02:19:28,900
DATE will allow us to store a date just like this,
with a 4-digit year, 2-digit month, and a 2-digit day.
2453
02:19:28,900 --> 02:19:30,500
We’re also storing their sex.
2454
02:19:30,500 --> 02:19:31,600
So, like male or female.
2455
02:19:31,600 --> 02:19:33,000
And that’s just a VARCHAR(1).
2456
02:19:33,000 --> 02:19:34,800
We're storing the salary.
2457
02:19:34,800 --> 02:19:38,300
And then we're also storing these supervisor ID
and the branch ID.
2458
02:19:38,300 --> 02:19:41,800
And if you guys remember
from the last video, the supervisor ID
2459
02:19:41,800 --> 02:19:45,500
is a foreign key which points to another employee.
2460
02:19:45,500 --> 02:19:49,600
And the branch ID is also a foreign key
which points to the branch table.
2461
02:19:49,600 --> 02:19:50,800
Now, here’s the thing.
2462
02:19:50,800 --> 02:19:53,300
We can’t actually make these foreign keys just yet
2463
02:19:53,300 --> 02:19:57,200
because the employee table
doesn’t technically exist yet.
2464
02:19:57,200 --> 02:20:01,000
And the branch table doesn’t technically exist yet
because I haven’t created them.
2465
02:20:01,000 --> 02:20:05,100
And so, I can’t define these guys
as foreign keys just yet.
2466
02:20:05,100 --> 02:20:08,200
We're going to do that later
and we'll add that in afterwards.
2467
02:20:08,200 --> 02:20:11,900
So, I’m going to go ahead and run this
and we'll create the employee table just like that.
2468
02:20:11,900 --> 02:20:14,700
So, now we're going to go ahead
and create the branch table.
2469
02:20:14,700 --> 02:20:17,400
So, I have the code for the branch table right here.
2470
02:20:17,400 --> 02:20:19,300
So, we're going to CREATE TABLE branch.
2471
02:20:19,300 --> 02:20:20,900
And this is just this guy down here.
2472
02:20:20,900 --> 02:20:24,000
So, it has a branch_id which is the PRIMARY KEY.
2473
02:20:24,000 --> 02:20:25,000
Branch name.
2474
02:20:25,000 --> 02:20:27,000
And the manager ID.
2475
02:20:27,000 --> 02:20:30,300
So, remember, the manager ID is down here in green.
2476
02:20:30,300 --> 02:20:33,000
The manager ID is also a foreign key.
2477
02:20:33,000 --> 02:20:39,700
So, the manager ID, we're actually going to be defining
as a foreign key which points to the employee table.
2478
02:20:39,700 --> 02:20:42,400
And then we have the manager start date,
which is DATE.
2479
02:20:42,400 --> 02:20:44,600
So, down here, I’m defining a foreign key.
2480
02:20:44,600 --> 02:20:48,100
So, in order to create a foreign key
I can just say FOREIGN KEY
2481
02:20:48,100 --> 02:20:52,200
and then inside of parenthesis put the name
of the column that I want to be the foreign key.
2482
02:20:52,200 --> 02:20:54,300
In our case, it’s mgr_id.
2483
02:20:54,300 --> 02:20:57,500
And then I can say that it references employee.
2484
02:20:57,500 --> 02:21:01,800
And then inside parenthesis just the name
of the column in the employee table,
2485
02:21:01,800 --> 02:21:03,700
which is going to be emp_id.
2486
02:21:03,700 --> 02:21:07,400
And then finally, I'm going to do one more thing
which is over here.
2487
02:21:07,400 --> 02:21:10,300
I’m going to say ON DELETE SET NULL.
2488
02:21:10,300 --> 02:21:15,300
And we’re going to talk more about what
ON DELETE SET NULL does in a future video.
2489
02:21:15,300 --> 02:21:18,300
But for now, just know that whenever
we're creating a foreign key,
2490
02:21:18,300 --> 02:21:21,200
we're going to put
ON DELETE SET NULL.
2491
02:21:21,200 --> 02:21:23,200
Or we can also put something called
ON DELETE CASCADE.
2492
02:21:23,200 --> 02:21:25,000
Which again, I’m going to
talk about in a future video.
2493
02:21:25,000 --> 02:21:30,400
But just put that in there and that’ll make it
a lot easier for us to manage this foreign key.
2494
02:21:30,400 --> 02:21:34,300
So, now I'm going to go ahead and run this
and we'll create the Branch table.
2495
02:21:34,300 --> 02:21:36,100
So, looks like that went well.
2496
02:21:36,100 --> 02:21:38,000
All right, so next thing we need to do
2497
02:21:38,000 --> 02:21:42,700
is we need to set the super_id and branch_id
of the Employee table as foreign keys.
2498
02:21:42,700 --> 02:21:47,600
So, remember, down here in the Branch table
we set the manager ID as a foreign key.
2499
02:21:47,600 --> 02:21:52,900
But we weren’t able to do that with the supervisor ID
or the branch ID in the employee table
2500
02:21:52,900 --> 02:21:57,000
because the Branch table and the Employee table
haven’t been created yet.
2501
02:21:57,000 --> 02:21:59,000
So, I’m going to show you guys how we can do that.
2502
02:21:59,000 --> 02:22:02,500
So, down here I have two little blocks of SQL code.
2503
02:22:02,500 --> 02:22:05,800
The first one is altering the table employee.
2504
02:22:05,800 --> 02:22:08,200
And I’m just saying ADD FOREIGN KEY.
2505
02:22:08,200 --> 02:22:11,500
And then inside of parenthesis
we're putting branch_id.
2506
02:22:11,500 --> 02:22:12,900
So, that’s going to be the foreign key.
2507
02:22:12,900 --> 02:22:16,400
REFERENCES branch, and then branch_id.
2508
02:22:16,400 --> 02:22:19,100
And ON DELETE we're going to SET NULL.
2509
02:22:19,100 --> 02:22:25,800
So, what this is going to do is it’s going to add
branch_id as foreign key to the employee table.
2510
02:22:25,800 --> 02:22:27,100
So, I’m going to run this.
2511
02:22:27,100 --> 02:22:29,100
And now this is going to be a foreign key.
2512
02:22:29,100 --> 02:22:31,800
And then down here we can do the same thing,
but for supervisor_id.
2513
02:22:31,800 --> 02:22:33,500
So, you see supervisor_id right there.
2514
02:22:33,500 --> 02:22:35,400
I’m going to go ahead and run this.
2515
02:22:35,400 --> 02:22:40,100
And this will add the supervisor ID as a foreign key
just like we did with the branch ID.
2516
02:22:40,100 --> 02:22:43,700
So, we needed to do that because
when we created the Employee table,
2517
02:22:43,700 --> 02:22:47,400
the Branch table and the Employee table
hadn’t been created yet
2518
02:22:47,400 --> 02:22:50,100
so we couldn't add them
as foreign key relationships.
2519
02:22:50,100 --> 02:22:52,500
All right, so now we're going to add the Client table.
2520
02:22:52,500 --> 02:22:55,200
So, you'll see over here, CREATE TABLE client.
2521
02:22:55,200 --> 02:23:01,500
And we're just storing the client_id as a primary key,
client_name, branch_id.
2522
02:23:01,500 --> 02:23:04,600
And then we're going to make
the branch_id a foreign key.
2523
02:23:04,600 --> 02:23:08,300
So, over here on the Client table
you'll see that the branch_id is a foreign key.
2524
02:23:08,300 --> 02:23:10,100
It points over to branch.
2525
02:23:10,100 --> 02:23:12,400
So, we're going to define that relationship here.
2526
02:23:12,400 --> 02:23:16,100
I’m just saying FOREIGN KEY (branch_id)
REFERENCES branch (branch_id).
2527
02:23:16,100 --> 02:23:19,300
And then once again we're just going to say
ON DELETE SET NULL.
2528
02:23:19,300 --> 02:23:22,000
So, let’s go ahead and create the Client table.
2529
02:23:22,000 --> 02:23:23,200
I’m just going to run this.
2530
02:23:23,200 --> 02:23:24,700
And that’ll create the Client table.
2531
02:23:24,700 --> 02:23:26,500
So, next we have the Works With table.
2532
02:23:26,500 --> 02:23:31,200
So, the Works With table is actually pretty unique
because it has a composite primary key.
2533
02:23:31,200 --> 02:23:35,700
So, the primary key has the employee ID
and the client ID.
2534
02:23:35,700 --> 02:23:40,600
And actually, what’s unique is that each
component of the primary key is a foreign key.
2535
02:23:40,600 --> 02:23:42,500
So, employee ID is a foreign key.
2536
02:23:42,500 --> 02:23:44,700
And the client ID is a foreign key.
2537
02:23:44,700 --> 02:23:47,200
And so, over here, we can create this table.
2538
02:23:47,200 --> 02:23:49,200
I have employee ID, client_id, total_sales.
2539
02:23:49,200 --> 02:23:52,300
The primary key is employee ID and client ID.
2540
02:23:52,300 --> 02:23:56,200
And then the foreign keys are
employee ID and client ID.
2541
02:23:56,200 --> 02:24:02,100
And you'll notice over here, instead of saying ON
DELETE SET NULL, I’m saying ON DELETE CASCADE.
2542
02:24:02,100 --> 02:24:07,400
And again, I’m going to talk more about ON DELETE
SET NULL and ON DELETE CASCADE in a future video.
2543
02:24:07,400 --> 02:24:13,400
But for now, just know that you need to have this here
in order for everything to kind of be set up correctly.
2544
02:24:13,400 --> 02:24:15,000
So, I’m going to go ahead and run this
2545
02:24:15,000 --> 02:24:18,900
and we'll be able to insert or create
the Works With table.
2546
02:24:18,900 --> 02:24:23,200
And then finally, we're going to create our last table
which is the Branch Supplier table.
2547
02:24:23,200 --> 02:24:26,700
And this is actually kind of similar to
the Works With table.
2548
02:24:26,700 --> 02:24:28,900
So, down here we have the Branch Supplier table.
2549
02:24:28,900 --> 02:24:30,600
It also has a composite key.
2550
02:24:30,600 --> 02:24:33,000
So, its key is made up of multiple columns.
2551
02:24:33,000 --> 02:24:38,600
And the branch_id column is a foreign key,
but the supplier_name column isn’t a foreign key.
2552
02:24:38,600 --> 02:24:41,600
So, this one is actually pretty interesting as well.
2553
02:24:41,600 --> 02:24:44,700
So, we have branch_id, supplier_name, supply_type.
2554
02:24:44,700 --> 02:24:47,100
And then the PRIMARY KEY
is branch_id and supplier_name.
2555
02:24:47,100 --> 02:24:49,800
And the foreign key is just branch_id.
2556
02:24:49,800 --> 02:24:55,100
And again, with this one, on the foreign key,
that’s also part of the primary key.
2557
02:24:55,100 --> 02:24:57,800
I’m just saying ON DELETE CASCADE.
2558
02:24:57,800 --> 02:25:00,200
So, that’s going to be what
we're going to need there.
2559
02:25:00,200 --> 02:25:01,900
So, I’m to go ahead and run this.
2560
02:25:01,900 --> 02:25:06,100
And you'll see over here
that everything got entered in correctly.
2561
02:25:06,100 --> 02:25:08,600
So, now we have all of these tables created, right?
2562
02:25:08,600 --> 02:25:11,700
We created all the tables for our database schema.
2563
02:25:11,700 --> 02:25:15,600
And so, what we're going to do now is we're going to
actually insert information into those tables.
2564
02:25:15,600 --> 02:25:18,200
Now, when we're inserting information
into these tables,
2565
02:25:18,200 --> 02:25:20,700
because we have all of these
like foreign key relationships,
2566
02:25:20,700 --> 02:25:23,500
we're actually going to have to do it a specific way.
2567
02:25:23,500 --> 02:25:26,400
And so, I’m going to walk you guys through
how we might do something like this.
2568
02:25:26,400 --> 02:25:28,700
And it’ll give you an idea of how you can do it.
2569
02:25:28,700 --> 02:25:30,900
So, I’m actually going to make the text
a little bit smaller.
2570
02:25:30,900 --> 02:25:32,500
So, over here I’m going to show you guys
2571
02:25:32,500 --> 02:25:36,600
how we could insert all of information
for the corporate branch.
2572
02:25:36,600 --> 02:25:40,900
So, inserting the employee
and the branch entries for the corporate branch.
2573
02:25:40,900 --> 02:25:44,600
Now, you'll notice over here that
the Employee table and the Branch table
2574
02:25:44,600 --> 02:25:47,700
have foreign keys that point to each other.
2575
02:25:47,700 --> 02:25:51,000
So, the employee table has an entry over here,
2576
02:25:51,000 --> 02:25:54,900
branch_id, which points to the branch
or points to a specific branch.
2577
02:25:54,900 --> 02:26:00,800
And each branch has a column here, manager ID,
which points to a specific employee.
2578
02:26:00,800 --> 02:26:02,400
So, there’s like this circular relationship.
2579
02:26:02,400 --> 02:26:06,700
So, when we're inserting these elements,
we're going to have to do it in a specific order.
2580
02:26:06,700 --> 02:26:10,400
So, over here I’m just starting with,
like I said, the corporate branch.
2581
02:26:10,400 --> 02:26:15,700
So, I’m inserting into the employee table
VALUES 100, David Wallace.
2582
02:26:15,700 --> 02:26:19,200
So, I’m inserting in this David Wallace row over here.
2583
02:26:19,200 --> 02:26:21,400
And you'll notice that I put all this stuff in here.
2584
02:26:21,400 --> 02:26:23,200
But when I get to branch_id,
2585
02:26:23,200 --> 02:26:27,300
which is this last element over here,
it should be 1, right?
2586
02:26:27,300 --> 02:26:30,200
So, David Wallace should belong
to the corporate branch.
2587
02:26:30,200 --> 02:26:34,900
But the problem is that the corporate branch
hasn’t been created yet.
2588
02:26:34,900 --> 02:26:39,200
So, I’m just going to set this equal to NULL
because that branch hasn’t been created yet.
2589
02:26:39,200 --> 02:26:42,300
And I’m going to go ahead
and insert David Wallace in there.
2590
02:26:42,300 --> 02:26:43,600
So, I’m just going to run this.
2591
02:26:43,600 --> 02:26:48,900
And then the what I'm going to do is I'm going to
insert the branch into the Branch table.
2592
02:26:48,900 --> 02:26:53,100
So, I’m inserting into the Branch table VALUES 1,
Corporate, 100.
2593
02:26:53,100 --> 02:26:56,800
So, now that I – since I already inserted
the David Wallace employee,
2594
02:26:56,800 --> 02:27:02,900
I can set David Wallace’s employee ID
as the manager ID on the branch row.
2595
02:27:02,900 --> 02:27:06,000
And so now I’m inserting in the corporate branch.
2596
02:27:06,000 --> 02:27:07,400
So, I’m going to go ahead and do that.
2597
02:27:07,400 --> 02:27:11,100
And then what I need to do now
is I need to update that David Wallace entry
2598
02:27:11,100 --> 02:27:15,000
to say that he works for
the corporate branch, right?
2599
02:27:15,000 --> 02:27:16,800
So, down here I’m saying UPDATE employee.
2600
02:27:16,800 --> 02:27:18,900
SET branch_id equal to 1.
2601
02:27:18,900 --> 02:27:21,100
WHERE employee ID is equal to 100.
2602
02:27:21,100 --> 02:27:23,600
So, that will go ahead and update David Wallace.
2603
02:27:23,600 --> 02:27:27,900
So, the last thing we're going to do now is just insert
the last employee into the corporate branch.
2604
02:27:27,900 --> 02:27:31,800
So you'll see Jan Levinson is actually
getting inserted into there.
2605
02:27:31,800 --> 02:27:33,600
So, I'm going to go ahead and run this.
2606
02:27:33,600 --> 02:27:36,900
And so, now we have all of our employees
inserted into the corporate branch.
2607
02:27:36,900 --> 02:27:39,400
I’m going to do the same thing
for the other branches.
2608
02:27:39,400 --> 02:27:41,200
So, I’ll do the same thing for the Scranton branch.
2609
02:27:41,200 --> 02:27:43,000
And here I have the code to do that.
2610
02:27:43,000 --> 02:27:46,700
So, again, I’m inserting in the manager
of the Scranton branch.
2611
02:27:46,700 --> 02:27:49,100
So, I’m doing that right now,
which is Michael Scott.
2612
02:27:49,100 --> 02:27:51,900
And then I’m inserting the
actual Scranton branch.
2613
02:27:51,900 --> 02:27:56,600
And then I’m updating Michael Scott
to say that he works at the Scranton branch.
2614
02:27:56,600 --> 02:28:01,500
And then finally, I’m adding in Angela, Kelly.
2615
02:28:01,500 --> 02:28:03,800
And I’m also adding in Stanley.
2616
02:28:03,800 --> 02:28:06,500
So, now I have all of the employees
in the Scranton branch.
2617
02:28:06,500 --> 02:28:11,400
And again, we have to do it that way
because we have this like circular relationship
2618
02:28:11,400 --> 02:28:15,500
with the foreign keys between
the Employee and the Branch table.
2619
02:28:15,500 --> 02:28:19,400
So, then finally we'll do the same thing
for the Stamford branch.
2620
02:28:19,400 --> 02:28:22,600
I’m inserting the manager of the Stamford branch.
2621
02:28:22,600 --> 02:28:25,100
And then I’m inserting the actually Stamford branch.
2622
02:28:25,100 --> 02:28:29,200
And them I’m updating the manager of the Stamford
branch to say that he works at the Stamford branch.
2623
02:28:29,200 --> 02:28:31,800
And then I’m adding in these other employees.
2624
02:28:31,800 --> 02:28:34,100
So, Andy Bernard and Jim Halpert.
2625
02:28:34,100 --> 02:28:38,400
All right, so now that we've done all stuff
with the employee table and the branch table,
2626
02:28:38,400 --> 02:28:40,400
we can kind of move onto doing the other ones.
2627
02:28:40,400 --> 02:28:43,300
And hopefully, that shows you how
you might insert information
2628
02:28:43,300 --> 02:28:48,800
or how you might have to insert information
into a more complex database schema, right?
2629
02:28:48,800 --> 02:28:51,900
When we're just inserting into the student table,
it’s really easy.
2630
02:28:51,900 --> 02:28:54,600
But when we have foreign keys linking
all over the place,
2631
02:28:54,600 --> 02:28:56,200
it can get a little bit complicated.
2632
02:28:56,200 --> 02:29:01,900
So, now though that is the most complex inserting
we're going to have to do.
2633
02:29:01,900 --> 02:29:03,600
So, now we can just insert normally.
2634
02:29:03,600 --> 02:29:06,000
So, we can INSERT INTO branch_supplier.
2635
02:29:06,000 --> 02:29:09,900
And I’m just going to go ahead and click through
all of these and insert them in turn just like this.
2636
02:29:09,900 --> 02:29:13,400
All right, so I’ve gone ahead
and ran each one of these INSERT statements.
2637
02:29:13,400 --> 02:29:15,600
So, we inserted everything
into the Branch Supplier table.
2638
02:29:15,600 --> 02:29:18,700
Now, I’m going to insert everything
into the Client table.
2639
02:29:18,700 --> 02:29:22,000
And again, this information is just the information
that you see over here
2640
02:29:22,000 --> 02:29:26,300
that’s just written out into, you know,
database or SQL commands.
2641
02:29:26,300 --> 02:29:29,200
So, I’m going to go ahead
and insert each one of these.
2642
02:29:29,200 --> 02:29:32,400
And then finally, we'll insert
into the Works With table.
2643
02:29:32,400 --> 02:29:35,700
So, again, this is just a bunch of numbers
and stuff like that.
2644
02:29:35,700 --> 02:29:42,900
All right, so now we've gone ahead and populated
all of these database tables with all the information.
2645
02:29:42,900 --> 02:29:44,300
So, why don’t we check it out?
2646
02:29:44,300 --> 02:29:48,000
I’m going to say like SELECT ALL from employee.
2647
02:29:48,000 --> 02:29:50,400
So lets see all the different employees that we have,
make sure everything worked.
2648
02:29:50,400 --> 02:29:53,500
So, down here you'll see that we have
all of these different employees.
2649
02:29:53,500 --> 02:29:56,200
We can do the same thing for like Works With.
2650
02:29:56,200 --> 02:29:58,200
So, let’s see if we got all that data.
2651
02:29:58,200 --> 02:29:59,900
And you can see that we do.
2652
02:29:59,900 --> 02:30:05,600
So, now our database is actually populated
with all of the information that I have over here.
2653
02:30:05,600 --> 02:30:09,100
So, again, you can get all that code
from the description below.
2654
02:30:09,100 --> 02:30:14,400
But hopefully this kind of shows you guys how, you
know, we can go about designing a database schema
2655
02:30:14,400 --> 02:30:19,700
or, you know, go about creating
a database schema like this inside of MySQL.
2656
02:30:19,700 --> 02:30:23,400
You can see it’s not as straight forward
as it was with the student table.
2657
02:30:23,400 --> 02:30:26,500
But we can do different things to make it happen.
2658
02:30:26,500 --> 02:30:30,200
[More Basic Queries]
2659
02:30:30,200 --> 02:30:33,700
In this tutorial I’m going to show you guys
some awesome SELECT statements
2660
02:30:33,700 --> 02:30:36,900
which will allow us to query the company database
2661
02:30:36,900 --> 02:30:39,800
that we've been setting up
for the last couple tutorials.
2662
02:30:39,800 --> 02:30:44,500
So, basically, I’m going to give us some little prompts
and we'll see if we can figure out how to get the data.
2663
02:30:44,500 --> 02:30:48,800
So, first thing we're going to do
is try to find all employees.
2664
02:30:48,800 --> 02:30:54,500
So, our first task is to get
all the employees from the employee table.
2665
02:30:54,500 --> 02:30:59,000
And you'll see over here, this is essentially
all of the data that we have stored in our database.
2666
02:30:59,000 --> 02:31:01,300
So, let’s see if we can find all employees.
2667
02:31:01,300 --> 02:31:05,800
I’m going to say SELECT and I’m just going to say *.
2668
02:31:05,800 --> 02:31:10,900
So, SELECT * would mean SELECT ALL COLUMNS
from the Employee table.
2669
02:31:10,900 --> 02:31:13,400
And this should actually do it.
2670
02:31:13,400 --> 02:31:16,200
So, just by saying this, I’m going to run this
and we'll see what we get.
2671
02:31:16,200 --> 02:31:19,900
So, down here in the results
we have returned all the employees.
2672
02:31:19,900 --> 02:31:20,500
Cool.
2673
02:31:20,500 --> 02:31:22,800
So, we were able to get all of the employees.
2674
02:31:22,800 --> 02:31:23,800
Let’s try another one.
2675
02:31:23,800 --> 02:31:26,300
How about we'll try to find all clients.
2676
02:31:26,300 --> 02:31:30,200
So, now instead of finding all employees,
we'll see if we can find all the clients.
2677
02:31:30,200 --> 02:31:32,200
So, basically it’s the same thing.
2678
02:31:32,200 --> 02:31:34,900
But all I have to do is change employee to client.
2679
02:31:34,900 --> 02:31:37,500
So, now we're grabbing information
from the Client table.
2680
02:31:37,500 --> 02:31:41,000
So, I’m going to run this and you'll see down here
we’re getting all of the clients.
2681
02:31:41,000 --> 02:31:41,600
Cool.
2682
02:31:41,600 --> 02:31:45,700
So, that’s, you know, how we can
just get all the stuff from a single table.
2683
02:31:45,700 --> 02:31:46,700
Let’s try another one.
2684
02:31:46,700 --> 02:31:49,200
Find all employees ordered by salary.
2685
02:31:49,200 --> 02:31:51,700
So, this one is a little bit different.
2686
02:31:51,700 --> 02:31:56,000
Not only do we want to find all the employees,
but we want to order them by how much they make.
2687
02:31:56,000 --> 02:31:59,800
So, we'll say SELECT ALL from employee.
2688
02:31:59,800 --> 02:32:01,300
And now we want to order.
2689
02:32:01,300 --> 02:32:03,700
So, we can just say ORDER BY.
2690
02:32:03,700 --> 02:32:06,800
And we want to specify what column
we want to order these by.
2691
02:32:06,800 --> 02:32:08,200
So, we could say salary.
2692
02:32:08,200 --> 02:32:09,500
So, now I’m going to run this.
2693
02:32:09,500 --> 02:32:12,500
And you'll see down here,
now we get all the employees
2694
02:32:12,500 --> 02:32:14,800
but they’re ordered by how much they make.
2695
02:32:14,800 --> 02:32:17,000
So, down here, this employee makes 250k a year.
2696
02:32:17,000 --> 02:32:19,700
And it goes all the way down to 55k a year.
2697
02:32:19,700 --> 02:32:21,100
Let’s see if we can do this though.
2698
02:32:21,100 --> 02:32:25,800
So, the richest employee,
or the employee that makes the most starts.
2699
02:32:25,800 --> 02:32:28,300
So, we can say DESC for descending.
2700
02:32:28,300 --> 02:32:30,700
And now this will list them all out in descending order.
2701
02:32:30,700 --> 02:32:33,600
So, the 250,000 guy starts up at the top.
2702
02:32:33,600 --> 02:32:35,500
And then we go down to 55,000.
2703
02:32:35,500 --> 02:32:36,100
All right.
2704
02:32:36,100 --> 02:32:37,100
So, that’s pretty cool.
2705
02:32:37,100 --> 02:32:39,600
And that shows how we could execute that query.
2706
02:32:39,600 --> 02:32:40,600
So, let’s try another.
2707
02:32:40,600 --> 02:32:44,500
It says Find all employees ordered by sex then name.
2708
02:32:44,500 --> 02:32:48,100
So, what we want to do is order
all the employees by what sex they are.
2709
02:32:48,100 --> 02:32:52,200
And then within those orderings, we want to order
them by their name, alphabetically.
2710
02:32:52,200 --> 02:32:53,600
So, I can say SELECT ALL FROM employee.
2711
02:32:53,600 --> 02:32:54,600
It’s the same.
2712
02:32:54,600 --> 02:32:57,100
ORDER BY – this time, we're going to do sex.
2713
02:32:57,100 --> 02:33:00,200
So, why don’t we do first name.
2714
02:33:00,200 --> 02:33:03,200
And then we'll do last name.
2715
02:33:03,200 --> 02:33:07,800
So, now when we run this, you'll see here
all of these ordered first by sex.
2716
02:33:07,800 --> 02:33:10,400
So, we get female and then here’s all the males.
2717
02:33:10,400 --> 02:33:13,100
And then within that,
they’re actually ordered alphabetically.
2718
02:33:13,100 --> 02:33:14,900
So, we'll start with the first name.
2719
02:33:14,900 --> 02:33:17,400
So, Angela is first, then Jan, Kelly.
2720
02:33:17,400 --> 02:33:19,400
And then these are all the girls.
2721
02:33:19,400 --> 02:33:21,300
And then down here we have the guys.
2722
02:33:21,300 --> 02:33:23,100
So, Andy, David, Jim, Josh, etc.
2723
02:33:23,100 --> 02:33:26,400
And then if there were any duplicates
with the first names
2724
02:33:26,400 --> 02:33:29,000
or there were, you know,
two that were the same,
2725
02:33:29,000 --> 02:33:31,900
then we would defer to the last name
as we specified up there.
2726
02:33:31,900 --> 02:33:34,200
All right, let’s try another query prompt.
2727
02:33:34,200 --> 02:33:36,800
So, we'll ask for a different piece of information.
2728
02:33:36,800 --> 02:33:41,300
So, it says Find the first 5 employees in the table.
2729
02:33:41,300 --> 02:33:43,600
So, this is a situation we could say FROM employee.
2730
02:33:43,600 --> 02:33:45,200
SELECT ALL FROM employee.
2731
02:33:45,200 --> 02:33:48,000
And here we can limit it to 5 employees.
2732
02:33:48,000 --> 02:33:49,900
So, SELECT ALL FROM employee.
LIMIT 5.
2733
02:33:49,900 --> 02:33:52,100
That will give us the first five employees.
2734
02:33:52,100 --> 02:33:55,400
So, you'll see down here,
we just get the first five like that.
2735
02:33:55,400 --> 02:33:59,900
All right, let’s do a few more
and we'll see if we can mix it up a little bit.
2736
02:33:59,900 --> 02:34:04,400
So, let’s try to find the first
and last names of all employees.
2737
02:34:04,400 --> 02:34:07,900
So, this time we're actually just trying
to get the first and the last names.
2738
02:34:07,900 --> 02:34:15,400
So, up here we could say – instead of SELECT *
we can just SELECT first_name and last_name.
2739
02:34:15,400 --> 02:34:16,200
Just like that.
2740
02:34:16,200 --> 02:34:17,900
So, over here I’m going to click Run.
2741
02:34:17,900 --> 02:34:21,600
And you'll see now instead of getting all that stuff,
we're just getting first and last name.
2742
02:34:21,600 --> 02:34:23,800
So, I’ll actually show you guys
another thing we can do.
2743
02:34:23,800 --> 02:34:24,800
So, here’s the prompt.
2744
02:34:24,800 --> 02:34:28,700
It says find the forename
and the surnames of all employees.
2745
02:34:28,700 --> 02:34:33,200
So, this is actually similar, but you'll see down here
when we return to the first and last names,
2746
02:34:33,200 --> 02:34:36,300
the columns were named first_name and last_name.
2747
02:34:36,300 --> 02:34:40,400
But there’s another keyword
that we can use in SQL which is called AS.
2748
02:34:40,400 --> 02:34:41,800
So, I can say SELECT first_name.
2749
02:34:41,800 --> 02:34:44,900
And I can say AS forename.
2750
02:34:44,900 --> 02:34:50,000
And we can say last_name AS surname.
2751
02:34:50,000 --> 02:34:53,300
And what this is going to is it’s going to
return the first names and last names,
2752
02:34:53,300 --> 02:34:56,200
but instead of naming
the columns first_name and last_name,
2753
02:34:56,200 --> 02:34:58,600
it’s going to name them forename and surname.
2754
02:34:58,600 --> 02:35:00,000
So we're going to go ahead and run this.
2755
02:35:00,000 --> 02:35:04,300
And you'll see we get exactly the same thing except
now it’s calling the columns forename and surname.
2756
02:35:04,300 --> 02:35:06,200
So, that’s kind of a useful thing you can do.
2757
02:35:06,200 --> 02:35:10,900
And there’ll be certain circumstances
where you want to name the columns differently
2758
02:35:10,900 --> 02:35:12,300
depending on what you need.
2759
02:35:12,300 --> 02:35:14,600
All right, so there’s one more I want to show you guys,
2760
02:35:14,600 --> 02:35:16,800
and I’m actually going to introduce you
to a new SQL keyword.
2761
02:35:16,800 --> 02:35:20,500
So, the prompt is find out all the different genders.
2762
02:35:20,500 --> 02:35:24,600
So, what we want to do is figure out
what are all the different genders
2763
02:35:24,600 --> 02:35:27,900
that employees are stored
as inside of the table.
2764
02:35:27,900 --> 02:35:31,100
So, what we can do is we can use
this special keyword called DISTINCT.
2765
02:35:31,100 --> 02:35:33,400
So, I can say SELECT DISTINCT.
2766
02:35:33,400 --> 02:35:36,700
And then the name of the column
that I want to select DISTINCT.
2767
02:35:36,700 --> 02:35:40,900
And what this is going to do is when I run this,
you'll see down here we're getting M, F.
2768
02:35:40,900 --> 02:35:43,000
So, we're getting male and female.
2769
02:35:43,000 --> 02:35:48,100
So, those are all of the different sexes
that are stored inside of the table.
2770
02:35:48,100 --> 02:35:50,600
I could do the same thing for something else.
2771
02:35:50,600 --> 02:35:53,300
So, we could do like SELECT DISTINCT branch_id.
2772
02:35:53,300 --> 02:35:56,800
And this will tell me all the different branch ID’s
that are stored on the employee.
2773
02:35:56,800 --> 02:36:00,300
So, you'll see down here
we have different branch ID’s 1, 2, and 3.
2774
02:36:00,300 --> 02:36:03,600
So, those are all of the distinct branch ID’s
that employees have.
2775
02:36:03,600 --> 02:36:08,000
And so, that’s this DISTINCT keyword is pretty useful
if you want to find out like, you know,
2776
02:36:08,000 --> 02:36:11,900
what are the different values that are stored
in a particular column.
2777
02:36:11,900 --> 02:36:14,800
All right, so that should kind of show you guys.
2778
02:36:14,800 --> 02:36:17,300
There’s a little bit more about
how we can use these select queries.
2779
02:36:17,300 --> 02:36:19,900
And it kind of gave you guys a chance
to see how we might query data
2780
02:36:19,900 --> 02:36:24,400
from the company database schema
that we set up.
2781
02:36:24,400 --> 02:36:28,300
[Functions]
2782
02:36:28,300 --> 02:36:32,000
In this tutorial I’m going to show you guys
some SQL functions.
2783
02:36:32,000 --> 02:36:37,500
Now, an SQL function is basically just a special little,
kind of like block of code that we can call,
2784
02:36:37,500 --> 02:36:39,600
which will do something for us.
2785
02:36:39,600 --> 02:36:43,900
So, these will like count things or they'll give us
averages or they'll add things together.
2786
02:36:43,900 --> 02:36:48,000
And they can be really useful for getting information
about the data in our tables.
2787
02:36:48,000 --> 02:36:50,600
So, I’m going to give you guys
a couple different prompts
2788
02:36:50,600 --> 02:36:53,300
and then we'll figure out how we could solve them.
2789
02:36:53,300 --> 02:36:56,800
So, over here, I have a prompt
that says Find the number of employees.
2790
02:36:56,800 --> 02:37:00,400
So, this is kind of a, you know,
maybe something that you’d want to do.
2791
02:37:00,400 --> 02:37:03,300
We want to figure out how many employees
are in the database.
2792
02:37:03,300 --> 02:37:05,600
So, what I could do is I could say SELECT.
2793
02:37:05,600 --> 02:37:09,000
And I can use a special SQL function called COUNT.
2794
02:37:09,000 --> 02:37:11,100
So, I can just type COUNT like that.
2795
02:37:11,100 --> 02:37:12,900
And then open and close parenthesis.
2796
02:37:12,900 --> 02:37:16,000
And in here I want to put whatever I want to count.
2797
02:37:16,000 --> 02:37:18,900
So, basically we could just put in like emp_id.
2798
02:37:18,900 --> 02:37:22,600
And this will basically tell us how many
employee ID’s are inside of the table.
2799
02:37:22,600 --> 02:37:27,000
And since the employee ID is the primary key,
that’ll tell us how many employees we have.
2800
02:37:27,000 --> 02:37:29,600
And then I can just say FROM employee.
2801
02:37:29,600 --> 02:37:31,700
So, basically what I’m doing here
2802
02:37:31,700 --> 02:37:37,600
is I’m asking SQL to select how many employees
are inside of the Employee table.
2803
02:37:37,600 --> 02:37:44,400
So, when I run this, you’ll see down here we get 9
because there’s 9 employees inside of the table.
2804
02:37:44,400 --> 02:37:47,900
And you can see over here
we have 100 through 108 which is 9.
2805
02:37:47,900 --> 02:37:51,600
Another thing we could do would be to count
how many employees have supervisors.
2806
02:37:51,600 --> 02:37:53,600
So, this is going to be a different number.
2807
02:37:53,600 --> 02:37:58,500
You’ll see over here that 1 employee,
David Wallace doesn’t actually have a supervisor.
2808
02:37:58,500 --> 02:38:00,200
So, we could check that out.
2809
02:38:00,200 --> 02:38:02,000
We could say SELECT.
2810
02:38:02,000 --> 02:38:04,100
And I can say super_id.
2811
02:38:04,100 --> 02:38:08,400
And now when I run this, we should get 8 instead of 9
which we do down there.
2812
02:38:08,400 --> 02:38:13,900
So, this will count how many entries
in the database table actually have values.
2813
02:38:13,900 --> 02:38:17,000
Another thing we can do –
and here’s another prompt,
2814
02:38:17,000 --> 02:38:19,400
which should be kind of interesting to see
if we can figure it out.
2815
02:38:19,400 --> 02:38:23,500
It says Find the number
of female employees born after 1970.
2816
02:38:23,500 --> 02:38:27,600
So, this is a little bit more specific,
but nonetheless it’s kind of the same.
2817
02:38:27,600 --> 02:38:31,500
So, first thing we want to do is count
how many employees we have.
2818
02:38:31,500 --> 02:38:35,200
So, I’m going to select COUNT
employee ID FROM employee.
2819
02:38:35,200 --> 02:38:38,000
But I want to limit the results that we get.
2820
02:38:38,000 --> 02:38:39,900
So, I’m going to say WHERE.
2821
02:38:39,900 --> 02:38:41,500
And here, I’m going to check these conditions.
2822
02:38:41,500 --> 02:38:44,400
So, I’m going to say sex is equal to female.
2823
02:38:44,400 --> 02:38:49,700
And birth_date is greater than.
2824
02:38:49,700 --> 02:38:52,000
And over here, I’m actually going to put in a date.
2825
02:38:52,000 --> 02:38:55,800
And so, when I put in I date,
I’m just going to put the 4-digit year.
2826
02:38:55,800 --> 02:38:58,500
So, I can say, 1970.
2827
02:38:58,500 --> 02:39:00,300
And then a hyphen.
2828
02:39:00,300 --> 02:39:02,000
And then put in the month.
2829
02:39:02,000 --> 02:39:04,000
So, we'll just say 01 and then 01.
2830
02:39:04,000 --> 02:39:07,700
So, this would be like January 1st, 1970.
2831
02:39:07,700 --> 02:39:11,000
So, this will tell us the number
of female employees born after 1970.
2832
02:39:11,000 --> 02:39:13,500
Or I guess we would put this as 1971.
2833
02:39:13,500 --> 02:39:14,700
So, let’s run this.
2834
02:39:14,700 --> 02:39:17,700
And now you'll see that we get a count of 2.
2835
02:39:17,700 --> 02:39:22,300
So, there are 2 female employees in
the database table that were born after 1970.
2836
02:39:22,300 --> 02:39:28,000
And you can see over here,
looks like we have one born in 1971.
2837
02:39:28,000 --> 02:39:29,000
Angela Martin.
2838
02:39:29,000 --> 02:39:31,100
And then one born in 1980.
2839
02:39:31,100 --> 02:39:32,700
So, that actually makes sense.
2840
02:39:32,700 --> 02:39:34,700
All right, lets try another one.
2841
02:39:34,700 --> 02:39:36,000
Here’s another prompt.
2842
02:39:36,000 --> 02:39:39,400
It says Find the average of all employee’s salaries.
2843
02:39:39,400 --> 02:39:41,300
So, this is a little bit different.
2844
02:39:41,300 --> 02:39:44,400
In this case, we want to find out
the average of all of the employee’s salaries.
2845
02:39:44,400 --> 02:39:46,200
So, instead of COUNT we can say AVG.
2846
02:39:46,200 --> 02:39:49,800
And then inside of parenthesis
we can just put again the column.
2847
02:39:49,800 --> 02:39:52,000
So, I can say salary.
2848
02:39:52,000 --> 02:39:55,900
And then over here we can say FROM employee.
2849
02:39:55,900 --> 02:39:58,800
And what this will do is it’ll tell us
the average of all the employee salaries.
2850
02:39:58,800 --> 02:40:00,200
So, I’m going to go ahead and run this.
2851
02:40:00,200 --> 02:40:05,700
And you'll see down here the average
looks like $92,888.
2852
02:40:05,700 --> 02:40:08,600
And so, if we wanted, we could filter this further.
2853
02:40:08,600 --> 02:40:12,900
So, let’s say I wanted to find the average
from all the employees who are male.
2854
02:40:12,900 --> 02:40:17,400
So, I could say WHERE sex
is equal to male, just like that.
2855
02:40:17,400 --> 02:40:20,200
And now this will give us
the average of all the male salaries.
2856
02:40:20,200 --> 02:40:24,300
So, here it looks like it’s a little higher, 101,333.
2857
02:40:24,300 --> 02:40:28,600
That’s probably because David Wallace
makes 250 grand a year.
2858
02:40:28,600 --> 02:40:30,700
All right, so that’s kind of interesting.
2859
02:40:30,700 --> 02:40:32,100
Let’s try another one.
2860
02:40:32,100 --> 02:40:33,300
So, how about this.
2861
02:40:33,300 --> 02:40:37,600
Why don’t we try to find the sum
of all employee salaries?
2862
02:40:37,600 --> 02:40:40,900
So, instead of finding the average,
we're going to find the sum.
2863
02:40:40,900 --> 02:40:43,600
So, over here, instead of AVG I can say SUM.
2864
02:40:43,600 --> 02:40:44,600
And that stands for SUM.
2865
02:40:44,600 --> 02:40:48,300
And what this will do is it’ll add
up all the entries for the salaries.
2866
02:40:48,300 --> 02:40:53,400
So, this would basically tell us like how much the
company is spending on payroll to pay the employees.
2867
02:40:53,400 --> 02:40:54,800
So, over here I’m going to click Run.
2868
02:40:54,800 --> 02:41:01,000
And you'll see the company is spending
a total of $836,000 on payroll.
2869
02:41:01,000 --> 02:41:03,800
So, that’s how much the company
is paying its employees.
2870
02:41:03,800 --> 02:41:06,900
I want to show you guys one more thing we can do
which is called aggregation.
2871
02:41:06,900 --> 02:41:11,200
And aggregation is basically
where we can use these functions
2872
02:41:11,200 --> 02:41:15,100
and we can display the data
that we get back in a more helpful way.
2873
02:41:15,100 --> 02:41:18,600
So, I have a prompt up here that will
kind of get us started with this.
2874
02:41:18,600 --> 02:41:23,500
So, the prompt is find out how many males
and how many females there are.
2875
02:41:23,500 --> 02:41:25,700
So, this is actually an interesting point.
2876
02:41:25,700 --> 02:41:28,200
Let’s say we want to figure out
how many males or females were in the company.
2877
02:41:28,200 --> 02:41:30,400
Well, I can say over here SELECT COUNT.
2878
02:41:30,400 --> 02:41:33,500
And instead of saying salary, I’m going to say sex.
2879
02:41:33,500 --> 02:41:36,000
And so, let’s say that we just did this, right?
2880
02:41:36,000 --> 02:41:41,300
This is going to tell us how many employees there are
that have an entry in the sex field, right?
2881
02:41:41,300 --> 02:41:42,700
So, we get 9.
2882
02:41:42,700 --> 02:41:48,300
But if we wanted to also display how many males
and how many females there are,
2883
02:41:48,300 --> 02:41:49,300
we're going to have to do a couple things.
2884
02:41:49,300 --> 02:41:51,100
So, over here I can say COUNT comma.
2885
02:41:51,100 --> 02:41:53,000
And then I can just say sex.
2886
02:41:53,000 --> 02:41:56,200
So, what this is going to do is it’s going to return
not only the count,
2887
02:41:56,200 --> 02:41:59,300
but also it’s going to return which sex it is.
2888
02:41:59,300 --> 02:42:00,300
So, I’m going to click run.
2889
02:42:00,300 --> 02:42:02,800
And you'll see over here there are 9.
2890
02:42:02,800 --> 02:42:04,000
And it says male.
2891
02:42:04,000 --> 02:42:06,300
But this still isn’t exactly what we want.
2892
02:42:06,300 --> 02:42:09,500
So, what we can do is we can come down here
and we can say GROUP BY.
2893
02:42:09,500 --> 02:42:14,900
And what this is going to do, is it’s going to group
this information by the column that I put over here.
2894
02:42:14,900 --> 02:42:19,200
So, now when I run this, you'll see
it’s going to tell us exactly what we want.
2895
02:42:19,200 --> 02:42:22,800
So, it’s going to say 3 Female and 6 male.
2896
02:42:22,800 --> 02:42:31,200
And that’s because I’m telling SQL – I’m telling MySQL
to group the information that it gets by sex.
2897
02:42:31,200 --> 02:42:38,200
So, it’s counting how many entries or how many
employees have an entry in the sex column.
2898
02:42:38,200 --> 02:42:44,300
And then what it’s doing is it’s printing this data out
alongside of whether they're male or female.
2899
02:42:44,300 --> 02:42:46,000
And it’s giving us that information.
2900
02:42:46,000 --> 02:42:48,400
So, that is what we would call like aggregation.
2901
02:42:48,400 --> 02:42:50,500
And its really awesome.
2902
02:42:50,500 --> 02:42:53,100
And you can use this GROUP BY keyword to do that.
2903
02:42:53,100 --> 02:42:54,400
So, let’s do that again.
2904
02:42:54,400 --> 02:42:55,600
Why don’t we try another one?
2905
02:42:55,600 --> 02:42:57,600
Here’s another prompt that we can look at.
2906
02:42:57,600 --> 02:43:01,100
It says Find the total sales of each salesman.
2907
02:43:01,100 --> 02:43:03,000
So, this is kind of interesting, right?
2908
02:43:03,000 --> 02:43:05,600
Down here we have this Works With table.
2909
02:43:05,600 --> 02:43:08,500
And this kind of gives the information
about which employees sell what.
2910
02:43:08,500 --> 02:43:12,000
And you'll notice I have like employee 105, right?
2911
02:43:12,000 --> 02:43:13,600
Right here, sold 55,000.
2912
02:43:13,600 --> 02:43:18,500
Employee 105 also sold 33,000
and also sold like 130,000.
2913
02:43:18,500 --> 02:43:23,600
So, what if we want to figure out the total
that each employee actually sold?
2914
02:43:23,600 --> 02:43:25,500
Well, I can do something similar.
2915
02:43:25,500 --> 02:43:27,900
So, I can come over here and I can say SUM.
2916
02:43:27,900 --> 02:43:31,200
And here we're going to say total_sales.
2917
02:43:31,200 --> 02:43:35,500
And then over here we'll print out the employee ID.
2918
02:43:35,500 --> 02:43:40,400
And instead of grabbing this from the employee table,
we're going to grab this from the Works With table.
2919
02:43:40,400 --> 02:43:43,700
And then we want to GROUP this BY employee ID.
2920
02:43:43,700 --> 02:43:49,000
So, basically what this is going to do is it’s going to
tell us how much each employee has sold.
2921
02:43:49,000 --> 02:43:53,900
So, you'll see over here,
employee 102 sold $282,000.
2922
02:43:53,900 --> 02:43:56,400
Employee 105 sold 218,000.
2923
02:43:56,400 --> 02:43:58,600
107 sold 31,000, etc.
2924
02:43:58,600 --> 02:44:02,300
So, we're able to get all of that
information given to us.
2925
02:44:02,300 --> 02:44:05,600
Now let’s say if we wanted
something slightly different.
2926
02:44:05,600 --> 02:44:08,500
So, let’s say that instead of finding
the sales of each salesman
2927
02:44:08,500 --> 02:44:14,200
we wanted to figure out how much money
each client actually spent with the branch.
2928
02:44:14,200 --> 02:44:18,300
Well, instead of using employee ID over here,
we can just say client ID.
2929
02:44:18,300 --> 02:44:21,000
And we'll change this to client ID as well.
2930
02:44:21,000 --> 02:44:25,100
And now what this will do is it’ll tell us
how much each client spent.
2931
02:44:25,100 --> 02:44:28,100
So, I’m going to run this and you'll see
down here we get all this information.
2932
02:44:28,100 --> 02:44:32,800
So, client 400 spent $55,000.
2933
02:44:32,800 --> 02:44:36,100
Client 401 spent $267,000, right?
2934
02:44:36,100 --> 02:44:42,000
And so, we can use aggregation in order to organize
the data that we get from using these functions.
2935
02:44:42,000 --> 02:44:46,100
So, I can add up the total sales of each client.
2936
02:44:46,100 --> 02:44:49,400
And I can group them by client ID.
2937
02:44:49,400 --> 02:44:51,200
And so, that’s kind of how that works.
2938
02:44:51,200 --> 02:44:53,900
And so, aggregation can be extremely useful.
2939
02:44:53,900 --> 02:44:55,700
And it’s definitely something
you want to play around with.
2940
02:44:55,700 --> 02:44:58,600
So, we have this entire database schema over here.
2941
02:44:58,600 --> 02:45:01,500
And so, what you want to do is just kind of, you know,
2942
02:45:01,500 --> 02:45:03,700
try to give yourself little prompts
like I’m giving up here, right?
2943
02:45:03,700 --> 02:45:06,000
So, I kind of wrote out these little prompts.
2944
02:45:06,000 --> 02:45:08,000
And they're kind of like little problems
that we need to solve.
2945
02:45:08,000 --> 02:45:12,700
So, we want to, you know, be able to figure out
all the different information from the table.
2946
02:45:12,700 --> 02:45:16,600
[Wildcards]
2947
02:45:16,600 --> 02:45:21,300
In this tutorial I’m going to talk to you guys about
wildcards and the LIKE keyword in SQL.
2948
02:45:21,300 --> 02:45:26,200
Now wildcards are basically a way
of defining different patterns
2949
02:45:26,200 --> 02:45:29,300
that we want to match specific pieces of data to.
2950
02:45:29,300 --> 02:45:35,000
So, this would be a way to kind of like grab data
that matches a specific pattern.
2951
02:45:35,000 --> 02:45:37,700
And I’m going to show you guys
exactly how this works.
2952
02:45:37,700 --> 02:45:39,300
So, I have over here a prompt.
2953
02:45:39,300 --> 02:45:43,600
And it just says Find any clients who are an LLC.
2954
02:45:43,600 --> 02:45:45,700
So, this is kind of an interesting prompt.
2955
02:45:45,700 --> 02:45:52,000
Let’s say that we want to query our database
and find any clients who, you know, were like an LLC.
2956
02:45:52,000 --> 02:45:55,700
And you'll see over here in the Client’s table,
we actually have one, John Daly Law, LLC, right?
2957
02:45:55,700 --> 02:45:57,300
So, this is a limited liability company.
2958
02:45:57,300 --> 02:46:00,800
And this is kind of what we're looking to find
with our search.
2959
02:46:00,800 --> 02:46:05,300
So, I’m going to show you guys how we can use
wildcards in order to find something like this.
2960
02:46:05,300 --> 02:46:11,600
So, I could say SELECT ALL FROM client
because I want to grab a client.
2961
02:46:11,600 --> 02:46:14,100
And now I can use the WHERE keyword.
2962
02:46:14,100 --> 02:46:15,700
So, I want to filter the results, right?
2963
02:46:15,700 --> 02:46:17,900
WHERE – I’m going to say client_name.
2964
02:46:17,900 --> 02:46:20,800
And I’m going to use this keyword called LIKE.
2965
02:46:20,800 --> 02:46:25,500
And LIKE is a special SQL keyword
which we're going to use with wildcards.
2966
02:46:25,500 --> 02:46:27,600
Now, we can say LIKE.
2967
02:46:27,600 --> 02:46:32,100
And then over here we want to write out
a little expression or a little statement.
2968
02:46:32,100 --> 02:46:34,500
So, I’m going to make an open
and closed quotation mark.
2969
02:46:34,500 --> 02:46:36,400
And we'll put a semicolon over here.
2970
02:46:36,400 --> 02:46:40,500
And now, inside of here, I can uses a couple of
different special characters.
2971
02:46:40,500 --> 02:46:43,100
So, basically what I can do here
is define a pattern, okay?
2972
02:46:43,100 --> 02:46:50,500
And if the client’s name – if the specific client’s name
matches the pattern that I define over here,
2973
02:46:50,500 --> 02:46:57,500
then this condition or this statement here will be true
and we'll end up returning that client.
2974
02:46:57,500 --> 02:47:02,600
And so, we can basically use wildcards
to do a bunch of different stuff.
2975
02:47:02,600 --> 02:47:07,100
So, inside of these quotation marks
we can use two special characters.
2976
02:47:07,100 --> 02:47:08,500
There is this percent sign.
2977
02:47:08,500 --> 02:47:11,200
And this stands for any number of characters.
2978
02:47:11,200 --> 02:47:14,600
And then there’s this underscore
which stands for one character.
2979
02:47:14,600 --> 02:47:18,700
And I can use these in order
to define certain patterns
2980
02:47:18,700 --> 02:47:24,800
that can be used by the database
in order to find what we need.
2981
02:47:24,800 --> 02:47:26,700
So, over here I could say LIKE.
2982
02:47:26,700 --> 02:47:28,500
And I could say %LLC.
2983
02:47:28,500 --> 02:47:36,100
Basically, what this pattern is saying is if the client’s
name is LIKE this pattern, then we want to return it.
2984
02:47:36,100 --> 02:47:41,000
So, in other words, if it’s any number of characters
and then an LLC at the end, then we want to return it.
2985
02:47:41,000 --> 02:47:44,900
So, this percent sign is going to stand
for any number of characters.
2986
02:47:44,900 --> 02:47:46,500
So, that means any characters.
2987
02:47:46,500 --> 02:47:48,400
Anything can come before that.
2988
02:47:48,400 --> 02:47:51,900
But that would mean that the name
has to end in LLC.
2989
02:47:51,900 --> 02:47:57,200
So, you'll see over here John Daly Law, LLC
has a bunch of characters here.
2990
02:47:57,200 --> 02:48:00,500
And then the last three characters are LLC.
2991
02:48:00,500 --> 02:48:01,900
And that’s kind of standard.
2992
02:48:01,900 --> 02:48:05,000
Most limited liability companies
will be set up like that.
2993
02:48:05,000 --> 02:48:08,200
It’ll be like company name, LLC or John Daly Law, LLC.
2994
02:48:08,200 --> 02:48:13,500
So, this wildcard, if there’s any company names
like that that end with LLC
2995
02:48:13,500 --> 02:48:17,400
is going to catch them
because it’s using this percent sign
2996
02:48:17,400 --> 02:48:20,900
because basically means
any number of characters can come before this.
2997
02:48:20,900 --> 02:48:23,200
And then LLC.
2998
02:48:23,200 --> 02:48:27,300
If you’re familiar with regular expressions,
this is very similar to regular expressions.
2999
02:48:27,300 --> 02:48:28,900
It’s just a more simplified version.
3000
02:48:28,900 --> 02:48:30,100
So, I’m just going to click Run.
3001
02:48:30,100 --> 02:48:34,000
And you'll see down here
we returned John Daly, LLC, just like that.
3002
02:48:34,000 --> 02:48:39,500
So, that’s how we could use these wildcards and this
LIKE keyword in order to find something like that.
3003
02:48:39,500 --> 02:48:41,400
So, why don’t we try another one?
3004
02:48:41,400 --> 02:48:45,100
So, I have another little prompt here
that we can see if we can figure it out.
3005
02:48:45,100 --> 02:48:51,000
So, it says Find any branch suppliers
who are in the label business.
3006
02:48:51,000 --> 02:48:52,600
Okay, so this is kind of interesting.
3007
02:48:52,600 --> 02:48:56,500
Find any branch suppliers
that are in the label business.
3008
02:48:56,500 --> 02:48:59,200
Well, over here we're going to change this.
3009
02:48:59,200 --> 02:49:04,500
So, instead of looking in client
we're going to look in branch_supplier.
3010
02:49:04,500 --> 02:49:07,000
And then down here
we're going to do the same thing.
3011
02:49:07,000 --> 02:49:11,100
Except here, we're just going
to say supplier_name.
3012
02:49:11,100 --> 02:49:14,300
So, over here in the Branch Supplier table we have
a bunch of different branch suppliers, right?
3013
02:49:14,300 --> 02:49:19,800
These would be like companies
that supply products to our company.
3014
02:49:19,800 --> 02:49:24,700
And you'll see there’s actually a couple of these
that have the word Label in them.
3015
02:49:24,700 --> 02:49:29,500
And so, if the word Label is in the company’s name,
then we know they probably sell labels.
3016
02:49:29,500 --> 02:49:32,500
And so, what I could do is I could say use a wildcard
3017
02:49:32,500 --> 02:49:37,500
to see if the word label shows up
anywhere inside of their names.
3018
02:49:37,500 --> 02:49:42,300
So, what I could do is I’m actually going to
come over here and I’m going to change this up.
3019
02:49:42,300 --> 02:49:45,100
So, basically we're going to have
any number of characters.
3020
02:49:45,100 --> 02:49:47,200
We're going to use this %.
3021
02:49:47,200 --> 02:49:49,600
And then I'll just say Label.
3022
02:49:49,600 --> 02:49:53,100
And then we'll make another one with another %.
3023
02:49:53,100 --> 02:49:57,000
So, actually, why don’t we check to see
if the word Label is actually in there.
3024
02:49:57,000 --> 02:50:00,300
So, it’s going to be % and then Label, just like that.
3025
02:50:00,300 --> 02:50:01,500
So, this is going to be our wildcard.
3026
02:50:01,500 --> 02:50:07,400
So, this will basically match if the supplier name
has the word Label in it somewhere.
3027
02:50:07,400 --> 02:50:08,900
So, now I’m going to run this.
3028
02:50:08,900 --> 02:50:12,300
And you'll see down here
we get this company, J.T. Forms & Labels.
3029
02:50:12,300 --> 02:50:15,200
And so, actually I thought there was two of them.
3030
02:50:15,200 --> 02:50:16,200
Let me see.
3031
02:50:16,200 --> 02:50:17,700
Okay, yeah.
3032
02:50:17,700 --> 02:50:19,300
So, it looks like I have a typo here.
3033
02:50:19,300 --> 02:50:24,400
So, it looks like when I was inserting the data
I spelled Labels wrong on this column.
3034
02:50:24,400 --> 02:50:27,300
So, that’s going to be why that one is not showing up.
3035
02:50:27,300 --> 02:50:32,000
But if we had – if I had spelled this correctly,
then Stamford Labels would also have shown up.
3036
02:50:32,000 --> 02:50:34,900
So, that kind of shows you guys
how we can do something like that.
3037
02:50:34,900 --> 02:50:36,300
So, why don’t we find another one.
3038
02:50:36,300 --> 02:50:41,200
All right, so this one says Find
any employee born in October.
3039
02:50:41,200 --> 02:50:42,400
So, this is kind of interesting.
3040
02:50:42,400 --> 02:50:48,800
You'll see over here on the employee table we're
actually storing the birthdates for all the employees.
3041
02:50:48,800 --> 02:50:52,100
And they're all structured the same exact way.
3042
02:50:52,100 --> 02:50:55,500
We have a 4-digit year, a hyphen,
and then the 2-digit month.
3043
02:50:55,500 --> 02:50:57,400
Now, October is the 10th month.
3044
02:50:57,400 --> 02:50:59,200
So, let’s see if we have any employees.
3045
02:50:59,200 --> 02:51:03,100
So, we have one employee down here,
Jim Halpert was born in October.
3046
02:51:03,100 --> 02:51:07,400
So, what we can do is let’s see if we can design
a wildcard that will figure that one out.
3047
02:51:07,400 --> 02:51:10,300
So, over here we can change this to employee.
3048
02:51:10,300 --> 02:51:11,500
So FROM employee.
3049
02:51:11,500 --> 02:51:18,200
And we want to check to see
if the birthdate is LIKE the wildcard.
3050
02:51:18,200 --> 02:51:21,900
So, what we can do is we can
actually use this character right here.
3051
02:51:21,900 --> 02:51:23,500
It’s this underscore.
3052
02:51:23,500 --> 02:51:26,800
So, the underscore represents any single character.
3053
02:51:26,800 --> 02:51:32,100
So, the percent sign represented
just like any number of random characters.
3054
02:51:32,100 --> 02:51:35,800
Any number of characters would match it.
3055
02:51:35,800 --> 02:51:38,000
And the underscore represents one character.
3056
02:51:38,000 --> 02:51:39,900
And we can use that to our advantage.
3057
02:51:39,900 --> 02:51:42,500
So, we know the way
that these dates are formatted, right?
3058
02:51:42,500 --> 02:51:45,600
It’s a 4-digit date, a hyphen.
3059
02:51:45,600 --> 02:51:49,300
Or it’s a 4-digit year, a hyphen,
and then the 2-digit month.
3060
02:51:49,300 --> 02:51:55,800
So, we could do 1, 2, 3, 4 underscores,
a hyphen, and then a 10.
3061
02:51:55,800 --> 02:51:58,100
And then we can do this percent sign.
3062
02:51:58,100 --> 02:52:05,300
So, what this is going to match, it’s going to match
with any four characters, a hyphen, and then 10.
3063
02:52:05,300 --> 02:52:09,400
So, this should give us all of the birthdays
that are in October.
3064
02:52:09,400 --> 02:52:10,600
So, I'm going to run this.
3065
02:52:10,600 --> 02:52:12,800
And you'll see over here we get Jim Halpert.
3066
02:52:12,800 --> 02:52:15,700
So, it says Jim Halpert
and he is indeed born in October.
3067
02:52:15,700 --> 02:52:17,300
So, you can do this for any month.
3068
02:52:17,300 --> 02:52:20,600
We can see if there’s any
employees born in February.
3069
02:52:20,600 --> 02:52:22,500
And you'll see down here,
3070
02:52:22,500 --> 02:52:26,600
it looks like we have two employees born in February,
also born in different years.
3071
02:52:26,600 --> 02:52:31,100
So, that kind of gives you an idea
of how we could do something like that.
3072
02:52:31,100 --> 02:52:33,300
Again, we're using these wildcards.
3073
02:52:33,300 --> 02:52:35,200
All right, so let’s try one more.
3074
02:52:35,200 --> 02:52:38,000
It says Find any clients who are schools.
3075
02:52:38,000 --> 02:52:40,100
So, this one is kind of interesting.
3076
02:52:40,100 --> 02:52:44,600
Basically, we're looking for any clients
that might be schools.
3077
02:52:44,600 --> 02:52:51,200
So, what we could do is we could say SELECT ALL
from client WHERE client_name.
3078
02:52:51,200 --> 02:52:53,400
So, why don’t we search the client name – is LIKE.
3079
02:52:53,400 --> 02:52:55,000
And then over here we'll define a wildcard.
3080
02:52:55,000 --> 02:52:58,300
Basically, let’s just look for anything
that has school in it.
3081
02:52:58,300 --> 02:53:02,400
So, we could say %school and then %.
3082
02:53:02,400 --> 02:53:05,300
So, this is obviously like a little bit general and broad,
3083
02:53:05,300 --> 02:53:07,700
but hopefully it should at least
give us all of the schools.
3084
02:53:07,700 --> 02:53:09,600
So, I’m going to go ahead and run this.
3085
02:53:09,600 --> 02:53:12,200
And you'll see down here,
it looks like we have 1 client that’s a school.
3086
02:53:12,200 --> 02:53:13,900
And it’s Dunmore Highschool.
3087
02:53:13,900 --> 02:53:15,500
And the branch ID is 2.
3088
02:53:15,500 --> 02:53:18,400
So, that is really the basics of using these wildcards.
3089
02:53:18,400 --> 02:53:22,900
So, again, we have the percent sign
which is going to represent any number of characters.
3090
02:53:22,900 --> 02:53:25,500
And then we have the underscore.
3091
02:53:25,500 --> 02:53:31,100
And the whole idea is we want to build these little
expressions here which we'll be able to match.
3092
02:53:31,100 --> 02:53:35,000
So, like the client name
should be able to match this expression.
3093
02:53:35,000 --> 02:53:38,200
And if it does, then we're going to
go ahead and return it.
3094
02:53:38,200 --> 02:53:40,300
And those can actually be really useful
3095
02:53:40,300 --> 02:53:43,800
when you’re just trying to, you know,
kind of query something from a database.
3096
02:53:43,800 --> 02:53:46,200
So, imagine that you're building
like a searching application
3097
02:53:46,200 --> 02:53:48,500
and the user entered a bunch of search terms.
3098
02:53:48,500 --> 02:53:52,600
You could use something like this in order to
search the database for you.
3099
02:53:52,600 --> 02:53:55,600
[Union]
3100
02:53:55,600 --> 02:53:59,300
In this tutorial I’m going to talk to you guys
about unions in SQL.
3101
02:53:59,300 --> 02:54:03,200
Now, a union is basically a special SQL operator
3102
02:54:03,200 --> 02:54:08,500
which we can use to combine the results
of multiple select statements into one.
3103
02:54:08,500 --> 02:54:13,200
So, I might have, you know, two or three
different select statements that I’m using.
3104
02:54:13,200 --> 02:54:17,700
And if I want it, I can combine all of them
into the same result
3105
02:54:17,700 --> 02:54:22,600
and get just a big list or a big table back
from the database.
3106
02:54:22,600 --> 02:54:24,000
So, I’m going to show you guys how we can do this.
3107
02:54:24,000 --> 02:54:27,300
And I’m actually going to give us some prompts
so we can kind of practice.
3108
02:54:27,300 --> 02:54:32,600
So, over here, I have prompt that says
Find a list of employee and branch names.
3109
02:54:32,600 --> 02:54:34,200
So, this is kind of interesting.
3110
02:54:34,200 --> 02:54:38,300
We can actually use the UNION operator
in order to do this.
3111
02:54:38,300 --> 02:54:44,900
So, first thing, why don we see how we could just grab
just the employee names and just the branch names.
3112
02:54:44,900 --> 02:54:49,200
So, if I wanted to just grab the employee names
I could just say SELECT first_name.
3113
02:54:49,200 --> 02:54:53,300
And why don’t we just grab the first name
FROM employee, right?
3114
02:54:53,300 --> 02:54:54,300
So, that’s pretty easy.
3115
02:54:54,300 --> 02:54:55,700
I mean that’s as easy as it gets.
3116
02:54:55,700 --> 02:54:57,800
We're just getting all the first names
from the employees.
3117
02:54:57,800 --> 02:54:59,600
Let’s do the same thing for branch name.
3118
02:54:59,600 --> 02:55:05,100
So, I could say SELECT branch_name
FROM branch, right?
3119
02:55:05,100 --> 02:55:07,000
Again, pretty simple.
3120
02:55:07,000 --> 02:55:10,500
So, this over here will give us all the names
of the employees.
3121
02:55:10,500 --> 02:55:14,300
So, if I was to just run this, you'll see
we get all the names of all the employees.
3122
02:55:14,300 --> 02:55:17,800
And if I was to run this, we get the names
of the all the branches.
3123
02:55:17,800 --> 02:55:21,500
But how can we combine those together
into one single statement
3124
02:55:21,500 --> 02:55:24,400
and then just get a list with
all that information in it?
3125
02:55:24,400 --> 02:55:26,000
Well, I can use the UNION keyword.
3126
02:55:26,000 --> 02:55:28,300
So, over here I can just say UNION.
3127
02:55:28,300 --> 02:55:30,500
I’m going to actually going to
get rid of this semicolon.
3128
02:55:30,500 --> 02:55:34,800
So, now we have one single SQL query which is going
to ask the relational database management system
3129
02:55:34,800 --> 02:55:41,000
to return not only the employee first names,
but also the branch names in a single column.
3130
02:55:41,000 --> 02:55:42,600
So, I’m going to go ahead and run this.
3131
02:55:42,600 --> 02:55:46,700
And you'll see down here
that we get this big column right here.
3132
02:55:46,700 --> 02:55:48,100
Just says first_name.
3133
02:55:48,100 --> 02:55:50,200
And then we have all of the names of the employees,
3134
02:55:50,200 --> 02:55:52,600
but down here we also have the names
of the branches.
3135
02:55:52,600 --> 02:55:54,900
So, Corporate, Scranton and Stamford.
3136
02:55:54,900 --> 02:55:57,100
We have all that information.
3137
02:55:57,100 --> 02:56:01,400
And it’s all combined
with the names of the employees.
3138
02:56:01,400 --> 02:56:02,900
So, this is actually pretty cool.
3139
02:56:02,900 --> 02:56:07,400
And when we're using unions,
you can do unions on a bunch of different things.
3140
02:56:07,400 --> 02:56:08,700
There are a couple rules though.
3141
02:56:08,700 --> 02:56:12,800
So, the first rule is that you have to
have the name number of columns
3142
02:56:12,800 --> 02:56:15,300
that you're getting in each SELECT statement.
3143
02:56:15,300 --> 02:56:18,100
So, in this first SELECT statement,
I’m grabbing one column.
3144
02:56:18,100 --> 02:56:20,600
In the second SELECT statement,
I’m grabbing one column.
3145
02:56:20,600 --> 02:56:24,100
If I was to come up here
and also try to grab like last_name,
3146
02:56:24,100 --> 02:56:26,500
now when I run this, we're going to get an error
3147
02:56:26,500 --> 02:56:30,000
because up here we have two columns
and down here we only have one.
3148
02:56:30,000 --> 02:56:31,200
So, that’s the first rule.
3149
02:56:31,200 --> 02:56:32,500
You can do that.
3150
02:56:32,500 --> 02:56:34,100
They have to have the same number of columns.
3151
02:56:34,100 --> 02:56:36,700
They also have to have a similar datatype.
3152
02:56:36,700 --> 02:56:40,600
So, first_name and branch_name,
they’re both strings, right?
3153
02:56:40,600 --> 02:56:46,200
So, they're both of a similar datatype,
so we're able to return them in the same result.
3154
02:56:46,200 --> 02:56:48,800
But if you had two things
that were very different datatypes
3155
02:56:48,800 --> 02:56:52,100
then it might not necessarily work out as well.
3156
02:56:52,100 --> 02:56:54,400
So, that is basically how we can do these.
3157
02:56:54,400 --> 02:56:57,400
And that kind of just show you guys
how we can use the UNION operator
3158
02:56:57,400 --> 02:56:59,900
in order to combine the results
from multiple statements.
3159
02:56:59,900 --> 02:57:02,500
If we wanted, we could add in another one too.
3160
02:57:02,500 --> 02:57:04,400
So, I could say like UNION again.
3161
02:57:04,400 --> 02:57:13,000
And now we can UNION all of that
with like SELECT client_name FROM client.
3162
02:57:13,000 --> 02:57:16,900
So, now I'm grabbing not just the names of the
employees and the branches, but also the clients.
3163
02:57:16,900 --> 02:57:22,800
So, when we run this now, you can see we're getting
this really long list that has all these names.
3164
02:57:22,800 --> 02:57:26,900
Now one thing you might notice is that
the column name up here is first_name.
3165
02:57:26,900 --> 02:57:33,500
So, this is saying that it’s first_name when in reality,
you know, that’s not necessarily what this is.
3166
02:57:33,500 --> 02:57:37,100
And so, over here you'll see that it’s first_name
3167
02:57:37,100 --> 02:57:41,800
because the first SELECT statement, the column
that we were grabbing was called first_name.
3168
02:57:41,800 --> 02:57:43,900
So, that’s why that’s showing up as first_name.
3169
02:57:43,900 --> 02:57:46,100
But if you wanted, you could change that.
3170
02:57:46,100 --> 02:57:48,400
So, I could say first_name AS
and then I can change the name.
3171
02:57:48,400 --> 02:57:50,800
So, I can just say like Company_Names.
3172
02:57:50,800 --> 02:57:57,400
And so, now the column name is going to be
Company_Names instead of first_name.
3173
02:57:57,400 --> 02:58:02,300
So, now this is just like all the different names
that we have stored in the company database.
3174
02:58:02,300 --> 02:58:05,400
So, hopefully that kind of makes sense
and that’s kind of how we can use UNIONs.
3175
02:58:05,400 --> 02:58:08,000
I’m going to show you guys a couple
other cool ones that we can do.
3176
02:58:08,000 --> 02:58:10,400
So, I’m going to pull up another prompt here.
3177
02:58:10,400 --> 02:58:12,300
Why don’t we take a look?
3178
02:58:12,300 --> 02:58:15,900
It says find a list of
all clients & branch suppliers’ names.
3179
02:58:15,900 --> 02:58:18,200
So, this is actually pretty similar.
3180
02:58:18,200 --> 02:58:23,500
So, we can SELECT client_name FROM client.
3181
02:58:23,500 --> 02:58:33,800
And then we can also UNION this with SELECT
supplier_name FROM branch_supplier.
3182
02:58:33,800 --> 02:58:37,900
And so, now we're going to get a table with
all the client names and all of the supplier names.
3183
02:58:37,900 --> 02:58:39,100
So, let’s run this.
3184
02:58:39,100 --> 02:58:41,100
And you'll see over here we get all that.
3185
02:58:41,100 --> 02:58:45,200
So, we have all of the clients
and then we have all of the suppliers.
3186
02:58:45,200 --> 02:58:49,700
And you'll notice over here on the Client table
and the Branch Supplier table,
3187
02:58:49,700 --> 02:58:53,200
both of these have a branch_id column.
3188
02:58:53,200 --> 02:58:55,400
So, the Branch Supplier has a branch_id column.
3189
02:58:55,400 --> 02:58:57,700
And the client has a branch_id column.
3190
02:58:57,700 --> 02:58:59,700
So, what we could do is we could use that.
3191
02:58:59,700 --> 02:59:01,400
So, I could say like SELECT client_name.
3192
02:59:01,400 --> 02:59:06,100
And we could also SELECT branch_id FROM client.
3193
02:59:06,100 --> 02:59:13,100
And then we can SELECT supplier_name and the branch_id from the Branch Supplier.
3194
02:59:13,100 --> 02:59:17,100
And that will give us now not only
the client names and the supplier names,
3195
02:59:17,100 --> 02:59:19,800
but also the branches that they're associated with.
3196
02:59:19,800 --> 02:59:22,700
So, you can see down here
we get all of that information.
3197
02:59:22,700 --> 02:59:25,100
Now, one thing I do want to point to
is you'll notice over here
3198
02:59:25,100 --> 02:59:29,200
we have branch_id up here
and branch_id down here.
3199
02:59:29,200 --> 02:59:35,400
So, the Branch Supplier table and the Client table
both have a column called branch_id.
3200
02:59:35,400 --> 02:59:38,000
And sometimes in a situation like this
it can get a little bit confusing
3201
02:59:38,000 --> 02:59:40,900
because we have the same column name.
3202
02:59:40,900 --> 02:59:43,300
But associated with different tables.
3203
02:59:43,300 --> 02:59:49,500
And so, what a lot of people will do is they'll prefix
these column names with the table name.
3204
02:59:49,500 --> 02:59:52,100
So, let’s say like client.branch_id.
3205
02:59:52,100 --> 02:59:57,900
Or they'll say branch_supplier.branch_id.
3206
02:59:57,900 --> 03:00:01,400
And what that does is it basically
just makes it a little bit more readable.
3207
03:00:01,400 --> 03:00:04,400
So, now I know that this branch ID
comes from the Client table.
3208
03:00:04,400 --> 03:00:07,300
And this branch ID
comes from the Branch Supplier table.
3209
03:00:07,300 --> 03:00:08,500
You don’t have to do that.
3210
03:00:08,500 --> 03:00:12,000
But in a lot of circumstances,
it can be extremely useful.
3211
03:00:12,000 --> 03:00:15,300
So, I want to show you guys
one more thing we can do with UNION,
3212
03:00:15,300 --> 03:00:17,100
so I’m going to give us another prompt here.
3213
03:00:17,100 --> 03:00:21,600
And it basically just says find a list of all money spent
or earned by the company.
3214
03:00:21,600 --> 03:00:23,100
So, this one is kind of interesting.
3215
03:00:23,100 --> 03:00:28,000
All the money that the company either spends
or earns, we want to combine into a single list.
3216
03:00:28,000 --> 03:00:33,300
So, basically, the company earns money
through the total sales down here.
3217
03:00:33,300 --> 03:00:37,800
And the company spends money by paying
its employees in the salary field up here.
3218
03:00:37,800 --> 03:00:39,600
So, we can combine those two.
3219
03:00:39,600 --> 03:00:46,000
So, I can just say like SELECT salary FROM employee.
3220
03:00:46,000 --> 03:00:58,100
And again, we can UNION that
with SELECT total_sales FROM works_with.
3221
03:00:58,100 --> 03:01:01,100
And so, now this will give us that column.
3222
03:01:01,100 --> 03:01:03,600
So, you'll see over here it’s all of this money
3223
03:01:03,600 --> 03:01:06,700
that’s either going in or out of
the branch combined together.
3224
03:01:06,700 --> 03:01:09,900
Hopefully, that gives you a good idea
of how UNIONs work.
3225
03:01:09,900 --> 03:01:12,200
Now, obviously, these are very simple examples.
3226
03:01:12,200 --> 03:01:17,100
But you can take this and kind of extrapolate it out
to more complex examples.
3227
03:01:17,100 --> 03:01:20,400
Union basically just combines
the results from two SELECT statements.
3228
03:01:20,400 --> 03:01:22,000
That’s essentially all it does.
3229
03:01:22,000 --> 03:01:26,300
But again, there are certain rules like you have to have
the same number of columns in both statements.
3230
03:01:26,300 --> 03:01:29,600
And they have to be like similar datatype
and stuff like that.
3231
03:01:29,600 --> 03:01:35,700
But UNIONs can be very useful for sort of combining
all this data into a single place.
3232
03:01:35,700 --> 03:01:39,600
[Joins]
3233
03:01:39,600 --> 03:01:42,700
In this tutorial I’m going to talk to you guys
about JOINs in SQL.
3234
03:01:42,700 --> 03:01:47,600
Well, JOIN is basically used to combine rows
from two or more tables
3235
03:01:47,600 --> 03:01:50,500
based on a related column between them.
3236
03:01:50,500 --> 03:01:55,000
So, JOINs can be really useful for combining
information from different tables
3237
03:01:55,000 --> 03:02:00,300
into a single result
which we can then use to, you know,
3238
03:02:00,300 --> 03:02:03,600
obviously find out specific information
that’s stored in our database.
3239
03:02:03,600 --> 03:02:07,600
So, in order to teach JOINs,
I’m actually going to have us do one thing.
3240
03:02:07,600 --> 03:02:11,100
We're going to insert another branch
into the Branch table.
3241
03:02:11,100 --> 03:02:13,400
So, over here I just have the code to do that.
3242
03:02:13,400 --> 03:02:15,600
I’m inserting INTO branch the VALUES.
3243
03:02:15,600 --> 03:02:17,700
So, the branch is going to have an ID of 4.
3244
03:02:17,700 --> 03:02:19,900
The branch is going to be called Buffalo.
3245
03:02:19,900 --> 03:02:24,200
And it’s not going to have a manager ID
and it’s not going to have a manager start date.
3246
03:02:24,200 --> 03:02:30,000
So, down here in this table that we've been using
so far, we have branches 1, 2, and 3.
3247
03:02:30,000 --> 03:02:31,800
Corporate, Scranton, and Stamford.
3248
03:02:31,800 --> 03:02:33,200
Now we're adding a Buffalo branch.
3249
03:02:33,200 --> 03:02:38,400
But the Buffalo branch doesn’t have a manager ID
and it doesn’t have a manager start date.
3250
03:02:38,400 --> 03:02:39,600
Both of those are NULL.
3251
03:02:39,600 --> 03:02:42,700
So, I went ahead and inserted this into my database.
3252
03:02:42,700 --> 03:02:46,400
And you'll see down here
this is now our branch database.
3253
03:02:46,400 --> 03:02:48,400
So, we have 1, 2, 3, 4.
3254
03:02:48,400 --> 03:02:49,700
All of these pieces of information.
3255
03:02:49,700 --> 03:02:54,000
And then the manager ID for Buffalo is NULL
and the manager start date is also NULL.
3256
03:02:54,000 --> 03:02:56,000
So, that’s actually going to come in handy
3257
03:02:56,000 --> 03:02:58,100
for what I’m going to be showing you guys
with these joins.
3258
03:02:58,100 --> 03:03:00,300
So, if I want to follow along, go ahead and do that.
3259
03:03:00,300 --> 03:03:01,800
But if you're not going to be following along,
3260
03:03:01,800 --> 03:03:06,100
just know that Buffalo doesn’t have a manager ID
or a manager start date.
3261
03:03:06,100 --> 03:03:09,800
So, now that we've inserted that into the database,
let’s get started.
3262
03:03:09,800 --> 03:03:17,100
So, I want to kind of show you guys what a JOIN is
before I actually talk to you about what it does.
3263
03:03:17,100 --> 03:03:19,400
So, I’m just going to go ahead
and show you guys an example.
3264
03:03:19,400 --> 03:03:20,800
I’m just going to paste in here.
3265
03:03:20,800 --> 03:03:22,900
And then we'll talk about the example
3266
03:03:22,900 --> 03:03:25,300
and you'll be able to see exactly
what the JOIN is doing.
3267
03:03:25,300 --> 03:03:28,200
So, over here I have this.
3268
03:03:28,200 --> 03:03:32,100
It says Find all branches
and the names of their managers.
3269
03:03:32,100 --> 03:03:35,600
So, we want to find all the branches in the database.
3270
03:03:35,600 --> 03:03:39,500
And for each of those branches
we want to find the name of the manager.
3271
03:03:39,500 --> 03:03:43,600
So, down here you'll notice
that I have this branch table.
3272
03:03:43,600 --> 03:03:48,700
And the branch has this column here mgr_id, right?
3273
03:03:48,700 --> 03:03:51,900
And inside the mgr_id
we have ID’s of different employees.
3274
03:03:51,900 --> 03:03:57,300
So, these ID’s relate to employee ID’s
which are stored up here.
3275
03:03:57,300 --> 03:04:04,000
Now, I want you guys to notice that
the employee ID column and the manager ID column
3276
03:04:04,000 --> 03:04:07,100
that we have down here are similar, right?
3277
03:04:07,100 --> 03:04:09,300
They’re both storing employee IDs.
3278
03:04:09,300 --> 03:04:13,200
And so, this is basically a column that is shared
3279
03:04:13,200 --> 03:04:16,300
between the Employee table
and the Branch table, right?
3280
03:04:16,300 --> 03:04:20,100
Both of those tables have a column
which stores employee ID’s.
3281
03:04:20,100 --> 03:04:25,400
And anytime you have a situation like that,
you can use something called a JOIN.
3282
03:04:25,400 --> 03:04:32,800
And like I said, a JOIN is used to combine rows from
two or more tables based on the related column.
3283
03:04:32,800 --> 03:04:37,300
So, in our case, the related column
is the ID’s of the employees.
3284
03:04:37,300 --> 03:04:43,200
So, over here, in order to find all the branches
and the names of their managers we can use a JOIN.
3285
03:04:43,200 --> 03:04:44,700
So, here I’m saying SELECT.
3286
03:04:44,700 --> 03:04:47,700
And I’m selecting employee.emp_id.
3287
03:04:47,700 --> 03:04:52,700
employee.first_name and branch.branch_name.
3288
03:04:52,700 --> 03:04:55,100
Now, up to this point in the course,
3289
03:04:55,100 --> 03:05:02,100
we’ve never used multiple table’s columns
up here in the SELECT statement.
3290
03:05:02,100 --> 03:05:06,600
So, normally we just say like employee.emp_id,
employee.first_name.
3291
03:05:06,600 --> 03:05:09,400
We wouldn't use – or we wouldn't specify anything
from the Branch table.
3292
03:05:09,400 --> 03:05:11,900
But because we're joining the tables,
3293
03:05:11,900 --> 03:05:17,400
we can actually specify that we want to
grab a column from the Branch table as well.
3294
03:05:17,400 --> 03:05:18,700
So, just keep that in mind.
3295
03:05:18,700 --> 03:05:22,900
So, when we get this table back,
we're going to be getting the employee ID,
3296
03:05:22,900 --> 03:05:26,000
the employee’s first name,
the branch’s name.
3297
03:05:26,000 --> 03:05:30,500
So, that will basically give us the branch
and the name of the branch manager.
3298
03:05:30,500 --> 03:05:33,600
And I want to grab that information from employee.
3299
03:05:33,600 --> 03:05:36,300
And then I’m going to say JOIN branch.
3300
03:05:36,300 --> 03:05:38,500
And so, when I say JOIN branch,
3301
03:05:38,500 --> 03:05:44,200
what this is going to do is it’s going to join
the Employee table and the Branch table together.
3302
03:05:44,200 --> 03:05:46,900
So, it’s going to join them together into one table.
3303
03:05:46,900 --> 03:05:51,200
And it’s going to join them together on a specific column.
3304
03:05:51,200 --> 03:05:55,100
Now, this is the column that both of these
are going to have in common.
3305
03:05:55,100 --> 03:05:57,500
So, I’m going to say Join branch ON.
3306
03:05:57,500 --> 03:06:05,700
And then over here, I can basically just say
employee.emp_id is equal to branch.mgr_id.
3307
03:06:05,700 --> 03:06:08,600
So, basically I want to combine these two tables.
3308
03:06:08,600 --> 03:06:12,300
Or I want to combine all of the rows
from the Employee table
3309
03:06:12,300 --> 03:06:14,500
and all of the rows from the Branch table,
3310
03:06:14,500 --> 03:06:22,700
as long as the manager ID of the branch row
is equal to the employee ID of the employee row.
3311
03:06:22,700 --> 03:06:25,400
So, I’m going to go ahead and run this
and you guys will see what happens.
3312
03:06:25,400 --> 03:06:27,600
And this should kind of clear up
exactly what’s happening.
3313
03:06:27,600 --> 03:06:30,100
So, down here we get three columns.
3314
03:06:30,100 --> 03:06:32,700
We get emp_id, first_name and branch_name.
3315
03:06:32,700 --> 03:06:35,000
And remember, we specified that up here.
3316
03:06:35,000 --> 03:06:39,900
I said employee – I want the ID, the first name,
and then I want the name of the branch.
3317
03:06:39,900 --> 03:06:42,600
So, down here, we're getting the employee ID.
3318
03:06:42,600 --> 03:06:43,800
So, it’s 100.
3319
03:06:43,800 --> 03:06:46,900
We’re getting the first name, which is David.
3320
03:06:46,900 --> 03:06:49,000
And we're getting the branch name Corporate.
3321
03:06:49,000 --> 03:06:54,100
So, this tells me that the manager
of the Corporate branch is named David.
3322
03:06:54,100 --> 03:06:57,500
The manager of the Scranton branch
is named Michael.
3323
03:06:57,500 --> 03:07:00,500
And the manager of the Stamford branch
is named Josh.
3324
03:07:00,500 --> 03:07:04,200
And if you look over there in our database table,
that’s exactly right.
3325
03:07:04,200 --> 03:07:10,600
And so, essentially, we combined a row
from the Branch table, this branch name,
3326
03:07:10,600 --> 03:07:16,700
with the rows from the Employee table, emp_id
and first_name into one single table, right?
3327
03:07:16,700 --> 03:07:23,100
But we only combined them when the employee ID
was equal to the branch’s manager ID, right?
3328
03:07:23,100 --> 03:07:25,300
We have a bunch of employees over here.
3329
03:07:25,300 --> 03:07:28,200
Michael, Angela, Kelly, Stanley, Josh, Andy, Jim.
3330
03:07:28,200 --> 03:07:33,900
But not all of these employees have their ID
down here in the mgr_id column.
3331
03:07:33,900 --> 03:07:39,000
So, only employees whose ID’s match
the value here in the this mgr_id column
3332
03:07:39,000 --> 03:07:43,700
were joined together into this combined table
that we got down here.
3333
03:07:43,700 --> 03:07:46,600
So, that is basically what a JOIN is.
3334
03:07:46,600 --> 03:07:50,700
We can take rows from one table,
combine them with rows from another table.
3335
03:07:50,700 --> 03:07:52,100
And it gives us information.
3336
03:07:52,100 --> 03:07:53,800
So, this is pretty useful information, right?
3337
03:07:53,800 --> 03:07:57,200
We got the name of the manager for a specific branch
3338
03:07:57,200 --> 03:08:00,900
even though that information
was stored in different tables.
3339
03:08:00,900 --> 03:08:04,800
So, this is kind of the most basic version of a JOIN.
3340
03:08:04,800 --> 03:08:07,900
And this is like just the normal join.
3341
03:08:07,900 --> 03:08:10,800
You can see I’m just using
this normal JOIN keyword up here.
3342
03:08:10,800 --> 03:08:13,800
But there’s actually a couple other types of JOINs.
3343
03:08:13,800 --> 03:08:16,700
So, there’s actually four basic types
of JOINs that we can use.
3344
03:08:16,700 --> 03:08:18,900
The first is just this general JOIN.
3345
03:08:18,900 --> 03:08:21,200
And this is what’s referred to as inner JOIN.
3346
03:08:21,200 --> 03:08:26,300
And the inner JOIN is going to combine rows
from the Employee table and the Branch table
3347
03:08:26,300 --> 03:08:29,600
whenever they have the shared column in common.
3348
03:08:29,600 --> 03:08:32,900
So, whenever the employee ID
is equal to the manager ID,
3349
03:08:32,900 --> 03:08:38,000
then that is going to get included
in the table that gets returned back to us.
3350
03:08:38,000 --> 03:08:40,000
But there’s a couple of other types of joins.
3351
03:08:40,000 --> 03:08:43,700
And I want to show you guys what they do.
3352
03:08:43,700 --> 03:08:45,300
So, there’s another which is called a LEFT JOIN.
3353
03:08:45,300 --> 03:08:48,600
And you can just say LEFT JOIN just like that.
3354
03:08:48,600 --> 03:08:51,300
And so, now instead of doing a normal JOIN
we're going to do what’s called a LEFT JOIN.
3355
03:08:51,300 --> 03:08:53,900
And I want to show you guys
what happens when I run this.
3356
03:08:53,900 --> 03:08:56,000
So, I’m going to go ahead and run this.
3357
03:08:56,000 --> 03:09:01,500
And you'll see over here that instead of
just getting David, Michael, and Josh,
3358
03:09:01,500 --> 03:09:05,600
we also got all of the other employees
from the Employee table.
3359
03:09:05,600 --> 03:09:08,500
So, we got Jan, Angela, Kelly,
Stanley, Andy, and Jim.
3360
03:09:08,500 --> 03:09:11,800
All of the employees got included in the results, right?
3361
03:09:11,800 --> 03:09:14,600
Not just the employees who are branch managers.
3362
03:09:14,600 --> 03:09:20,300
So, when we just use an inner JOIN, only the
employees who are branch managers got included.
3363
03:09:20,300 --> 03:09:24,700
But when we used the LEFT JOIN,
all of the employees got included.
3364
03:09:24,700 --> 03:09:26,100
And here’s the reason why.
3365
03:09:26,100 --> 03:09:31,000
With the LEFT JOIN,
we include all of the rows from the left table.
3366
03:09:31,000 --> 03:09:34,900
So, in our case, the left table
is the table over here, right?
3367
03:09:34,900 --> 03:09:36,300
It’s the employee table.
3368
03:09:36,300 --> 03:09:40,400
And the left table is basically
the one that is included in the FROM statement.
3369
03:09:40,400 --> 03:09:45,300
So, whenever we use this LEFT JOIN,
that means all of the rows in the Employee table
3370
03:09:45,300 --> 03:09:46,800
are going to get included in the results.
3371
03:09:46,800 --> 03:09:50,600
But only the rows in the Branch table that matched
3372
03:09:50,600 --> 03:09:53,700
are going to get included
because the Branch table is like the right table.
3373
03:09:53,700 --> 03:09:57,000
There’s another type of JOIN we can use
which is called a RIGHT JOIN.
3374
03:09:57,000 --> 03:09:58,800
And so, I can just say RIGHT.
3375
03:09:58,800 --> 03:10:00,900
Now, this is going to do the opposite.
3376
03:10:00,900 --> 03:10:06,000
So, instead of including all of the rows
from the Employee table, no matter what,
3377
03:10:06,000 --> 03:10:09,700
now it’s going to include all of the rows
from the Branch table, no matter what.
3378
03:10:09,700 --> 03:10:11,100
So, I’m going to go ahead and run this.
3379
03:10:11,100 --> 03:10:15,400
And down here you'll see we have all of the rows
from the Branch table.
3380
03:10:15,400 --> 03:10:21,300
So, not only did we get the rows with had managers,
but we also got the rows which didn’t.
3381
03:10:21,300 --> 03:10:27,000
So, down here, remember, the Buffalo branch
which we just added didn’t have a manager.
3382
03:10:27,000 --> 03:10:29,500
So, it didn’t actually get linked to an employee.
3383
03:10:29,500 --> 03:10:31,700
So, therefore, both of these things were NULL.
3384
03:10:31,700 --> 03:10:34,200
So, in the LEFT JOIN, we got all of the employees.
3385
03:10:34,200 --> 03:10:36,400
In the RIGHT JOIN, we got all of the branches.
3386
03:10:36,400 --> 03:10:40,900
In other words, in the LEFT JOIN
we got all of the rows from the LEFT table.
3387
03:10:40,900 --> 03:10:44,200
In the RIGHT JOIN we got all the rows
from the RIGHT table.
3388
03:10:44,200 --> 03:10:48,300
So, in certain circumstances
you're going to want to do either one of those.
3389
03:10:48,300 --> 03:10:53,000
So, those are the three basic types of JOINs
that we can use in MySQL.
3390
03:10:53,000 --> 03:10:56,700
There’s actually a fourth type of JOIN
that I want to talk to you guys about.
3391
03:10:56,700 --> 03:11:01,800
Unfortunately, we can’t do it in MySQL
but it’s called a FULL OUTER JOIN.
3392
03:11:01,800 --> 03:11:07,900
And basically, in a FULL OUTER JOIN, it’s basically
a LEFT JOIN and a RIGHT JOIN combined.
3393
03:11:07,900 --> 03:11:11,600
So, remember, in the LEFT JOIN,
we grabbed all the employees
3394
03:11:11,600 --> 03:11:13,100
and the RIGHT JOIN
we grabbed all the branches.
3395
03:11:13,100 --> 03:11:16,900
In a FULL OUTER JOIN you would grab
all of the employees and all of the branches
3396
03:11:16,900 --> 03:11:21,200
no matter if they met this condition or not.
3397
03:11:21,200 --> 03:11:27,800
So, like I said, there’s not like – we can’t just come
over here and say like FULL JOIN in MySQL.
3398
03:11:27,800 --> 03:11:29,200
We're not going to be able to do that.
3399
03:11:29,200 --> 03:11:34,300
But I just wanted to let you guys know about it
so that you kind of have context for it.
3400
03:11:34,300 --> 03:11:38,000
So, these JOINs are really simple
and they're really easy.
3401
03:11:38,000 --> 03:11:40,900
And they can be really useful
if you want to combine information.
3402
03:11:40,900 --> 03:11:43,300
So, something as simple as getting the names
of all the branch managers
3403
03:11:43,300 --> 03:11:49,100
and what branches they manage
is really, really easy if we just use JOINs.
3404
03:11:49,100 --> 03:11:53,000
[Nested Queries]
3405
03:11:53,000 --> 03:11:56,400
In this tutorial I'm going to talk to you guys
about nested queries in SQL.
3406
03:11:56,400 --> 03:12:02,600
Now, nested query is basically a query where
we're going to be using multiple select statements
3407
03:12:02,600 --> 03:12:05,000
in order to get a specific piece of information.
3408
03:12:05,000 --> 03:12:08,700
So, a lot of times we're going to
want to get very specific information
3409
03:12:08,700 --> 03:12:12,200
and we're going to need to use
the results of one SELECT statement
3410
03:12:12,200 --> 03:12:16,100
to inform the results of another SELECT statement.
3411
03:12:16,100 --> 03:12:20,600
So, this is a little bit more advanced and this is kind of
when we're getting into more advanced query writing.
3412
03:12:20,600 --> 03:12:23,700
But I want to show you guys how this works
3413
03:12:23,700 --> 03:12:28,600
because a lot of information that you’re going to
want to get is going to involve using nested queries.
3414
03:12:28,600 --> 03:12:32,100
So, let’s go ahead and put a prompt up on the screen.
3415
03:12:32,100 --> 03:12:38,400
It says Find names of all employees
who have sold over $30,000 to a single client.
3416
03:12:38,400 --> 03:12:44,300
So, we want to get the names of the employees,
if they've sold more than 50k to a client.
3417
03:12:44,300 --> 03:12:47,400
So, the first thing I would do
if we were trying to figure this out,
3418
03:12:47,400 --> 03:12:51,900
figure out how to write this query
is let’s just look at the information that we have.
3419
03:12:51,900 --> 03:12:54,400
So, down here we have this Works With table.
3420
03:12:54,400 --> 03:12:57,900
And the Works With table has total sales, right?
3421
03:12:57,900 --> 03:13:01,700
And each one of these rows defines
how much a particular employee
3422
03:13:01,700 --> 03:13:03,800
has sold to a particular client, right?
3423
03:13:03,800 --> 03:13:08,700
So, employee 105 sold $55,000
to client 400, etc, right?
3424
03:13:08,700 --> 03:13:12,800
So, over here we have part
of the information, right?
3425
03:13:12,800 --> 03:13:15,500
In other words, here we have the total sales.
3426
03:13:15,500 --> 03:13:21,000
But what we don’t have is the employee’s first name
and they're last name, right?
3427
03:13:21,000 --> 03:13:22,800
We don’t have the actual employee’s name.
3428
03:13:22,800 --> 03:13:26,700
What we do have though is the ID
of the employee who did it, right?
3429
03:13:26,700 --> 03:13:28,600
So, we have the employee’s ID.
3430
03:13:28,600 --> 03:13:34,300
And we can use the employee’s ID
in order to get their first name and their last name.
3431
03:13:34,300 --> 03:13:38,400
So, in this case we had part of the data here
on the Works With table.
3432
03:13:38,400 --> 03:13:41,400
And we have part of the data up here
on the Employee table.
3433
03:13:41,400 --> 03:13:45,100
And this is a situation
where we can use a nested query.
3434
03:13:45,100 --> 03:13:47,800
So, the first thing I’m going to do
is I’m going to write a query
3435
03:13:47,800 --> 03:13:54,300
which is going to get me all of the employee ID’s
that have sold more than $30,000 to a single client.
3436
03:13:54,300 --> 03:13:56,800
All right, so we're going to start with Step 1,
3437
03:13:56,800 --> 03:14:01,700
which means we're getting all of the employee ID’s
from here if they’ve sold more than 30k.
3438
03:14:01,700 --> 03:14:04,200
So, let’s go ahead and write that query.
3439
03:14:04,200 --> 03:14:06,600
Shouldn't be too hard considering
all the stuff that we know.
3440
03:14:06,600 --> 03:14:14,200
So, I’m going to SELECT emp_id FROM works_with.
3441
03:14:14,200 --> 03:14:23,400
And I’m going to select it WHERE total_sales
is greater than 30,000.
3442
03:14:23,400 --> 03:14:25,300
And we'll go ahead and end this.
3443
03:14:25,300 --> 03:14:28,100
And actually, up here,
I’m just going to prefix this with the table name.
3444
03:14:28,100 --> 03:14:31,400
So, I’m going to say works_with.emp_id.
3445
03:14:31,400 --> 03:14:36,700
And then down here we'll say works_with.total_sales just so it’s more clear.
3446
03:14:36,700 --> 03:14:38,800
Especially when we get into nested queries
3447
03:14:38,800 --> 03:14:44,100
it’s usually useful to prefix everything with the table
name just in case we have repeated column names.
3448
03:14:44,100 --> 03:14:45,500
So, over here I’m going to run this.
3449
03:14:45,500 --> 03:14:49,800
And this should give us all the ID’s of the employees
who have sold more than 30,000.
3450
03:14:49,800 --> 03:14:53,300
So, you’ll see we get 102 and 105
shows up three times.
3451
03:14:53,300 --> 03:14:56,200
So, it looks like 105 has sold a lot of paper.
3452
03:14:56,200 --> 03:15:02,900
And so, now we have all of the ID’s of the employees
who have sold more than $30,000 worth of products.
3453
03:15:02,900 --> 03:15:08,000
And so, what we can do now is we can figure out
from this information,
3454
03:15:08,000 --> 03:15:10,700
we want to get those employee’s
first names and last name.
3455
03:15:10,700 --> 03:15:13,900
And so, I’m going to go ahead
and write another query up here.
3456
03:15:13,900 --> 03:15:15,300
I’m going to say SELECT.
3457
03:15:15,300 --> 03:15:19,000
And I’m going to say employee.first_name.
3458
03:15:19,000 --> 03:15:22,500
And why don’t we do employee.last_name.
3459
03:15:22,500 --> 03:15:26,000
And we're going to SELECT this FROM employee.
3460
03:15:26,000 --> 03:15:28,800
And then over here we're going to say WHERE.
3461
03:15:28,800 --> 03:15:31,900
And this is where we're going to go ahead
and use a nested query.
3462
03:15:31,900 --> 03:15:39,200
So, basically I want to select all of the employees
whose ID’s we got from this query right here.
3463
03:15:39,200 --> 03:15:44,500
So, what I could do is I could say
employee.emp_id IN –
3464
03:15:44,500 --> 03:15:49,200
and remember, the IN keyword is going to
give us a result if the employee ID
3465
03:15:49,200 --> 03:15:53,700
is in values that we specify
inside of these parenthesis.
3466
03:15:53,700 --> 03:15:58,000
So, what I could do is
I can actually nest this query inside of there.
3467
03:15:58,000 --> 03:16:03,700
So, I can go ahead and take this and I can just paste it
right in here in between these parenthesis.
3468
03:16:03,700 --> 03:16:06,900
And one thing you want to keep in mind
is just how this is formatted.
3469
03:16:06,900 --> 03:16:11,200
So, you’ll see I formatted this
and it’s indented from this query over here.
3470
03:16:11,200 --> 03:16:15,100
And then I’m going to get rid of this semicolon
and we'll put a semicolon over here.
3471
03:16:15,100 --> 03:16:17,400
So, basically what this is saying is I want to get
3472
03:16:17,400 --> 03:16:20,700
the first name and the last name
FROM the Employee table
3473
03:16:20,700 --> 03:16:26,200
where the employee ID is IN the result of this query.
3474
03:16:26,200 --> 03:16:29,900
So, if the employee ID got returned from this query,
3475
03:16:29,900 --> 03:16:34,800
which gave us the ID’s of all of the employees
who have sold over 30,000,
3476
03:16:34,800 --> 03:16:37,400
then we're going to return
their first name and last name.
3477
03:16:37,400 --> 03:16:38,900
So, let’s go ahead and run this.
3478
03:16:38,900 --> 03:16:43,000
And you'll see over here,
now we're getting the names of the two employees.
3479
03:16:43,000 --> 03:16:48,300
So, Michael Scott sold over 30,000
and Stanley Hudson also sold over 30,000.
3480
03:16:48,300 --> 03:16:53,400
So, that is actually a really cool way
where we can find out that information.
3481
03:16:53,400 --> 03:16:55,400
So, that’s how we can use a nested query, right?
3482
03:16:55,400 --> 03:16:57,700
A lot of times you'll use this IN keyword.
3483
03:16:57,700 --> 03:17:00,400
I’ll also show you some other examples
where we use other things.
3484
03:17:00,400 --> 03:17:05,700
But in that case, we're basically checking
to see if the employee ID is IN this result.
3485
03:17:05,700 --> 03:17:08,800
All right, so now I have another prompt here.
3486
03:17:08,800 --> 03:17:13,900
It says Find all clients who are handled
by the branch that Michael Scott manages.
3487
03:17:13,900 --> 03:17:15,800
So, this is another interesting one.
3488
03:17:15,800 --> 03:17:18,100
It says assume you know Michael’s ID.
3489
03:17:18,100 --> 03:17:20,300
So, we're going to assume that we know
what Michael Scott’s ID is.
3490
03:17:20,300 --> 03:17:24,800
This is another one where again,
we're going to need to grab data from one table
3491
03:17:24,800 --> 03:17:27,900
in order to inform the data from another table.
3492
03:17:27,900 --> 03:17:30,600
So, the first thing that we want to be able to do
3493
03:17:30,600 --> 03:17:34,700
is figure out the branch ID of the branch
that Michael Scott manages, right?
3494
03:17:34,700 --> 03:17:39,600
So, over here we have our branches
and each one has a manager ID, right?
3495
03:17:39,600 --> 03:17:44,500
And so, what we need to do is able to figure out
which of these branches Michael Scott manages.
3496
03:17:44,500 --> 03:17:51,600
Then once we have that information we can figure out
all of the clients that use that branch ID, right?
3497
03:17:51,600 --> 03:17:55,300
So, over here, the manager ID will map us
to Michael Scott.
3498
03:17:55,300 --> 03:17:59,800
And the branch ID will actually map us
to the Client table over here
3499
03:17:59,800 --> 03:18:02,400
because it has the branch ID as a foreign key.
3500
03:18:02,400 --> 03:18:06,100
So, the first thing we'll do is we'll figure out
what branch Michael Scott manages.
3501
03:18:06,100 --> 03:18:07,700
So, that should be easy enough.
3502
03:18:07,700 --> 03:18:09,200
We can just say SELECT.
3503
03:18:09,200 --> 03:18:18,400
And actually we'll just do the branch.branch_id
FROM branch WHERE.
3504
03:18:18,400 --> 03:18:21,700
And remember, we’re going to assume
that we know Michael Scott’s ID.
3505
03:18:21,700 --> 03:18:30,100
So, I can just say branch.branch_id is equal to –
and Michael Scott’s ID is 102.
3506
03:18:30,100 --> 03:18:32,800
So, I can just say is equal to 102.
3507
03:18:32,800 --> 03:18:36,900
And so, what this should do, is it should give us
the branch ID of the branch that he manages.
3508
03:18:36,900 --> 03:18:38,900
In this case – actually, whoops.
3509
03:18:38,900 --> 03:18:41,400
Instead of branch_id this needs to be mgr_id.
3510
03:18:41,400 --> 03:18:44,500
And this is going to give us 2, right?
3511
03:18:44,500 --> 03:18:50,600
Because 2 is the Scranton branch
which is the branch that Michael Scott manages.
3512
03:18:50,600 --> 03:18:52,200
So, now that we have this piece of information,
3513
03:18:52,200 --> 03:18:57,800
all we want to do is just get all of the clients
that are handled by that branch.
3514
03:18:57,800 --> 03:19:00,200
So, we can just say SELECT.
3515
03:19:00,200 --> 03:19:02,500
And why don’t we just get the client name?
3516
03:19:02,500 --> 03:19:10,200
So, it’ll say client.client_name FROM client WHERE.
3517
03:19:10,200 --> 03:19:18,800
And over here, we're basically just going to say
WHERE client.branch_id is equal to.
3518
03:19:18,800 --> 03:19:23,300
And over here we're going to set
an equal to the result of this query.
3519
03:19:23,300 --> 03:19:28,700
So, we're going to set it equal to the result of getting
the ID of the branch that Michael Scott manages.
3520
03:19:28,700 --> 03:19:31,300
So, down here, we'll put this statement
3521
03:19:31,300 --> 03:19:34,000
and you'll see, again,
I’m just embedding this in here.
3522
03:19:34,000 --> 03:19:37,300
So, what’s going to happen is when
the relational database management system
3523
03:19:37,300 --> 03:19:41,800
sees an embedded SQL statement like this,
it’s going to execute this first.
3524
03:19:41,800 --> 03:19:44,600
And then it’s going to execute the outer one.
3525
03:19:44,600 --> 03:19:47,300
So, it starts inner and then it slowly goes outer.
3526
03:19:47,300 --> 03:19:53,000
So, we'll be able to get the branch ID
where Michael Scott is the manager.
3527
03:19:53,000 --> 03:19:56,300
And then we can use that information
to find all of the clients.
3528
03:19:56,300 --> 03:19:57,900
So, over here, I’m just going to click Run.
3529
03:19:57,900 --> 03:20:00,600
And I’m going to go ahead
and get rid of this semicolon right here.
3530
03:20:00,600 --> 03:20:03,100
And now we can go ahead and run this.
3531
03:20:03,100 --> 03:20:04,700
And you'll see we get all of these clients.
3532
03:20:04,700 --> 03:20:08,200
So, we get like Dunmore Highschool, Lackawana
County, Scranton White pages, and FedEx.
3533
03:20:08,200 --> 03:20:11,200
So, those are all the clients that are managed
by the Scranton branch.
3534
03:20:11,200 --> 03:20:13,500
Now, there is one more thing I want to point out
3535
03:20:13,500 --> 03:20:18,000
which is you’ll notice down here we're setting
client.branch_id equal to.
3536
03:20:18,000 --> 03:20:22,100
We're checking to see
if it’s equal to the result of this statement.
3537
03:20:22,100 --> 03:20:27,800
But here’s the problem, is this statement isn’t
necessarily guaranteed to only return one value.
3538
03:20:27,800 --> 03:20:32,900
So, if this – so, let’s say that Michael Scott
was the manager at like multiple branches,
3539
03:20:32,900 --> 03:20:35,600
it’s possible that this would return multiple values.
3540
03:20:35,600 --> 03:20:38,900
So, what we want to do is just come down here
and say, LIMIT 1.
3541
03:20:38,900 --> 03:20:42,100
And that’ll make sure that we only get 1 of these.
3542
03:20:42,100 --> 03:20:45,200
So, now if I click Run, you'll see it does the same thing.
3543
03:20:45,200 --> 03:20:48,900
Although, now we're just making sure
that we only have 1.
3544
03:20:48,900 --> 03:20:51,400
So, anytime you're using something like a quality,
3545
03:20:51,400 --> 03:20:55,700
it’s always a good idea to limit it down to 1
unless you’re looking for a situation
3546
03:20:55,700 --> 03:20:59,300
where Michael Scott is going to
be managing multiple branches.
3547
03:20:59,300 --> 03:21:01,200
In which case, we can use IN instead.
3548
03:21:01,200 --> 03:21:04,900
All right, so that’s kind of a little dip
into nested queries.
3549
03:21:04,900 --> 03:21:07,500
Now, obviously these can get very complex.
3550
03:21:07,500 --> 03:21:12,800
And really, you know, what’s important is that you
have a solid understanding of the fundamentals.
3551
03:21:12,800 --> 03:21:18,000
If you don’t understand the fundamentals that we've
kind of talked about up to this point in this video,
3552
03:21:18,000 --> 03:21:21,200
then using nested queries is really going to
confuse the crap out of you.
3553
03:21:21,200 --> 03:21:25,700
All nested queries are is it’s just kind of like
one query informing another query,
3554
03:21:25,700 --> 03:21:27,800
maybe informing another query, right?
3555
03:21:27,800 --> 03:21:32,500
We just use the results from one query
to get results from another query, etc.
3556
03:21:32,500 --> 03:21:38,000
And as long as you can break the nested query
up into its individual parts,
3557
03:21:38,000 --> 03:21:40,900
you should have absolutely no problem writing these.
3558
03:21:40,900 --> 03:21:46,000
And really, the best way to get good at writing more
complex queries like this is just to practice.
3559
03:21:46,000 --> 03:21:48,900
So, the more you practice writing nested queries
3560
03:21:48,900 --> 03:21:52,200
and using all these things in combination,
the better you’re going to get at it.
3561
03:21:52,200 --> 03:21:56,000
[On Delete]
3562
03:21:56,000 --> 03:21:59,900
In this tutorial, I’m going to talk to you guys
about deleting entries in the database
3563
03:21:59,900 --> 03:22:03,600
when they have foreign keys associated to them.
3564
03:22:03,600 --> 03:22:07,300
So, this is actually a pretty interesting topic.
3565
03:22:07,300 --> 03:22:12,200
And over here in our company database we have
a pretty complex database schema, right?
3566
03:22:12,200 --> 03:22:17,300
We have all sorts of foreign keys
that are linking between all sorts of places.
3567
03:22:17,300 --> 03:22:19,600
And I want to pose to you guys a scenario.
3568
03:22:19,600 --> 03:22:24,300
So, imagine over here I have my Branch table
and I have my Employee table.
3569
03:22:24,300 --> 03:22:30,000
So, imagine that I came over here in my Employee
table and I deleted one of the employees, right?
3570
03:22:30,000 --> 03:22:32,900
So, let’s say that I deleted Michael Scott.
3571
03:22:32,900 --> 03:22:36,000
So, over here we have this employee, Michael Scott.
3572
03:22:36,000 --> 03:22:38,000
And his branch ID is 2.
3573
03:22:38,000 --> 03:22:44,100
So, Michael Scott has a foreign key here defined,
which is branch_id, right?
3574
03:22:44,100 --> 03:22:46,800
So, branch_id, Michael Scott’s branch ID is 2
3575
03:22:46,800 --> 03:22:52,900
which means that Michael Scott belongs to the
Scranton branch which is right down there, right?
3576
03:22:52,900 --> 03:22:54,800
But let’s pose something.
3577
03:22:54,800 --> 03:22:59,300
Like imagine that we were to delete
Michael Scott from the database, right?
3578
03:22:59,300 --> 03:23:04,600
Well, what’s going to happen this manager ID
down here?
3579
03:23:04,600 --> 03:23:09,900
So, if we delete Michael Scott,
we delete the employee with ID 102.
3580
03:23:09,900 --> 03:23:11,800
What’s going to happen to the manager ID?
3581
03:23:11,800 --> 03:23:18,300
The manager ID is supposed to be linking us
to an actual row in the Employee table.
3582
03:23:18,300 --> 03:23:23,400
But if we delete Michael Scott, then all of a sudden
102, that doesn’t mean anything, right?
3583
03:23:23,400 --> 03:23:24,900
Because Michael Scott is gone.
3584
03:23:24,900 --> 03:23:29,400
His employee ID is no longer inside of
our Employee table.
3585
03:23:29,400 --> 03:23:32,900
And this is what I’m going to talk to you guys
about today,
3586
03:23:32,900 --> 03:23:37,400
which is different things that we can do
in order to handle this situation.
3587
03:23:37,400 --> 03:23:40,400
So, specifically, I’m going to talk to you guys
about two things.
3588
03:23:40,400 --> 03:23:42,100
One is called ON DELETE SET NULL.
3589
03:23:42,100 --> 03:23:44,700
And the other is called ON DELETE CASCADE.
3590
03:23:44,700 --> 03:23:50,000
So, there’s really two things that we can do
when this situation occurs.
3591
03:23:50,000 --> 03:23:52,900
And so, the first thing would be ON DELETE SET NULL.
3592
03:23:52,900 --> 03:23:58,700
And ON DELETE SET NULL is basically
where if we delete one of these employees,
3593
03:23:58,700 --> 03:24:05,200
that means that the manager ID that was associated
to that employee is going to get set to NULL.
3594
03:24:05,200 --> 03:24:10,300
ON DELETE CASCADE is essentially
where if we delete the employee
3595
03:24:10,300 --> 03:24:13,000
whose ID is stored in the manager ID column,
3596
03:24:13,000 --> 03:24:16,700
then we're just going to delete
this entire row in the database.
3597
03:24:16,700 --> 03:24:20,800
So, I’m going to go ahead
and show you guys basically how this works.
3598
03:24:20,800 --> 03:24:25,700
So, over here I actually have the code
for creating this branch table.
3599
03:24:25,700 --> 03:24:28,700
And this is the code that we used
in one of the previous videos
3600
03:24:28,700 --> 03:24:31,600
when I was showing you guys
how to create this database.
3601
03:24:31,600 --> 03:24:34,300
You'll notice over here on the Branch table it says
3602
03:24:34,300 --> 03:24:40,000
FOREIGN KEY (mgr_id) REFERENCES
employee(emp_id).
3603
03:24:40,000 --> 03:24:43,500
And over here I said ON DELETE SET NULL.
3604
03:24:43,500 --> 03:24:49,800
Basically, what I’m saying here is that if
the employee ID in the Employee table gets deleted,
3605
03:24:49,800 --> 03:24:53,700
I want to set the manager ID equal to NULL.
3606
03:24:53,700 --> 03:24:56,400
And so, let me show you guys
how this is going to work.
3607
03:24:56,400 --> 03:24:59,900
So, over here I’m going to go ahead
and delete Michael Scott from the database
3608
03:24:59,900 --> 03:25:01,900
and we'll see what happens.
3609
03:25:01,900 --> 03:25:10,900
And so, I’m just going to go ahead and type out
DELETE FROM employee WHERE emp_id is equal to –
3610
03:25:10,900 --> 03:25:14,400
and we're just going to put Michael Scott’s
employee ID which is 102.
3611
03:25:14,400 --> 03:25:15,800
And I’m going to go ahead and run this.
3612
03:25:15,800 --> 03:25:18,900
And you'll see over here it says 1 rows affected.
3613
03:25:18,900 --> 03:25:23,000
But I want to show you guys
what happened inside of the branch.
3614
03:25:23,000 --> 03:25:26,300
So, I’m just going to SELECT ALL from branch.
3615
03:25:26,300 --> 03:25:28,500
And let’s go ahead and run this.
3616
03:25:28,500 --> 03:25:34,000
You'll see down here the manager ID
is now set to NULL.
3617
03:25:34,000 --> 03:25:38,700
And that’s because over here –
because we deleted Michael Scott, right?
3618
03:25:38,700 --> 03:25:42,400
So, we deleted the Michael Scott,
the 102 entry in the employee table.
3619
03:25:42,400 --> 03:25:48,800
And so, now the manager ID which was storing that as
a foreign key is just going to be set equal to NULL.
3620
03:25:48,800 --> 03:25:51,600
And that’s because that’s what we defined up here.
3621
03:25:51,600 --> 03:25:56,500
So, in a situation like that,
because we said ON DELETE SET NULL,
3622
03:25:56,500 --> 03:25:59,400
now that entry is just going to be equal to NULL.
3623
03:25:59,400 --> 03:26:03,100
And the same thing actually happened
inside of the Employee table.
3624
03:26:03,100 --> 03:26:10,300
So, if I was to SELECT ALL from
the Employee table and I ran this,
3625
03:26:10,300 --> 03:26:14,800
you'll see now that a lot of these supervisor ID’s
are also set equal to NULL.
3626
03:26:14,800 --> 03:26:18,900
And if you remember back to
when we created the company database,
3627
03:26:18,900 --> 03:26:26,200
when we created the employee table, the super_id
also had ON DELETE SET NULL associated to it.
3628
03:26:26,200 --> 03:26:29,900
And so, that’s why when we deleted Michael Scott,
all of the employees,
3629
03:26:29,900 --> 03:26:34,600
namely these three employees right here
who had Michael Scott as their supervisor,
3630
03:26:34,600 --> 03:26:38,300
you can see all these employees were
at branch number 2.
3631
03:26:38,300 --> 03:26:42,600
All of their super ID’s ended up getting set equal to
NULL because we had ON DELETE SET NULL there.
3632
03:26:42,600 --> 03:26:45,300
So, that is ON DELETE SET NULL.
3633
03:26:45,300 --> 03:26:46,900
And that’s basically how that works.
3634
03:26:46,900 --> 03:26:49,800
I want to show you guys also
how ON DELETE CASCADE works.
3635
03:26:49,800 --> 03:26:51,800
So, we have this Branch Supplier table.
3636
03:26:51,800 --> 03:26:55,300
And the Branch Supplier table also
had a foreign key like this,
3637
03:26:55,300 --> 03:26:59,200
but instead of saying ON DELETE SET NULL,
we said ON DELETE CASCADE.
3638
03:26:59,200 --> 03:27:05,500
And when we use ON DELETE CASCADE,
what that means is that if the branch ID
3639
03:27:05,500 --> 03:27:09,800
that’s stored as the foreign key
in the Branch Supplier table gets deleted,
3640
03:27:09,800 --> 03:27:13,700
then we're just going to delete the entire row
in the database.
3641
03:27:13,700 --> 03:27:19,000
So, down here in Branch Supplier,
you'll see that I have all of this stuff, right?
3642
03:27:19,000 --> 03:27:24,800
So, I have like Hammer Mill supplies paper
to branch_id 2.
3643
03:27:24,800 --> 03:27:28,500
Or Uni-ball supplies writing utensils
to branch_id number 2.
3644
03:27:28,500 --> 03:27:34,600
If I was to delete Branch 2, in other words,
if I was to delete the branch that had an ID of 2,
3645
03:27:34,600 --> 03:27:39,500
then all of the rows that had branch_id 2 here
would just get deleted.
3646
03:27:39,500 --> 03:27:41,300
So, I’m going to show you guys how that works.
3647
03:27:41,300 --> 03:27:44,200
So, over here, we're going to go ahead and do that.
3648
03:27:44,200 --> 03:27:50,600
So, we're just going to DELETE FROM branch
WHERE branch_id is equal to 2.
3649
03:27:50,600 --> 03:27:54,200
And so, when I go ahead and delete this,
what’s going to happen
3650
03:27:54,200 --> 03:28:00,000
is all of those branch supplier rows are going to
get deleted that had 2 as their foreign key.
3651
03:28:00,000 --> 03:28:04,100
So, now I’ll just SELECT ALL from branch_supplier.
3652
03:28:04,100 --> 03:28:11,200
And when I run this query, you'll notice
that there’s no longer any branch ID’s 2 in here.
3653
03:28:11,200 --> 03:28:16,800
In other words, we got rid of all of the suppliers
that supplied Branch 2 when we deleted Branch 2.
3654
03:28:16,800 --> 03:28:19,400
And that what’s ON DELETE CASCADE
is going to do for us.
3655
03:28:19,400 --> 03:28:23,500
Instead of just setting those equal to NULL,
it’s going to go ahead and just delete them entirely.
3656
03:28:23,500 --> 03:28:26,600
So, now that we kind of understand
the difference between those two,
3657
03:28:26,600 --> 03:28:29,500
I want to talk to you guys about the different
situations where we might use them.
3658
03:28:29,500 --> 03:28:36,000
And actually, the Branch Supplier table
and the Branch table are actually really good examples.
3659
03:28:36,000 --> 03:28:39,900
So, in the Branch table we used ON DELETE SET NULL.
3660
03:28:39,900 --> 03:28:43,800
And it was okay for us to use ON DELETE SET NULL
3661
03:28:43,800 --> 03:28:48,900
because the manager ID on the Branch table
is just a foreign key.
3662
03:28:48,900 --> 03:28:51,100
It’s not actually a primary key.
3663
03:28:51,100 --> 03:28:57,100
And so, the manager ID isn’t like
absolutely essential for the Branch Table.
3664
03:28:57,100 --> 03:29:02,000
However, if we look down here
in the Branch Supplier table,
3665
03:29:02,000 --> 03:29:08,300
you'll notice that the branch_id, in other words,
the foreign key here is also part of the primary key.
3666
03:29:08,300 --> 03:29:11,800
Which means the branch ID
on the Branch Supplier table
3667
03:29:11,800 --> 03:29:16,100
is absolutely crucial for this row
in the database, right?
3668
03:29:16,100 --> 03:29:20,400
And so, if the branch ID here,
if this branch disappears,
3669
03:29:20,400 --> 03:29:27,000
we can’t set this to NULL because
a primary key can’t have a NULL value, right?
3670
03:29:27,000 --> 03:29:28,800
And so, this can’t be NULL.
3671
03:29:28,800 --> 03:29:30,900
You have to just delete the entire thing.
3672
03:29:30,900 --> 03:29:35,400
And so, that’s why we use ON DELETE CASCADE
as opposed to ON DELETE SET NULL.
3673
03:29:35,400 --> 03:29:39,500
And honestly, you know,
it’s really up to you which one you want to use.
3674
03:29:39,500 --> 03:29:42,700
But just know that if you have a situation
like Branch Supplier,
3675
03:29:42,700 --> 03:29:47,800
where a foreign key is also a primary key
or also a component of a primary key,
3676
03:29:47,800 --> 03:29:53,400
then it always has to be ON DELETE CASCADE
otherwise you're going to run into trouble.
3677
03:29:53,400 --> 03:29:56,600
So, that’s the basics of those different ON DELETEs.
3678
03:29:56,600 --> 03:29:58,900
So, ON DELETE SET NULL and ON DELETE CASCADE.
3679
03:29:58,900 --> 03:30:04,600
And both of those are extremely useful when we're
defining foreign key relationships between tables.
3680
03:30:04,600 --> 03:30:08,100
[Triggers]
3681
03:30:08,100 --> 03:30:12,400
In this tutorial I’m going to talk to you guys
about using triggers in SQL and MySQL.
3682
03:30:12,400 --> 03:30:18,500
Well, a trigger is basically a block of SQL code
which we can write
3683
03:30:18,500 --> 03:30:22,600
which will define a certain action that should happen
3684
03:30:22,600 --> 03:30:26,500
when a certain operation gets performed
on the database.
3685
03:30:26,500 --> 03:30:30,800
So, I could write a trigger which would
basically tell MySQL to do something
3686
03:30:30,800 --> 03:30:36,300
when like an entry was added
into a particular table in the database.
3687
03:30:36,300 --> 03:30:38,600
Or when something was deleted
from a database table.
3688
03:30:38,600 --> 03:30:43,900
And basically I can say like Hey, anytime, you know,
a row gets deleted from this table,
3689
03:30:43,900 --> 03:30:46,300
I want you to like insert something
into something else.
3690
03:30:46,300 --> 03:30:50,600
So, triggers can be extremely
powerful and extremely useful.
3691
03:30:50,600 --> 03:30:53,300
So, I'm going to show you guys
basically how they work
3692
03:30:53,300 --> 03:30:55,400
and we'll just talk about like
setting them up and everything.
3693
03:30:55,400 --> 03:31:00,700
So, the first thing we have to do, at least to follow
along with this tutorial is we're going to create a table.
3694
03:31:00,700 --> 03:31:02,800
And you don’t have to create this table.
3695
03:31:02,800 --> 03:31:05,200
I’m just doing this so we can illustrate
what’s happening.
3696
03:31:05,200 --> 03:31:07,700
But this is not necessary for triggers.
3697
03:31:07,700 --> 03:31:09,900
But I’m creating a table called trigger_test.
3698
03:31:09,900 --> 03:31:13,800
And it’s just going to have one column
which is just going to be a message.
3699
03:31:13,800 --> 03:31:15,700
And I’m just going to go ahead and create this.
3700
03:31:15,700 --> 03:31:18,500
So, we're creating this table trigger test.
3701
03:31:18,500 --> 03:31:22,200
And now what we can do
is we can start writing out some triggers.
3702
03:31:22,200 --> 03:31:28,500
Now, when we're using MySQL – and up to this point
in this course we've been using this program PopSQL
3703
03:31:28,500 --> 03:31:33,300
which is actually an awesome program for, you know,
writing out different SQL commands.
3704
03:31:33,300 --> 03:31:36,900
And it’s been really great because it’s, you know,
a bit easy for us to visual stuff.
3705
03:31:36,900 --> 03:31:39,000
But when we're going to write triggers,
3706
03:31:39,000 --> 03:31:42,900
we're going to have to define
the triggers over here in the command line.
3707
03:31:42,900 --> 03:31:47,600
And that’s just because
there’s one special thing that we have to do
3708
03:31:47,600 --> 03:31:52,100
which is change the SQL delimiter
that we're going to use.
3709
03:31:52,100 --> 03:31:53,700
And I’ll talk to you guys about that in a second.
3710
03:31:53,700 --> 03:31:56,800
But in order to do that, we're going to
have to do it inside of the terminal.
3711
03:31:56,800 --> 03:32:03,100
So, if you're on Windows you can just go down
and open up the MySQL Command Line Client.
3712
03:32:03,100 --> 03:32:05,000
So, it’s this guy right here.
3713
03:32:05,000 --> 03:32:06,200
That’s what I have open.
3714
03:32:06,200 --> 03:32:07,900
It might ask you to log in.
3715
03:32:07,900 --> 03:32:12,600
If you're on the OSX and you're using Terminal,
you can just type in –
3716
03:32:12,600 --> 03:32:18,900
if you just type in MySQL -u root -p.
3717
03:32:18,900 --> 03:32:22,800
And then hit Enter, it should prompt you for your
password and then you should be logged in.
3718
03:32:22,800 --> 03:32:25,900
And so, that’s how you can
get to this screen over here.
3719
03:32:25,900 --> 03:32:28,500
And then once we're in here
we're going to want to use the database.
3720
03:32:28,500 --> 03:32:30,600
So, I’m just going to say use giraffe.
3721
03:32:30,600 --> 03:32:35,100
And giraffe is the name of the database
that I created like in the first tutorial.
3722
03:32:35,100 --> 03:32:38,400
So, whatever the database you created was,
you can just use that.
3723
03:32:38,400 --> 03:32:41,400
And then over here, so once we have all that set up,
3724
03:32:41,400 --> 03:32:43,700
now we're ready to go
and start creating these triggers.
3725
03:32:43,700 --> 03:32:50,400
So, I need to actually execute the trigger code
over here inside of the command line,
3726
03:32:50,400 --> 03:32:54,700
but we can actually just write it out over here inside
PopSQL so it’s a little bit easier to see.
3727
03:32:54,700 --> 03:32:58,800
I’m actually just going to show you guys
some different triggers
3728
03:32:58,800 --> 03:33:00,500
and then I’ll kind of talk to you about it.
3729
03:33:00,500 --> 03:33:02,900
So, I’m going to go ahead and paste one over here.
3730
03:33:02,900 --> 03:33:06,600
And this is actually a very simple trigger.
3731
03:33:06,600 --> 03:33:09,800
So, the trigger is actually right here,
what I have highlighted.
3732
03:33:09,800 --> 03:33:12,200
And then you'll see over here, I’m saying DELIMITER.
3733
03:33:12,200 --> 03:33:15,100
So, I'm going to talk to you guys about the trigger first
3734
03:33:15,100 --> 03:33:17,800
and then I’ll talk to you guys
about what that delimiter is doing.
3735
03:33:17,800 --> 03:33:21,700
So, we can basically create a trigger
by saying CREATE and then I can say TRIGGER.
3736
03:33:21,700 --> 03:33:22,600
We're going to give this a name.
3737
03:33:22,600 --> 03:33:24,200
I’m just going to call it my_trigger.
3738
03:33:24,200 --> 03:33:31,200
And I can say BEFORE INSERT ON employee,
FOR EACH ROW BEGIN INSERT INTO trigger_test.
3739
03:33:31,200 --> 03:33:32,600
So, what does all this mean?
3740
03:33:32,600 --> 03:33:34,100
Basically, I’m defining my trigger.
3741
03:33:34,100 --> 03:33:35,100
I’m giving it a name.
3742
03:33:35,100 --> 03:33:40,200
And I’m saying that before something
gets inserted on the Employee table,
3743
03:33:40,200 --> 03:33:44,900
so before anything, you know, any new items
gets inserted on the Employee table,
3744
03:33:44,900 --> 03:33:49,400
for each of the new items that are getting inserted,
3745
03:33:49,400 --> 03:33:54,900
I want to insert into the trigger test table
the values ‘added new employee’.
3746
03:33:54,900 --> 03:33:58,400
So, basically what happens
is when I define this trigger,
3747
03:33:58,400 --> 03:34:02,700
that means that before anything gets
inserted on the employee table now,
3748
03:34:02,700 --> 03:34:06,200
I’m going to go ahead
and preform whatever is down here.
3749
03:34:06,200 --> 03:34:10,300
And in our case, I’m just inserting into trigger tests,
the values, ‘added new employee’.
3750
03:34:10,300 --> 03:34:11,500
So, that’s basically all it is.
3751
03:34:11,500 --> 03:34:17,000
We're basically configuring MySQL to insert
a value into the Trigger Test table
3752
03:34:17,000 --> 03:34:20,700
whenever a value gets inserted
into the Employee table.
3753
03:34:20,700 --> 03:34:24,600
And this can be really useful
because it automates things, right?
3754
03:34:24,600 --> 03:34:30,600
I can automate something that happens every time
a record gets inserted into the employee table.
3755
03:34:30,600 --> 03:34:33,400
Now, over here we have these little DELIMITERs.
3756
03:34:33,400 --> 03:34:36,100
And this DELIMITER is actually
a special keyword in MySQL.
3757
03:34:36,100 --> 03:34:39,800
What this will do is it’ll change the MySQL delimiter.
3758
03:34:39,800 --> 03:34:43,900
So, normally, the MySQL delimiter
is a semicolon, right?
3759
03:34:43,900 --> 03:34:47,100
So, if I said like SELECT ALL from employee.
3760
03:34:47,100 --> 03:34:49,200
I would end this off with a semicolon.
3761
03:34:49,200 --> 03:34:51,000
That’s the delimiter, right?
3762
03:34:51,000 --> 03:34:53,100
That delimits the different SQL commands.
3763
03:34:53,100 --> 03:34:57,000
But when we're writing these triggers out,
you'll notice that over here
3764
03:34:57,000 --> 03:35:02,300
inside of these FOR EACH and this END
I have to use this semicolon over here.
3765
03:35:02,300 --> 03:35:08,000
And so, because I’m using the semicolon
to end off this SQL command right here,
3766
03:35:08,000 --> 03:35:14,800
I can’t actually use that same delimiter
in order to end off the trigger creation.
3767
03:35:14,800 --> 03:35:19,500
So, you have to put the semicolon here
in order for this to work.
3768
03:35:19,500 --> 03:35:24,800
But if I don’t change the delimiter,
then this semicolon is basically going to tell SQL
3769
03:35:24,800 --> 03:35:27,700
that we're done creating the trigger,
even though we're clearly not.
3770
03:35:27,700 --> 03:35:31,800
And so, what I’m doing up here
is I’m changing the delimiter to two dollar signs.
3771
03:35:31,800 --> 03:35:35,000
So, basically now instead of the delimiter
being a semicolon,
3772
03:35:35,000 --> 03:35:36,700
the delimiter is going to be two dollar signs.
3773
03:35:36,700 --> 03:35:41,200
And you'll see, I create the trigger
and then I'm using the two dollar signs
3774
03:35:41,200 --> 03:35:43,800
to delineate that the trigger is done being created.
3775
03:35:43,800 --> 03:35:48,100
And then I can just delinear back to a semicolon.
3776
03:35:48,100 --> 03:35:51,400
Now, the reason that I have to
do this over here in the terminal
3777
03:35:51,400 --> 03:35:55,100
is because in PopSQL you can’t
actually configure the delimiter.
3778
03:35:55,100 --> 03:36:00,600
So, the delimiter is actually something
that’s defined not on the like text editor level.
3779
03:36:00,600 --> 03:36:01,800
It’s defined like over here.
3780
03:36:01,800 --> 03:36:04,500
So, basically we have to execute this over there.
3781
03:36:04,500 --> 03:36:10,000
So, what I’m going to do now is I’m actually going
to execute all of these pieces of SQL code over here.
3782
03:36:10,000 --> 03:36:11,800
So, I’m just going to change the delimiter.
3783
03:36:11,800 --> 03:36:13,000
So, I'm going to paste this in.
3784
03:36:13,000 --> 03:36:14,000
I'll hit Enter.
3785
03:36:14,000 --> 03:36:18,900
And now I’m going to paste in the actually part
where I’m creating the trigger.
3786
03:36:18,900 --> 03:36:21,000
So, over here we'll paste this.
3787
03:36:21,000 --> 03:36:22,800
And I’m just going to hit Enter.
3788
03:36:22,800 --> 03:36:25,600
And then finally, we're going to change
the DELIMITER back.
3789
03:36:25,600 --> 03:36:27,900
So, I’m going to change this back to a semicolon.
3790
03:36:27,900 --> 03:36:31,600
So, hopefully now this trigger is all set up
inside of MySQL.
3791
03:36:31,600 --> 03:36:35,700
So, one thing we can do to test it
is just to add in another employee.
3792
03:36:35,700 --> 03:36:40,400
So, I'm going to go ahead
and add another employee into the Employees table.
3793
03:36:40,400 --> 03:36:43,000
So, we're going to add in Oscar Martinez.
3794
03:36:43,000 --> 03:36:45,900
And let’s go ahead and do that.
3795
03:36:45,900 --> 03:36:47,500
And so, we added in Oscar.
3796
03:36:47,500 --> 03:36:52,400
And now what I’m going to do is SELECT
from the Trigger Test table.
3797
03:36:52,400 --> 03:36:55,900
So, assuming our trigger got set up correctly,
3798
03:36:55,900 --> 03:36:59,200
when we inserted an employee
into the Employee table,
3799
03:36:59,200 --> 03:37:04,100
it should have also inserted something
into Trigger Test that said Added New Employee.
3800
03:37:04,100 --> 03:37:07,600
So, let’s go ahead and run this SELECT statement
and we'll see what happens.
3801
03:37:07,600 --> 03:37:11,100
So, you'll see down here we get a message that says
added new employee.
3802
03:37:11,100 --> 03:37:12,300
So, it looks like it worked, right?
3803
03:37:12,300 --> 03:37:17,800
The trigger got set up correctly and therefore when
we inserted something into the Employee table
3804
03:37:17,800 --> 03:37:22,500
we actually ended up updating the Trigger Test table
with a new entry as well.
3805
03:37:22,500 --> 03:37:26,800
And so, that is basically how we can use
triggers to do something like that.
3806
03:37:26,800 --> 03:37:30,100
So, I want to show you guys
a couple other things we can do with triggers.
3807
03:37:30,100 --> 03:37:32,800
I’ll show you guys another one right now.
3808
03:37:32,800 --> 03:37:36,300
I’m actually going to, again, paste it
and then we'll kind of talk about it.
3809
03:37:36,300 --> 03:37:40,300
So, this one is actually very similar
to the one which just made.
3810
03:37:40,300 --> 03:37:45,100
But instead of over here,
saying like added new employee.
3811
03:37:45,100 --> 03:37:48,500
Instead, I'm saying NEW.first_name.
3812
03:37:48,500 --> 03:37:50,300
And so, what this is allowing me to do
3813
03:37:50,300 --> 03:37:55,900
is it’s actually allowing me to access a particular
attribute about the thing that we just inserted.
3814
03:37:55,900 --> 03:37:59,100
So, again, we're inserting something
on the employee table.
3815
03:37:59,100 --> 03:38:02,200
NEW is going to refer to the row
that’s getting inserted.
3816
03:38:02,200 --> 03:38:05,700
And then I can access specific columns from that row.
3817
03:38:05,700 --> 03:38:11,200
So, NEW.first_name will give me the first name
of the employee that’s getting inserted.
3818
03:38:11,200 --> 03:38:16,300
So, now if I was to come down here and
I’m actually just going to insert another employee.
3819
03:38:16,300 --> 03:38:18,800
So, we're going to insert Kevin Malone.
3820
03:38:18,800 --> 03:38:20,800
And let’s go ahead and do that.
3821
03:38:20,800 --> 03:38:21,900
And actually, whoops.
3822
03:38:21,900 --> 03:38:24,300
I have to update the trigger over here.
3823
03:38:24,300 --> 03:38:26,400
So, once again, I’m going to do the same thing.
3824
03:38:26,400 --> 03:38:30,900
I’m just going to paste in all of this code over here
on the command line.
3825
03:38:30,900 --> 03:38:32,300
So, we'll paste in the trigger.
3826
03:38:32,300 --> 03:38:35,000
And actually need to change the name
on this real quick.
3827
03:38:35,000 --> 03:38:38,600
So, we'll say my_trigger1
is what we're going to call that.
3828
03:38:38,600 --> 03:38:40,300
And that’s going to go ahead.
3829
03:38:40,300 --> 03:38:44,400
And then we'll change the delimiter
back to a semicolon.
3830
03:38:44,400 --> 03:38:48,100
All right, so now let’s go ahead
and add in our Kevin Malone employee.
3831
03:38:48,100 --> 03:38:49,300
So, I’m going to run this.
3832
03:38:49,300 --> 03:38:50,500
So, we added Kevin.
3833
03:38:50,500 --> 03:38:52,600
Now if we SELECT ALL from Trigger Test,
3834
03:38:52,600 --> 03:38:57,900
you'll see down here not only did we add
a new employee, it says added new employee.
3835
03:38:57,900 --> 03:38:59,700
That was that first trigger that we set up.
3836
03:38:59,700 --> 03:39:02,800
But we also added the employees name
which was Kevin, right?
3837
03:39:02,800 --> 03:39:06,700
So, we were able to grab a specific piece
of information from the new row
3838
03:39:06,700 --> 03:39:09,800
that got inserted and that’s going
to show up down there.
3839
03:39:09,800 --> 03:39:13,400
All right, so there’s one more thing
I want to show you with these triggers.
3840
03:39:13,400 --> 03:39:15,900
And it’s actually going to be a more complex trigger.
3841
03:39:15,900 --> 03:39:18,900
So, this is how we can use conditionals.
3842
03:39:18,900 --> 03:39:21,800
So, I can use something like IF, ELSEIF, and ELSE.
3843
03:39:21,800 --> 03:39:26,300
So, over here we have this trigger.
3844
03:39:26,300 --> 03:39:29,800
So, it’s basically the same exact thing
as we did before.
3845
03:39:29,800 --> 03:39:32,800
TRIGGER my_trigger BEFORE INSERT ON employee.
3846
03:39:32,800 --> 03:39:34,100
And then for each row.
3847
03:39:34,100 --> 03:39:35,700
This time we're using an IF statement.
3848
03:39:35,700 --> 03:39:39,200
So, I’m saying IF NEW.sex is equal to male,
3849
03:39:39,200 --> 03:39:43,100
THEN INSERT INTO trigger_test VALUES
added male employee.
3850
03:39:43,100 --> 03:39:48,800
ELSEIF NEW.sex is equal to F, INSERT
INTO trigger_test added female.
3851
03:39:48,800 --> 03:39:52,500
ELSE INSERT INTO trigger_test,
added other employee.
3852
03:39:52,500 --> 03:39:53,500
So, we're using IF ELSE logic.
3853
03:39:53,500 --> 03:39:57,800
And basically, it’s just if this condition up here is true,
then we do this.
3854
03:39:57,800 --> 03:39:59,900
Otherwise, we check this condition.
3855
03:39:59,900 --> 03:40:01,100
If that’s true, we do this.
3856
03:40:01,100 --> 03:40:02,300
Otherwise, we do that.
3857
03:40:02,300 --> 03:40:06,000
So, if you’ve ever programmed before,
then you're probably familiar with an IF statement.
3858
03:40:06,000 --> 03:40:12,100
So, this is a very special type of trigger
because we're using conditionals.
3859
03:40:12,100 --> 03:40:17,500
So, I’m going to go ahead
and we'll put this one over here on the terminal.
3860
03:40:17,500 --> 03:40:19,100
So, change the DELIMITER.
3861
03:40:19,100 --> 03:40:23,200
And then we're going to put this guy over here.
3862
03:40:23,200 --> 03:40:24,400
And whoops.
3863
03:40:24,400 --> 03:40:26,300
Again, I forgot to change the name.
3864
03:40:26,300 --> 03:40:27,500
So, this will be called trigger2.
3865
03:40:27,500 --> 03:40:29,300
And put this over here.
3866
03:40:29,300 --> 03:40:33,200
And then finally, we're just going to
change the DEMILITER back.
3867
03:40:33,200 --> 03:40:36,500
All right, so now lets – again,
we're going to insert an employee.
3868
03:40:36,500 --> 03:40:39,000
So, I’m going to go ahead
and insert a female employee.
3869
03:40:39,000 --> 03:40:41,200
So, why don’t we insert Pam Beesly.
3870
03:40:41,200 --> 03:40:44,300
So, Pam Beesly is going to be a female.
3871
03:40:44,300 --> 03:40:51,200
Which means when we insert Pam Beesly, hopefully
it should say added female into the Trigger Test table.
3872
03:40:51,200 --> 03:40:52,500
So, I’m going to run this.
3873
03:40:52,500 --> 03:40:54,100
And we added the employee.
3874
03:40:54,100 --> 03:40:55,900
Now, let’s SELECT ALL FROM trigger_test.
3875
03:40:55,900 --> 03:40:59,000
And so all of these triggers are actually going to
compound on each other.
3876
03:40:59,000 --> 03:41:01,500
So, we should have quite a few entries in here.
3877
03:41:01,500 --> 03:41:05,000
So, we'll see when we added Pam,
it said added new employee, Pam.
3878
03:41:05,000 --> 03:41:06,200
Added female.
3879
03:41:06,200 --> 03:41:09,900
So, that third trigger that we just created
actually ended up working.
3880
03:41:09,900 --> 03:41:13,100
So, you'll notice over here
we've been creating triggers for INSERT.
3881
03:41:13,100 --> 03:41:18,000
But you can also create triggers for UPDATE
and you can also make one for DELETE.
3882
03:41:18,000 --> 03:41:21,900
So, anytime they're trying to INSERT, UPDATE,
or DELETE, you can create a trigger.
3883
03:41:21,900 --> 03:41:26,000
So, you can also do – in addition to BEFORE
you could also do AFTER.
3884
03:41:26,000 --> 03:41:31,400
So, in certain circumstances
you won’t want to insert into trigger_test before.
3885
03:41:31,400 --> 03:41:32,600
You’d want to INSERT AFTER.
3886
03:41:32,600 --> 03:41:35,200
And you can go ahead and control it just like that.
3887
03:41:35,200 --> 03:41:39,700
So, but basically, that’s all the, you know,
the main stuff that we can do with triggers.
3888
03:41:39,700 --> 03:41:41,500
These are very, very useful.
3889
03:41:41,500 --> 03:41:44,800
And they'll allow you to do a bunch of cool stuff.
3890
03:41:44,800 --> 03:41:46,800
We can also drop a trigger.
3891
03:41:46,800 --> 03:41:46,800
So, I can say like over here in the terminal
I can just say DROP TRIGGER.
3892
03:41:46,800 --> 03:41:56,200
And it would be like my_trigger.
3893
03:41:56,200 --> 03:41:58,000
So, this will drop my_trigger.
3894
03:41:58,000 --> 03:42:01,700
And now, my_trigger is no longer going to be active.
3895
03:42:01,700 --> 03:42:04,100
So, triggers are, like I said, very useful.
3896
03:42:04,100 --> 03:42:08,000
And it’s a really great way to kind of control
what happens when other stuff happens.
3897
03:42:08,000 --> 03:42:11,700
And you can automate a lot of the stuff
on the backend of your database.
3898
03:42:11,700 --> 03:42:16,000
[ER Diagrams Intro]
3899
03:42:16,000 --> 03:42:18,700
In this tutorial I’m going to talk to you guys
about ER diagrams.
3900
03:42:18,700 --> 03:42:22,600
More specifically I’m just going to give you guys
an introduction to ER diagrams.
3901
03:42:22,600 --> 03:42:26,000
And we'll talk about how ER diagrams
are put together,
3902
03:42:26,000 --> 03:42:29,500
all the different symbols in the ER diagrams
and what they represent.
3903
03:42:29,500 --> 03:42:32,400
Now, when you're designing a database,
3904
03:42:32,400 --> 03:42:36,100
one of the most important things
is designing a database schema.
3905
03:42:36,100 --> 03:42:41,300
And a database schema is basically just all
the different tables and the different attributes
3906
03:42:41,300 --> 03:42:43,400
that are going to be on those tables.
3907
03:42:43,400 --> 03:42:46,000
So, maybe you have some requirements
for the different data
3908
03:42:46,000 --> 03:42:48,300
that needs to get stored inside of your database
3909
03:42:48,300 --> 03:42:51,300
and the different relationships that
that data is going to have.
3910
03:42:51,300 --> 03:42:55,000
Well, you can use an ER diagrams
to act as a middleman
3911
03:42:55,000 --> 03:43:00,400
between database or storage requirements
and the actual database schema
3912
03:43:00,400 --> 03:43:03,000
that’s going to get implemented
in the database management system.
3913
03:43:03,000 --> 03:43:07,300
So, an ER diagram is a great way
to kind of take, you know,
3914
03:43:07,300 --> 03:43:10,500
data storage requirements
like business requirements.
3915
03:43:10,500 --> 03:43:15,600
And sort of convert them
into an actual database schema.
3916
03:43:15,600 --> 03:43:18,900
So, we can use the ER diagram to map out
the different relationships
3917
03:43:18,900 --> 03:43:22,800
and the different entities
and the different attributes for those entities.
3918
03:43:22,800 --> 03:43:27,700
And it can just be a really great way to organize
our data into a database schema.
3919
03:43:27,700 --> 03:43:34,000
So, an ER diagram is basically just a little diagram that
consists of different shapes and symbols and text.
3920
03:43:34,000 --> 03:43:40,000
And it all kind of gets combined together to end up
defining, you know, a relationship model.
3921
03:43:40,000 --> 03:43:42,600
So, without further ado, let’s get started.
3922
03:43:42,600 --> 03:43:48,300
I’m going to show you guys all the different
basic parts of an ER diagram
3923
03:43:48,300 --> 03:43:49,700
and we'll kind of construct our own ER diagram.
3924
03:43:49,700 --> 03:43:54,000
And it’ll kind of give you guys an idea of all the
different symbols and stuff that we're going to use.
3925
03:43:54,000 --> 03:43:59,000
So, in this example I'm going to be using
the example of like a school.
3926
03:43:59,000 --> 03:44:03,000
So, let’s say that I’m working for a school
and my boss comes to me and he’s like,
3927
03:44:03,000 --> 03:44:10,000
“Hey Mike, I need you to design a database schema or
I need you to design an ER diagram for our database.”
3928
03:44:10,000 --> 03:44:13,100
So, maybe this database is going to store information
about different students.
3929
03:44:13,100 --> 03:44:17,000
And then maybe information
about like the classes that those students take.
3930
03:44:17,000 --> 03:44:20,000
So, let’s start looking at the different parts
of the ER diagram.
3931
03:44:20,000 --> 03:44:23,700
So, the first thing I want to talk to you guys
about are entities.
3932
03:44:23,700 --> 03:44:27,200
And an entity is just an object that we want
to model and store information about.
3933
03:44:27,200 --> 03:44:32,300
So, for our school database we might want
to store information about a particular student.
3934
03:44:32,300 --> 03:44:37,600
So, inside of our ER diagram we can put an entity
which is just going to be a square, just like this.
3935
03:44:37,600 --> 03:44:40,900
And then we're going to
have the name of the entity that we're storing.
3936
03:44:40,900 --> 03:44:42,400
So, it’s going to be student.
3937
03:44:42,400 --> 03:44:44,400
Next we can define attributes.
3938
03:44:44,400 --> 03:44:48,300
So, attributes are specific pieces
of information about an entity.
3939
03:44:48,300 --> 03:44:50,300
So, over here we have our student entity.
3940
03:44:50,300 --> 03:44:54,600
And then we might want to store like
the student’s name, the student’s grade number.
3941
03:44:54,600 --> 03:44:56,100
So, like, what grade are they in.
3942
03:44:56,100 --> 03:44:57,300
And then their GPA.
3943
03:44:57,300 --> 03:44:59,300
So, we can store all different types of attributes.
3944
03:44:59,300 --> 03:45:03,200
And we're going to make these little ovals and we're
going to connect them to the entity just like that.
3945
03:45:03,200 --> 03:45:06,100
So, the attribute is going to have the name
of the attribute inside of an oval,
3946
03:45:06,100 --> 03:45:08,600
connected to our square entity.
3947
03:45:08,600 --> 03:45:11,000
We can also define a primary key.
3948
03:45:11,000 --> 03:45:17,200
A primary key is going to be an attribute that’s going
to uniquely identify an entry in the database table.
3949
03:45:17,200 --> 03:45:20,700
So, you'll see over here I actually colored
the primary key different.
3950
03:45:20,700 --> 03:45:23,700
Now, generally, for an ER diagram,
you’re not going to be using colors.
3951
03:45:23,700 --> 03:45:26,600
I just did that so it’s kind of easier
for us to wrap our minds around.
3952
03:45:26,600 --> 03:45:30,300
But whenever we're defining a primary key,
we're always going to underline.
3953
03:45:30,300 --> 03:45:33,500
So, a primary key is just like a normal attribute,
but we're going to underline.
3954
03:45:33,500 --> 03:45:35,700
So, here, our primary key is a student ID.
3955
03:45:35,700 --> 03:45:37,600
And then I just have the GPA.
3956
03:45:37,600 --> 03:45:40,000
So, you know, obviously I could put
all of those attributes here.
3957
03:45:40,000 --> 03:45:42,500
But I’m just using two for now just to keep it simple.
3958
03:45:42,500 --> 03:45:46,200
So, we have our primary key, student ID,
which is underlined, and then we have our GPA.
3959
03:45:46,200 --> 03:45:48,000
And they're both connected to our entity.
3960
03:45:48,000 --> 03:45:51,100
Next, we can define composite attributes.
3961
03:45:51,100 --> 03:45:55,200
So, these would be attributes
that could be broken up into sub attributes.
3962
03:45:55,200 --> 03:45:57,900
So, for example, if we wanted to store
the student’s name,
3963
03:45:57,900 --> 03:46:01,900
but we can also store their first name
and they're last name.
3964
03:46:01,900 --> 03:46:05,800
So, name could be broken up further
into first name and last name.
3965
03:46:05,800 --> 03:46:10,300
And so, in the case of a composite attribute you'll
notice that we have the main attribute here.
3966
03:46:10,300 --> 03:46:11,800
It’s connected to the entity.
3967
03:46:11,800 --> 03:46:15,000
And then off of that main attribute
we have two other attributes
3968
03:46:15,000 --> 03:46:17,900
fname and lname
for first name and last name.
3969
03:46:17,900 --> 03:46:21,800
We can also define a multi-valued attribute.
3970
03:46:21,800 --> 03:46:26,900
So, if there’s any attributes in your data model
that could have more than one value,
3971
03:46:26,900 --> 03:46:30,000
then you can put them in a multi-valued attribute
3972
03:46:30,000 --> 03:46:32,600
which looks just like an attribute
except we have an extra circle.
3973
03:46:32,600 --> 03:46:33,900
So, it’s just two circles.
3974
03:46:33,900 --> 03:46:35,900
And then inside, the name of the attribute.
3975
03:46:35,900 --> 03:46:36,900
So, clubs, for example.
3976
03:46:36,900 --> 03:46:39,800
Like a student might be involved
in a bunch of different clubs.
3977
03:46:39,800 --> 03:46:43,300
And so, clubs would be a multi-valued attribute.
3978
03:46:43,300 --> 03:46:45,500
In other words, it could have more than one value.
3979
03:46:45,500 --> 03:46:48,000
Like a student is not going
to have more than one GPA.
3980
03:46:48,000 --> 03:46:49,600
They're not going to have more than one name.
3981
03:46:49,600 --> 03:46:51,300
They're not going to have more than one student ID.
3982
03:46:51,300 --> 03:46:54,500
But they might have more than
one club that they belong to.
3983
03:46:54,500 --> 03:46:56,100
So, next step is a derived attribute.
3984
03:46:56,100 --> 03:47:00,600
And a derived attribute is an attribute
that can be derived from the other attributes
3985
03:47:00,600 --> 03:47:01,800
that we're keeping track of.
3986
03:47:01,800 --> 03:47:05,300
So, we're not going to actually
keep track of the derived attribute.
3987
03:47:05,300 --> 03:47:09,400
But it’s just a way that we can sort of notate attributes
3988
03:47:09,400 --> 03:47:12,100
that could be derived from the attributes
that we're storing.
3989
03:47:12,100 --> 03:47:14,100
So, down here I have my derived attribute.
3990
03:47:14,100 --> 03:47:16,600
And you'll notice that it’s just an oval
with these dashed lines.
3991
03:47:16,600 --> 03:47:18,000
It’s called has_honors.
3992
03:47:18,000 --> 03:47:22,900
So, has_honors is an attribute
that we could derive from this GPA.
3993
03:47:22,900 --> 03:47:27,800
So, maybe the school is going to say
that anybody with
3994
03:47:27,800 --> 03:47:29,800
a GPA of 3,500 or above is going to have honors.
3995
03:47:29,800 --> 03:47:33,100
Well, we could derive that just from the GPA.
3996
03:47:33,100 --> 03:47:36,800
So, we're not actually going to be
keeping track of this attribute,
3997
03:47:36,800 --> 03:47:41,900
but it’s an attribute that we can derive
from the GPA that we are keeping track of.
3998
03:47:41,900 --> 03:47:43,500
So, we can just denote it like that.
3999
03:47:43,500 --> 03:47:46,000
And sometimes it’s useful to denote
our derived attributes.
4000
03:47:46,000 --> 03:47:49,100
So, we can also have multiple entities.
4001
03:47:49,100 --> 03:47:51,300
So, over here you'll see I have my student entity.
4002
03:47:51,300 --> 03:47:54,400
But I can also define another entity
which would be like class.
4003
03:47:54,400 --> 03:47:59,500
And so, a class would be like a particular class
that a student is taking, right?
4004
03:47:59,500 --> 03:48:03,400
So, if I was in school, I might take like biology
or chemistry, right?
4005
03:48:03,400 --> 03:48:05,800
That would be what this class is over here.
4006
03:48:05,800 --> 03:48:11,000
And then you'll see over here we have our primary key
which is just going to be class ID.
4007
03:48:11,000 --> 03:48:14,000
And so, when we have multiple entities,
4008
03:48:14,000 --> 03:48:17,300
we’re going to want to define relationships
between those entities.
4009
03:48:17,300 --> 03:48:19,800
So, what I can do is I can define a relationship.
4010
03:48:19,800 --> 03:48:22,200
And a relationship is basically
just this diamond over here.
4011
03:48:22,200 --> 03:48:27,500
And the relationship would basically
denote a student is going to take a class.
4012
03:48:27,500 --> 03:48:30,800
So, a relationship is kind of like a verb, right?
4013
03:48:30,800 --> 03:48:34,500
It’s the student is related to the class
in some way, right?
4014
03:48:34,500 --> 03:48:36,100
So, the student is going to take a class.
4015
03:48:36,100 --> 03:48:39,200
And a class can be taken by students.
4016
03:48:39,200 --> 03:48:41,200
So, you can read this both ways.
4017
03:48:41,200 --> 03:48:46,200
You can say the student takes a class
or you can say that the class is taken by a student.
4018
03:48:46,200 --> 03:48:49,400
And we can also define participation.
4019
03:48:49,400 --> 03:48:53,900
So, you'll notice that the relationship
I’m connecting the two entities using these lines.
4020
03:48:53,900 --> 03:48:56,800
So, the student is connected to the relationship
using a single line.
4021
03:48:56,800 --> 03:48:59,900
And the classes connected to the relationship
using a double line.
4022
03:48:59,900 --> 03:49:01,700
So, when you're defining relationships
4023
03:49:01,700 --> 03:49:06,600
you can define the participation
of the particular entities to that relationship.
4024
03:49:06,600 --> 03:49:11,000
So, when I use a single line,
this indications partial participation.
4025
03:49:11,000 --> 03:49:15,100
What this means is that
not all students need to take a class.
4026
03:49:15,100 --> 03:49:17,000
So, when I use the single line.
4027
03:49:17,000 --> 03:49:21,100
I’m basically saying that only some
of the students have to take a class, right?
4028
03:49:21,100 --> 03:49:23,800
Not all students necessarily have to be taking a class.
4029
03:49:23,800 --> 03:49:28,100
When I use this double line,
it indicates total participation.
4030
03:49:28,100 --> 03:49:34,000
Which means that all of the classes need to be taken
by at least a single student, right?
4031
03:49:34,000 --> 03:49:40,000
So, that means all classes must participate
in this Takes relationship.
4032
03:49:40,000 --> 03:49:43,000
So, all classes need to have students
that are taking them.
4033
03:49:43,000 --> 03:49:46,500
So, you couldn't have a class
that has no students taking it.
4034
03:49:46,500 --> 03:49:50,000
All classes have to have students that are taking it.
4035
03:49:50,000 --> 03:49:53,500
And, you know, maybe that’s not
what you’d want in your database.
4036
03:49:53,500 --> 03:49:56,500
But in this case, that’s how
we can denote something like that.
4037
03:49:56,500 --> 03:50:03,300
So, I could use total participation to denote that
all classes need to participate in this relationship.
4038
03:50:03,300 --> 03:50:07,800
In other words, all classes need
to have a student taking the class.
4039
03:50:07,800 --> 03:50:11,700
So, that’s basically how we can define relationships.
4040
03:50:11,700 --> 03:50:14,700
And then obviously, partial participation
and total participation.
4041
03:50:14,700 --> 03:50:20,300
And so, over here, we can also define attributes
about a particular relationship.
4042
03:50:20,300 --> 03:50:21,900
So, we have our Takes relationship.
4043
03:50:21,900 --> 03:50:27,400
And you'll notice that I’m defining at attribute
about this relationship which is grade.
4044
03:50:27,400 --> 03:50:33,100
So, a student will take a class and the student
will get a particular grade for that class, right?
4045
03:50:33,100 --> 03:50:36,300
So, I might take biology and maybe I get
like a B+ in biology.
4046
03:50:36,300 --> 03:50:39,800
Well, that grade isn’t necessarily stored
on the student entity
4047
03:50:39,800 --> 03:50:42,300
and it isn’t necessarily stored on the class entity.
4048
03:50:42,300 --> 03:50:44,100
It’s stored on the relationship, right?
4049
03:50:44,100 --> 03:50:48,400
So, the only way I can get a grade from a class
is if I take it, right?
4050
03:50:48,400 --> 03:50:53,200
And so, that’s why the relationship attribute
is stored on the relationship.
4051
03:50:53,200 --> 03:50:55,300
And sometimes that'll come in handy.
4052
03:50:55,300 --> 03:50:59,400
And so, we can also define relationship cardinality.
4053
03:50:59,400 --> 03:51:03,900
And relationship cardinality is the number of instances
of an entity from a relationship
4054
03:51:03,900 --> 03:51:05,700
that can be associated with the relation.
4055
03:51:05,700 --> 03:51:08,200
Now, I understand that’s
a very confusing definition.
4056
03:51:08,200 --> 03:51:11,700
And I think relationship cardinality
is something that trips a lot of people up,
4057
03:51:11,700 --> 03:51:14,100
so I’m going to try to give you guys
a good explanation of it.
4058
03:51:14,100 --> 03:51:17,800
So, over here we have a student
and a student can take a class.
4059
03:51:17,800 --> 03:51:21,100
But we can define relationship cardinalities on that.
4060
03:51:21,100 --> 03:51:26,600
Basically, what this means is that a student
can take any number of classes.
4061
03:51:26,600 --> 03:51:29,300
So, when we say M, that refers to any number.
4062
03:51:29,300 --> 03:51:33,100
So, a student could take
basically multiple classes, right?
4063
03:51:33,100 --> 03:51:35,600
A student could take 2, or 3, or 4 classes.
4064
03:51:35,600 --> 03:51:38,100
And we can define the same thing for the class.
4065
03:51:38,100 --> 03:51:42,600
So, we could say a class is taken
by any number of students, right?
4066
03:51:42,600 --> 03:51:45,800
So, a class can be taken by 5 or 10 or 30 students.
4067
03:51:45,800 --> 03:51:48,100
That’s basically what that would define.
4068
03:51:48,100 --> 03:51:51,600
So, this would be an NM cardinality relationship.
4069
03:51:51,600 --> 03:51:54,400
But we can also define
other cardinality relationships.
4070
03:51:54,400 --> 03:51:56,300
So, we could say like 1 to 1.
4071
03:51:56,300 --> 03:52:01,600
So, in a 1 to 1 cardinality relationship
we would say that a student can take one class
4072
03:52:01,600 --> 03:52:05,300
and a class can be taken by one student.
4073
03:52:05,300 --> 03:52:09,800
We can also say like 1 to N,
the cardinality relationship,
4074
03:52:09,800 --> 03:52:15,900
which would be a student could take one class
and a class could be taken by many students.
4075
03:52:15,900 --> 03:52:20,200
Or, you could reverse it and say a class
can be taken by one student,
4076
03:52:20,200 --> 03:52:23,700
but a student can take any number of classes.
4077
03:52:23,700 --> 03:52:28,800
And then again, you guys saw NM which
would be a student can take any number of classes
4078
03:52:28,800 --> 03:52:30,900
and a class can be taken by any number of students.
4079
03:52:30,900 --> 03:52:35,400
So, it’s useful to define that relationship
cardinality in an ER diagram
4080
03:52:35,400 --> 03:52:41,900
because that’s actually going to relate to
how we want to design our database schema
4081
03:52:41,900 --> 03:52:43,800
when it’s eventually time to do that.
4082
03:52:43,800 --> 03:52:48,300
And also, like this is something that
could be defined in data modeling requirements.
4083
03:52:48,300 --> 03:52:52,800
So, if the requirements comes to you and says
a student can only take one class at a time,
4084
03:52:52,800 --> 03:52:56,900
well, that’s something that you want to be
able to represent inside of the ER diagram.
4085
03:52:56,900 --> 03:52:59,900
So, that’s kind of how
we can represent relationship cardinality.
4086
03:52:59,900 --> 03:53:03,200
And then finally, the last thing
I want to show you guys
4087
03:53:03,200 --> 03:53:06,500
are weak entity types
and identifying relationships.
4088
03:53:06,500 --> 03:53:08,300
So, actually, I think I’m in the way here.
4089
03:53:08,300 --> 03:53:10,600
But where my head is, it just says class.
4090
03:53:10,600 --> 03:53:12,200
So, you guys kind of saw it before.
4091
03:53:12,200 --> 03:53:17,100
So, a weak entity as entity that cannot
be uniquely identified by it’s attributes alone.
4092
03:53:17,100 --> 03:53:23,700
Basically, a weak entity is an entity
that’s going to rely on or depend on another entity.
4093
03:53:23,700 --> 03:53:27,400
So, over here I have an example
of a weak entity which would be an exam.
4094
03:53:27,400 --> 03:53:31,200
So, a class can have an exam, right?
4095
03:53:31,200 --> 03:53:34,500
So, an exam is something –
it’s sort of like an entity, right?
4096
03:53:34,500 --> 03:53:37,200
You know, a test or whatever
that someone might be taking.
4097
03:53:37,200 --> 03:53:39,200
An exam might have an exam ID.
4098
03:53:39,200 --> 03:53:44,100
But in this case, an exam
can’t exist without a class, right?
4099
03:53:44,100 --> 03:53:50,100
In other words, for an exam to exist,
it has to be associated with a class, right?
4100
03:53:50,100 --> 03:53:52,900
An exam, you're not just going to like
have an exam stored, right?
4101
03:53:52,900 --> 03:53:57,000
We're only going to have an exam
that’s going to be associated with a class.
4102
03:53:57,000 --> 03:54:00,100
So, this is what would we call a weak entity type.
4103
03:54:00,100 --> 03:54:04,800
It’s an entity that cannot be uniquely identified
by its attributes alone.
4104
03:54:04,800 --> 03:54:08,500
And we can also define an identifying relationship.
4105
03:54:08,500 --> 03:54:13,600
And an identifying relationship is a relationship
that serves to uniquely identify the weak entity.
4106
03:54:13,600 --> 03:54:18,300
So, an exam can be uniquely identified
when it’s paired with a class.
4107
03:54:18,300 --> 03:54:22,200
Which I realize, my head is in the way of,
but you guys know it’s there.
4108
03:54:22,200 --> 03:54:25,300
So, I can say that a class has an exam.
4109
03:54:25,300 --> 03:54:27,700
And an exam is had by a class.
4110
03:54:27,700 --> 03:54:30,400
The exam doesn’t exist on its own.
4111
03:54:30,400 --> 03:54:32,900
It only exists in the context of a class.
4112
03:54:32,900 --> 03:54:35,500
And this is kind of more of an abstract idea.
4113
03:54:35,500 --> 03:54:40,000
And actually, in the next video
we're going to look more at weak entity types.
4114
03:54:40,000 --> 03:54:44,900
But this should be at least a little bit of an example
and an introduction into weak entity types
4115
03:54:44,900 --> 03:54:49,600
and identifying the relationships
which we're just notating by these double lines.
4116
03:54:49,600 --> 03:54:54,100
So, the exam has double square and the identifying
relationship has a double triangle.
4117
03:54:54,100 --> 03:54:55,800
And also, one more thing to note.
4118
03:54:55,800 --> 03:54:59,100
Then whenever we have a weak entity
and identifying relationship,
4119
03:54:59,100 --> 03:55:03,800
the weak entity always has to have total participation
in the identifying relationship.
4120
03:55:03,800 --> 03:55:10,100
In other words, all exams must have a class,
but not all classes need to have an exam.
4121
03:55:10,100 --> 03:55:16,300
All right, so that is kind of all of the sort of basic things
that you're going to encounter in an ER diagram.
4122
03:55:16,300 --> 03:55:21,800
And really, everything that you see right here
is kind of like all of the stuff
4123
03:55:21,800 --> 03:55:23,400
that you might seen an ER diagram.
4124
03:55:23,400 --> 03:55:30,400
And really, you can use all of these different symbols
in order to represent a data model.
4125
03:55:30,400 --> 03:55:32,600
And what you'll see is we can take this ER diagram
4126
03:55:32,600 --> 03:55:37,700
and we can actually convert into
an actual database schema.
4127
03:55:37,700 --> 03:55:41,000
And that’s why these are really
useful sort of middle-man
4128
03:55:41,000 --> 03:55:43,500
between requirements
and the actual database schema.
4129
03:55:43,500 --> 03:55:44,900
So, hopefully that makes sense.
4130
03:55:44,900 --> 03:55:49,800
In the next video we're going to actually walk through
an example of constructing our own ER diagram,
4131
03:55:49,800 --> 03:55:51,300
so that should be kind of cool.
4132
03:55:51,300 --> 03:55:53,500
And that should kind of give you guys
more of an idea of how these work.
4133
03:55:53,500 --> 03:55:57,200
[Designing an ER Diagram]
4134
03:55:57,200 --> 03:56:00,900
In this tutorial I’m going to walk you guys
through creating an ER diagram
4135
03:56:00,900 --> 03:56:03,500
based off of some data requirements.
4136
03:56:03,500 --> 03:56:06,200
So, over here I have this document.
4137
03:56:06,200 --> 03:56:08,000
It’s called Company Data Requirements.
4138
03:56:08,000 --> 03:56:13,400
And basically, what this document does
is it describes all of the different data,
4139
03:56:13,400 --> 03:56:17,100
all the pieces of information and the relationships
between those pieces of information.
4140
03:56:17,100 --> 03:56:20,800
And this is a good example
of a document that, you know,
4141
03:56:20,800 --> 03:56:24,200
you might get if you’re working for a company
and they want you to design a database.
4142
03:56:24,200 --> 03:56:27,700
Let’s say that they want you to design a database
to store information about a company.
4143
03:56:27,700 --> 03:56:30,400
Well, they might give you this document.
4144
03:56:30,400 --> 03:56:36,100
And then your job would be to take this document
and convert it into a database schema
4145
03:56:36,100 --> 03:56:38,800
which you can then, you know,
store information in and all that.
4146
03:56:38,800 --> 03:56:44,300
So, this document will describe all the data and
it will describe the relationships between the data.
4147
03:56:44,300 --> 03:56:47,200
But it will do it in, you know, in English, right?
4148
03:56:47,200 --> 03:56:49,000
It’ll do it in a very high-level manner.
4149
03:56:49,000 --> 03:56:53,300
You know, it’s not going to get into
database specifics or anything like that.
4150
03:56:53,300 --> 03:57:00,500
So, your job would be to take this information and
then, you know, design database schema from it.
4151
03:57:00,500 --> 03:57:04,000
And so, what you can do is you can take this.
4152
03:57:04,000 --> 03:57:05,400
You can convert it into an ER diagram
4153
03:57:05,400 --> 03:57:09,300
and then you can take that ER diagram
and convert it into a database schema.
4154
03:57:09,300 --> 03:57:12,100
So, I’m going to show you guys the first step of that
4155
03:57:12,100 --> 03:57:16,000
which would be to take a document like this
and convert it into an ER diagram.
4156
03:57:16,000 --> 03:57:21,200
Which in the last video, I kind of walked you guys
through what an ER diagram was and all that stuff.
4157
03:57:21,200 --> 03:57:23,600
So, let’s go ahead and take a look at this document.
4158
03:57:23,600 --> 03:57:25,400
I’m going to read through it.
4159
03:57:25,400 --> 03:57:28,900
And then what we're going to do is we're going to
design an ER diagram based off of it.
4160
03:57:28,900 --> 03:57:33,200
So, over here it’s just Company Data Requirements.
4161
03:57:33,200 --> 03:57:35,300
So, we're defining the data
and all that stuff in a company.
4162
03:57:35,300 --> 03:57:37,500
So, it says the company is organized into branches.
4163
03:57:37,500 --> 03:57:42,200
Each branch has a unique number, a name,
and a particular employee who manages it.
4164
03:57:42,200 --> 03:57:45,300
The company makes its money by selling to clients.
4165
03:57:45,300 --> 03:57:48,600
Each client has a name
and a unique number to identify it.
4166
03:57:48,600 --> 03:57:51,200
The foundation of the company is its employees.
4167
03:57:51,200 --> 03:57:55,500
Each employee has a name,
birthday, sex, salary, and a unique number.
4168
03:57:55,500 --> 03:57:58,300
An employee can work for one branch at a time
4169
03:57:58,300 --> 03:58:01,200
and each branch will be managed
by one of the employees that work there.
4170
03:58:01,200 --> 03:58:05,300
We’ll also want to keep track of
when the current manager started as manager.
4171
03:58:05,300 --> 03:58:09,200
An employee can act as a supervisor
for other employees at the branch.
4172
03:58:09,200 --> 03:58:13,500
An employee may also act as a supervisor
for employees at other branches.
4173
03:58:13,500 --> 03:58:16,500
An employee can have at most one supervisor.
4174
03:58:16,500 --> 03:58:18,800
A branch may handle a number of clients,
4175
03:58:18,800 --> 03:58:22,500
with each client having a name
and a unique number to identify it.
4176
03:58:22,500 --> 03:58:26,000
A single client may only be handled
by one branch at a time.
4177
03:58:26,000 --> 03:58:30,100
Employees can work with clients controlled
by their branch to sell them stuff.
4178
03:58:30,100 --> 03:58:33,500
If necessarily, multiple employees can work
with the same client.
4179
03:58:33,500 --> 03:58:36,600
We'll want to keep track of
how many dollars worth of stuff
4180
03:58:36,600 --> 03:58:39,100
each employee sells to each client they work with.
4181
03:58:39,100 --> 03:58:42,300
Many branches will need to work with suppliers
to buy inventory.
4182
03:58:42,300 --> 03:58:47,200
For each supplier we'll keep track of their name
and the type of product they're selling the branch.
4183
03:58:47,200 --> 03:58:49,900
A single supplier may supply products
to multiple branches.
4184
03:58:49,900 --> 03:58:54,200
So, this is our Company Data Requirements document.
4185
03:58:54,200 --> 03:58:55,400
And there’s a lot here, right?
4186
03:58:55,400 --> 03:58:58,700
It kind of took me like over a minute
to go through and read all of this.
4187
03:58:58,700 --> 03:59:01,600
And so, if you're given a document like this,
4188
03:59:01,600 --> 03:59:05,300
how do you go about converting this
into a database schema?
4189
03:59:05,300 --> 03:59:08,700
So, the first thing we want to do
is create an ER diagram.
4190
03:59:08,700 --> 03:59:10,800
So, what I’m going to do is I’m going to walk through
4191
03:59:10,800 --> 03:59:16,300
and show you guys how we can create an ER diagram
for these data requirements, okay?
4192
03:59:16,300 --> 03:59:20,000
And then in the next video I’ll show you guys
how you can convert that ER diagram
4193
03:59:20,000 --> 03:59:21,800
into an actual database schema.
4194
03:59:21,800 --> 03:59:24,100
So, let’s go ahead and take a look.
4195
03:59:24,100 --> 03:59:28,600
I’m going to walk you guys through each line
in that requirements document
4196
03:59:28,600 --> 03:59:30,400
and we'll convert it into our ER diagram.
4197
03:59:30,400 --> 03:59:33,900
So, over here it says the company
is organized into branches.
4198
03:59:33,900 --> 03:59:36,200
Each branch has a unique number and a name.
4199
03:59:36,200 --> 03:59:39,000
So, you'll notice that I’ve mode bold branches.
4200
03:59:39,000 --> 03:59:41,100
So, branch is going to be our entity, right?
4201
03:59:41,100 --> 03:59:42,500
We're defining an entity branch.
4202
03:59:42,500 --> 03:59:46,600
And it’s going to have two attributes, a branch ID
which is going to be our primary key, right?
4203
03:59:46,600 --> 03:59:48,400
The branch has a unique number.
4204
03:59:48,400 --> 03:59:51,500
So, that, to me, tells me
that it’s going to be the primary key.
4205
03:59:51,500 --> 03:59:53,600
And then obviously, the branch name.
4206
03:59:53,600 --> 03:59:57,900
Next we have the company
makes its money by selling to clients.
4207
03:59:57,900 --> 04:00:01,100
So, right away there we have another entity.
4208
04:00:01,100 --> 04:00:03,600
Each client has a name
and a unique number to identify it.
4209
04:00:03,600 --> 04:00:08,500
So, here we have our client
which has their client ID which identifies it.
4210
04:00:08,500 --> 04:00:12,400
And then their client name
which is just going to be the name.
4211
04:00:12,400 --> 04:00:16,500
And then over here it says the foundation
of the company is its employees.
4212
04:00:16,500 --> 04:00:21,200
Each employee has a name, birthday, sex, salary
and a unique number to identify it.
4213
04:00:21,200 --> 04:00:26,100
So, over here we have our employee and we have
the employee ID which is the primary key.
4214
04:00:26,100 --> 04:00:28,600
Birthday, name, so we get first and last name.
4215
04:00:28,600 --> 04:00:30,800
And salary and then sex.
4216
04:00:30,800 --> 04:00:34,900
And then so over here we also have a derived
attribute which is going to be age.
4217
04:00:34,900 --> 04:00:40,200
So, from the employees birth date we could derive
how old they are at any given point.
4218
04:00:40,200 --> 04:00:46,200
So, here we have our three entities
that we got from this requirements document.
4219
04:00:46,200 --> 04:00:51,500
So, now over here it says the employee can work
for one branch at a time.
4220
04:00:51,500 --> 04:00:55,800
So, over here we have a relationship, Work For, right?
4221
04:00:55,800 --> 04:00:56,600
This is like a verb.
4222
04:00:56,600 --> 04:01:04,600
So, an employee over here can work for a branch and
a branch can have an employee working for it, right?
4223
04:01:04,600 --> 04:01:06,000
So, that’s our relationship.
4224
04:01:06,000 --> 04:01:11,300
And you'll notice over here
I defined these as total participation.
4225
04:01:11,300 --> 04:01:15,600
So, I’m saying that all branches
must have employees working for them.
4226
04:01:15,600 --> 04:01:17,700
That’s this double line right here.
4227
04:01:17,700 --> 04:01:21,400
And I’m also saying all employees
must work for a branch.
4228
04:01:21,400 --> 04:01:26,900
So, both of those entities have a total participation
in the Works For relationship.
4229
04:01:26,900 --> 04:01:30,400
And that wasn’t rigorously defined
inside of the document.
4230
04:01:30,400 --> 04:01:35,700
But that’s just something that I kind of, you know
set there as the person designing the ER diagram.
4231
04:01:35,700 --> 04:01:38,400
And then over here
we have our cardinality relationship.
4232
04:01:38,400 --> 04:01:40,800
So, I'm saying that – basically what this says
4233
04:01:40,800 --> 04:01:44,900
is that a branch can have
any number of employees working for it.
4234
04:01:44,900 --> 04:01:48,300
And an employee can work for one branch.
4235
04:01:48,300 --> 04:01:50,600
So, I’m going to say that one more time.
4236
04:01:50,600 --> 04:01:53,100
A branch can have any number
of employees working for it.
4237
04:01:53,100 --> 04:01:56,100
And an employee can work for one branch.
4238
04:01:56,100 --> 04:01:59,400
That’s what that cardinality relationship
is defining right there.
4239
04:01:59,400 --> 04:02:02,700
So, next we have another relationship.
4240
04:02:02,700 --> 04:02:06,600
It says each branch will be managed
by one of the employees that work there.
4241
04:02:06,600 --> 04:02:11,100
We’ll also want to keep track of
when the current manager started as manager.
4242
04:02:11,100 --> 04:02:14,300
So, over here we have another relationship
which is manage, right?
4243
04:02:14,300 --> 04:02:17,000
An employee can manage a branch.
4244
04:02:17,000 --> 04:02:22,900
So, employee can manage a branch
and a branch can be managed by an employee.
4245
04:02:22,900 --> 04:02:27,900
And then you'll also notice over here that
on this relationship we've defined an attribute.
4246
04:02:27,900 --> 04:02:32,200
So, we want to keep track of when
the employee started as the manager, right?
4247
04:02:32,200 --> 04:02:36,400
So, when does the employee start as the manager
and that’s what we're defining over here.
4248
04:02:36,400 --> 04:02:39,900
So, we're defining this attribute
on the actual relationship.
4249
04:02:39,900 --> 04:02:42,000
And now let’s take a look at the participation.
4250
04:02:42,000 --> 04:02:46,500
So, all branches must have someone managing them.
4251
04:02:46,500 --> 04:02:49,300
So, you'll notice that we have
this full participation here, right?
4252
04:02:49,300 --> 04:02:53,200
Every branch is going to participate
in that Manages relationship.
4253
04:02:53,200 --> 04:02:54,900
All branches need to have a manager.
4254
04:02:54,900 --> 04:02:58,400
But over here on the employee
it’s partial participation, right?
4255
04:02:58,400 --> 04:03:01,600
Not all employees need to be managers of a branch.
4256
04:03:01,600 --> 04:03:06,900
In fact, by a large majority most employees
will not be the manager of a branch.
4257
04:03:06,900 --> 04:03:11,400
And so, that’s why we define this as single
participation or partial participation.
4258
04:03:11,400 --> 04:03:16,600
So, not all employees are going to manage a branch,
but all branches will be managed by employees.
4259
04:03:16,600 --> 04:03:19,400
And then over here
we have our cardinality relationships.
4260
04:03:19,400 --> 04:03:24,300
So, we're saying that an employee
can manage one branch
4261
04:03:24,300 --> 04:03:27,000
and a branch can be managed by one employee.
4262
04:03:27,000 --> 04:03:29,100
So, that kind of makes sense.
4263
04:03:29,100 --> 04:03:31,900
So, down here we also have another relationship.
4264
04:03:31,900 --> 04:03:36,200
It says an employee can act as a supervisor
for other employees at the branch.
4265
04:03:36,200 --> 04:03:40,300
An employee may also act as a supervisor
for employees at other branches.
4266
04:03:40,300 --> 04:03:42,800
That employee can have at most one supervisor.
4267
04:03:42,800 --> 04:03:45,200
So, over here we get this supervision relationship.
4268
04:03:45,200 --> 04:03:51,200
Now you'll notice that the supervision relationship is
actually a relationship that an employee has to itself.
4269
04:03:51,200 --> 04:03:54,000
So, this is a relationship between employees.
4270
04:03:54,000 --> 04:03:59,800
So, over here we have an employee
can be supervised by another employee
4271
04:03:59,800 --> 04:04:02,600
and an employee can be the supervisor
of another employee, right?
4272
04:04:02,600 --> 04:04:10,000
So, over here, basically we're saying that an employee
can be the supervisee of only one supervisor.
4273
04:04:10,000 --> 04:04:12,400
So you can only have one supervisor.
4274
04:04:12,400 --> 04:04:16,500
But an employee can supervise
any number of employees.
4275
04:04:16,500 --> 04:04:23,500
So, one more time, I’ll just say that an employee can
be supervised by one other employee, one supervisor.
4276
04:04:23,500 --> 04:04:28,200
And a supervisor can be the supervisor
of any number of employees.
4277
04:04:28,200 --> 04:04:30,900
All right, so over here we have another relationship.
4278
04:04:30,900 --> 04:04:33,300
It says a branch may handle a number of clients,
4279
04:04:33,300 --> 04:04:38,200
however a single client may only be handled
by one branch at a time.
4280
04:04:38,200 --> 04:04:43,200
So, over here we have the new relationship
between the branch and the client.
4281
04:04:43,200 --> 04:04:48,900
So, I’m saying that a branch can handle a client
and a client can be handled by a branch, right?
4282
04:04:48,900 --> 04:04:53,000
So, maybe a branch might have a bunch of clients
that it works with, or whatever.
4283
04:04:53,000 --> 04:04:56,200
And so, the client has a total participation
in this relationship.
4284
04:04:56,200 --> 04:04:59,500
That means that every client must be handled
by a branch.
4285
04:04:59,500 --> 04:05:02,600
But if the branch has a partial participation,
4286
04:05:02,600 --> 04:05:05,900
which means that not all branches
need to have clients, right?
4287
04:05:05,900 --> 04:05:09,600
Maybe you’d have like a corporate branch
that doesn’t work with any clients.
4288
04:05:09,600 --> 04:05:12,400
Or maybe you’d have like an RND branch
that doesn’t work with clients.
4289
04:05:12,400 --> 04:05:14,300
But you'd have other branches that do.
4290
04:05:14,300 --> 04:05:16,300
And so, that’s why we would define that relationship.
4291
04:05:16,300 --> 04:05:18,600
And then also we have our cardinality relationship
4292
04:05:18,600 --> 04:05:22,400
which would be a branch can handle
any number of clients, right?
4293
04:05:22,400 --> 04:05:24,500
So, the branch can handle N clients.
4294
04:05:24,500 --> 04:05:26,900
And a client can be handled by one branch.
4295
04:05:26,900 --> 04:05:29,400
So, if you're a client, you can only work
with one branch.
4296
04:05:29,400 --> 04:05:32,100
But if you’re branch, you can work
with multiple clients.
4297
04:05:32,100 --> 04:05:34,500
And that’s what we're defining over here.
4298
04:05:34,500 --> 04:05:37,600
All right, and then over here
we have another relationship.
4299
04:05:37,600 --> 04:05:40,500
And I realize this is getting a little cluttered
and I’m actually in the way.
4300
04:05:40,500 --> 04:05:45,800
But it says employees can work with clients controlled
by their branch to sell them stuff.
4301
04:05:45,800 --> 04:05:50,500
If necessary, multiple employees can work
with the same client.
4302
04:05:50,500 --> 04:05:54,000
So, now we have a relationship
between employees and clients.
4303
04:05:54,000 --> 04:05:58,500
So, we have an employee works with a client.
4304
04:05:58,500 --> 04:06:01,100
And then a client can work with an employee.
4305
04:06:01,100 --> 04:06:03,000
Now, notice the participation.
4306
04:06:03,000 --> 04:06:06,500
So, all clients must work with an employee.
4307
04:06:06,500 --> 04:06:09,700
But not all employees must work with clients, right?
4308
04:06:09,700 --> 04:06:15,100
So, all clients need to interact with the branch
through an employee,
4309
04:06:15,100 --> 04:06:17,900
but not all employees need to interact with clients.
4310
04:06:17,900 --> 04:06:22,800
You'll also see down here it says we'll want to keep
track of how many dollars worth of stuff
4311
04:06:22,800 --> 04:06:25,100
each employee sells to each client they work with.
4312
04:06:25,100 --> 04:06:30,400
So, on this Works With relationship,
we're defining this attribute, right?
4313
04:06:30,400 --> 04:06:35,900
So, the employee can sell to the client
and the client can buy from the employee
4314
04:06:35,900 --> 04:06:38,600
and that’s where we're getting this from.
4315
04:06:38,600 --> 04:06:40,200
And finally, we'll look at the cardinality.
4316
04:06:40,200 --> 04:06:44,300
So, a client can work with any number of employees.
4317
04:06:44,300 --> 04:06:46,600
And an employee can work
with any number of clients.
4318
04:06:46,600 --> 04:06:51,500
And so, that’s basically the relationship
that we get from this line up here.
4319
04:06:51,500 --> 04:06:57,000
All right, so over here we have our final
little section of this requirements document.
4320
04:06:57,000 --> 04:07:00,700
It says many branches will need to work
with suppliers to buy inventory.
4321
04:07:00,700 --> 04:07:06,200
For each supplier we'll keep track of their name
and the type of product they're selling the branch.
4322
04:07:06,200 --> 04:07:09,500
A single supplier may supply products
to multiple branches.
4323
04:07:09,500 --> 04:07:16,100
So, this is an example where we would need
to use a weak entity and an identifying relationship.
4324
04:07:16,100 --> 04:07:18,500
So, over here we have this
weak entity branch supplier.
4325
04:07:18,500 --> 04:07:21,200
And it has a supplier name and a supply type,
4326
04:07:21,200 --> 04:07:25,500
but the branch supplier is going to
supply a specific branch.
4327
04:07:25,500 --> 04:07:30,800
Now, we want to keep track of which branch suppliers
are supplying which branches.
4328
04:07:30,800 --> 04:07:35,500
And in order to do that, we're going to
have to use this identifying relationship.
4329
04:07:35,500 --> 04:07:40,200
So, we can say the branch supplier supplies
a branch and a branch gets supplied by a supplier.
4330
04:07:40,200 --> 04:07:43,900
And you can see that we have
these cardinality ratios over here.
4331
04:07:43,900 --> 04:07:48,800
So, this is basically our entire ER diagram, right?
4332
04:07:48,800 --> 04:07:54,900
We have from that requirements document we've
been able to map out all of the different entities,
4333
04:07:54,900 --> 04:07:59,000
all the different attributes on the entities
and all the different relationships.
4334
04:07:59,000 --> 04:08:01,500
And basically, we get this diagram.
4335
04:08:01,500 --> 04:08:05,800
And this diagram is – it’s just linking
all that information together, right?
4336
04:08:05,800 --> 04:08:10,900
It’s visually representing all of that information
in a way that is defining it.
4337
04:08:10,900 --> 04:08:14,800
So, what we can do now
is we can take this ER diagram
4338
04:08:14,800 --> 04:08:18,900
and depending on the different relationships,
the different cardinality ratios,
4339
04:08:18,900 --> 04:08:20,500
the different participations,
4340
04:08:20,500 --> 04:08:24,600
we can actually go ahead and convert this
into a database schema
4341
04:08:24,600 --> 04:08:28,400
which I'm going to show you guys
how to do in the next video.
4342
04:08:28,400 --> 04:08:33,300
So, stick around for that and we'll go ahead and
design our database based off this ER diagram.
4343
04:08:33,300 --> 04:08:37,200
[Converting ER Diagrams to Schemas]
4344
04:08:37,200 --> 04:08:43,200
In this tutorial I’m going to show you guys how to
convert an ER diagram into a database schema.
4345
04:08:43,200 --> 04:08:47,500
So, basically we're going to take
all of this information inside this ER diagram
4346
04:08:47,500 --> 04:08:49,700
which we created in the last tutorial.
4347
04:08:49,700 --> 04:08:55,000
And we're actually going to take this
and use it to create an actually database schema.
4348
04:08:55,000 --> 04:09:01,400
So, from this diagram right here we'll be able to,
you know, create and define actually database tables
4349
04:09:01,400 --> 04:09:04,700
that we can use in our relational database.
4350
04:09:04,700 --> 04:09:06,300
So, let’s go ahead and get started.
4351
04:09:06,300 --> 04:09:09,000
I’m going to show you guys basically step by step
4352
04:09:09,000 --> 04:09:14,500
how we can start converting this
into database tables and database relations.
4353
04:09:14,500 --> 04:09:19,200
So, the first step, Step 1,
is the mapping of regular entity types.
4354
04:09:19,200 --> 04:09:23,100
So, for each regular entity type,
we want to create a relation
4355
04:09:23,100 --> 04:09:28,400
which is just a table, that includes
all of the simple attributes of that entity.
4356
04:09:28,400 --> 04:09:31,100
So, here we have all of our regular entities.
4357
04:09:31,100 --> 04:09:33,300
We have branch, client, and employee.
4358
04:09:33,300 --> 04:09:36,800
And so, what we want to do
is just create relations or, you know,
4359
04:09:36,800 --> 04:09:40,300
basically database tables for each one
of those regular entities.
4360
04:09:40,300 --> 04:09:45,600
And then the columns of those relations are going to
be all of these attributes that we defined.
4361
04:09:45,600 --> 04:09:50,500
So, from that, from everything that’s inside of the
green squares we're going to get something like this.
4362
04:09:50,500 --> 04:09:52,700
So, we have our employee relation.
4363
04:09:52,700 --> 04:09:55,000
We have the employee ID which is the primary key.
4364
04:09:55,000 --> 04:09:58,100
First name, last name, birthdate, sex, and salary.
4365
04:09:58,100 --> 04:10:01,600
And we have the Branch with that information.
4366
04:10:01,600 --> 04:10:02,600
And then we have the Client.
4367
04:10:02,600 --> 04:10:06,700
So, I just want to show you guys
when we had a composite attribute,
4368
04:10:06,700 --> 04:10:11,200
so this name attribute over here,
we're just storing the sub-attribute.
4369
04:10:11,200 --> 04:10:13,800
So, we're just storing first name and last name.
4370
04:10:13,800 --> 04:10:17,100
All right, so here we have three relations
and lets see if we can start adding to those.
4371
04:10:17,100 --> 04:10:20,400
So, Step 2 is the mapping of weak entity types.
4372
04:10:20,400 --> 04:10:24,300
So, over here we have one weak entity type
which is inside of this green square.
4373
04:10:24,300 --> 04:10:28,200
For weak entity type we want to create
a relation or a table
4374
04:10:28,200 --> 04:10:33,500
that includes all of the simple attributes
of the weak entity.
4375
04:10:33,500 --> 04:10:37,100
And the primary key for the new relation should be
4376
04:10:37,100 --> 04:10:40,800
the partial key of the weak entity
plus the primary key of its owner.
4377
04:10:40,800 --> 04:10:47,500
In this case, the primary key of the owner is going to
be branch ID because the branch is the owner.
4378
04:10:47,500 --> 04:10:49,300
In other words, the branch is the entity
4379
04:10:49,300 --> 04:10:54,400
that’s participating in the identifying relationship
with branch supplier.
4380
04:10:54,400 --> 04:10:56,700
So, over here we'll see what happens.
4381
04:10:56,700 --> 04:10:59,300
So, you'll see we get this new table, Branch Supplier.
4382
04:10:59,300 --> 04:11:04,900
And the primary key is branch_id supplier_name,
and supply_type.
4383
04:11:04,900 --> 04:11:09,100
So, for this table, we included the supplier’s name
and the branch ID.
4384
04:11:09,100 --> 04:11:12,600
Both of those come together
to make our composite key.
4385
04:11:12,600 --> 04:11:13,800
It’s a compound key.
4386
04:11:13,800 --> 04:11:17,900
And then we have supply type
and then we end up with this.
4387
04:11:17,900 --> 04:11:21,000
So, now we have four tables, Employee,
Branch, Client, and Branch Supplier.
4388
04:11:21,000 --> 04:11:23,800
And they all have their associated attributes.
4389
04:11:23,800 --> 04:11:28,700
So, Step 3 is the mapping of
binary 1 to 1 relationship types.
4390
04:11:28,700 --> 04:11:33,900
Now, a binary relationship is a relationship
that has two entities participating in it.
4391
04:11:33,900 --> 04:11:37,800
For example, all of the actual relationships up here
are binary.
4392
04:11:37,800 --> 04:11:40,600
In other words, there’s two parties
that are participating.
4393
04:11:40,600 --> 04:11:43,700
And what we want to do is map 1 to 1 relationships.
4394
04:11:43,700 --> 04:11:47,400
So, we only have a single 1 to 1 relationship here.
4395
04:11:47,400 --> 04:11:48,600
It’s this manages relationship.
4396
04:11:48,600 --> 04:11:52,700
So, a branch can be managed by one employee
and an employee can manage 1 branch.
4397
04:11:52,700 --> 04:11:59,700
So, for each 1 to 1 binary relationship
we want to include one side of the relationship
4398
04:11:59,700 --> 04:12:02,300
as a foreign key in the other.
4399
04:12:02,300 --> 04:12:05,200
And we want to favor total participation.
4400
04:12:05,200 --> 04:12:11,500
So, in this case, we want to basically include
the primary key of one of these entities
4401
04:12:11,500 --> 04:12:15,600
as a foreign key in the other entity’s relation.
4402
04:12:15,600 --> 04:12:18,500
And we always want to favor the total participation.
4403
04:12:18,500 --> 04:12:22,200
So, if a particular entity has total participation
in their relationship,
4404
04:12:22,200 --> 04:12:25,200
then you want to add the foreign key
onto that entity.
4405
04:12:25,200 --> 04:12:27,700
So, in this case, branch has a total participation,
4406
04:12:27,700 --> 04:12:32,600
so we're going to add the employee’s ID
as a foreign key in the branch relation.
4407
04:12:32,600 --> 04:12:36,500
If both of them are partial participation
or both of them are total participation,
4408
04:12:36,500 --> 04:12:38,500
then you can just use your own discretion.
4409
04:12:38,500 --> 04:12:41,600
But in this case, it’s pretty clear
that we're going to use branch.
4410
04:12:41,600 --> 04:12:47,600
So, over here on the branch relation, I added in
a foreign key which is just manager ID.
4411
04:12:47,600 --> 04:12:54,100
And this is a foreign key which points to
this employee’s ID up here.
4412
04:12:54,100 --> 04:12:56,800
So, that’s how we're going to link
those two together.
4413
04:12:56,800 --> 04:13:00,500
Step 4 is the mapping of
binary 1 to N relationship types.
4414
04:13:00,500 --> 04:13:04,700
So, unlike a 1 to 1 relation,
now we're looking for 1 to N.
4415
04:13:04,700 --> 04:13:07,600
And you'll see that we have three of them here.
4416
04:13:07,600 --> 04:13:09,000
So, branch handles a client.
4417
04:13:09,000 --> 04:13:12,500
An employee supervises or is supervised
by another employee.
4418
04:13:12,500 --> 04:13:15,800
And a branch has employees working for it.
4419
04:13:15,800 --> 04:13:17,600
So, basically what we want to do
4420
04:13:17,600 --> 04:13:25,800
is we want to include the one side’s primary key
as a foreign key on the inside relation or table.
4421
04:13:25,800 --> 04:13:28,900
So, basically, what this means is that –
4422
04:13:28,900 --> 04:13:32,400
okay, so for example,
in this case we have a branch and an employee.
4423
04:13:32,400 --> 04:13:35,000
I want to include the one side’s primary key, right?
4424
04:13:35,000 --> 04:13:38,300
In other words, I want to include
the branch’s primary key
4425
04:13:38,300 --> 04:13:42,800
because that’s on the one side as a foreign key
on the employee relation.
4426
04:13:42,800 --> 04:13:45,700
So, basically on the employee relation,
4427
04:13:45,700 --> 04:13:50,200
now we're going to have a branch ID column
that will store a foreign key to the branch.
4428
04:13:50,200 --> 04:13:53,100
Same thing down here for a client and branch.
4429
04:13:53,100 --> 04:13:55,500
So, the branch over here is on the 1 side.
4430
04:13:55,500 --> 04:14:02,300
And basically, what that means is that we're going to
store in the Client table a foreign key to the branch.
4431
04:14:02,300 --> 04:14:05,800
And then same goes for this supervisor relationship.
4432
04:14:05,800 --> 04:14:11,500
So, on the Employee table we want to
store a foreign key to the supervisor.
4433
04:14:11,500 --> 04:14:14,100
And so, over here let’s take a look.
4434
04:14:14,100 --> 04:14:17,800
So, employee we have a super_id
and we have branch_id.
4435
04:14:17,800 --> 04:14:23,100
And that’s because the branch was
on the one side of the relationship
4436
04:14:23,100 --> 04:14:25,600
and the employee was on the N side of the relationship.
4437
04:14:25,600 --> 04:14:30,100
Which means that we're going to go ahead
and store the branch’s ID as a foreign key
4438
04:14:30,100 --> 04:14:31,500
on the Employee table.
4439
04:14:31,500 --> 04:14:34,400
And then the same goes for the supervisor ID.
4440
04:14:34,400 --> 04:14:37,500
So, obviously, with the supervisor ID,
it’s pointing to the Employee table.
4441
04:14:37,500 --> 04:14:39,500
So, we have to store it on the Employee table.
4442
04:14:39,500 --> 04:14:44,600
And then down here in the Client table
we stored as a foreign key the branch ID.
4443
04:14:44,600 --> 04:14:50,600
And again, that’s because the branch
was on the 1 side of that cardinality relationship.
4444
04:14:50,600 --> 04:14:54,200
You see, branch is on the one side
and the client was on the N side.
4445
04:14:54,200 --> 04:14:59,800
And so, that’s why we stored branch_id
as a foreign key on the Client table.
4446
04:14:59,800 --> 04:15:04,900
All right, and then Step 5 is the mapping
of binary M to N relationship types.
4447
04:15:04,900 --> 04:15:08,600
So, in this case, we only have one instance of this.
4448
04:15:08,600 --> 04:15:12,300
A client can work with an employee
and an employee can work with a client.
4449
04:15:12,300 --> 04:15:16,600
So, what we want to do in this case
is we want to create a new relation.
4450
04:15:16,600 --> 04:15:18,600
So, we're actually going to create a new table
4451
04:15:18,600 --> 04:15:23,900
whose primary key is a combination
of both entity’s primary keys.
4452
04:15:23,900 --> 04:15:27,400
And we’re also going to
include any relationship attributes.
4453
04:15:27,400 --> 04:15:30,700
So, over here, the client’s primary key is client_id.
4454
04:15:30,700 --> 04:15:32,900
The employee’s primary key is emp_id.
4455
04:15:32,900 --> 04:15:35,600
So, what we're going to do is create a new table
4456
04:15:35,600 --> 04:15:40,300
where we have a composite key
which is the employee ID and the client ID.
4457
04:15:40,300 --> 04:15:44,200
And actually, this would be
what we would call a compound key
4458
04:15:44,200 --> 04:15:49,900
because both of it’s keys are actually foreign keys.
4459
04:15:49,900 --> 04:15:53,900
And we want to store in this relationship
the attribute on the relationship,
4460
04:15:53,900 --> 04:15:57,800
or any attributes on the relationship
which in this case is just sales.
4461
04:15:57,800 --> 04:16:01,500
So, down here we created a new table
or a new relation which is Works On.
4462
04:16:01,500 --> 04:16:05,300
And you'll notice the key
is employee ID and client ID.
4463
04:16:05,300 --> 04:16:10,300
So, both of these individual parts of the primary key
are actually foreign keys themselves.
4464
04:16:10,300 --> 04:16:12,300
So, this is a special situation.
4465
04:16:12,300 --> 04:16:14,300
And then over here we have total sales
4466
04:16:14,300 --> 04:16:19,500
which was the attribute that we stored
on the relationship like that.
4467
04:16:19,500 --> 04:16:28,500
And so, basically that, in essence, is going to allow us
to take this ER diagram and convert it into relations.
4468
04:16:28,500 --> 04:16:34,600
Now if you have more complex relationships,
like if you have nonbinary relationships
4469
04:16:34,600 --> 04:16:38,300
then it gets a little bit more complex
when we're mapping them.
4470
04:16:38,300 --> 04:16:42,000
In this case, I’m just kind of looking at
basic ER diagrams.
4471
04:16:42,000 --> 04:16:43,600
I don’t want to get too complex.
4472
04:16:43,600 --> 04:16:50,800
So, in this case, in five steps, we're able to basically
convert the ER diagram into a set of relations.
4473
04:16:50,800 --> 04:16:56,300
But if you do have more advanced types of ER
diagrams, then there are going to be more steps.
4474
04:16:56,300 --> 04:17:02,000
But for our cases, there’s only going to be five steps
that we need to basically convert this into relations.
4475
04:17:02,000 --> 04:17:07,000
And so, now basically what we have here
is we have our database tables, right?
4476
04:17:07,000 --> 04:17:10,000
Each of these relations is itself a database table.
4477
04:17:10,000 --> 04:17:12,700
So, when I'm designing my database now,
4478
04:17:12,700 --> 04:17:16,800
I know I have to have an Employee table with
all this stuff, a Branch table with all this stuff.
4479
04:17:16,800 --> 04:17:18,400
Client table, etc.
4480
04:17:18,400 --> 04:17:21,700
And so, what we can do also
and what you'll see a lot of times
4481
04:17:21,700 --> 04:17:25,000
is people will draw little arrows
to define the relationships.
4482
04:17:25,000 --> 04:17:29,500
So, this can get a little bit messy
which is why I kind of saved it for the end.
4483
04:17:29,500 --> 04:17:32,700
But you can see over here, for employee,
4484
04:17:32,700 --> 04:17:38,400
on the employee’s foreign keys
I’m drawing arrows to what they relate to.
4485
04:17:38,400 --> 04:17:42,300
So, for example, super_id,
I have an arrow going back to emp_id.
4486
04:17:42,300 --> 04:17:46,200
branch_id I have a little line here going over
to branch_id.
4487
04:17:46,200 --> 04:17:50,300
Mgr_id over here, we have a line
going up to emp_id, etc.
4488
04:17:50,300 --> 04:17:54,600
So, this is basically just like mapping out
all the different relationships.
4489
04:17:54,600 --> 04:17:56,800
This, like I said, it gets a little messy.
4490
04:17:56,800 --> 04:18:01,100
And it’s pretty difficult to read
if you have more than a couple tables.
4491
04:18:01,100 --> 04:18:06,200
But you also – you'll see people doing this a lot,
so I just wanted to show you guys how that works.
4492
04:18:06,200 --> 04:18:10,300
So, now that we have our, you know,
essentially our relations, our database tables,
4493
04:18:10,300 --> 04:18:12,200
we can actually create a database.
4494
04:18:12,200 --> 04:18:17,100
So, over here I have an example of what
a database might look like from these relations.
4495
04:18:17,100 --> 04:18:19,300
So, we have our actually database.
4496
04:18:19,300 --> 04:18:21,200
So, up here we have all our employees.
4497
04:18:21,200 --> 04:18:23,200
And you'll notice we have our employee ID
4498
04:18:23,200 --> 04:18:25,900
so we can define like the supervisor
of each employee.
4499
04:18:25,900 --> 04:18:30,000
So, like Angela Martin’s supervisor
is employee number 101.
4500
04:18:30,000 --> 04:18:32,600
So, Angela Martin’s supervisor
is Michael Scott, right.
4501
04:18:32,600 --> 04:18:35,500
You'll see how it easy it is now for us
to define all this stuff.
4502
04:18:35,500 --> 04:18:38,600
Angela Martin also works at branch_id number 2.
4503
04:18:38,600 --> 04:18:40,400
So, that links over here to the branch.
4504
04:18:40,400 --> 04:18:42,100
So, branch 2 is Scranton, etc.
4505
04:18:42,100 --> 04:18:45,100
And then we have our Client table over here.
4506
04:18:45,100 --> 04:18:47,000
And we have our Works With table.
4507
04:18:47,000 --> 04:18:50,600
So, the Works With table
has the employee ID and the client ID.
4508
04:18:50,600 --> 04:18:53,300
And then we have our Branch Supplier table.
4509
04:18:53,300 --> 04:18:59,900
So, all of these got basically put into our database
and then we started putting information in there.
4510
04:18:59,900 --> 04:19:05,800
And so, really what this is, is it’s a way for us
to go from just a set of requirements
4511
04:19:05,800 --> 04:19:11,900
like we saw in the last video, to our actual
finished database table, what you see here.
4512
04:19:11,900 --> 04:19:15,000
In designing relational database schemas,
4513
04:19:15,000 --> 04:19:17,900
and you know, the schema
is just like this whole thing, right?
4514
04:19:17,900 --> 04:19:19,700
It’s not super easy.
4515
04:19:19,700 --> 04:19:24,800
If you have a very simple database, you know,
if you have a very simple set of database storage
4516
04:19:24,800 --> 04:19:28,000
or requirements, then obviously the schema
is going to be very simple
4517
04:19:28,000 --> 04:19:30,800
and you might not need something like an ER diagram.
4518
04:19:30,800 --> 04:19:35,700
But with something like this,
an ER diagram is hugely useful.
4519
04:19:35,700 --> 04:19:38,400
So, here’s the thing, you don’t necessarily
need the ER diagram, right?
4520
04:19:38,400 --> 04:19:39,800
You don’t need it.
4521
04:19:39,800 --> 04:19:43,900
But it’s a really, really great way
to convert requirements
4522
04:19:43,900 --> 04:19:48,600
into an actual database schema
or a set of relations.
4523
04:19:48,600 --> 04:19:51,300
And so, that kind of show you guys
how you can do that.
4524
04:19:51,300 --> 04:19:53,300
Now, here’s the thing with ER diagram.
4525
04:19:53,300 --> 04:19:54,800
I only showed you guys one example.
4526
04:19:54,800 --> 04:20:00,100
And I think this is actually a pretty good example
because it covers all of the main use cases.
4527
04:20:00,100 --> 04:20:06,000
But, you know, the only way that your going to get
good at using the ER diagrams and building them
4528
04:20:06,000 --> 04:20:09,500
and, you know, converting them into database
schemas is just by doing it a bunch.
4529
04:20:09,500 --> 04:20:11,300
And so, just by practicing.
4530
04:20:11,300 --> 04:20:15,200
So, obviously, I’m not going to spend, you know,
dozens of videos doing dozens of these examples,
4531
04:20:15,200 --> 04:20:19,900
but hopefully this example kind of shows you guys
the basics so now you can go on
4532
04:20:19,900 --> 04:20:22,600
and, you know, design your own ER diagrams
4533
04:20:22,600 --> 04:20:25,900
and then convert them into database schemas
following those rules.
4534
04:20:25,900 --> 04:20:27,900
And all of the rules that I showed you guys
4535
04:20:27,900 --> 04:20:32,000
for converting ER diagram into relations
that’s going to apply to any ER diagram.
4536
04:20:32,000 --> 04:20:35,100
So, what we talked about in this video will, you know,
4537
04:20:35,100 --> 04:20:38,600
you can basically take any ER diagram
and convert into something like this.
440242
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.