Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,450 --> 00:00:06,940
Okay, now that we have the PG admin with us, let us create a new database.
2
00:00:08,550 --> 00:00:10,230
This will be a test database.
3
00:00:10,320 --> 00:00:13,290
To create this database, we will go to the database option.
4
00:00:13,290 --> 00:00:18,090
Right click on it and click on the Create database option.
5
00:00:18,570 --> 00:00:21,570
We'll just give a name to this database, which will be list.
6
00:00:23,700 --> 00:00:25,350
And we will save this database.
7
00:00:26,660 --> 00:00:31,820
Once this database is created, we will get a notification saying Database connected.
8
00:00:34,360 --> 00:00:37,960
So the database is now connected and we have a new database.
9
00:00:39,220 --> 00:00:41,350
Within this database, we can add tables.
10
00:00:41,350 --> 00:00:46,440
But since this is a test database, we'll look at how to delete this database.
11
00:00:46,450 --> 00:00:52,480
Also, we'll just right click on this database and click on the delete option.
12
00:00:54,200 --> 00:00:54,770
Okay.
13
00:00:57,040 --> 00:01:00,500
This we saw is the graphical way of doing this.
14
00:01:00,520 --> 00:01:06,670
We can also create a database using SQL queries, but that we will not be covering right now.
15
00:01:07,060 --> 00:01:12,850
We'll start with creating a new database, which we'll be using for writing further queries and we'll
16
00:01:12,850 --> 00:01:16,260
be adding tables and datas into that database.
17
00:01:16,270 --> 00:01:18,970
So let's create a database called training.
18
00:01:27,680 --> 00:01:29,750
The second line is the owner.
19
00:01:29,780 --> 00:01:33,860
This is the user which is creating this database.
20
00:01:34,160 --> 00:01:39,800
There are other users also to whom you can give special rights to access their database.
21
00:01:40,130 --> 00:01:45,470
We'll be learning about creating different users and assigning them rights in the later part of this
22
00:01:45,470 --> 00:01:46,010
course.
23
00:01:46,580 --> 00:01:51,440
For now, the owner is the sole user that we have for this database, which is Postgres.
24
00:01:52,290 --> 00:01:53,040
Save it.
25
00:01:58,920 --> 00:02:01,870
Now you can see that training databases created.
26
00:02:02,690 --> 00:02:03,950
In this database.
27
00:02:04,100 --> 00:02:07,010
Next, what we will do is add tables.
28
00:02:07,800 --> 00:02:15,270
So this training database is going to be a database of customers and products and the transactions that
29
00:02:15,270 --> 00:02:16,620
those customers are doing.
30
00:02:16,740 --> 00:02:19,740
So we'll have three different tables in this database.
31
00:02:19,950 --> 00:02:23,580
First will be of customers, which will contain customer data.
32
00:02:23,820 --> 00:02:26,880
Second will be of products which will contain product data.
33
00:02:27,270 --> 00:02:29,710
And third will be of the transactions.
34
00:02:29,730 --> 00:02:36,300
So, for example, customer is buying product B, So the third table will have information like customer
35
00:02:36,300 --> 00:02:41,670
A bought this much quantity of product B for this much amount.
36
00:02:42,180 --> 00:02:45,810
So we'll create these three tables and add data to it.
37
00:02:47,140 --> 00:02:52,000
So to create a table, we'll be using create table command.
38
00:02:53,110 --> 00:02:58,660
So before writing this one, let's look at the syntax and example of this command.
39
00:02:59,590 --> 00:03:04,540
So when we are creating a table, it is important that we design it very carefully.
40
00:03:04,570 --> 00:03:10,180
A better design table will be taking up only required space and it will be quick in giving back the
41
00:03:10,180 --> 00:03:11,470
response to queries.
42
00:03:11,680 --> 00:03:18,310
So to create a table using the create command, we need to name the table and also give me the names
43
00:03:18,310 --> 00:03:20,800
of all the columns and the data types.
44
00:03:21,570 --> 00:03:26,040
If you look at the syntax of this command, this starts with create table.
45
00:03:26,950 --> 00:03:27,430
After that.
46
00:03:27,430 --> 00:03:29,200
You mentioned the name of that table.
47
00:03:29,620 --> 00:03:32,470
And after that we have a bracket.
48
00:03:32,500 --> 00:03:35,320
Within this bracket, you will mention all the columns.
49
00:03:36,440 --> 00:03:41,450
For each column, you need to mention the data type, which this column will contain.
50
00:03:41,570 --> 00:03:45,200
We will be discussing these data types in the later part of the course.
51
00:03:46,700 --> 00:03:49,660
Next part is defining column constraints.
52
00:03:49,670 --> 00:03:53,810
This I have put in square brackets as it is optional.
53
00:03:53,840 --> 00:03:57,470
You need not give all the constraints while defining the table.
54
00:03:57,830 --> 00:04:02,170
So you list down all the columns, give the table constraints if any.
55
00:04:02,180 --> 00:04:04,580
You close the command using a semicolon.
56
00:04:05,210 --> 00:04:08,450
When writing a query, you can have any number of spaces.
57
00:04:08,450 --> 00:04:10,190
These spaces are immaterial.
58
00:04:11,300 --> 00:04:14,960
You can use multiple lines so you can press enter after any word.
59
00:04:15,900 --> 00:04:19,590
And it is very important that you close the query using a semicolon.
60
00:04:20,920 --> 00:04:22,090
You did not remember this.
61
00:04:22,090 --> 00:04:27,010
When you practice writing queries, this will become your habit while writing a query.
62
00:04:27,040 --> 00:04:28,590
There are some good practices.
63
00:04:28,600 --> 00:04:33,460
For example, some developers write all the key words in capital letters.
64
00:04:33,850 --> 00:04:35,170
It is not mandatory.
65
00:04:35,200 --> 00:04:38,140
You did not write create table in capital letters.
66
00:04:39,100 --> 00:04:41,950
To just to segregate and make it more readable.
67
00:04:41,980 --> 00:04:47,590
You can keep all the SQL commands as capital and other as normal case.
68
00:04:48,160 --> 00:04:53,080
Now we look at what all constraints are available to be put on the columns.
69
00:04:53,710 --> 00:04:56,350
Majorly, there are these six types of constraints.
70
00:04:56,620 --> 00:04:57,970
First is not null.
71
00:04:58,000 --> 00:05:02,980
If you define any column to be not null, it cannot have blank value.
72
00:05:02,980 --> 00:05:04,090
It cannot be null.
73
00:05:04,180 --> 00:05:10,840
So this check will ensure that whichever column is mandatory for you while entering data, you don't
74
00:05:10,840 --> 00:05:11,920
miss out that value.
75
00:05:12,310 --> 00:05:16,930
If you miss that value while storing the data, it will give you back an error.
76
00:05:18,310 --> 00:05:25,360
Default constraint can be defined to store the default value if any input value is missing for that
77
00:05:25,360 --> 00:05:26,680
particular set.
78
00:05:27,970 --> 00:05:33,670
Unique constraint will ensure that that particular cell has a unique value within that column.
79
00:05:35,650 --> 00:05:41,530
Using the check constraint, you can specify a certain criteria against which the data inputted will
80
00:05:41,530 --> 00:05:42,220
be checked.
81
00:05:43,030 --> 00:05:45,720
The last two are the key constraints.
82
00:05:45,730 --> 00:05:49,840
You can specify any column to be a primary key or a foreign key.
83
00:05:50,020 --> 00:05:52,150
Let us look at these two types of keys.
84
00:05:53,380 --> 00:05:59,920
A primary key is a column or a set of columns which makes the values in a table unique.
85
00:06:00,370 --> 00:06:05,290
Primary key therefore ensures that there are no duplicate records in the database.
86
00:06:05,590 --> 00:06:09,430
If there are more than two columns used as a primary key, it is called a composite.
87
00:06:09,640 --> 00:06:15,730
The foreign key is usually a column that references another column of another table.
88
00:06:15,760 --> 00:06:18,220
Let us look at this from an example.
89
00:06:19,450 --> 00:06:26,020
So suppose we have one table, which is a customer table, and it is storing customer ID, customer's
90
00:06:26,020 --> 00:06:27,970
first name and customer's last name.
91
00:06:28,540 --> 00:06:34,300
We define customer ID to be primary key to identify each individual customer.
92
00:06:34,330 --> 00:06:37,060
Customer ID will not be duplicate.
93
00:06:37,090 --> 00:06:39,970
Each customer will have only one customer ID.
94
00:06:40,840 --> 00:06:46,780
Now in the second table, which is the order table and contains the description of all the orders that
95
00:06:46,780 --> 00:06:47,560
we are getting.
96
00:06:48,130 --> 00:06:49,990
Order ID is the primary key.
97
00:06:50,560 --> 00:06:55,060
For each individual order we will have one unique order ID.
98
00:06:56,050 --> 00:06:59,220
But the customer ID is a foreign key.
99
00:06:59,440 --> 00:07:07,870
What this means is this column will have values which are available only in the customer ID column of
100
00:07:07,870 --> 00:07:08,950
the customer table.
101
00:07:09,070 --> 00:07:17,410
It will not have any other value to this foreign key column is related to the primary key of the customer
102
00:07:17,410 --> 00:07:24,850
table and it is restricting the values in this column by the superset of values in the customer ID of
103
00:07:24,850 --> 00:07:25,690
customer table.
104
00:07:26,880 --> 00:07:33,420
So if you can imagine the values in the order table, there would be several orders which will have
105
00:07:33,430 --> 00:07:34,620
same customer ID.
106
00:07:34,650 --> 00:07:42,180
So if the same customer is ordering different products for that order, the customer ID will be same.
107
00:07:42,360 --> 00:07:49,680
So the primary key of orders will be unique, but the foreign key will be having duplicate values within
108
00:07:49,680 --> 00:07:50,250
a table.
109
00:07:50,430 --> 00:07:55,570
So having duplicate values for foreign key is allowed, but it is not allowed for primary key.
110
00:07:56,220 --> 00:08:04,260
Secondly, when you want to find out first name and last name of a particular customer with a particular
111
00:08:04,260 --> 00:08:05,100
order ID.
112
00:08:05,490 --> 00:08:12,390
So, for example, if I want to find out the customer who ordered with the order ID one, I'll see the
113
00:08:12,390 --> 00:08:20,400
customer ID of the customer in the order table, and then I'll find out what is the first name and last
114
00:08:20,400 --> 00:08:24,300
name of that customer with that particular customer ID in the customer table.
115
00:08:24,660 --> 00:08:26,500
So this is the concept of keys.
116
00:08:26,520 --> 00:08:32,070
It is very important to specify which will be your primary key and which will be your foreign key.
117
00:08:33,100 --> 00:08:34,510
With this information.
118
00:08:34,600 --> 00:08:41,020
Let us now create the first table, the customer table in our database to create a table.
119
00:08:42,690 --> 00:08:45,660
Will start writing the SQL query.
120
00:08:45,690 --> 00:08:48,310
If you remember how we got this screen.
121
00:08:48,330 --> 00:08:52,470
We clicked on the database and selected the query tool.
122
00:08:52,800 --> 00:08:56,610
So right click on the training database and.
123
00:08:57,410 --> 00:08:58,790
Select the query tool.
124
00:09:01,750 --> 00:09:04,000
So this is where we write the query.
125
00:09:05,620 --> 00:09:07,360
Start writing create table.
126
00:09:11,620 --> 00:09:13,810
Clear table is the command.
127
00:09:14,200 --> 00:09:16,840
After this, we will specify the table name.
128
00:09:17,690 --> 00:09:18,920
Which is customer table.
129
00:09:21,660 --> 00:09:23,550
After this, we'll start a bracket.
130
00:09:24,580 --> 00:09:27,680
And within this bracket, we will list on all the columns.
131
00:09:27,700 --> 00:09:29,020
First is the cost ID.
132
00:09:29,680 --> 00:09:34,480
Then we write data type, which will be integer in.
133
00:09:36,100 --> 00:09:39,640
And next will be my next column.
134
00:09:43,810 --> 00:09:44,770
First name.
135
00:09:47,010 --> 00:09:48,060
This will have.
136
00:09:49,050 --> 00:09:51,060
Pitiable characters were Cat.
137
00:09:56,270 --> 00:09:58,550
Last name as.
138
00:09:59,700 --> 00:10:00,810
Variable characters.
139
00:10:04,060 --> 00:10:05,520
Then we'll have age.
140
00:10:11,460 --> 00:10:12,100
Integer.
141
00:10:12,630 --> 00:10:13,800
Then you will have.
142
00:10:15,420 --> 00:10:16,220
Emily.
143
00:10:24,590 --> 00:10:25,860
This variable characters.
144
00:10:27,070 --> 00:10:27,490
That's a.
145
00:10:28,640 --> 00:10:30,020
So we close the bracket.
146
00:10:31,630 --> 00:10:36,460
And remember to put Semicolon to finish this query.
147
00:10:37,510 --> 00:10:38,830
To run this query.
148
00:10:39,770 --> 00:10:45,770
You'll need to either click on this button, which is execute refresh, or you can see that F five is
149
00:10:45,770 --> 00:10:46,490
also highlighted.
150
00:10:46,490 --> 00:10:50,300
You can just click on F five to execute the query.
151
00:10:52,850 --> 00:10:53,090
Here.
152
00:10:53,090 --> 00:10:57,230
You can see that Curry returned successfully in 172 seconds.
153
00:10:58,130 --> 00:10:59,100
So you're good.
154
00:10:59,150 --> 00:11:02,300
He has run to see the table.
155
00:11:02,810 --> 00:11:03,950
Open the database.
156
00:11:03,980 --> 00:11:07,040
Go to schemas within public.
157
00:11:08,660 --> 00:11:11,150
Inside these tables, you'll be able to see a table.
158
00:11:11,150 --> 00:11:14,630
But if it is not refreshed, click on Refresh first.
159
00:11:15,900 --> 00:11:19,530
There'll be one table created, which is the customer table.
160
00:11:23,160 --> 00:11:27,420
If you want to look at the columns, also, if you open the customer table.
161
00:11:29,350 --> 00:11:30,750
We you click on this plus sign.
162
00:11:32,490 --> 00:11:37,670
There's the column option, Customer ID, first name, last name, age, email ID.
163
00:11:37,710 --> 00:11:40,260
These are the five columns that we created.
14831
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.