How to get the “DEFAULT” value of a column?

Como pegar o valor “DEFAULT” de uma coluna?
Default

Imagine you need to “fetch” the “DEFAULT” value of a specific column programmatically… How to do that? Fortunately, MySQL provides a very cool function for that. It’s the DEFAULT() function.

It’s worth remembering! The DEFAULT value of a column is the value that the database server (MySQL) will assign to a specific column if no value is provided for it during an insert.

This function is interesting because it allows us to know, in advance, the default value that will be assigned to a given column, in case the application doesn’t provide it.

Here’s an example:

sqlCopiarEditarmysql> CREATE TABLE teste_default -> ( -> Flag CHAR(2) DEFAULT 'PG', -> data DATE DEFAULT '2012-12-31', -> vr INT DEFAULT 300 -> ) -> ; Query OK, 0 rows affected (0.04 sec) mysql> insert into teste_default set FLAG='SP'; Query OK, 1 row affected (0.02 sec) mysql> select * from teste_default; +------+------------+-----+ | Flag | data | vr | +------+------------+-----+ | SP | 2012-12-31 | 300 | +------+------------+-----+ 1 row in set (0.00 sec) mysql> select DEFAULT(flag), DEFAULT(data), DEFAULT(vr) from teste_default; +------------------+------------------+-----------------+ | DEFAULT(flag) | DEFAULT(data) | DEFAULT(vr) | +------------------+------------------+-----------------+ | PG | 2012-12-31 | 300 | +------------------+------------------+-----------------+ 1 row in set (0.00 sec)

Schedule a meeting here

Visit our Blog

Learn more about databases

Learn about monitoring with advanced tools

Default

Have questions about our services? Visit our FAQ

Want to see how we’ve helped other companies? Check out what our clients say in these testimonials!

Discover the History of HTI Tecnologia

Compartilhar: