1. Home
  2. Explore
  3. Computer Science
  4. SQL Database Course

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

SQL Database Course

Transcript

SQL Tutorial - Full Database Course for Beginners In this course I’m going to teach you everything you need to know to get started using SQL. Now SQL is a language which is used to interact with relational database management systems. And a relational database management system is basically just a software application which we can use to create and manage different databases. And so, we're going to talk about all of this stuff in this course. We’re going to start off with the basics. So, we'll just look at what is a database. We’ll talk about the different types of databases. We'll talk about what SQL is and what it actually means and how you can use it to create databases. And then we're going to go ahead and we're going to install something called a relational database management system. Which like I said, is just software that we can use to manage a database. We're going to install a relational database management system called MySQL.

And MySQL is one of the most popular database management systems for beginners. And also, just in general. So, MySQL is a great first system to learn. And so, once we have that all install, then we'll start writing SQL. So, we can write out little SQL code, little queries in order to create databases and create database tables and, you know, input information, retrieve information. And then we're going to get into writing SQL queries. And queries are used to query a database. So, we'll create a database. We’ll populate it with information. And I’ll show you guys how you can write these little SQL queries to get specific pieces of information. So, we'll start off with the basics and we'll just learn all of the fundamentals. And then I’m going to show you guys some more advanced techniques to getting information out of a database. And finally, I’m going to show you guys how you can actually design database schemas.

So, a database schema is basically just like all of the different tables and all the different relations that the database is going to store. And if you don’t understand what any of that means, don’t worry, we're going to cover all of it. But this course will cover basically everything about SQL, all of the fundamentals, all of the things that you need to get started. And we'll also look at database design and schema design. So, it’s going to be a pretty awesome course. I’m excited to be bringing you guys this stuff. And just know that all of the code, all the stuff that’s in this course is going to be available on the Giraffe Academy website. GiraffeAcademy.com. And there’s also going to be some additional things on the website that isn’t going to be in course just because I didn’t have time to cover all of it. So, check out the website for more information. But without further ado, lets get into it.

Let’s learn SQL which is one of the most popular languages for not only jobs, but just for developers in general. [What is a database?] In this video I’m going to give you guys a full introduction into databases. So, we're not going to get too specific on any one point. Basically, I want to show you guys just what databases are in general. We're going to talk about how databases are used, why we use databases. And the different types of databases that you’ll find out in the world.

So, this should basically just give you an introduction to databases in general. If you don’t know anything about databases, then this video will kind of walk you through and get you up to speed with what you need to know so you can then go off and start learning how to use an actual database. So, I just have a little PowerPoint slide here that I want to kind of walk you guys through. And we'll look at some different features of databases. So, the first question is, “What is a database?” You’ll often see databases abbreviated as DB. So DB would stand for database. And, a database is any collection of related information. What I’m sharing with you guys here is the absolute most general definition of this word. A database really, if you want to get down to it, is just any collection of related information. So, that could be something like a phonebook,

a shopping lists, a to-do list, your five best friends, Facebook’s userbase. All of these are examples of databases. All of these are collections of information that store related stuff, right? So, the phone book, right? This stores people’s phone numbers. Their names and their phone numbers. It’s a collection of related information. A to-do list – this stores a list of things that you want to do during the day, right? It’s related information. Your five best friends. That’s information.

It’s information that you’re storing somewhere. You have – you know, if I came up to you and asked you who your five best friends were, I’m sure you could list them off. That, in essence, is a database. It’s a collection of related information. And then Facebook’s userbase, right? So, all of the users that Facebook keeps track of and stores. That’s another example of a database. So, databases can be stored in different ways. So, you could store a database on paper. If I had a shopping list or a to-do list, I might just scribble that down on a piece of paper. You could store a database in your mind. So, your five best friends, for example. If I came up to you and I asked you, “List off your five best friends.” And you probably don’t have it written down somewhere. You don’t have an app on your phone that tells you who all your friends are, right? You just know that information in your mind naturally.

