- Read Tutorial
- Watch Guide Video
So this section is going to be all about functions but before we can get into functions. I think it's time that we walk through how to change a datatype in our database. So right now if we try out a function that we already know about which is the “MIN” function.
If I try this on guides_revenue from guides and if I run this it's going to supposedly run without error. Except that it brings back a 1000 and that is not what we're looking for because if we run SELECT * FROM guides
.
Then run this you can see that our minimum should actually be 500 and this is because we have a varchar inside of our database with the guides revenue, when it really should be a floating-point get away with just casting this which is what we've done before. So we can say cast guides revenue and then we can say as unsigned.
Then this would work. So if I run this you can see the minimum is now 500 and everything is good except let's talk about changing it. Now I did want to give the example of casting and so is good for a while to keep this as a varchar.
You are most likely at some point in your development journey going to be dealing with old legacy data and you're going to be dealing with plenty of integer values. Those integer values could actually be stored as varchars inside of the database. Now let's talk about how we can actually change an entire data type.
So I am going to delete SELECT MIN(CAST(guides_revenue AS UNSIGNED))
. Let's come into our guides table and if you click on the little wrench icon next to guides here, we can come in switch this up.
So as you can see right now it's a VAR car and I'm going to come down and change it all the way down to float. So if I and I could change it to a decimal two either one would work and in fact let's change it to decimal.
The difference between a decimal and a flow is a decimal is even more specific. So when you're dealing with financial data then decimals most likely going to be what you want to use. Here we have changed this to decimal. Now if I click on apply this brings up the dialog box and it says alter table and it says change this column guides_revenue to DECIMAL and it still has to be not null.
Now if I hit apply everything worked properly. So let's come back and I'm going to run the same query run it and you can see everything seems like it still is back into the state it was before.
Now let's try our query. So you say SELECT MIN (guides_revenue) FROM guides
.
So now if I run this now it's working. Do you see the difference there? The difference was before this, our data is being stored in a string format in a VARchar or various character format. That can cause a few issues whenever you're dealing with financial data. Not just a few it causes quite a few.
So we are forced to cast and change the datatype which sometimes you want to do but then other times it's just easier to change the data type entirely. So now, this actually works. That is how you can change the data type in SQL and in the next few guides we're going to take a deep dive in two functions.