Placeholder Image

字幕列表 影片播放

  • Oh everyone, I hope you're having a good day.

  • My name's Kyle and this is Webb.

  • Have simplified.

  • We make the Web easy to understand, inaccessible for everyone.

  • In this video.

  • I'm going to be going over everything that you need to know about Sequel In order to do about 95% of the stuff that you'll ever need to do with Sequel, it's going to be a long video because I have quite a bit to cover.

  • So I'm going to start by talking about what sequel is and why it's important for you.

  • And then, for the majority of this video, I'm going to go over everything that you need to know about.

  • Sequel.

  • All the syntax, all the language and everything that you're going to use when you're using sequel in your day to day life and sequel is a lot like CSS and that it's very simple to understand and use and learn.

  • But the complexity of actually using it in the different things you can do with it is what makes it difficult and hard to master.

  • That's why I've included a list of exercises in the description I have again Hub Repo, That'll have a bunch of exercises with their solutions and the results so that you can work through those exercises after watching this video to get a better understanding of how sequel works and how to use it.

  • Then coming up next week, I'm going to go over the solutions for all those different questions that I have in the repo.

  • So make sure you stick around for the video next week as well, which will be linked at the end of this video if you're watching it after it's out.

  • Also, I'm going to be creating additional videos relating to topics that are more difficult to understand as we go through this.

  • So in the coming weeks and months, I'm going to have additional videos going over the more difficult topics of sequel.

  • So let me know in the comments down below, which topics you find the most confusing from this video so that I could make sure to dedicate extra time and videos to making those easier to understand for you.

  • So, without any further ado, let's get started to get started.

  • We first need to talk about what sequel is, and it's luckily fairly straightforward sequel, which stands for structured query language is essentially just a language that's designed for creating, reading, updating and deleting data from databases.

  • And pretty much any relational database manager system will use sequel as the base line for how it accesses its data for creating, reading, updating and a leading.

  • So essentially, when you learn sequel, you're able to interact with pretty much any relational database manager system using your sequel background.

  • And each relational database management system will handle higher level or lower level specific tasks that are used much less often in sometimes individual ways.

  • But everything to find in sequel is universal between all of the different database manager systems.

  • And now, before we can actually jump into learning why sequel is so important, we first need to talk about and understand what a database is and kind of how they work a little bit.

  • Essentially, a database is just a collection of data in separated out into different tables, and these tables are individual models of data, so you may have a user table.

  • You may have a product's table, you may have orders table, and all these tables will be linking to each other in order to create connections between the different data.

  • So then you have a table which contains data for a single model inside of your relation.

  • And then inside of that table you have different columns and different rose.

  • The rose are the different records of your individual models.

  • So if you have one user that will quite toe one record or row inside of your table, and two users will be to separate records or arose in that table, and then all of the properties of that user, such as their i d name email, password, those are all going to be columns inside of your database.

  • And essentially, it's just a table with columns and rows that represent your different records and different properties of those records and in the different ways that that data is related to each other, is how it becomes a relational database system, where you can link data from different tables to Data's and other tables.

  • And that's how you can create a complex data layout system using databases.

  • They're fairly straightforward and essentially just have to think about it as a collection of different tables that represent different objects inside of your data.

  • Now let's quickly talk about why it's important to learn sequel.

  • It's fairly straightforward as to why it's so important.

  • And that's because sequel deals with data and data is everywhere.

  • Almost every application that you use, whether it's on your phone, whether it's on your computer on the Internet, it has some form of data that it needs to save somewhere and databases, which your sequel are one of the greatest and easiest ways to store data for any small scale, or especially for large scale applications.

  • Which is why you see databases being used absolutely everywhere across development in any form.

  • And this is why it's so crucial to learn sequel, because as a developer, no matter what you work on, you will eventually encounter Sequel and I have to work with databases.

  • And knowing sequel at a strong level will help you significantly in your development career.

  • Now that we understand why sequel is so important and what sequel is, let's jump into my secret workbench in order to demonstrate the syntax of sequel and then talk about the different commands that we can use with sequel in order to create, read, update and delete our data.

  • If you haven't already downloaded my secret workbench.

  • I recommend checking out my last video, which I'll link up in the top corner and in the description blow that will tell you how to download my sequel server and my secret workbench on your computer in order to follow along.

  • I now have my sequel, Workbench Open and connected to my local my sequel server.

  • But I don't have any files open for me to be able to run sequel commands.

  • In order to do that, I need to click this icon in the top left corner here that allowed me to create a new sequel tab for executing queries.

  • When I click on that, open up a file for me that I can start writing a sequel inside of.

  • So now let's talk a little bit about the sequel Syntax, which is luckily really straightforward to understand.

  • There are different keywords in sequel, such as the keyword Select Where From and all of these different keywords.

  • The highlight and blew most likely for you if you're using my steeple workbench, and if using some form of other editor, they're going to highlight in a specific color so that you can distinguish your key words from your non keywords, and these key words are not at all case sensitive.

  • So, for example, this selected I have here I could write it like this select.

  • I could write all lower case.

  • I could read it with capital letters just randomly thrown throughout it.

  • It doesn't matter as long as you write the word select.

  • It is going to work as your select keyword for sequel and sequel is using a combination of keywords, table names and column names in order to string together a different query.

  • So, for example, we could just say select From then you put the table name that you want to select from.

  • It's like the column here, and essentially, this is a sequel command.

  • They have a bunch of different keywords.

  • You have different table names, different column names, and then at the end of your sequel command, you need to put a semi colon.

  • Now this is not required in every single database management system.

  • But if you wanted to write to different sequel queries in one file, you need some Nicholas to separate them.

  • So I recommend always ending a cynical into the end of your sequel statement, no matter what you're doing.

  • Also, even though the key words can be written in all caps all lower case for any other combination of uppercase or lower case, it is almost always best practice and the standard to write all of your keywords in full upper case in order to distinguish them from your column names and table names, which will most likely be in lower case format as a post uppercase.

  • Also, if you need to write a string insider sequel, use single quotes and then put the string inside of those single coach to distinguish that you have a string instead of some form of keyword or some form of table or column name.

  • So now that we have out of the way, let's get started with actually creating a database for us to use.

  • In this example, I'm going to create a database for a record company, which will have bands, albums and songs inside of it for you to be able to work with.

  • Now that we've got the syntax out of the way, we're going to create a database for a record company, which is going to contain tables for bands and albums.

  • So to get started, we need to create this database because we don't actually have any databases in our sequel server that we've created yet.

  • So what's removal of this?

  • And we're going to write the Create data base, command this command a super straight for it.

  • Just write the words create and then database and name of the database that you want to create and, in our case, what is going to create a database called Test.

  • So we put test end it with a semi colon and then inside of my super server, there's two different ways that you can run a command.

  • There's this fighting both icon on the left here that just executes absolutely everything inside of this file over here, or it will execute whatever you highlight.

  • So if you want to just execute a few commands, you can highlight them and then click this excrement lightning bolt in order.

  • Execute them or there's the second option, which is this lightning bolt with the cursor.

  • And then I'll just execute whatever statement your cursor is inside of which is the method I usually use for running sequel commands inside of Sequel server.

  • So if we just click on this icon right here, we'll create a database called Test and you'll notice nothing actually happens.

  • It doesn't look like anything is created.

  • And that's because my sequel, Workbench, doesn't actually update the u I when you create new things very quickly.

  • So if you go over to the scheme this section, this is just where your databases air listed.

  • If you click refresh, it'll actually populate our database down here of test.

  • If we open that up, you see that we have an empty test database with no tables or anything else inside of it.

  • And that's exactly what we want, since we used to create database to create that database.

  • But since we're not creating a database called Test, we actually want our database to be about something else.

  • Let's remove this database.

  • To do that, we're going to use the drop data base command, and then we just put the name of the database again that we want to drop afterwards, which is test in our case ended with a semi colon, and if we run that statement, you see that over here on the left are test data base has been removed And if you refresh it, you see it won't pop back up.

  • And that's because we've dropped that database, all of the tables inside of it and all of the data inside of it.

  • And now this is something that you were almost never going to use because dropping a database deletes all of the data inside of that database.

  • And once you have your data in your database, you almost never want to completely destroy it.

  • So this is a command that you almost never use.

  • But it's good to know that it exists in case you accidentally create a database that you don't actually want.

  • In our case, that's exactly what we did.

  • So now it was create the actual database that we want, which, as I mentioned earlier, is going to be for a record company.

  • So create database.

  • Put the name of our database, which is going to be a record company in here.

  • And if we run that and refresh down here, you now see that we have a record company database and we can start adding tables to this database and start adding data into those tables now in order to make it so that our sequel queries that we're running over here in our file actually run against the database that we just created.

  • We need to tell sequel that we're going to use that database and to do that, we just used the use command.

  • So we type and use and in the name of the database that we want to run our queries on.

  • So in this case, we just want to run them on the record company database that we just created.

  • And if we just hit that to execute, we're now using the record company database and you see that it is bowled over here in my sequel, Workbench, which tells us that we're now using that data base.

  • So we run commands such as creating tables or adding data.

  • It'll add it and create it on the record company database.

  • Otherwise, it won't actually know what database we want to run these commands on.

  • So now we can work on creating our first table.

  • This is going to be done in a very similar fashion toe.

  • How create database was done.

  • But instead of using database, where is going to use create table and then the name of the table that we want to create, and we're just going to do a test table to start here.

  • And now, as I mentioned, earlier tables have columns inside of them that represent the different properties of the object that it's representing.

  • So when we create our table, we need to tell it what columns we wanted to create with.

  • So we create parentheses, and inside of these parentheses, we're going to put the different columns that we want for our table.

  • So, for example, in this table we're just going to add one calm.

  • She's gonna call it a test column, and then we need to give that column a type because our database needs to know what type of data it's storing.

  • For example, is it going to be a string?

  • Is that imager?

  • Is it a date floating point number?

  • We need to tell our database what type of data that holds.

  • So in our case, what she's into and again, since this is a key word, I like to keep it all upper case in order to distinguish it from my column names and table names, and we don't need to end any of this with a semi colon because all of this three lines right here is a single sequel command.

  • So we just want to put the sequel, the semi colon at the end of that command.

  • And this inside of here is not actually a command, so we don't want to end it with Symbicort.

  • Otherwise, we're going to get an error.

  • So now if we put our cursor inside of this command click to execute it, and if we refresh our scheme over here on the left, you'll see that we now have a little drop down by our tables and we have a test table and inside of that table in our columns we have our test column, which is a type of imager, and that's awesome.

  • But let's say we want to add another column to our table.

  • We forgot to add it in the beginning, and we don't want to go back and change this create table because we already have data in there, and if we recreate the table, we're going to lose that data.

  • So we have a command called altar table, which will allow us to change properties of our table after we create it.

  • So we just type in altar table and then the name of the table that we want to alter in our example.

  • It's going to be the test table, and then we tell it what we want to do.

  • So we're going to tell you want to add, and we want to add a calm So we're gonna put the column name here where it's gonna call it another column.

  • And then we say what type we want that calm to be.

  • And in our case, we're going to use a string, which in sequel there's many different ways to determine a string.

  • But the easiest way is using a bar char, which essentially says this is a variable length character ray, which is essentially just what a string is.

  • And then we need to tell the var char the maximum length that it could be.

  • So our case will just say 255 is the maximum length that our string will be.

  • So this will create a string column with a max length of 2 55 that is going to be named another column, and then we'll end that with a semi colon and you'll notice that I've created a line break in here.

  • I added this onto another wine and sequel.

  • Actually doesn't care about lying breaks in the statement.

  • It just reads it until it sees this semi corn.

  • So I could put as many line breaks in here is I wanted to, and it would still work just fine, even if I had it on multiple lines or all in one line.

  • So now if we run that, refresh our schema, you'll see that we now have another comb added to our columns of our test database, and that's great.

  • We now know how to create database tables, and we know how to add columns to those tables after we've created them.

  • And that's perfect.

  • So now, since we have this test table and we don't actually want it, let's look at dropping that table, which works exactly the same as dropping a database.

  • We just go in here type and drop table and then named the table, and if we run that, you'll see that our test table completely removes itself from the table section in the schema of the record company.

  • Now that we've removed that table that we don't actually want Let's work on adding a table for a band that we're going to use inside of our record company database, since we want to represent different bands for our record company.

  • So what's right?

  • Create table and we're gonna card table bands because it's going to just hold off our bands again.

  • We want our parentheses in order to say what our columns are going to be inside of here, and in our case, our bands are just going to have a name.

  • That's all we really care about is the name of the band.

  • So we use name as our column name again.

  • We want this to be a ve archer, and we'll just say 2 55 again for the length, and we never want a band to not have a name.

  • So to make sure our band always has a name, we're going to add, not know to our column.

  • So we just put not Noel here, and this says that our column can no longer have any noble values inside of it, which means it must always have a name defined.

  • This is a great way in order to force your table toe, have different values to find and little throne air.

  • If you try to insert a band that does not have a name, and now you may think that that's all we need to do to create a band.

  • But what if a band has the same name as another band?

  • That can happen.

  • How do we distinguish these two bands from each other?

  • And that's where using an I.

  • D column comes in handy in almost every table that you create inside of a database.

  • You're going to want to add an I D column in order to uniquely identify that row in that table from all of the other rose inside that table.

  • So up here, which is at a com or call it I d.

  • We want this just be an imager because that's an easy way to distinguish different things.

  • That could be 1234 and we easily could distinguish them from one another.

  • We never want this to be no again, just like the name.

  • And since this I D is something that is going to distinguish our records, we don't actually want to add this I d.

  • When we insert records, we wanted to be automatically generated whenever we add a new band to our table.

  • So we're going to give it the auto increments property.

  • And this just tells the table that we want to automatically increment this I d every time we add a new band, so the first band will be won.

  • The second band will be two and three and four and so on, and it will constantly auto increment this number for us without us having to do anything.

  • One last thing that we need to do.

  • We need to add a column in between our i D and name calm.

  • So that sequel knows that these are two separate columns, and we use that comma to separate them, just like we use Simek commas Semi Colon.

  • Sorry, in order to separate our different sequel commands instead of our file.

  • And lastly, since this I D.

  • Is going to be the identify rhe of our table, it is what's called a primary key on a primary key is the primary identifying column for that table.

  • And that's what you used to say that this is unique, and it is going to be what identifies an individual record inside of a table So we want to tell sequel that our I.

  • D column is our primary key.

  • So down here we're gonna use the primary key keyword in order to define a primary key and then inside of parentheses, we put what our primary key is, and in our case, it is the I.

  • D call.

  • Now, if we run this command and refresh our schema, you'll see that we now have the band's table inside that bands table.

  • We have these different columns, and you also see that we haven't index for our Primary Key, which tells sequel that this is what distinguishes are banned from the other band records inside of our table, which allows it to do quick queries.

  • If we give it an I D, it'll be much quicker than if we query on, say, the name column inside of our band table.

  • While around the case of creating tables, let's create the album table that's going to contain the different albums for our different bands inside of our database so we could just use create table again.

  • We'll call this table albums sensible container albums and then inside of here we're gonna put our parentheses, but the seeming Colin at the end of it, and now we want to define our different columns are first, calm again.

  • We're going to have that I d column to uniquely identify are different albums.

  • So it will say I d make it an energy not know again since we don't want this to ever be empty and auto increment lastly, so that automatically take care of increment in this number as we add new albums.

  • Next, we want our album have a name again.

  • So this is going to look very similar to our band of the top we want to create of our car.

  • We're gonna make it to 55.

  • Just sense.

  • It's the same as everything else.

  • And again, we don't want this to ever be empty.

  • So we'll say, not know so that every album will have a name and the last thing that our albums are going to have as we want to know when they were released.

  • So we're going to add a release year onto our albums would have this release year column will make it an imager, and we don't care if this is no, because maybe we don't know when the album's going to release.

  • Or maybe it hasn't been announced when this album were released.

  • So we want this to stay no so we won't put, not know on here.

  • And then we need to build to connect an album to a band.

  • But we can't just put a band column inside of here, and we can't put all the band information inside of the album.

  • We need to reference the band table from inside of our album table, and that's where this I D that we created up here comes in handy because now we can say that I d in the album's table and that will allow us to reference the band's table from within the album's table.

  • So in here, what's add a band I D.

  • Calm, which is what is going to have the idea of the band that this album is four.

  • So this is going to be an imager because it's the same as this idea Pierre and we want to make sure that it's not know because we don't ever want a album to not have a band, since every album needs to be composed by some band, and then we need to define our primary key Justus.

  • We did above, so we'll put primary key.

  • And then I D says the idea is our primary key for the album table as well.

  • And then the very last thing that we need to do is this band I.

  • D is referencing this band's table, which is referred to as a foreign key, which is any form of key that references a table other than itself.

  • So Albums has the primary key because that is the key defining the album records from each other.

  • The uniqueness and band I D is referencing the band's table, so that is a foreign key referencing a foreign table.

  • So we needed to find that relationship between the band I D and the band Table.

  • To do this, we're going to use the foreign key property and then inside of parentheses, we want to put what are foreign.

  • Key is which is banned I D.

  • So this is so far very smart, a primary key.

  • But we need to tell our foreign key what table it references.

  • So we're going to say that it references the band table and then we need to tell what column it is referencing, and it is referencing the I.

  • D call inside of that band table.

  • And now we have our foreign key set up between our albums and our bands.

  • So that sequel will no longer let us create an album if we give it a band I D.

  • That doesn't already exist in the band table.

  • Also, if we try to delete a band that has albums linking to that band, it will throw on air saying that you have it albums that exist for this band so you can't delete the band unless you also delete the albums that go with that band.

  • So before we run this, I need to fix one error that I made, and that is we're referencing our table.

  • We called our table bands, so we need to make sure that we reference that table exactly by name.

  • Where should be bands instead of band, which I accidentally written?

  • And now if we run this code, you'll see that if we come over to our tables and we refresh, we're now going to have this album's table and inside of this album table instead of our foreign keys, you'll see that we have a foreign key linking our albums to our band table.

  • We also have all the calls that we've created and the index for a primary key and band I D, which allows us to do quick searches for these different columns inside of our database.

  • Now that we've finally gotten all of the tables that we needed created, we can actually start working on adding data door tables and querying that table from our tables because that's really what Sequels for is for adding data and reading that data.

  • So let's get started by inserting some vans inside of our band table.

  • Let's go down here a little ways.

  • Do we have a little bit of space we want to do is we want to insert into the band's table and we want to supply the different values for the bands that we want to insert, So we're going to use the insert into command, and then you put the table name you want to insert data into, and in our case there's going to be the band's table.

  • And then after that you need to put all of the different columns that you want to insert into inside of parentheses.

  • and in our case, we only have one column, which is the name calm instead of our band table.

  • As I mentioned earlier, the I D column instead of our band table automatically generates itself so we don't actually need toe enter this when we add date it into our table.

  • So we inserted into our band's we're going to insert a name, and then we need to put the different values that we want to insert.

  • So we use the values keyword and then after that, in parentheses, we're going to put the name of the band, since that corresponds with this name column that we defined.

  • So let's just add Iron Maiden to our database at the semi colon at the end.

  • And if we run this command, you'll see that nothing happens.

  • But we've actually added that band into our database.

  • So let's add a few more bands into our database and then start querying these different bands.

  • So another way to insert data into the database is we're going to use this insert into command again and then put the band name or the table name Sorry, which is bands, the columns again, values and then if we want to put more than one entry inside of our table at the same time, instead of having to rewrite all of the different inserts into band's name, all that stuff over and over, all we have to do is put the different columns that we want.

  • So in here, what say we want to enter the band deuce, And then if we wanted to enter another band, what we do is but a comma and then inside of more parentheses.

  • We put the columns values for the next entry, so we'll put in avenge seven fold.

  • And then, lastly, we'll put in the band anchor with Symbicort at the end.

  • And now if we run this statement, it'll add three different bands to our band table, and it will give all of them a unique I.

  • D.

  • That'll be auto incremental on its own.

  • So if we run that command and now we have all four of those bands inside of our band's table and we can actually start quarrying the data from our table.

  • So to query the data from our table, we're going to use the select command so you just write out select and then the second thing that you want to write is the different columns that you want to select.

  • But if you want to select every column, all you need to do is put a star, and then I'll select every single column that you're Aquarian, and then we need to tell it what table we want to query from.

  • So we want a query from the band's table.

  • Now, if we just end that with a semicolon and execute that, you'll see that we did our different results.

  • You see that we have our i d calm, our name column and then the four names that we entered, as well as four unique I.

  • D.

  • S that were automatically generated by the database from our auto increment up above that we created when we created our table.

  • And that's great.

  • But what if, for example, we only wanted to get to bands back instead of all of the different bands To do that, we would just do this select exactly the same as before.

  • We're going to select from bands, but we need to tell it a limit.

  • So we just say limit.

  • I must say, we just want to bands.

  • So this is going to just get us the 1st 2 bands from our query.

  • And if we run that you not see that we just get Iron Maiden and Deuce, which were the 1st 2 bands returned by this query.

  • We can also get just certain columns instead of gain all the calls.

  • So to do that, we're going to do the same thing.

  • But instead of putting a star here, we're going to put a name, the calm we want to get, which, in our case, we just want the name calling.

  • So we'll say select name from bands.

  • And if we run, then we now just get the name column being returned and were no longer getting that I d home from the band's table.

  • Another nice thing that you can do is you can actually rename the columns in order to be easier to be red or used inside of your program.

  • So what's right up our slug statement again?

  • And let's say we want to select the I D.

  • But we want to change the name of our I d looks so we'll say as alias the name as something else.

  • I will say we want it to be returned his i.

  • D all upper case, and then we could do the same thing for the name.

  • So if you want to select multiple columns, just put a comma between them.

  • Will say we want the name column.

  • We want this to be written as a band name.

  • And then we just say We're gonna get them from bands and will end that with a semi colon.

  • And if we run that, you'll see that the titles for different columns have changed to be the same as what we wrote in the as here for our aliases of our calling names.

  • These aliases are really useful because you can also reference what you alias later in your execution, which will see when we start talking about more conflicts uses of the select statement as well as other statements and sequel.

  • The last thing that I want to talk about with the select statement before we move on is that you can actually order the way the elements inside of your select statement are rendered.

  • So what's right?

  • Another slept here.

  • What is going to select everything from the band's table again?

  • And then let's say we want to order them by the name.

  • We could just stay order by and then we write what we want to order them by and in our case, would you want to order by the name calm.

  • So if we run this you see that now instead of being ordered by I d there ordered in alphabetical order of the name that we supplied.

  • But if we wanted to reverse that order, we could just do it in descending order instead of ascending order.

  • So we write descending at the end here, run this And now they're in reverse alphabetical order and by default, descending is set to a sending which is given gave us the alphabetical order the first time.

  • But if you just leave this off, it'll just default to ascending order, which is what we had originally done.

  • And as you can see, when we run that it orders them in ascending order, which is what this order by property is great at doing so.

  • Now that we've warned about many of the different ways that we can select data from our table, let's add in some albums to our albums table down below.

  • So we need to use our insert into, as we've talked about earlier put title of the table that we want to insert into which is albums and then all of the different columns that we want to insert into.

  • So we want to insert into the name column, the release year calm and the band I D.

  • So we can link the idea of the album to a specific band that wrote that album, and then we want all the different values that we want to insert, and we're going to insert a bunch of different elements into our albums table in this one at statement.

  • So in parentheses here we see that our first column is the name calm.

  • So we're gonna put the name of our album is going to be the number of the beast.

  • And then we're going to put the release year of that album, which is going to be 1985 and then the idea of the band that wrote that album.

  • So Iron Maiden wrote that album.

  • Their ideas one.

  • So we're going to put a one here as the I d of the band, and then we'll put a comma will go down to the next time just to make this easier to read.

  • And we'll add an R next statement, which is going to be another album by Iron Maiden called Power Slave.

  • Really sure of that.

  • 1984 and again, a band idea of one, and we can enter another album.

  • This one is going to be nightmare, released in 2018 and this is by deuce.

  • So we're going to use the idea of two here, which corresponds with Deuce for the band.

  • And again, another album, which is also called Nightmare, This one released in 2010.

  • And this one's by Avenge seven Fultz.

  • We're going to use the idea of three and then, lastly, we're going to add one last album, this one was gonna call test album.

  • It's not going to have a release date because we don't know when this is actually released, and we're going to say that this was an album put out by Avenge seven Fold and and that with a semi colon.

  • And now if we run this, we actually added all those different albums to our albums table, and we can select them just by using the select statement that we talked about earlier so against I slept star from albums.

  • And if we run that, you said we have all the different albums being returned down here with their release year and the band I D that they correspond with.

  • And if you look at test album, you'll see that this is no for the release year because it doesn't actually have a year that released since we didn't actually supply a year, which is exactly what we want.

  • As I talked about earlier, we can select just the name from our albums table so we can slice select the name from albums.

  • If we run this, we're going to get all the names of the different albums inside of our database.

  • You'll notice that nightmare shows up twice because that album there's actually two nightmare outfits instead of our database.

  • But for this query, we just, for example, want to get the name off all the albums in our database, but we only want the unique names.

  • We don't want to get the same name back twice.

  • So in order to get only unique grows from our database, we just put distinct instead of our select query here.

  • And if we run that, you'll see that now we only get the names that are unique inside of our database and this distinct wine.

  • All it does is say everything that gets returned, which in our case, is just the name it compares them.

  • And if any of them are the same, it just removes all of them except for one.

  • So you only get one unique row for every single item inside of your database instead of getting duplicates.

  • If, for example, in our case we have to nightmare albums, it'll now only return one, which is exactly what we want in this case.

  • And now, if any of you are Iron Maiden fans, you may have noticed that I actually put in the wrong release year for the number of the beast album, which I did on purpose.

  • I promise you, I'm not terrible with knowing the years.

  • And in order to change that, we need to use the update query inside of sequel.

  • So we come down here just right update, and we need to put the table name that we want to update.

  • So in our case, we want to update the album's table, and then we need to tell her table what we want.

  • Update.

  • So we're gonna say we want to set the release year, and in our case, the release year for this album should be 1982 instead of 1985.

  • And then if we just run this right now, this will update every single album inside of our table, tohave the release year of 1982 which is definitely not what we want.

  • All we want to do is update the release year of a single record inside of our table.

  • So let's query all of our albums again by selecting this rope here, and we can see that this album has an idea of one that we want to change the release year to 1982 instead in 1985.

  • So if we scroll down here, we can use what is called the Wear Statement, which could be added to almost every sequel query in orderto filter down the actual results.

  • Painter returned so we can say where we put what we want a query on, so we'll use the idea calm.

  • We want to say where the I d equals one.

  • We want to do this query.

  • So we want to update the album's table by setting their release year to 1982 for every record for the I D equals one, which will just be one single record in our case.

  • And now if we run this, you'll notice.

  • Looks like nothing happens.

  • But if we create our albums table again, you'll see that the number of the beast really sure has been updated to 1982 from the original 1985 that we inserted into our table.

  • This update method is incredibly useful for what ever your data changes inside your database, which is something that is going to happen all the time inside of an application.

  • As I mentioned, this wear statement could be added to the end of multiple different statements, and one of the cases where it's used all the time is when you want to filter what you're selecting from the database.

  • In our case, we're spent selecting every room on the table, but this is normally not what you want to do.

  • What if you want to just select all of the albums that were released before the year 2000 we could write our select statement.

  • We'll just slept all the columns from our table albums.

  • And then we just say our wear statements so we can say where the release year.

  • In our case, we want the release year to be before 2000 so we'll just say less than 2000.

  • And if we run this, you'll see that it on.

  • Lee returns albums with a release year that occurred before the year 2000 and the wear cause has so many different ways that you can filter by, and it's incredibly useful.

  • Another way that we can filter is we can filter on the string by using wild cards to filter where the string contains certain parts of it.

  • So let me just write this up because it's much easier to explain by looking at it rather than me trying to talk about it.

  • So we'll select from the album's table or the name, and we want to say where it is, like so it's just going to be similar to the string that we give it so where the name is like and then inside of these quotes.

  • Whatever we put is what is going to compare the string, too.

  • So if we put a percent sign that says that everything inside of this percent area could be absolutely anything, it could be either.

  • No characters as many characters you want, it doesn't matter.

  • So let's put percent e r.

  • And then another percent sign.

  • So this says we want any amount of characters anywhere before this, and then the letters e.

  • R.

  • Somewhere in order inside of the string and then any amount of characters after that inside of our string.

  • But the same make on to the end and we'll run that.

  • And you see that we get the results for the number of the beast and power slave.

  • And that's because if you look at their name, they have e.

  • R.

  • Inside of the Navy somewhere and some amount of characters before it and some amount of characters after, which is what these 2% signs are equal to.

  • It could be a little bit confusing how that percent signed works, but essentially just think about it as it could be.

  • Anything it doesn't really matter.

  • So really, this is just checking.

  • If there's e.

  • R.

  • Somewhere inside of the string and you can also combine different where causes inside of your single query.

  • So in our case, if we wanted to select where the name has e.

  • R inside of it or where the band I d is equal to two, we could just say or fanned.

  • Idea equals two, and now this will check for this cause right here or of his cause over here and return both of those sets of results.

  • So if we run this, you'll see that we get both of our E.

  • R.

  • Results up here and we get the nightmare album because that's released by Band number two based on this band I D.

  • To which we queried on a pier in the or claws.

  • We could also make and be a different one.

  • So if we wanted to select from our albums again, and we wanted to say where the release year is equal toe 1984 and we want to make it so it's only for a band, I d.

  • One.

  • And if we run this, it'll check for the release your of 1984 and a band idea of one.

  • And on Lee Return Rose that have both of these statements working.

  • So we run that.

  • You'll see that we get the power sleep album because it has a release you of 1984 and it was put out by Band number one.

  • Now they're just two more quick ways that I want to talk about how the wear statement can be used and that is if you wanted to filter between two different values.

  • So if we want to select from the albums, we want a filter or the release year release here, we want to say where the release year is between two different numbers.

  • So we want to get all the albums between 2000 and 2018 so we just use the between keyword.

  • We put the beginning value the minimum value and and then we put the maximum value that we want to filter between.

  • And if we run this, you see we get on Lee albums released between 2000 and 2018.

  • The last thing that we can do with the wear statement because we can filter for things that are no.

  • So in our case, we can say where the release year is no, and this will return to us all of the records inside of her albums table that don't have a release your set.

  • And if we run this you see that we get this test album back because it's the only album in our database but has no revisions set for it.

  • And we don't really want this test album because this was a mistake.

  • We didn't actually want this data.

  • We should probably remove this data so we can use the delete Command, which we just right by doing delete from which is very similar to our select from we put the table name, which in our case is albums.

  • And then if we run this right now, it will delete every single row from our albums table, which we definitely do not want to do.

  • So we need to add a wear clause to tell where we want to delete.

  • So in our case, it was going to say where I d is equal to five.

  • Since that is the I d of our test album as we can see down here.

  • And if we run this you see, nothing actually looks like it happens.

  • But if we try to just select all of her albums.

  • What do that real quick?

  • We select all of our albums and run that you'll see that that fifth I d is no longer here because we deleted it from our database.

  • Now with that last statement of delete from being written, we've talked about the four main ways that sequel is used to interact with data by creating it, reading it, updating it and deleting it.

  • And this is really the cornerstone toe everything that you need to do with Sequel.

  • But there's quite a few more really unique and really powerful features that you're going to use all the time in Sequel.

  • But I want to talk about and the 1st 1 is the joint statement, which allows us to join two different tables together on different properties.

  • So in our case, we created this band I D.

  • Calm so we can join our band I d toe are banned table and ordered it queried the different albums for a different band or the different bands for different albums, and it really allows us to create powerful queries that allow us to create relations between our data inside of our database, which is what makes sequel so powerful.

  • So let's get started by just doing the most basic joint statement on our different albums and bands.

  • So if we got on here, we just need to do a basic select to start.

  • So we say we want to select everything from the band's table, and then we tell the band's table that we want to join it on the album's table.

  • So we'll say, Join fall by the name of the table we want to join on, which is the album's table.

  • And then we need to tell sequel.

  • How we want to join these tables because all joined does is check a query to say, Are these things equal?

  • Essentially.

  • So we say we want to query the album's table on when the band's Di di.

  • So we're acquiring this table, and then we want to get the I.

  • D column from that table.

  • So we're just getting the idea of our band.

  • We want to check when it's equal to the band I d.

  • From our albums table.

  • So we're saying the album's table calling band I D.

  • And we're comparing the different values inside of these rose together to see when they are equal.

  • We want to select them from our bands and our albums, who put a semi colon and run that you'll see that we get all of our band information being returned, as well as all over album information being returned for all of the bands that have albums inside of our database.

  • As you can see, this band, I D.

  • Is equal to the band idea in the album's table.

  • For all of our different records inside of our table, as you can see wishes exactly what Joint is doing.

  • It is just joining those two together on the Band Idea column and the i D calm of the band's table.

  • And then it shows output in the band information and the album information.

  • Because when we saying Slug Star, it's selected everything from the band's table and the album's table.

  • You'll also notice that The Iron Maiden Rose is actually duplicated here, and that's because it has two different albums Associate ID to that single band.

  • So it's listing the band twice and then the albums for each one over in section where the album is listed.

  • And there's multiple different ways that you can join in sequel three, specifically for my sequel and that is using inner join wishes exactly what the basic joint does, so you can either right inner join like this or this joint.

  • If we run that, you'll see that we get the exact same results.

  • There's also left join and right join.

  • So an easy way to understand how these different joints work is an inner join combines data where there is both of value on the table on the left, which in our case, is the band's table.

  • It's whatever table you right first as the tables on the left, on the table.

  • On the right is the album's table, which is the table you're joining onto it on.

  • Lee returns values that have a match.

  • So if, for example, the i.

  • D.

  • Is in the table here on the left and the idea is in the table on the right, left Join will allow us to have all of the bands that don't have any albums will also show up because left joined list everything from the left side, which is the first table you list in the from Here, the band's table.

  • It'll list all of those tables, even if they don't have a matching albums.

  • So let's copy this here and do a left joint instead of an inner joy.

  • So if we change in ER to be left and we run that, you'll see that we now get this result down here for the anchor band and you'll see that there's actually no albums associated with this band at all.

  • But it's being returned because we're doing a left joint, which joins with this from statement right here.

  • And it's saying, even if there is nothing for its okay, because we're left joining.

  • So we're returning everything on the left side, no matter what.

  • And then if we wanted to do a right join, it'll joint on the right side.

  • So if there is an album with no band associative, if would still return that album.

  • But in our case, as you can see, there are no albums that don't have any bands.

  • So in order to make it look like this right joins working.

  • If we spot albums up to here and bands down to here now, the right set of our table is this band's table, and if we run this, you'll see that now we get this anchor band here again because it's on the right side of our query.

  • So it's on a joint statement.

  • And even though it has nothing on the left side, which is this album section, it's still being returned because we're doing a right joint instead of doing a left joint or an inner join.

  • So for the most part, you're only going to use inner joins and left joint because right joints are essentially the same thing is left join just flipped, which makes it a little bit more confusing to reason within your mind.

  • So inner joins are really useful when you only want to get records back when there is both of value in the table on the left, which were selecting from and the table on the right, which you're joining, too.

  • And left joints are really useful when you just want to get absolutely everything from the left side table, which you're selecting from, and then just get the things from the right side table if they exist.

  • And if they don't still return the thing from the left side, even if it doesn't have anything on the right side, these air to very common queries that you're going to use all the time and sequel because the point of using a relational database system is so you can have relations in your database.

  • It's literally in the name, and that's what these drawing and statements allow you to do is to build a query data based on these different relations.

  • Now we're finally armed to the very last topic that I want to talk about in this video, which is aggregate functions and grouping by in your different sequel statement.

  • Let's first start by talking about aggregate functions because they're very straightforward to understand what's going on here a little ways.

  • What is going to create a select?

  • And instead of selecting an individual column from our table, we're going to use an aggregate function in order to select the aggregate of our data.

  • So we want to select the average, so we use average here a V G and inside of here we're going to place the column name that we want to average over so well, average the release year because we want to figure out what the average

Oh everyone, I hope you're having a good day.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

60分鐘學會SQL (Learn SQL In 60 Minutes)

  • 4 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字