And so, that’s another way that you can store a database. You can just store the information in your mind. You can store information on a computer. And this is probably the most common use case is people will create a database and they’ll store all the information on their computer. This PowerPoint, the PowerPoint that we're looking at right now. This is an example of a database. It has related information on it. And I’m using it to teach this lesson. And then finally like a comment section. So, if you go down to the comment section of this video, that’s a database, right? It’s a general database that’s storing comments for the video. So, that really, in essence, is everything you need to know about databases to get started. It’s a collection of related information that can be stored in different ways. So, now that we understand the very general definition,

I want to kind of walk you guys through more specifically what we can do with databases. So, over here I have another slide. It’s Computers + Databases equals heart. Now, the point I’m trying to drive home with this slide is that storing a database, storing a collection of related information on a computer is extremely useful. And computers are actually great for storing databases. So, I have here two things. We have over here on the left Amazon vs a Shopping List. So, these are two examples of situations where we have a database. We kind of talked about how a shopping list is a database. It’s a collection of related information, right? A collection of products that you want to buy from the store. Amazon.com is also a database. Amazon is storing all this product information, all this user information. They’re storing reviews on the products, the prices of the products. They’re storing all of this information.

So, let’s take a look and we'll compare and contrast these two databases and we'll see why computers are the preferred medium for storing data. So, Amazon.com keeps track of products, reviews, purchase orders, credit cards, users, media, etc. So, Amazon is keeping track of so much information. Trillions of pieces of information need to be stored and readily available. Over here we have a shopping list. A shopping list keeps track of consumer products that need to be purchased, right? We're talking 10 to 20 pieces of information need to be stored and readily available. So, Amazon, we need to store trillions of pieces of information. With the shopping list, we need to store 10 or 20. Both of these are databases, but the one we have trillions of stuff. The other one we have 10 to 20 things. And so, over here on Amazon, the information on Amazon is extremely valuable and it’s critical to Amazon.com’s functioning, right?

So, the information that Amazon is storing it’s database is absolutely essential. And another thing is that security is essential, right? Amazon stores people’s personal information, like social security number or credit card, address, phone. Like that information needs to be locked down and secure. A shopping list on the other hand, the information is for convenience’s sake only, right? It’s not absolutely necessary for shopping. Like if you didn’t have a shopping list, you could still go to the store and, for the most part, you’d be able to find everything that you need. Also, with the shopping list, security is just not important at all, right? If you drop your shopping list on the ground – I mean if somebody else was to pick it up and look at it, it’s like not a big deal. It’s a shopping list. Finally, over here on Amazon, the information is stored on a computer.

Whereas with the shopping list, the information is stored maybe on a piece of paper, or it could even just be stored in someone’s memory, right? So, maybe you just memorized your shopping list and that’s where you’re storing your shopping list database. So, what I’m trying to show you guys is that a database doesn’t just have to be on a computer and it doesn’t just have to be like in your memory or on a piece of paper, right? Databases are in all of these different environments. But here’s the thing, for an application like Amazon, storing information on something like a computer makes it really easy to store trillions of pieces of information. And really easy to secure the data and make sure that security is taken care of. And it also makes it really easy to back up that information and duplicate that information and store that information on different computers.

So, the main point of this slide is that computers are great at keeping track of large amounts of information. And so, going forward, we're going to be talking about how can we create databases on the computers because computers are so good at storing that information. So, let’s talk about how we can go about creating databases on a computer. Now, a database could be as simple as like a text file where you store information. Or it could be like an Excel file, right? Microsoft Excel file.

But generally, if you’re going to be using a database with an application or you’re going to be using a database to store huge amounts of information, a lot of times what people will do is they’ll use special software which is designed to help you create and maintain the database. This is called database management systems. So a database management system is a special software program that helps users create and maintain a database on a computer. So, it makes it really easy for us to manage large amounts of information. So, if you’re a company like Amazon and you have trillions of pieces of information that you need to keep track of, well, the database management system can make it pretty easy for you to store trillions of pieces of information, right? It’s not like all that information is just in like some single text file. The database management system will make it really easy for you to store that.

