Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,256 --> 00:00:02,304
In the last video
1
2
00:00:02,816 --> 00:00:04,352
We saw how to delete the rows
2
3
00:00:04,608 --> 00:00:05,376
In a table
3
4
00:00:06,400 --> 00:00:09,472
Let us now look at how to change the structure of a table
4
5
00:00:10,496 --> 00:00:13,312
If I run select star command now on the table
5
6
00:00:17,152 --> 00:00:19,456
We can see that the table has
6
7
00:00:19,712 --> 00:00:20,480
Five columns
7
8
00:00:22,016 --> 00:00:24,576
And each column has a column name
8
9
00:00:24,832 --> 00:00:26,112
and a column type
9
10
00:00:27,904 --> 00:00:29,440
Adding or deleting a column
10
11
00:00:29,696 --> 00:00:32,256
or changing this column name or this type
11
12
00:00:32,512 --> 00:00:35,840
On adding constraint is called changing the structure of table
12
13
00:00:36,864 --> 00:00:40,960
To change the structure of the table we will be using alter table command
13
14
00:00:41,984 --> 00:00:43,520
Let us look at the presentation
14
15
00:00:45,824 --> 00:00:49,408
So the syntax of this alter command goes like this
15
16
00:00:49,920 --> 00:00:53,248
We will write alter table and then write the table name
16
17
00:00:53,760 --> 00:00:56,064
And then we'll specify the action
17
18
00:00:57,088 --> 00:01:00,160
actions can be broadly classified into three parts
18
19
00:01:00,672 --> 00:01:02,720
one will be actions on columns
19
20
00:01:02,976 --> 00:01:07,328
Which will include adding deleting modifying or renaming a column
20
21
00:01:08,096 --> 00:01:08,608
or
21
22
00:01:09,120 --> 00:01:10,656
It can be on constraints
22
23
00:01:10,912 --> 00:01:15,008
Which will include adding or deleting a constraint or you can add or delete an index
23
24
00:01:16,288 --> 00:01:19,872
Let us first look at how to add or delete a column
24
25
00:01:22,432 --> 00:01:25,504
To add a column we will write alter table
25
26
00:01:25,760 --> 00:01:26,784
And the table name
26
27
00:01:27,040 --> 00:01:27,808
Then type
27
28
00:01:28,064 --> 00:01:28,576
Add
28
29
00:01:29,088 --> 00:01:33,952
And then write the column name and then specify the data type of that column name
29
30
00:01:34,464 --> 00:01:37,792
Let's go to the PG admin interface
30
31
00:01:41,120 --> 00:01:43,936
Let us type alter table
31
32
00:01:45,984 --> 00:01:47,008
Customer table
32
33
00:01:48,288 --> 00:01:50,592
this is the table name
33
34
00:01:52,128 --> 00:01:57,760
then Add then what will be the column name it will be test column
34
35
00:01:59,552 --> 00:02:03,136
And it contains a
35
36
00:02:03,392 --> 00:02:05,952
VARCHAR type of
36
37
00:02:07,744 --> 00:02:08,256
Data
37
38
00:02:08,512 --> 00:02:09,792
Let us run this command
38
39
00:02:14,656 --> 00:02:15,936
Query was successful
39
40
00:02:16,448 --> 00:02:19,520
Let us run the select star command again
40
41
00:02:21,568 --> 00:02:22,336
You can see
41
42
00:02:23,360 --> 00:02:26,688
at the end of the table we have a new column called test
42
43
00:02:27,456 --> 00:02:30,528
And it has that particular data type that we specified which is
43
44
00:02:31,040 --> 00:02:32,320
Variable characters
44
45
00:02:33,088 --> 00:02:35,392
With 255 maximum limit
45
46
00:02:37,440 --> 00:02:39,488
This is how we added a column in our table
46
47
00:02:40,000 --> 00:02:42,048
No let us delete this column
47
48
00:02:42,304 --> 00:02:44,096
Using the delete command
48
49
00:02:44,608 --> 00:02:46,144
Let us look at the syntax first
49
50
00:02:46,656 --> 00:02:48,704
To delete a column
50
51
00:02:48,960 --> 00:02:51,264
Drop command is used. So we will
51
52
00:02:51,776 --> 00:02:55,872
type Alter table tablename then write drop
52
53
00:02:56,384 --> 00:02:57,664
and then the column name
53
54
00:02:58,432 --> 00:03:02,528
we can also write drop column and then specify the column name
54
55
00:03:03,040 --> 00:03:05,344
Let us try these two variants
55
56
00:03:14,304 --> 00:03:16,352
Drop
56
57
00:03:18,400 --> 00:03:19,168
Test
57
58
00:03:19,424 --> 00:03:20,960
Let's run this
58
59
00:03:23,264 --> 00:03:24,288
it was successful
59
60
00:03:24,544 --> 00:03:30,176
let us run the select start command again. you can see the last column is now deleted
60
61
00:03:30,688 --> 00:03:35,040
You can also write drop column and then specify the column name
61
62
00:03:36,320 --> 00:03:38,368
I'll add the test
62
63
00:03:38,880 --> 00:03:45,024
column again and now I will delete it again using the Drop column command
63
64
00:04:01,408 --> 00:04:04,480
Just run the select star command again to check
64
65
00:04:05,248 --> 00:04:07,552
Yes the test column is
65
66
00:04:07,808 --> 00:04:08,576
Deleted
66
67
00:04:10,112 --> 00:04:11,648
Next we will look at how to
67
68
00:04:12,160 --> 00:04:13,696
Modify a column
68
69
00:04:13,952 --> 00:04:19,327
What can we change in a column, we can change the data type that column will be containing
69
70
00:04:19,583 --> 00:04:20,863
If earlier
70
71
00:04:21,119 --> 00:04:24,191
Age is an integer type of column
71
72
00:04:24,703 --> 00:04:28,287
We can change that data type to variable characters
72
73
00:04:29,311 --> 00:04:31,359
The syntax for that will be
73
74
00:04:31,615 --> 00:04:32,639
Alter table
74
75
00:04:32,895 --> 00:04:35,711
Table name and then alter column
75
76
00:04:36,479 --> 00:04:40,575
Column name and then you specify the new data type for that column
76
77
00:04:40,831 --> 00:04:42,879
Let us go and change the data type of
77
78
00:04:43,135 --> 00:04:45,183
age column from integer to
78
79
00:04:45,439 --> 00:04:47,743
Variable characters
79
80
00:05:00,287 --> 00:05:03,871
Alter table customer table then alter column
80
81
00:05:04,127 --> 00:05:08,735
Column name
81
82
00:05:10,527 --> 00:05:16,671
Type then the new type which will be VARCHAR
82
83
00:05:16,927 --> 00:05:22,815
Run this command
83
84
00:05:23,839 --> 00:05:26,399
It was successful
84
85
00:05:26,655 --> 00:05:29,215
Run the select star command to look at your data
85
86
00:05:29,471 --> 00:05:32,799
You can see age which was earlier integer
86
87
00:05:33,055 --> 00:05:34,847
Is now variable character type
87
88
00:05:35,359 --> 00:05:38,943
now you want to rename any particular column
88
89
00:05:39,967 --> 00:05:43,039
Say for example we have email id column here
89
90
00:05:43,551 --> 00:05:45,855
If you want to rename to customer email
90
91
00:05:46,879 --> 00:05:49,183
How to do that let us look at the syntax
91
92
00:05:50,463 --> 00:05:52,767
So to rename a column this is the syntax
92
93
00:05:53,023 --> 00:05:54,047
Alter table
93
94
00:05:54,303 --> 00:05:57,119
Mention the table name then rename column
94
95
00:05:58,143 --> 00:06:00,959
first you will write the column name the old column name
95
96
00:06:01,215 --> 00:06:03,007
Then the keyword to
96
97
00:06:03,263 --> 00:06:06,079
and then you specify the new column name
97
98
00:06:06,847 --> 00:06:09,407
Let us go and change the name of
98
99
00:06:09,663 --> 00:06:10,943
Email id column
99
100
00:06:11,711 --> 00:06:17,855
Alter table tablename then
100
101
00:06:18,111 --> 00:06:21,439
Rename column
101
102
00:06:21,951 --> 00:06:25,023
Old column name
102
103
00:06:25,279 --> 00:06:30,911
then to new column name
103
104
00:06:34,239 --> 00:06:36,543
Run this command
104
105
00:06:39,103 --> 00:06:40,127
It was successful
105
106
00:06:40,639 --> 00:06:42,431
let us run the select star command
106
107
00:06:42,943 --> 00:06:44,479
You can see the
107
108
00:06:44,735 --> 00:06:48,319
Column name which was earlier email ID is now customer email
108
109
00:06:49,599 --> 00:06:50,879
Now let us look at
109
110
00:06:51,135 --> 00:06:51,903
How to
110
111
00:06:52,927 --> 00:06:56,255
Add or delete a constraint
111
112
00:06:56,511 --> 00:07:00,863
so till now we did not have any constraint on any of the columns
112
113
00:07:01,887 --> 00:07:03,679
If I want to add a constraint
113
114
00:07:03,935 --> 00:07:04,703
Such as
114
115
00:07:04,959 --> 00:07:08,543
My first name or the customer ID cannot be null
115
116
00:07:09,567 --> 00:07:11,359
Show that whenever I am getting the
116
117
00:07:11,615 --> 00:07:13,151
Data entered from any source
117
118
00:07:13,919 --> 00:07:19,039
My database checks and confirms that it is conforming to the specified constraints
118
119
00:07:20,575 --> 00:07:24,415
To do that we will need to add that particular constraint to our table
119
120
00:07:24,927 --> 00:07:26,207
To add that constraint
120
121
00:07:26,463 --> 00:07:28,511
This is the Syntax alter table
121
122
00:07:28,767 --> 00:07:33,631
Table name alter column column name set to not null
122
123
00:07:34,143 --> 00:07:36,959
so this particular column cannot be null
123
124
00:07:37,215 --> 00:07:40,543
Whenever you will be inserting data into any particular column
124
125
00:07:40,799 --> 00:07:44,895
This column must have some value if it is not having any value it will give an error
125
126
00:07:46,687 --> 00:07:48,735
so let us go and run this query
126
127
00:07:48,991 --> 00:07:54,879
Alter table customer table
127
128
00:07:55,135 --> 00:07:59,999
Alter column
128
129
00:08:00,767 --> 00:08:01,535
Column name
129
130
00:08:03,327 --> 00:08:06,143
This is
130
131
00:08:06,399 --> 00:08:12,543
Cust ID and this should be not null so set not null
131
132
00:08:12,799 --> 00:08:18,175
So the not null constraint will be applied to
132
133
00:08:18,431 --> 00:08:19,199
Cust ID now
133
134
00:08:19,711 --> 00:08:21,503
let us run this
134
135
00:08:22,271 --> 00:08:28,415
This is successful. Now let us try to insert values where customer ID is missing
135
136
00:08:29,695 --> 00:08:31,231
so we will insert into
136
137
00:08:31,487 --> 00:08:34,815
Customer table
137
138
00:08:37,375 --> 00:08:41,471
we'll need to specify the column names
138
139
00:08:43,007 --> 00:08:47,615
It'll start with first name last name
139
140
00:08:49,663 --> 00:08:54,783
Age and email ID
140
141
00:08:55,807 --> 00:09:01,439
Customer email
141
142
00:09:01,695 --> 00:09:07,071
And then it will have values
142
143
00:09:07,839 --> 00:09:10,655
ABCD
143
144
00:09:10,911 --> 00:09:11,679
Aa
144
145
00:09:14,495 --> 00:09:16,543
bb
145
146
00:09:19,871 --> 00:09:24,735
25
146
147
00:09:32,159 --> 00:09:34,207
And an email id
147
148
00:09:45,215 --> 00:09:47,007
So when I run this command
148
149
00:09:47,519 --> 00:09:48,799
I get this error
149
150
00:09:49,311 --> 00:09:51,871
Null value in column customer ID
150
151
00:09:52,127 --> 00:09:56,735
Which violates the not null constraint. this is the constraint we applied earlier
151
152
00:09:57,247 --> 00:09:59,551
Now if I want to remove this constraint
152
153
00:10:00,063 --> 00:10:03,647
How to remove this particular constrain. Let us go and check the syntax first
153
154
00:10:03,903 --> 00:10:08,255
Syntax to remove the not null constraint is same as above but
154
155
00:10:08,511 --> 00:10:11,839
Instead of the set keyword, we will be using the Drop keyword
155
156
00:10:12,351 --> 00:10:18,495
so let us go and write this command with the Drop keyword
156
157
00:10:18,751 --> 00:10:22,591
Alter table tablename
157
158
00:10:25,919 --> 00:10:29,247
Alter column
158
159
00:10:29,503 --> 00:10:35,647
Cust ID
159
160
00:10:37,183 --> 00:10:39,743
Drop not null
160
161
00:10:39,999 --> 00:10:46,143
So this will drop the constraint of not null. Let us run this
161
162
00:10:48,447 --> 00:10:52,287
Ran successfully. Now let us try to insert the earlier one
162
163
00:10:54,847 --> 00:10:59,199
This also ran successfully. Let us run the Select start command to check if
163
164
00:10:59,455 --> 00:11:00,479
the Data is added
164
165
00:11:00,991 --> 00:11:04,319
You can see cust ID is having null value
165
166
00:11:04,575 --> 00:11:05,087
because
166
167
00:11:05,343 --> 00:11:07,391
It did not have the constraint of not null
167
168
00:11:08,671 --> 00:11:09,439
Next
168
169
00:11:10,719 --> 00:11:14,815
now if you remember we also discussed that there is a constraint named check
169
170
00:11:15,071 --> 00:11:19,423
If you want to put a check on the value that any particular column is containing
170
171
00:11:20,703 --> 00:11:22,495
so let us now add a constraint
171
172
00:11:23,263 --> 00:11:25,823
Where the customer ID value
172
173
00:11:26,079 --> 00:11:29,407
Cannot be less than zero so it cannot be negative
173
174
00:11:29,919 --> 00:11:34,015
So we'll add a constraint that the value should be greater than equal to 1
174
175
00:11:34,783 --> 00:11:36,831
so let us go to the PG admin interface
175
176
00:11:37,087 --> 00:11:38,623
And add this constraint
176
177
00:11:39,135 --> 00:11:45,279
Alter table tablename
177
178
00:11:48,351 --> 00:11:52,703
Add constraint constraint
178
179
00:11:52,959 --> 00:11:55,007
Mention the column name
179
180
00:11:55,263 --> 00:11:58,079
Cust ID
180
181
00:11:58,591 --> 00:12:00,895
And check
181
182
00:12:01,663 --> 00:12:05,759
In bracket cust ID
182
183
00:12:06,015 --> 00:12:08,063
Should be greater than
183
184
00:12:08,319 --> 00:12:10,367
Equal to zero greater than zero
184
185
00:12:14,207 --> 00:12:15,487
let's run this
185
186
00:12:18,047 --> 00:12:19,327
It ran successfully
186
187
00:12:19,583 --> 00:12:22,143
Now let us try to insert
187
188
00:12:22,399 --> 00:12:28,543
a value where cust ID will be negative. So insert into table name
188
189
00:12:33,919 --> 00:12:36,735
Values
189
190
00:12:37,503 --> 00:12:38,527
-1
190
191
00:12:41,087 --> 00:12:42,111
a
191
192
00:12:43,903 --> 00:12:45,439
cc, dd
192
193
00:12:53,375 --> 00:12:54,399
87
193
194
00:12:54,655 --> 00:12:55,679
and an email id
194
195
00:12:55,935 --> 00:13:01,823
cd@ xyz.com
195
196
00:13:11,295 --> 00:13:14,367
So if you will run this we should get an erro
196
197
00:13:15,391 --> 00:13:16,159
and we do
197
198
00:13:17,439 --> 00:13:23,327
If you read the error it says new row for relation customer table violates check constraint
198
199
00:13:23,839 --> 00:13:25,887
that's the check constraint that we added
199
200
00:13:26,399 --> 00:13:28,703
and it is violating that particular constraint
200
201
00:13:31,007 --> 00:13:32,031
Apart from this
201
202
00:13:32,287 --> 00:13:33,823
You can specify
202
203
00:13:34,591 --> 00:13:35,871
Whether a particular
203
204
00:13:36,127 --> 00:13:38,687
Column is a primary key for you are not
204
205
00:13:39,711 --> 00:13:43,807
And you can also specify a column to be a foreign key
205
206
00:13:44,575 --> 00:13:46,879
We discussed primary and foreign key earlier also
206
207
00:13:47,135 --> 00:13:50,463
And in the later part of the course you will find another separate video
207
208
00:13:50,719 --> 00:13:52,511
on primary and foreign keys
208
209
00:13:53,279 --> 00:13:56,863
so let us add this constraint of primary key and foreign key
209
210
00:13:57,631 --> 00:14:00,191
on our table. first let's look at the
210
211
00:14:00,703 --> 00:14:01,215
syntax
211
212
00:14:01,471 --> 00:14:03,007
So to make a column
212
213
00:14:03,519 --> 00:14:04,799
A primary key
213
214
00:14:05,055 --> 00:14:07,871
This is a Syntax alter table table name
214
215
00:14:08,639 --> 00:14:09,919
Add primary key
215
216
00:14:10,943 --> 00:14:11,967
and then the column name
216
217
00:14:13,247 --> 00:14:13,759
So
217
218
00:14:14,015 --> 00:14:16,831
if we go and make cust ID our primary key
218
219
00:14:18,879 --> 00:14:20,159
We'll write
219
220
00:14:20,415 --> 00:14:21,439
Alter table
220
221
00:14:23,231 --> 00:14:29,375
Table name add primary key
221
222
00:14:35,007 --> 00:14:38,335
then the column name
222
223
00:14:42,431 --> 00:14:43,199
This will make
223
224
00:14:43,711 --> 00:14:45,247
cust ID as the primary key
224
225
00:14:45,503 --> 00:14:49,855
So the column name should be in brackets bracket cust ID
225
226
00:14:52,415 --> 00:14:53,695
and we run this command
226
227
00:14:58,559 --> 00:15:02,143
So you can see that we are getting an error message which is saying
227
228
00:15:02,655 --> 00:15:05,215
Column cust ID contains null values
228
229
00:15:05,727 --> 00:15:11,871
So we are unable to make cust id as a primary key because it is containing a null value where as
229
230
00:15:12,127 --> 00:15:14,687
Primary key cannot have null values
230
231
00:15:15,455 --> 00:15:21,599
Therefore what we'll do is we will delete the row which has the null value and then we'll
231
232
00:15:21,855 --> 00:15:22,879
Make it the primary key
232
233
00:15:26,463 --> 00:15:32,351
Delete from customer table so if you remember this we did in the previous videos
233
234
00:15:32,863 --> 00:15:35,935
How to delete rows from a Table
234
235
00:15:36,191 --> 00:15:38,751
We'll not put any condition we'll clean the table
235
236
00:15:39,263 --> 00:15:40,031
Delete from
236
237
00:15:40,287 --> 00:15:45,151
Customer table delete from customer table we will run
237
238
00:15:48,223 --> 00:15:51,807
Now, our table has no values
238
239
00:15:52,575 --> 00:15:55,391
Now that my table is clean, I'll insert one value
239
240
00:15:55,647 --> 00:15:56,671
Into our table
240
241
00:15:56,927 --> 00:16:00,511
Using insert into command let us run this insert into command
241
242
00:16:05,631 --> 00:16:10,239
So if I run select star I can see that I have that
242
243
00:16:10,495 --> 00:16:16,127
One value
243
244
00:16:20,223 --> 00:16:26,367
So you can see that we have that value. Now let us make cust ID as the primary key
244
245
00:16:26,623 --> 00:16:32,767
Alter table customer table add primary key
245
246
00:16:38,655 --> 00:16:43,263
brackets cust ID
246
247
00:16:43,519 --> 00:16:48,639
run this command
247
248
00:16:52,223 --> 00:16:55,039
It ran successfully. it is now a primary key
248
249
00:16:56,831 --> 00:16:57,343
So to
249
250
00:16:57,599 --> 00:16:59,647
Make any particular column as a foreign key
250
251
00:17:00,159 --> 00:17:04,511
we have given a syntax here. this presentation will be part of the resources for this lecture
251
252
00:17:05,023 --> 00:17:10,655
I'll suggest that you open this presentation look at the syntax and try to do it on your own
252
253
00:17:11,423 --> 00:17:14,495
So in this video we have learnt how to change the structure of the table
253
254
00:17:15,263 --> 00:17:18,079
now it is time to master all the commands of data querying
254
255
00:17:19,103 --> 00:17:20,383
See you in the next video
19842
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.