1
00:00:00,090 --> 00:00:04,560
In this video, we're going to talk about renaming tables and also renaming columns.

2
00:00:04,680 --> 00:00:06,300
If you want to rename a table.

3
00:00:06,570 --> 00:00:07,800
We have two choices.

4
00:00:07,800 --> 00:00:13,890
We can do a simple command, a simple statement, which is rename table and then the current name,

5
00:00:13,890 --> 00:00:15,930
and then to the new name.

6
00:00:16,290 --> 00:00:20,100
Or I also can do the same thing but put alter table in front of it.

7
00:00:20,130 --> 00:00:23,820
Either one works, but this is shorter, so rename table.

8
00:00:23,820 --> 00:00:25,260
Let's try an example.

9
00:00:25,980 --> 00:00:29,310
We have let's see, show tables.

10
00:00:29,940 --> 00:00:30,930
We have companies.

11
00:00:30,930 --> 00:00:33,660
Let's rename companies to be suppliers to start.

12
00:00:33,660 --> 00:00:35,670
So select start from companies.

13
00:00:37,320 --> 00:00:43,380
I want to rename table that's called companies to suppliers.

14
00:00:43,890 --> 00:00:47,820
And now if I try and select start from companies, it doesn't exist.

15
00:00:47,820 --> 00:00:55,620
But if I select star from employers or what am I doing suppliers, we see the same table.

16
00:00:55,620 --> 00:01:02,730
It just had a new name, so now it's called suppliers and I could rename it back using the alter table

17
00:01:02,730 --> 00:01:03,480
syntax.

18
00:01:03,480 --> 00:01:05,970
So this is the other way of renaming a table.

19
00:01:05,970 --> 00:01:15,450
We use alter table, This is the alter table docs and then we do the table name and then rename to the

20
00:01:15,450 --> 00:01:16,290
new table name.

21
00:01:16,290 --> 00:01:25,890
So if you prefer this syntax alter table and then suppliers rename to and then what do I want to rename

22
00:01:25,890 --> 00:01:26,580
it to?

23
00:01:27,240 --> 00:01:28,620
Let's go back to companies.

24
00:01:29,790 --> 00:01:34,560
And now if I do show tables, we have a company's table.

25
00:01:34,560 --> 00:01:39,420
If I select star from suppliers doesn't exist.

26
00:01:39,420 --> 00:01:41,310
Select star from companies though.

27
00:01:42,700 --> 00:01:43,450
There we are.

28
00:01:43,480 --> 00:01:45,640
We see the same table as before.

29
00:01:45,940 --> 00:01:48,900
So two different ways of renaming a table.

30
00:01:48,910 --> 00:01:51,840
But now I want to talk about renaming columns.

31
00:01:51,880 --> 00:01:58,450
So when we want to rename a column, we use Alter table the table we're trying to alter and then rename

32
00:01:58,450 --> 00:02:02,920
column what the current name is and then what we want to change it to.

33
00:02:02,920 --> 00:02:07,690
Like the if it's called name, we'll rename it as biz name.

34
00:02:08,050 --> 00:02:09,940
So I'll show an example of this.

35
00:02:10,660 --> 00:02:13,420
What do we have that we could rename?

36
00:02:13,450 --> 00:02:15,790
How about in our company's table?

37
00:02:15,970 --> 00:02:19,480
We have name, let's call it company name.

38
00:02:19,480 --> 00:02:21,830
So I want to rename this column.

39
00:02:21,850 --> 00:02:22,210
Right.

40
00:02:22,210 --> 00:02:24,370
If I do select star from companies just one more time.

41
00:02:24,370 --> 00:02:29,610
Here we see it's called name or describe companies.

42
00:02:29,620 --> 00:02:31,270
We see the column name.

43
00:02:31,480 --> 00:02:32,720
So let's rename that.

44
00:02:32,740 --> 00:02:40,360
Let's do a alter table, the table name that we're altering, and then what we're trying to do, rename

45
00:02:40,360 --> 00:02:47,590
column and this is important in this example, rename column column is required.

46
00:02:47,590 --> 00:02:48,820
It's not optional.

47
00:02:48,970 --> 00:02:57,400
Whereas with ADD column as we saw, that was optional Drop column column is optional, but with rename

48
00:02:57,400 --> 00:02:58,390
column is required.

49
00:02:58,390 --> 00:03:00,730
So that's another reason I like to use column always.

50
00:03:00,730 --> 00:03:01,960
It's just be consistent.

51
00:03:01,990 --> 00:03:05,980
Then you don't have to remember when you can use it and when you can't or when it's optional.

52
00:03:06,820 --> 00:03:10,840
Okay, so rename column old column name to the new column name.

53
00:03:11,020 --> 00:03:18,130
So rename column and it's called name right now to company name.

54
00:03:19,320 --> 00:03:22,650
All right, let's run that here.

55
00:03:22,680 --> 00:03:24,120
It's called name, as we see.

56
00:03:24,750 --> 00:03:30,280
I rename it if I describe companies now, it is called Company name.

57
00:03:30,300 --> 00:03:34,740
If I select start from companies, we see company name.

58
00:03:35,070 --> 00:03:37,200
So that's how we can rename a column.

59
00:03:37,200 --> 00:03:38,100
Next.

60
00:03:38,100 --> 00:03:43,020
What about if we wanted to change the data type or constraints or something on that column?