Database management systems can also handle security. So, they can make it so only certain people with the usernames and passwords can access the data. It’ll also help you to backup your data and import and export data from other sources. So, if you have a bunch of information and you want to back it up, a database management system can help you do that. Database management systems can also interact with software applications. So, you know, Amazon.com is a website. And it’s interacting with the Amazon database which is stored mostly likely using a database management system. So, you could write a program that could interact with the database management system. All right, so let’s take a look at this quick little diagram that I have here. So, we have Amazon over here. And this would be like Amazon.com. And Amazon is communicating with a database management system. And I don’t work for Amazon so I’m not 100% sure exactly how they're doing this.

But for the most part, this is the general use case. So, we have our database management system which is this little box here. And the database management system is creating and storing and keeping track of a database. So, the database management system isn’t the actual database. The database management system is the software application that is creating, maintaining, updating, deleting information from the actual database. So, Amazon.com will interact with the database management system in order to create, read, update, and delete information. So, Amazon isn’t creating or reading or updating this information directly. Amazon is telling the database management system to do that for it. And by going through the database management system, we can be sure that all the data is getting stored correctly and there’s going to be no problems with the data. So, let’s talk about C.R.U.D. And this is an acronym that you’re going to hear a lot. C-R-U-D.

It stands for Create, Read, Update, and Delete. You’ll also hear people call this Create, Retrieve, Update, and Delete. Now, CRUD represents the four main operations that we're going to be doing with the database. So, you’re going to be creating information in the database. So, creating new database entries. You’re going to be reading information from the database. So, you know, retrieving or getting the information that you already stored in there. You’re going to be updating the existing information. And you're going to be deleting the information that’s already in there. And these are the core four operations that we want the database management system to perform for us. So, any good database management system is going to be able to do all four of these things. So, now that we've talked a little bit about database management systems, I want to talk to you guys about the two main types of databases that you'll find in computing.

So, the first is called a relational database. You’ll hear people refer to these as SQL[Sequel] databases or S-Q-L databases. And then we also have what are called non-relational databases. And you’ll hear people refer to non-relational databases as no-SQL or not just SQL databases. We're going to talk about what SQL is in a second. But I want to show you guys these two types. So, a relational database which we have over here on the left, organizes data into one or more tables.

So, each table has columns and rows and a unique key identifies each row. Now, relational databases are by far the most popular types of databases. And relational databases are going to store everything inside of these tables. So, we'll organize all the data that we want to store inside of these predefined tables and then we can just insert information into there. A relational database is a lot like an Excel spreadsheet. So, if you’ve ever used a spreadsheet where we have columns and rows and we're storing information, that’s essentially what a relational database is. And then over here on the right we have non-relational databases. And a non-relational database is basically just any type of database that’s not a relational database. So, like I said, relational databases are by far the most popular type. And so, because they’re so popular, any other type of database that’s not technically relational is just referred to as non-relational.

And non-relational databases, you know, it’s very general. Because any database that’s not relational is getting put into this category. So, non-relational database is organized database in anything but a traditional table. So, you’ll have things like key-value stores documents which would be kind of like JavaScript Object Notation. Or like XMl. They’ll store data in graphs. Or even flexible tables. So, really non-relational database is a very general category. It’s just anything that’s not relational. So, let’s take a look real quick at a relational database. Or you’ll hear people, like I said, refer to this as a SQL database. So, over here, we have two examples of tables. So, this would be basically how we would store information in a relational database. So, over here I have a student table. And this student table might store individual students. So, you’ll see over here we have an ID, a name, and a major.

So, for every single one of my students, I have an entry just like this. And you'll notice over here, I’m giving each of these entries an ID. And that ID will uniquely identify that row in the table. And over here, we have a users table. So, maybe you're creating users for your application. So, they have a username, a password, and then an email, right? So, the username is something that’s going to be unique. It’s something that uniquely identifies each row in the table. And then we're also storing like password and email. So, we kind of talked about how relational databases store data in tables. And when we want to create a relational database, we can use a relational database management system. And a relational database management system, or an RDBMS is just a database management system that helps you create and maintain a relational database. And some of the most popular are my MySQL, Oracle, PostgreSQL, and MariaDB.

