1 00:00:01,910 --> 00:00:10,340 All companies store data. From a simple record book of a small business, maybe even on a journal or a 2 00:00:10,340 --> 00:00:11,580 piece of paper, 3 00:00:11,690 --> 00:00:17,810 to massive companies like Amazon that need computers to store Petabytes of data. 4 00:00:17,810 --> 00:00:26,000 Now in order to store this information pretty much every company and sometimes individuals too need something 5 00:00:26,000 --> 00:00:27,940 called a database. 6 00:00:28,070 --> 00:00:35,060 We saw in the last section how hard it was to have our server remember a new user. Every time the server 7 00:00:35,060 --> 00:00:35,810 crashed 8 00:00:35,810 --> 00:00:40,030 The variable would get reset and then it would start all over. 9 00:00:40,460 --> 00:00:48,400 Or if we wanted to do a login or a signin check and iterate through the e-mails and passwords, doing 10 00:00:48,470 --> 00:00:52,160 a loop through javascript that's inefficient. 11 00:00:52,160 --> 00:00:54,240 That's where database come to the rescue. 12 00:00:54,990 --> 00:00:58,850 So let's ask ourselves what is a database. 13 00:00:58,890 --> 00:01:01,960 A database is a collection of data. 14 00:01:02,050 --> 00:01:03,540 And what we mean by data- 15 00:01:03,850 --> 00:01:05,730 There are many forms of them right. 16 00:01:05,770 --> 00:01:10,360 There can be numbers, there can be dates, there could be password hashes, 17 00:01:10,360 --> 00:01:17,440 there could be user information. Databases allow us to organize this data in a way that is useful to 18 00:01:17,440 --> 00:01:24,340 us and it makes data management easy and something came out of that. 19 00:01:24,650 --> 00:01:29,930 And it's called database management system or DBMS for short. 20 00:01:29,990 --> 00:01:35,120 It's a collection of programs which allows us to access databases and work with data. 21 00:01:35,450 --> 00:01:39,740 And it also allows controlled access to database users. 22 00:01:41,600 --> 00:01:50,820 So a DBMS is something in this box something that allows us a tool or a piece of software that allows 23 00:01:50,820 --> 00:01:59,420 us to communicate with the database, store information that's useful for us and also allows us to update, 24 00:02:00,050 --> 00:02:05,780 insert, delete, look up whatever's in the database. 25 00:02:05,840 --> 00:02:12,980 And this idea of a DBMS really started to take shape in 1960s and now it is at the forefront of many 26 00:02:12,980 --> 00:02:14,130 exciting things. 27 00:02:14,150 --> 00:02:19,850 There's an entire industry of Engineers that just work with databases because it is so important. 28 00:02:21,100 --> 00:02:27,130 Now there are two types of DBMS that are really popular right now and that we're going to talk about 29 00:02:27,130 --> 00:02:27,980 in this course. 30 00:02:29,820 --> 00:02:36,310 And these two types are represented really well by Postgres and MongoDB. 31 00:02:36,450 --> 00:02:43,230 Let's talk about the first one which is relational database. And this is probably the most popular. As 32 00:02:43,230 --> 00:02:44,600 you can see on the right hand side. 33 00:02:44,610 --> 00:02:49,410 You see that all of these are relational databases. 34 00:02:49,630 --> 00:02:56,890 Postgres, Oracle, SQL server, you may have heard of mySQL, SQLite. 35 00:02:57,230 --> 00:03:03,840 These are all types of databases that have this relational database tag to them. 36 00:03:04,100 --> 00:03:06,770 And the beauty with them is that they're all pretty much similar. 37 00:03:06,770 --> 00:03:10,630 They all follow a same standard format. 38 00:03:10,640 --> 00:03:19,510 Relational databases consist of two or more tables with columns and rows. 39 00:03:19,530 --> 00:03:27,180 So in this case users is a table and full_name, username, text, created_at are columns and whatever 40 00:03:27,180 --> 00:03:30,900 values they have here are rows. 41 00:03:31,230 --> 00:03:34,690 Each row represents an entry and each column stores 42 00:03:34,700 --> 00:03:39,710 a very specific type of information like name, address or phone numbers. 43 00:03:39,930 --> 00:03:48,050 And then the relation between tables and field is called a schema. In a relational database 44 00:03:48,170 --> 00:03:55,480 the schema must be clearly defined before any information can be added. And if this is confusing right 45 00:03:55,480 --> 00:03:59,230 now don't worry we're actually going to create these databases so it makes sense to you. 46 00:03:59,500 --> 00:04:05,470 If we had Twitter for example you can see over here the types of tables that we would have and how we 47 00:04:05,470 --> 00:04:07,130 would organize it. 48 00:04:07,240 --> 00:04:15,400 We would have a users table, a tweets table, a following table and we can connect all this information. 49 00:04:15,420 --> 00:04:23,220 For example by connecting different pieces of the table. For example the username will be the same in 50 00:04:23,220 --> 00:04:27,560 the users table as it is in the tweets table. 51 00:04:27,570 --> 00:04:37,530 That way we can say that the username in the tweets table is the foreign key of the username in the 52 00:04:37,530 --> 00:04:46,090 users table. And then from_user will be the foreign key of username in the following table. 53 00:04:46,110 --> 00:04:56,400 Now things like full name or id, something that identifies each row in a table is called a primary key 54 00:04:57,060 --> 00:05:01,940 and we'll go through that later on in this section. 55 00:05:01,940 --> 00:05:07,130 All right so all these databases- we're going to learn about Postgres SQL but they're all pretty 56 00:05:07,130 --> 00:05:07,670 much the same. 57 00:05:07,670 --> 00:05:14,810 So once you know one it's fairly easy to pick up the other ones- how do they actually communicate with 58 00:05:15,660 --> 00:05:22,230 the server with the Backend. Well all relational databases use something called SQL. 59 00:05:26,510 --> 00:05:34,960 And SQL allows us to communicate just like HTTP did between the frontend and the backend. With SQL 60 00:05:34,960 --> 00:05:42,410 we're able to communicate with the database and modify or get or update however we want. 61 00:05:43,040 --> 00:05:45,620 And we'll have a video on this topic as well. 62 00:05:45,620 --> 00:05:49,820 All right so that's relational databases. 63 00:05:49,820 --> 00:05:51,740 What about the second type. 64 00:05:51,800 --> 00:05:58,870 The second type is called a non-relational database or a NoSQL database. 65 00:05:59,000 --> 00:06:05,000 And once again there are many many different types of databases with some cool names like CouchDB and 66 00:06:05,000 --> 00:06:06,680 HYPERTABLE. 67 00:06:06,810 --> 00:06:12,810 Now a mongoDB or a non-relational database lets you build an application without having to define 68 00:06:12,810 --> 00:06:14,900 the schema first 69 00:06:14,970 --> 00:06:26,400 unlike a relational database. If we go back to relational database this schema or these tables have to 70 00:06:26,490 --> 00:06:27,750 kind of be predefined right. 71 00:06:27,750 --> 00:06:35,930 We need to know how our app is going to look, make these tables so that once the app is public we can 72 00:06:35,940 --> 00:06:45,030 start entering user information and tweets and followers. With a NoSQL or non relational database 73 00:06:45,030 --> 00:06:47,050 we can just define it as we go. 74 00:06:47,340 --> 00:06:53,410 And they all have different ways of storing this information so each one is very, very different. 75 00:06:53,490 --> 00:06:58,650 And this is another powerful reason to use a non relational database is that, if your data requirements 76 00:06:58,650 --> 00:07:05,790 aren't clear at the outset of your project and maybe you have a massive amount of unstructured data. You 77 00:07:05,790 --> 00:07:11,390 may not have the luxury of developing a relational database with a clearly defined schema. 78 00:07:11,480 --> 00:07:18,390 They offer instead a greater flexibility in that a non relational database are more like folders, just 79 00:07:18,390 --> 00:07:21,510 assembling related information of all types. 80 00:07:22,340 --> 00:07:26,560 Now MongoDB is something called document oriented. 81 00:07:26,600 --> 00:07:31,740 It stores information as documents. 82 00:07:31,750 --> 00:07:33,700 Let me illustrate this point to you. 83 00:07:33,700 --> 00:07:37,170 I have my desktop here with two folders. 84 00:07:37,540 --> 00:07:40,330 One is a relational database. 85 00:07:40,330 --> 00:07:48,920 And here I have users, tweets, profile, following. And you can think of relational databases with this type 86 00:07:48,920 --> 00:07:56,540 of storage where this folder lives somewhere on a computer that stores this information. And any time 87 00:07:56,540 --> 00:08:02,600 we need to ask a database for something, well we grab the users and if we also want to grab the 88 00:08:02,600 --> 00:08:09,680 tweets of the user. We find the user that we're interested in. Then find the tweets of the user that we're 89 00:08:09,680 --> 00:08:17,510 also interested in with the foreign key that links to the users and we can also find however many followers 90 00:08:17,540 --> 00:08:18,680 that user has. 91 00:08:18,680 --> 00:08:22,780 We grab that information and display it on our web app. 92 00:08:25,480 --> 00:08:27,360 MongoDB on the other hand 93 00:08:28,240 --> 00:08:37,100 will actually have each user as a document. So if I wanted to grab the first user. 94 00:08:37,419 --> 00:08:43,740 Well I just grabbed this document and it has all the followers, the tweets, the profile everything is 95 00:08:43,740 --> 00:08:51,590 in this one document. And you might be thinking "MongoDB looks like a better choice than having this 96 00:08:53,100 --> 00:08:54,390 way of doing things right?" 97 00:08:55,560 --> 00:09:00,000 Well, it depends on your need and that's why databases is such a complex topic. 98 00:09:00,030 --> 00:09:02,460 It really, really depends on your situation. 99 00:09:02,490 --> 00:09:08,760 You can see this MongoDB way being very useful if you have something like a profile maybe a linkedin 100 00:09:08,760 --> 00:09:09,410 profile. 101 00:09:09,630 --> 00:09:15,390 But if you needed something where you're working with just following data or just tweets data, maybe with 102 00:09:15,430 --> 00:09:19,020 the tweets you want to see what is the average tweet size. 103 00:09:19,140 --> 00:09:25,530 Well it's really easy to just grab this file and calculate that versus this way where you might have 104 00:09:25,530 --> 00:09:28,810 to extract it from each one of the users. 105 00:09:29,010 --> 00:09:31,900 So it really depends on your needs. 106 00:09:32,050 --> 00:09:40,990 Let me show you another diagram that might be useful. When we look at relational versus non relational 107 00:09:40,990 --> 00:09:42,000 databases. 108 00:09:42,220 --> 00:09:44,950 You can think of relational as 109 00:09:45,040 --> 00:09:54,250 Each table has a Blog Posts, a Blog Tag, a Blog Comments and it links these tables to give you the blog information 110 00:09:55,030 --> 00:10:02,170 versus a non-relational that stores this entire block post in an entry and has comments, tags, categories 111 00:10:02,590 --> 00:10:04,200 and all other related data 112 00:10:04,210 --> 00:10:05,920 Just in one single place. 113 00:10:05,950 --> 00:10:08,890 So what does MongoDB use to communicate. 114 00:10:09,420 --> 00:10:16,130 If we go back to our example here we had SQL for relational databases such as Postgres. 115 00:10:16,200 --> 00:10:26,760 Well MongoDB has its own what we call query language and it is just the MongoDB query language. 116 00:10:31,690 --> 00:10:33,690 But they both aim to do the same thing. 117 00:10:33,700 --> 00:10:40,060 That is to communicate with the database, provide an easy way for the server to communicate with the 118 00:10:40,060 --> 00:10:41,690 database. 119 00:10:41,810 --> 00:10:46,280 And in this section we're going to be talking about that and we're going to finally connect the dots 120 00:10:46,850 --> 00:10:51,510 and show you the power of databases once they're connected to a server. 121 00:10:51,560 --> 00:10:53,600 I'll see in the next one. Bye-bye.