Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fsdolphin's avatar

Understanding Database Column Types

First of all let me apologize in advance about the silly question but I have zero experience with databases.

What are the advantages of defining the type of an input in a database?

For instance if I defined the following two fields...

        $table->integer('year');
        $table->float('price');

Will the return type be different when you query the database?

The reason for my question is because when I define a column as integer it does let me input numbers with decimals and I was expecting some sort of database error, I know I should have some validation but I thought that since you defined the type the database would only except that type.

Where can I get an introduction on general Database Column Types?

Thanks

0 likes
5 replies
SaeedPrez's avatar

MySQL documentation is a good place to start.

And you can always google mysql data types for more information.

It's also good if you understand how collation works as it can return different (sorting) results.

1 like
willvincent's avatar

The reason for my question is because when I define a column as integer it does let me input numbers with decimals and I was expecting some sort of database error

The database is rounding those to the closest whole number though, so when you fetch that data back out of the DB, it's not going to be a float.

Also of note is that float by default may not have enough precision after the decimal point for you.. I actually prefer double to float a lot of the time...

ohffs's avatar

It's worth noting that if you're using sqlite it will let you store pretty much anything in a column no matter what you define it as (which might also explain storing a float on an integer column). Eg :

sqlite> create table testeroo (ix integer, fl float);
sqlite> insert into testeroo values(1, 1);
sqlite> insert into testeroo values('fred', 'ginger');
sqlite> insert into testeroo values(1.9, 3.4);
sqlite> select * from testeroo;
1|1.0
fred|ginger
1.9|3.4
1 like

Please or to participate in this conversation.