There’s a bunch of these that are, you know, extremely popular. And relational database management systems use something called Structured Query Language, or SQL. And SQL is a standardized language for interacting with relational database management systems. So, remember, a relational database management system is just a software application that we can use in order to create, maintain and do different things to our relational database. And SQL or Structured Query Language is the language that we can use to interact with those relational database management systems. So, we can use SQL to perform CRUD operations as well as other administrative tasks like user management, security, backup, etc. And we can use SQL to define tables and structures. So, a relational database uses tables in order to organize its information. And we can use SQL to define those tables. And then insert information into those tables.

And SQL is a standardized language which means it’s pretty much used on every relational database management system. However, different relational database management systems will implement SQL just a little bit differently. So, not all SQL code that you use on one relational database management system will port over to another one without slight modification. So, now let’s talk a little bit about non-relational databases. And in non-relational databases is just anything that’s not relational. So, a non-relational database stores data in anything but a, you know, a static table. So, I’m showing you guys a couple different examples of non-relational databases and how they would store data. So, over here we have a document database. And this would store information inside of like little objects or documents. So, we're talking about things like JSON or XML.

Basically, I think JSON is kind of a very popular format and that’s kind of what I have up here. So, JSON is JavaScript Object Notation. So, there’s a lot of non-relational databases that will store data in documents just like this. They’ll also store data in graphs. So, over here we have graphs. And there’s like these little nodes. And then there’s, you know, connecting lines between the nodes which would represent like relationships. And then over here we have a key value hash. So, we would have a key and it gets mapped to a value. And that could really be anything. It could be a string. It could be JSON. It could be a graph. It could be anything like that. So, these are just three common examples of what SQL or non-relational databases might look like or how they might store their data. And just like with relational databases we can have non-relational database management systems. And these help users to create and maintain non-relational databases.

So, some examples would be mongoDB, DynamoDB, apache cassandra, firebase. There’s a bunch of these popular noSQL databases that have been sprouting up over the last decade or so. And these are all implementation specific. So, unlike relational database management systems where we have SQL which is just a standard language that can interact with all of them, there isn’t a standard language for interacting with non-relational database management system. So, generally, the non-relational database management system will implement their own language for performing CRUD and administrative operations on the database. So, now that we've looked at the different types of databases, I want to talk to you guys about some core concepts within databases. So, one of the most common things that you’re going to be doing are queries. So, a database query is a request that’s made to the database management system for specific information.

So, you’ll hear the word query thrown around a lot. And generally, a query is you’re just asking the database management system to give you some information. So, as a databases structure becomes more and more complex, it becomes more difficult to get the specific pieces of information that we want. And if you have a very complex database layout or schema, then getting a specific piece of information can be a little bit tricky. And that’s why we write database queries. So, can write a very complex database query which in a lot of ways is like a program, if you’ve ever used a programming language before. And that query will then instruct the relational database management system to grab a specific piece or specific pieces of information from the database. So, a good way to think of a query, is it’s kind of like a Google search, right? So, if I go on Google and I would generally type in the specific information that I want.

And then Google would give me the information that matches that search query. That’s a lot like a database query. Except with a database query we're not going to be writing the query in English like we would with Google. So, I can, you know, go up to my Google search bar and I can type something in in English or Spanish or whatever language you speak. But with a relational database management system we have to write our queries using either SQL or a specific language that’s meant for that database management system. So, let’s wrap this up real quick. We kind of covered a lot of the main points with databases. So, a database is any collection of related information. It’s a very general term. And computers are great for storing databases because computers are really fast. They can store lots of pieces of information. And they can handle things like security really easily.

And database management system make it easy to create, maintain, and secure a database on a computer. Database management systems will allow you to perform the CRUD operations and other administrative tasks on the database. There’s two main types of databases, relational and non-relational databases. Relational databases use SQL and they store data in tables with rows and columns. Non-relational databases store data using other data structures. So, things that aren’t relational database tables. And then we talked a little bit about querying. A query is just a require that you would make to the database management system for a specific piece of information. So, that is kind of the basics, the overall high-level basics of databases. And really, if you understand everything in this PowerPoint, then you have a good grasp of database fundamentals.

