For example when you set de default value to 3, how can you later read this default value?
Code: Select all
ALTER TABLE `configuration` CHANGE `id` `id` INT( 10 ) NOT NULL DEFAULT '3';Moderator: General Moderators
Code: Select all
ALTER TABLE `configuration` CHANGE `id` `id` INT( 10 ) NOT NULL DEFAULT '3';Code: Select all
SELECT column FROM table WHERE id = 3;Is there an alternative?DEFAULT() was added in MySQL 4.1.0.
Sorry, but I don't want to read the values stored in a field, but the DEFAULT setting of a field.bdlang wrote:I'm not sure what you mean, are you asking how to retrieve all records with a value of 3?Code: Select all
SELECT column FROM table WHERE id = 3;
Thanks, this will indeed give the needed information.feyd wrote:SHOW CREATE TABLE should give you the information.
Code: Select all
mysql_get_client_info()
//or
mysql_query("SELECT VERSION() as mysql_version");Ok, SHOW CREATE TABLE tablename (as feyd has already mentioned) or DESC tablename will show you the value; are you trying to retrieve this via PHP? Run the query and retrieve the Default field using mysql_result().WaldoMonster wrote: Sorry, but I don't want to read the values stored in a field, but the DEFAULT setting of a field.
Code: Select all
$default = '';
$query = mysql_query('DESC configuration');
while($result = mysql_fetch_array($query))
{
if ($result['Field'] == 'id')
$default = $result['Default'];
}I wondered from the query "DESC configuration". What does it return ?WaldoMonster wrote:Thanks for all the fast and valuable feedback.
Here I have a working solution for php:Code: Select all
$default = ''; $query = mysql_query('DESC configuration'); while($result = mysql_fetch_array($query)) { if ($result['Field'] == 'id') $default = $result['Default']; }
In this case, `configuration` is the name of the table, not some magic MySQL 'configuration' keyword, in case that's what you're wondering. DESC is short for DESCRIBE, which returns general column information from said table.dibyendrah wrote: I wondered from the query "DESC configuration". What does it return ?
Code: Select all
mysql> DESCRIBE configuration;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| name | char(35) | No | | | |
| color | char(35) | No | | | |
| id | int(10) | | | 3 | |
+------------+----------+------+-----+---------+----------------+Code: Select all
mysql> DESCRIBE configuration id;
+------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+----------+------+-----+---------+----------------+
| id | int(10) | | | 3 | |
+------------+----------+------+-----+---------+----------------+Code: Select all
$query = mysql_query('DESCRIBE configuration id');
$default = mysql_fetch_array($query);
$default = $default['Default'];Thanks, It will also make the code one line shorterbdlang wrote:Ah. I'm sorry I didn't post it sooner; if you notice, my previous post recommends using mysql_result(), I'd use that over retrieving a two index array (0, 'Default'). A bit more efficient.
Code: Select all
select COLUMN_DEFAULT
from INFORMATION_SCHEMA.COLUMNS
where
TABLE_SCHEMA=database()
and TABLE_NAME='$table_name'
and COLUMN_NAME='$column_name'