In this video, we're going to talk about renaming tables and also renaming columns.
If you want to rename a table.
We have two choices.
We can do a simple command, a simple statement, which is rename table and then the current name,
and then to the new name.
Or I also can do the same thing but put alter table in front of it.
Either one works, but this is shorter, so rename table.
Let's try an example.
We have let's see, show tables.
We have companies.
Let's rename companies to be suppliers to start.
So select start from companies.
I want to rename table that's called companies to suppliers.
And now if I try and select start from companies, it doesn't exist.
But if I select star from employers or what am I doing suppliers, we see the same table.
It just had a new name, so now it's called suppliers and I could rename it back using the alter table
syntax.
So this is the other way of renaming a table.
We use alter table, This is the alter table docs and then we do the table name and then rename to the
new table name.
So if you prefer this syntax alter table and then suppliers rename to and then what do I want to rename
it to?
Let's go back to companies.
And now if I do show tables, we have a company's table.
If I select star from suppliers doesn't exist.
Select star from companies though.
There we are.
We see the same table as before.
So two different ways of renaming a table.
But now I want to talk about renaming columns.
So when we want to rename a column, we use Alter table the table we're trying to alter and then rename
column what the current name is and then what we want to change it to.
Like the if it's called name, we'll rename it as biz name.
So I'll show an example of this.
What do we have that we could rename?
How about in our company's table?
We have name, let's call it company name.
So I want to rename this column.
Right.
If I do select star from companies just one more time.
Here we see it's called name or describe companies.
We see the column name.
So let's rename that.
Let's do a alter table, the table name that we're altering, and then what we're trying to do, rename
column and this is important in this example, rename column column is required.
It's not optional.
Whereas with ADD column as we saw, that was optional Drop column column is optional, but with rename
column is required.
So that's another reason I like to use column always.
It's just be consistent.
Then you don't have to remember when you can use it and when you can't or when it's optional.
Okay, so rename column old column name to the new column name.
So rename column and it's called name right now to company name.
All right, let's run that here.
It's called name, as we see.
I rename it if I describe companies now, it is called Company name.
If I select start from companies, we see company name.
So that's how we can rename a column.
Next.
What about if we wanted to change the data type or constraints or something on that column?