And as we go forward, we're going to learn more and more things about databases like how to create them, how to store data, and how to organize our data so that it’s easy to retrieve when we want it. [Tables and Keys] In this tutorial, we're going to look at some of the core concepts in relational databases. So, we're going to talk about how we store information in relational databases. Namely, in tables. Let’s talk about the different things about those tables. We're going to talk about things called keys. We're also going to look at rows and columns. We'll talk about different tables can be related to each other and how we can define relationships between tables. So, this will really just give you guys a full introduction into some of the core concepts of relational databases which you can then apply when creating your own relational database using a relational database management system. So, let’s get started.

Over here I have an example of a table. And this is a student table. And so, the student table defines specific information about a student. So, up here we have the column names. So, the student ID, the same of the student, and then the major. So, I’m storing three pieces of information about each student, right? They have their ID, their name, and their major. So, over here we have Jack. His student ID is 1. And he’s majoring in biology. Down here, we have Claire. Her student ID is 3 and she is majoring in English. So, all tables in relational databases are going to have two things. They’re going to have columns and they're going to have rows. Now, columns are these vertical sections right here. So, a column would define a single attribute, right? So, we have our name column. And inside the name column we have the names of all the students. We have the major column. Inside the major column we have the majors of all the students, right? Makes sense.

And then we have rows. And a row is an individual entry in the student table. So, a row or a horizontal entry would represent a single student, right? So, in a single row we're storing the student ID, the name, and the major. So, the columns represent a single attribute and the row represents an entry or an actual student. Whenever we're creating a table in a relational database we always want to have one very special column which is called the primary key.

And a primary key is basically an attribute which uniquely defines the row in the database. So, it’s an attribute about a specific entry which will uniquely define it. So, over here, we have student ID. And notice that I colored this in red and I also underlined it. And this underline is basically going to specify that this column or this attribute of the student is going to be the primary key. In other words, we can use this attribute to uniquely identify a specific row.

So, over here we have Kate and she’s a sociology major and her ID is 1. So, we would say that Kate’s primary key is 1. Now, let me show you guys why primary keys can come in handy. So, down here, inside of these gold squares I have two entries in this database. So, we have two students, both of which are named Jack. And both of which are biology majors. Now, this isn’t that, you know, special of a case, right? You can imagine that in a school you might have two students with the same name who are the same major. But in this case, how can we differentiate between this guy and this guy? Well, we can use this primary key. So, even though the name and the major of this row in the table are the same as the name and the major of this row, the student ID is different. The primary key is different. And that’s the whole point of a primary key, is that the primary key is always going to be unique for each row in the table.

So, even if all of the attributes of the row are the same, the primary key won’t be. And therefore we can differentiate between the two rows. So, I can differentiate between this Jack and this Jack by referring to their student IDs. And so, generally, whenever we have a table in a relational database, you always want to define a primary key. And a primary key can be anything. It can be a number. It could be a string of text. It could really be anything that you want. But it has to uniquely identify the specific row. So, in other words, another row in this student table couldn't have a primary key of 2. So, down here I have another example of a database table. This is a user table. So, just like that student table, except it’s storing information about users. So, over here we have email, password, date created and type. So, email is going to be our primary key. You can see I put that in red and I also underlined it.

And these emails are unique to each entry in the table, right? So fakemail@fake.co. No, other rows in this table could have that exact email. And then over here we're storing the password. We're also storing a date. So, a lot of times in databases you can store date values or time values. And then over here we're storing a type. And so, basically whenever you want to store data, what you’re going to do is define a table. So, you’re going to define, you know, all this information up here and then you'll just insert specific pieces of information into that table. And so, over here we have one more example of a database table. And we're actually going to be looking at this example as we go forward. So, this is an example of an employee. So, imagine that we have like a company database and we were storing information about employees. So, we have the employee ID which is the primary key. We're storing first name, last name, birthdate, sex, and salary.

