3.16 Primary Keys
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
7 hours 36 minutes
I welcome back to Model Three sequel programming were in less than five. Tables were moving into Sub Lesson 5.3 primary keys
and the previous lesson. We created a simple small table within the D Beaver environment, but we left out an important feature. In fact, this is a future that every table within a database should have, and that is the primary key.
The primary key has a very special purpose, and that purpose is to provide a unique value for every record in the table, so that when programmers work with those database tables, they can use primary keys and foreign keys to know what data belongs to, what data from one table to the next table.
So their purpose is really for communication between tables. Now, for example, let's say that Social Security number was a primary key.
Everyone in the United States is supposed to have a unique Social Security number. We didn't allow it to be no, so it's not allowed to be knows. And that's another important feature of the primary key is the camp. You know,
I need to be unique,
so if we then had an address table
and you saw your Social Security number in that address table. Next to address fields such as, you know, ST ST ST City, you would know that address belongs to you. And what we've done then is we've created a address model and a person model and those air tables, and we connect them by this Social Security number.
Now, this example, we're not going to use this. Oh so Social Security number. We're going to use what you more commonly see, which is a special I. D field that just gets a unique value for every record that you add.
So let's go ahead and start doing that.
So we're going to add a new column.
We're going to call it I D.
Now. We don't want it to be native, and we'll go ahead and give us some more memory. So we'll say you can have more memory by saying bigots
Now The size of that imager varies can vary by system and database. We just know that it's going to be larger than a regular indenture. Never say. Do not worry about negative numbers. Use all available memory to reach the highest positive number that you could possibly reach if the database table grows at large
and in our example, it won't.
And we're gonna say it's not no
has to be filled in an auto increments. We don't have to worry about this column. It ah, increments by itself.
I'm gonna go ahead and click, Okay,
Now, if you try to click save now, we will get in there.
And that's because there's a rule that says the auto column and must be defined as a key, which is what we need to do. And to do that in d B, we're gonna go into the constraints area by clicking on constraints. We're gonna right click and we're gonna say, Create new constraint
And it's already got
some of the options selected that we need. There's gonna be a primary key
personnel to score Piquet for Primary Key
and I'm gonna select I d
and Click. OK,
so what we've done if he created a primary key by creating a new column called I D
give it a data type of big ant, unsigned
said, You can't be no and you auto increment.
We then went into constraints area
and we defined our constraint by saying this column
I D is the primary key. Now let's go ahead and click save, and this time it will work
Okay, it looks like we hit a little bit of a snag. Let's go ahead and get this fixed. So what it's happening is De Beaver is trying Thio
create the auto increment column,
and it's failing on this part right here. It never sees this part.
So let's go in and fix is on the way we're going to do that
is we're going to you
get rid of the auto increments Check box,
now it saved it.
It is a primary key, and now we're going to turn on the auto increments and click, save
and click persists.
And there we go. So we had to break it apart a little bit, because when we tried to do it all at once, the beaver did not automatically arrange it in the right order that my sequel would understand.
And that happens from time to time in various environments. And it's why it's a good reason Thio get used to looking at the code and understanding what might be going wrong,
because sometimes in these environments, they do it. They they offer a lot of help.
But sometimes things don't get arranged quite right. And you have to break it apart.
And, ah, adjusted a little bit to make it work. And usually, if you're in an environment like that, you can just start to take it one step at a time, which is, you know, one step, save one step save and get to your destination.
Now, to recap on what we covered in this lesson, as we briefly discussed primary keys in their purpose, Primary key every database tables should have won. Its primary purpose is to designate
a unique value for every record they don't repeat.
And they can't be empty.
And they're very important.
And their importance is for communication.
Across tables by a primary keys and foreign keys. We will discuss foreign Keyes and the next lesson.
I hope you enjoy this lesson. I hope you got your primary key working, and I'll see you in the next lesson. Thank you.