This has been a bit of a long section, but we've got one more topic to cover around creating tables,
inserting data constraints.
We're going to talk about this key portion that we see printed out.
Currently, it's empty when we describe one of our tables, so I described cats four or any of our tables.
So far, Key just has nothing there.
So what is this key thing?
Well, right now with our table, I could insert rows that are identical to one another.
In fact, I could do that very easily.
Right?
If I just insert empty cats, we get those default values.
So now, if I select star from cats for we have a whole bunch of rows that are completely identical,
and maybe that's fine to have cats that have the same name and the same age, but it makes it really
hard to work with these rows and distinguish them from one another.
It's just really a challenge in the real world.
If we had data that looks like this, we would make sure we added on some sort of unique identifier.
And this is a really common thing to do in the world of SQL, not just my SQL, but pretty much all
of SQL.
Not pretty much all of SQL.
Definitely all of SQL.
We add unique IDs to rows so that we can differentiate them even when all the other data looks the same.
So this is something we'll do for almost every single table you ever make is add some form of a unique
identifier.
So in our case we could do something very simple just add a numeric ID that starts at one and then goes
to two, three and four.
And this is a really common thing to do, not just to have a unique ID, but to just have it be an integer
that starts at one and then goes up by one for each row.
So this way, even if we all have every row is Monte Tabby ten, they're still guaranteed to be unique
as long as the cat ID is unique.
So there's a couple of ways of doing this.
One is I guess just make this column and say it's a number, but that doesn't necessarily require it
to be unique.
If I just made an integer column, well, what would stop me from adding one and then one and then one
and then one in the same way that all the ages can be set to ten.
We don't want to do that.
Instead, we're going to declare our column, the ID column, the primary key for this table.
So a primary key is a fancy way of saying a unique identifier.
We'll talk about something called foreign keys later.
So key is a term in the world of SQL.
Primary key is going to be some column that's used as a unique identifier for each individual row,
so they have to be unique within the table.
So when we define a table, we can add on the primary key constraint to some column, usually an ID
column.
So here I have a cat ID, which honestly I'd probably just call ID.
We know that it's in the cat's table, but just to make it clearer here, I called it Cat ID, It's
a number.
It can't be null.
Now we're going to come back to this.
Not an old thing in a moment.
And it's a primary key, so that tells my SQL this.
It's not only required, but it has to be unique.
So if I just copy and paste this.
And put it in my terminal here.
First of all, if I do a describe the table, which I called Unique Katz We'll see.
That key is set to pry for primary for Cat ID.
So what this means is that it has to be there.
It has to be unique.
If I try and insert into unique cats and I insert cat ID name and then age values, we have, I don't
know, we'll start with one.
And the name can be Bingo.
Age can be two.
We insert that.
It works great as far as we can tell.
Now, if I recall that line and I try and do the same thing, but this time my cat that I'm inserting
is called Bongo.
And Bongo is three, but I leave one as my cat ID, I get an error.
It says duplicate entry one for this key, this unique primary key.
You can't do that.
If I do a select star from Unique Cat, we don't see that second row we tried to insert.
So we have to have that unique value.
I could then go back and say, All right, values too will be my cat ID.
Now this is kind of annoying to have to keep going and manually adding in some unique number.
There's nothing stopping me, by the way, from just jumping to 999 or -99.
It's not a constraint that says this thing has to go up by one.
Instead, it just says it's a primary key.
It has to be unique and it has to be a number because we said it was an integer.
Anyway, I think I will stop here.
We have a bit more to talk about the primary keys in the next video, but I want to keep you sane and
keep your hair not pulled out of your head.
So I'll split it and I'll see you in a second.
I don't know if it makes a difference.
I don't think so.