And we're storing all of this specific information about an employee. Now, I want you guys to notice the primary key over here. So, employee ID, this is just a, you know, some sort of random number that we’ve assigned to each employee, right? This employee’s ID is 100. This employee ID is 101, etc. And this over here, this employee ID, this is what we would call a surrogate key. And a surrogate key is basically a key that has no mapping to anything in the real world, right? It’s essentially just, you know, like in this case a random number that we assign to an employee, right? So this, employee Jan has an employee ID of 100. That doesn’t necessarily mean anything. 100 is just a value that we're using to represent Jan inside of the database. And so, we would refer to that as a surrogate key, which is just a type of primary key. Surrogate key is a key that has no mapping to anything in the real world. We can also have something called a natural key.

And over here you'll see that I have the same exact table except instead of having employee ID, I have employee SSN. And SSN stands for Social Security Number. So, Social Security Number is a number that we use here in the United States in order to uniquely identify each citizen. So, in this case we're using the Social Security Number in order to uniquely identify each row in the table. In other words, we're using the Social Security Number as the primary key of the table.

And this is an example of what we would call a natural key. And this is a key that has a mapping or has a purpose in the real world, not just in the database. So, a lot of times you'll hear people refer to surrogate keys or natural keys and that’s the difference. A surrogate key is a primary key that has no mapping to the real world. And a natural key is a key that has a mapping to the real world, just like Social Security Number. So, those are kind of the two different types of primary keys that you might see being stored. Another thing I want to talk to you guys about are what are called foreign keys. And a foreign key is basically an attribute that we can store on a database table that will link us to another database table. So, over here I have this same exact employee table. And then I also have this other attribute over here, branch_id. And you notice that I colored this in green.

And this is what we would call a foreign key. And a foreign key stores the primary key of a row in another database table. So, here we have our employee table and I’m defining information about the employee. But let’s say that an employee belongs to a specific branch in our company, right? So, a company might have different branches. And we can store the information about what branch the employee belongs to inside of a foreign key. So, the foreign key over here is actually a primary key inside of another table. In our case, a branch table. So, let me show you guys that. So, down here, we have this branch table in our database, right? So, the branch is it’s own separate table. And we have – the branch has a primary key over here which is 2, 3, 1. And the branch names are Scranton, Stamford, and Corporate. So this in itself is its own table, right? It’s the branch table.

And over here we can define which branch a specific employee belongs to, by referring to the ID, the primary key of the branch. So, here, Jan Levinson, her branch ID is 1, which means she is in the corporate branch, right? Because branch ID number 1 is corporate. Michael Scott, his branch ID is 2, which means he’s in the Scranton branch. So this number is mapping this row over here into this other table. And that’s what a foreign key does.

A foreign key is essentially just a way that we can define relationships between the two tables. So, a foreign key is just a primary key of another table. So, Andy Bernard, right? His branch ID is 3 which means he belongs to the Stamford branch. And so, that’s what a foreign key does. A foreign key allows us to link up or define relationships between tables. I can say that Andy belongs to the Stamford branch and I can define that using the foreign key.

And over here you'll see on the branch table, I also defined another foreign key which is manager ID. mgr_id, that would stand for manager ID. And now this is actually going to be a foreign key which connects branch to the employee table. So, manager ID is going to be the ID of a particular employee who is the manager of the branch. So, let’s take a look at the Scranton branch. So, the branch ID is 2. The name of the branch is Scranton, and the manager ID is 101. So, let’s go check it out. So, over here in the employee table, employee 101 is Michael Scott. What that means is that Michael Scott is the manager of the Scranton branch, right? We were able to define that relational by using these foreign keys. So, down here, the Stamford branch, the manager is 102. If we come up here, we can see employee 102 is Josh Porter. So, Josh Porter is the manager of the Stamford branch. Corporate over here, the manager ID is 108. So, over here we didn’t actually include 108 over there.

