Next up, we've got a quick aside.
I want to talk about quotation marks When we are working with text in MySQL, which we have been right
with all the var char columns, we provide those values instead of quotes.
And technically in my SQL, depending on the mode that it's running in, you could use double quotes
or single quotes, but that is not the case in other relational database management systems, aka other
flavors of SQL.
You don't want to use double quotes, you'll actually get errors.
So it's a good practice to always use single quotes to wrap around whatever text we're providing when
we're inserting or working with data.
So let's make a really quick table.
Let's call this shop.
So create table shops and it's just going to have a single thing in there called Name, which is a bar
chart of, I don't know, 100 OC.
It looks like I have an error.
Oh, I'm missing a second closing paren.
So now I'm going to insert into this table insert into shops.
We only have a name to insert and the values will just be something like, I don't know, Shoe emporium,
whatever.
All right.
So we would use single quotes around this text.
Technically, I could use double quotes with the way that I'm running my SQL right now, but it's best
to not do that to avoid any confusion, especially if you try writing SQL in any other SQL database
flavor.
So this will work.
But what about if I have a quote within my piece of text?
Within my string, for example, we have something like Mario's Pizza.
Well, this is going to be a problem because my SQL, just like any other programming language, if
you've worked with them, is going to look for the opening quote in a closing quote and it thinks this
is the piece of text and then it doesn't know what this is.
So if I hit enter here, it first of all, doesn't even acknowledge that I've hit enter because I had
an opening quote, a closing quote and then another opening quote.
So it thinks this is all inside of a string.
And in fact, right here, see how the cursor hit?
The prompt has a quote there.
That's my school's way of telling me, I'm waiting for you to close the quote.
So we don't want to do this.
We have an error.
It doesn't work.
So what we need to do instead is escape that quote and the way that we can escape a single quote or
I guess an apostrophe in this case is to put a backslash in front of it.
Now, my SQL will encounter that and it realizes that's not actually going to be a quote that is terminating
this piece of text.
It's just a quote, character inside this larger piece of text.
So now if I do a select star from shops, we'll see that Mario's pizza is correctly formatted.
Now, I can also do the same thing for double quotes if I needed double quotes inside of a piece of
text.
This is going to be a stupid example because there's no real reason to have double quotes here.
But imagine I had a piece of text like she said.
Ha ha.
Well, if I have double quotes inside of single quotes, I don't have to escape anything.
This is totally fine.
Let's see what happens.
Select star from shops.
And those double quotes made it inside our piece of text.
But if I did happen to be working with double quotes and then I wanted a double quote inside, I could
escape that double quote character.
But I don't think it's even worth showing you because as you'll recall, a couple of minutes ago, I
talked about how you want to use single quotes on the outside, always in my SQL, even if you can get
away with double quotes, sometimes just stick with single quotes to be consistent and you won't have
to worry about it.
And then if you need to escape single quotes, use the backslash, single quote character and if you
need to, you could put double quotes inside of single quotes.
There's no problem with that.
That's just considered regular text.
We can do whatever we want.
And here it's just when we start an end, a string that we have to pay special attention.
Okay, not the most exciting topic, but something that we have to get out of the way.
So that's it.
You're single quotes.
That's all you should remember.