So, obviously, this isn’t like, you know, a huge table of employees. But the manager for the corporate branch would be the employee with the ID of 108, whatever that would be inside of this table. So, that’s what a foreign key does. A foreign key is able to help us to define relationships between the tables, right? I can define which branch a specific employee belongs to by including the branch’s ID as a foreign key in the employee table. Similarly, I can define which employee is the manager of a branch by including that employee’s ID as a foreign key in the branch table. And so, that’s really why foreign keys are going to come in handy. And it’s also important to note that a particular table can have more than one foreign key on it. So, ever here I have my employee table, right? And it’s the same exact thing as we have before. But I also added another attribute or other column over here which is super_id.

And that stands for supervisor ID. And so, what this is going to do is it’s going to define who is the supervisor of a particular employee. And what’s cool about this foreign key is it’s actually relating back to the same table. So, an employee can be a supervisor of another employee. So, over here we have Angela Martin, right? Her employee ID is 103. And her supervisor ID is 101. And that means her supervisor is the employee with the ID 101. So, if we come over here we can look.

Angela Martin’s supervisor is Michael Scott because Michael Scott has an employee ID of 101. So, Michael Scott. Michael Scott’s super ID is 100. That means Michael Scott’s supervisor is the employee with ID 100, which is Jan Levison. So, we can these this foreign key over here on the employee table to define relationships between employees. So, before we define the relationship between the employee table and the branch table, and now we're defining a relationship between the employee table and the employee table, right? And so, what’s cool about this is then basically inside of this table, we can tell what the different supervisor or supervisee relationships are, right? I can tell that Josh Porter’s supervisor is Jan Levinson. And I can tell that Angela Martin’s supervisor is Michael Scott. Defining that information by using a foreign key. All right, so let’s take a look at another table.

I actually added in another table down here. So, over here we have employee, just like we had before. We have branch. And then we also have branch supplier. And branch supplier is another table that we can look at. So, a branch supplier would basically define who the suppliers are for specific branches. So, we have these branches. Maybe they're selling a certain product. Maybe they're selling like paper products. And the branch suppliers would define who are the suppliers for that branch. And you'll notice up here, the primary key actually consists of two columns. And this is what we would call a composite key or a composite key. And a composite key is basically a key that needs two attributes. So, this key is made up of two columns, right? It’s made up of branch_id and supplier_name. So, branch_id is actually going to refer to the specific branch. And supplier_name is going to refer to the specific supplier, right?

So, we can say over here that Hammer Mill supplies paper to branch number 2. So Hammer Mill supplies paper to Scranton. Uni-ball supplies writing utensils to branch ID number 2, which is Scranton. Patriot Paper supplies paper to branch ID number 3 which is Stamford. So, inside of this branch supplier table, I’m able to define which different suppliers are supplying what to which different branches. Now, the reason that I need this composite key or this composite key, is because the supplier name doesn’t uniquely identify each row. And the branch_id doesn’t uniquely identify each row. Only together can they uniquely identify each row, right? So, for example, supplier name, Hammer Mill. You’ll notice that Hammer Mill shows up here and it also shows up here. So, Hammer Mill supplies to branch number 2. And they also supply to branch number 3. Uni-ball supplies to branch number 3 and Uni-ball supplies to branch number 2.

So, this column has repeated values, right? This column has Hammer Mill repeated. It has Uni-ball repeated. branch_id also has those things repeated, right? So, branch_id 2 shows up here a bunch of times. branch_id 3 shows up here a bunch of times, right? And so, the branch_id column can’t uniquely identify the table. And the supplier name column can’t uniquely identify the table. Only together can they uniquely identify it, right? So, we would say Hammer Mill supplies branch 2.

That combination only shows up once. We wouldn't put that combination again because we’re already defining it. So, Hammer Mill supplies branch 2. Hammer Mill supplies branch 3. These two rows are uniquely identified by the identified by the supplier name and the branch ID. And so, over here in these employee and the branch table, we only find one column as the primary key. But over here, we define two columns as the primary key, which would be a composite key. And that’s actually pretty common, where we're going to have two columns uniquely identifying each row. All right, so down here I want to show you guys one more example. And this is going to show you one other way that we can define like different relationships with these tables. So, I actually added in two other tables. We have a client table and we have this Works With table. So, let me kind of walk you guys through this a little bit. We still have employee. We still have branch.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent