Is there a way to change te value of the Extra column that is shown with the SHOW COLUMNS/DESCRIBE sentences?
The documentation about this column states the following:
Extra
Any additional information that is available about a given column. The
value is nonempty in these cases:
auto_increment for columns that have the AUTO_INCREMENT attribute.
on update CURRENT_TIMESTAMP for TIMESTAMP or DATETIME columns that
have the ON UPDATE CURRENT_TIMESTAMP attribute.
VIRTUAL GENERATED or VIRTUAL STORED for generated columns.
DEFAULT_GENERATED for columns that have an expression default value.
I have the next table columns information but I wish to remove the Extra value of the start_date column.
Is there a way to do this?
+--------------------+--------------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------------+------+-----+-------------------+-------------------+
| id_machine_product | "int(10) unsigned" | NO | PRI | NULL | auto_increment |
| ... | ... | ... | ... | ... | ... |
| start_date | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+--------------------+--------------------+------+-----+-------------------+-------------------+
EDIT:
I have implemented a fingerprint validation method in PHP that diffs the DESCRIBE tables values, I have database versions in production that doesn't have that Extra value even though those columns have an expression default value, so currently, I wish to alter that value so I don't get errors from my implemented fingerprint validation method in my development environment.
The production databases are in Mysql < 8.0 so, as per Bill Karwin's answer, I'm having trouble with my MySQL development environment version that is 8.0
It's not clear from your question why you want to eliminate the Extra information. It's just noting that the column's default is an expression.
To make the Extra field blank, you must make the column's default either a constant value or NULL.
mysql> create table foo ( id int unsigned primary key, start_date timestamp not null default current_timestamp);
mysql> show columns from foo;
+------------+------------------+------+-----+-------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+-------------------+-------------------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
+------------+------------------+------+-----+-------------------+-------------------+
mysql> alter table foo modify start_date timestamp default null;
mysql> show columns from foo;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | YES | | NULL | |
+------------+------------------+------+-----+---------+-------+
Note that the Extra information "DEFAULT_GENERATED" is only present in MySQL 8.0. I suspect it's related to the new feature to support expressions in the DEFAULT clause. Any other expression also results in this Extra information.
mysql > alter table foo modify start_date timestamp default (now() + interval 1 hour);
mysql> show columns from foo;
+------------+------------------+------+-----+---------------------------+-------------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------------------------+-------------------+
| id | int(10) unsigned | NO | PRI | NULL | |
| start_date | timestamp | YES | | (now() + interval 1 hour) | DEFAULT_GENERATED |
+------------+------------------+------+-----+---------------------------+-------------------+
Topicstarters comment
I have implemented a fingerprint validation method in PHP that diffs
the DESCRIBE tables values, I have database versions in production
that doesn't have that Extra value even though those columns have an
expression default value, so currently, I wish to alter that value so
I don't get errors from my implemented fingerprint validation method
in my development environment.
The more standard SQL method would be which also works in MySQL 8
Query
SELECT
information_schema.COLUMNS.COLUMN_NAME AS 'Field'
, information_schema.COLUMNS.COLUMN_TYPE AS 'Type'
, information_schema.COLUMNS.IS_NULLABLE AS 'Null'
, information_schema.COLUMNS.COLUMN_KEY AS 'Key'
, information_schema.COLUMNS.COLUMN_DEFAULT AS 'Default'
, information_schema.COLUMNS.EXTRA AS 'Extra'
FROM
information_schema.TABLES
INNER JOIN
information_schema.COLUMNS ON information_schema.TABLES.TABLE_NAME = information_schema.COLUMNS.TABLE_NAME
WHERE
information_schema.TABLES.TABLE_NAME = '<table>'
This query should match the output of DESCRIBE
Then you could use REPLACE() on information_schema.COLUMNS.EXTRA output to remove or edit the way you want. For example removing extra features like DEFAULT_GENERATED or VIRTUAL GENERATED (generated columns)
you need an alter table statement. Something like
ALTER TABLE `document` MODIFY COLUMN `start_date ` INT AUTO_INCREMENT;
You can set a default value like
DEFAULT 1 NOT NULL
Related
Pretty straight forward question here, I think this should work but it doesn't. Why doesn't it?
CREATE TABLE INVOICE(
INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE
)
It doesn't work because it's not supported
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
According to this documentation, starting in MySQL 8.0.13, you will be able to specify:
CREATE TABLE INVOICE(
INVOICEDATE DATE DEFAULT (CURRENT_DATE)
)
MySQL 8.0.13 was released to General Availability in October 2018. The release info is located here.
declare your date column as NOT NULL, but without a default. Then add this trigger:
USE `ddb`;
DELIMITER $$
CREATE TRIGGER `default_date` BEFORE INSERT ON `dtable` FOR EACH ROW
if ( isnull(new.query_date) ) then
set new.query_date=curdate();
end if;
$$
delimiter ;
Currently from MySQL 8 you can set the following to a DATE column:
In MySQL Workbench, in the Default field next to the column, write: (curdate())
If you put just curdate() it will fail. You need the extra ( and ) at the beginning and end.
create table the_easy_way(
capture_ts DATETIME DEFAULT CURRENT_TIMESTAMP,
capture_dt DATE AS (DATE(capture_ts))
)
(MySQL 5.7)
I have the current latest version of MySQL: 8.0.20
So my table name is visit, my column name is curdate.
alter table visit modify curdate date not null default (current_date);
This writes the default date value with no timestamp.
----- 2016-07-04 MariaDB 10.2.1 -- Release Note -- -----
Support for DEFAULT with expressions (MDEV-10134).
----- 2018-10-22 8.0.13 General Availability -- -- -----
MySQL now supports use of expressions as default values in data type specifications. This includes the use of expressions as default values for the BLOB, TEXT, GEOMETRY, and JSON data types, which previously could not be assigned default values at all. For details, see Data Type Default Values.
As the other answer correctly notes, you cannot use dynamic functions as a default value. You could use TIMESTAMP with the CURRENT_TIMESTAMP attribute, but this is not always possible, for example if you want to keep both a creation and updated timestamp, and you'd need the only allowed TIMESTAMP column for the second.
In this case, use a trigger instead.
I came to this page with the same question in mind, but it worked for me!, Just thought to update here , may be helpful for someone later!!
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+---------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | NULL | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+---------+----------------+
4 rows in set (0.003 sec)
MariaDB [niffdb]> ALTER TABLE invoice MODIFY inv_dt DATE NOT NULL DEFAULT (CURRENT_DATE);
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+-----------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | curdate() | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+-----------+----------------+
4 rows in set (0.002 sec)
MariaDB [niffdb]> SELECT VERSION();
+---------------------------+
| VERSION() |
+---------------------------+
| 10.3.18-MariaDB-0+deb10u1 |
+---------------------------+
1 row in set (0.010 sec)
MariaDB [niffdb]>
While creating a table, you have to use CURRENT_DATE() function as default value. Please see below example I just tested.
CREATE TABLE SALES_DATA (
SALES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
SALES_GIRL_ID INT UNSIGNED NOT NULL,
SALES_DATE DATE NOT NULL DEFAULT (CURRENT_DATE()),
TOTAL_SALES FLOAT(6, 2),
PRIMARY KEY (SALES_ID),
FOREIGN KEY (SALES_GIRL_ID) REFERENCES SALES_GIRLS(ID)
);
Pretty straight forward question here, I think this should work but it doesn't. Why doesn't it?
CREATE TABLE INVOICE(
INVOICEDATE DATE NOT NULL DEFAULT CURRENT_DATE
)
It doesn't work because it's not supported
The DEFAULT clause specifies a default value for a column. With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column
http://dev.mysql.com/doc/refman/5.5/en/create-table.html
According to this documentation, starting in MySQL 8.0.13, you will be able to specify:
CREATE TABLE INVOICE(
INVOICEDATE DATE DEFAULT (CURRENT_DATE)
)
MySQL 8.0.13 was released to General Availability in October 2018. The release info is located here.
declare your date column as NOT NULL, but without a default. Then add this trigger:
USE `ddb`;
DELIMITER $$
CREATE TRIGGER `default_date` BEFORE INSERT ON `dtable` FOR EACH ROW
if ( isnull(new.query_date) ) then
set new.query_date=curdate();
end if;
$$
delimiter ;
Currently from MySQL 8 you can set the following to a DATE column:
In MySQL Workbench, in the Default field next to the column, write: (curdate())
If you put just curdate() it will fail. You need the extra ( and ) at the beginning and end.
create table the_easy_way(
capture_ts DATETIME DEFAULT CURRENT_TIMESTAMP,
capture_dt DATE AS (DATE(capture_ts))
)
(MySQL 5.7)
I have the current latest version of MySQL: 8.0.20
So my table name is visit, my column name is curdate.
alter table visit modify curdate date not null default (current_date);
This writes the default date value with no timestamp.
----- 2016-07-04 MariaDB 10.2.1 -- Release Note -- -----
Support for DEFAULT with expressions (MDEV-10134).
----- 2018-10-22 8.0.13 General Availability -- -- -----
MySQL now supports use of expressions as default values in data type specifications. This includes the use of expressions as default values for the BLOB, TEXT, GEOMETRY, and JSON data types, which previously could not be assigned default values at all. For details, see Data Type Default Values.
As the other answer correctly notes, you cannot use dynamic functions as a default value. You could use TIMESTAMP with the CURRENT_TIMESTAMP attribute, but this is not always possible, for example if you want to keep both a creation and updated timestamp, and you'd need the only allowed TIMESTAMP column for the second.
In this case, use a trigger instead.
I came to this page with the same question in mind, but it worked for me!, Just thought to update here , may be helpful for someone later!!
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+---------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | NULL | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+---------+----------------+
4 rows in set (0.003 sec)
MariaDB [niffdb]> ALTER TABLE invoice MODIFY inv_dt DATE NOT NULL DEFAULT (CURRENT_DATE);
Query OK, 0 rows affected (0.003 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [niffdb]> desc invoice;
+---------+--------+------+-----+-----------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------+------+-----+-----------+----------------+
| inv_id | int(4) | NO | PRI | NULL | auto_increment |
| cust_id | int(4) | NO | MUL | NULL | |
| inv_dt | date | NO | | curdate() | |
| smen_id | int(4) | NO | MUL | NULL | |
+---------+--------+------+-----+-----------+----------------+
4 rows in set (0.002 sec)
MariaDB [niffdb]> SELECT VERSION();
+---------------------------+
| VERSION() |
+---------------------------+
| 10.3.18-MariaDB-0+deb10u1 |
+---------------------------+
1 row in set (0.010 sec)
MariaDB [niffdb]>
While creating a table, you have to use CURRENT_DATE() function as default value. Please see below example I just tested.
CREATE TABLE SALES_DATA (
SALES_ID INT UNSIGNED NOT NULL AUTO_INCREMENT,
SALES_GIRL_ID INT UNSIGNED NOT NULL,
SALES_DATE DATE NOT NULL DEFAULT (CURRENT_DATE()),
TOTAL_SALES FLOAT(6, 2),
PRIMARY KEY (SALES_ID),
FOREIGN KEY (SALES_GIRL_ID) REFERENCES SALES_GIRLS(ID)
);
I have a table that contains NULL values. This table is meant only to store numerical values, except the second column which contains a time-stamp for each record. This table has been in use for some time and so has accumulated a lot of NULL values in varying columns. Here's the table's description:
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
| results_id | int(11) | NO | PRI | NULL | auto_increment |
| time_stamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| test_col | int(11) | YES | | NULL | |
| test_col-total | int(11) | YES | | NULL | |
| test_col_B | int(11) | YES | | NULL | |
| test_col_B-total | int(11) | YES | | NULL | |
+-----------------------------------------+-----------+------+-----+-------------------+----------------+
12 rows in set (0.01 sec)
I now want to UPDATE/ALTER the table so that:
from now on any NULL value being added to the table is handled and processed as a '0' value instead (really interested to know if this is indeed possible; if it is then I wont need to change a load of INSERT queries in a lot of my Python scripts elsewhere!)
all stored NULL values are updated/changed to '0'.
I am entirely stuck with this because on the one hand I want my SQL query to update a new rule to the table while on the other change current NULL values and as a novice this is a little more intermediate for my current understanding.
So far I have:
ALTER TABLE `results` MODIFY `<col_name>` INT(11) NOT NULL;
And I will do this for each column that currently allows NULL values. However, I do not know how to change stored NULL values to '0'.
Any input appreciated.
to change NULL values to 0
try
UPDATE results SET `col_name` = 0 WHERE `col_name` IS NULL;
to change columns to have NOT NULL and default to 0 try
ALTER TABLE results MODIFY `col_name` INT(11) NOT NULL DEFAULT 0;
you have to do it in the above order, i just tested this on http://sqlfiddle.com/
First change your values to 0 where they are null:
UPDATE results SET col1 = 0 WHERE col1 IS NULL;
...
Then you can add a DEFAULT of 0, that will be added whenever you supply no values to that table on an insert
ALTER TABLE `results` MODIFY `<col_name>` INT(11) NOT NULL DEFAULT 0;
I am trying to change an existing column in a table I have to allow for null values and then set the default value to null. I tried running the following but it does not seem to be updating the table:
mysql> ALTER TABLE answers_form MODIFY sub_id int unsigned NULL DEFAULT NULL;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc answers_form;
+--------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+-------+
| answer_id | int(10) unsigned | NO | PRI | 0 | |
| sub_id | int(10) unsigned | NO | PRI | 0 | |
| form_id | int(10) unsigned | NO | PRI | NULL | |
| value | varchar(255) | NO | | NULL | |
| non_response | bit(1) | YES | | b'0' | |
+--------------+------------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
Can anyone see what I am doing wrong here?
its a primary key , mysql doesn't allow any part of the primary key to be null, which does make the fact that it allows a default value of null for the form_id odd, however the docs at
http://dev.mysql.com/doc/refman/5.5/en/optimizing-primary-keys.html
say "Query performance benefits from the NOT NULL optimization, because it cannot include any NULL values".
Just out of curiosity, does it actually allow you to put in null values in the form_id field?
You have 2 non-nullable columns with the default value of null. This shouldn't be allowed by your database engine. If it is, it is rather far from a best practice.
sub_id is listed as a primary key
From the MySQL docs (5.7, but other versions say the same thing):
A PRIMARY KEY is a unique index where all key columns must be defined
as NOT NULL. If they are not explicitly declared as NOT NULL, MySQL
declares them so implicitly (and silently).
As to the discussion about the Non-null columns having a Default of NULL...
The NULL value in the Default column means that there is no default, not that the default is NULL.
Fiddle: http://sqlfiddle.com/#!2/c718d/1
If I create a simple table like so:
CREATE TABLE name_num(
Number INT PRIMARY KEY,
Name TEXT NOT NULL
);
And then do desc name_num, I get:
| FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA |
---------------------------------------------------
| Number | int(11) | NO | PRI | (null) | |
| Name | text | NO | | (null) | |
Again, from the MySQL docs:
If the column cannot take NULL as the value, MySQL defines the column with no explicit DEFAULT clause. Exception: If the column is defined as part of a PRIMARY KEY but not explicitly as NOT NULL, MySQL creates it as a NOT NULL column (because PRIMARY KEY columns must be NOT NULL), but also assigns it a DEFAULT clause using the implicit default value. To prevent this, include an explicit NOT NULL in the definition of any PRIMARY KEY column.
I have a tableA with following output from desc tableA command:
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | NO | | | |
| city | varchar(50) | YES | | NULL | |
| state | char(2) | YES | | NULL | |
| country | varchar(30) | YES | | NULL | |
| notes | longtext | YES | | NULL | |
| type | varchar(50) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
Now there are 3 columns with NOT NULL constraints:
id
name
type
For columns id and type, I need to remove the default constraint. Basically I want Default: None. I do not want to use the workarounds eg . setting default to '' for a varchar.
The difference between NULL, NONE, and '' is made more clear from this discussion Default-values-for-varchar-and-int-mysql-data-types
I tried using the command:
alter table tableA alter column type drop default;
The query runs fine, but no rows are affected. And no change in Default value is shown when I run describe command.
If I set the default value to '' I run into different issue - the database allows the entry of empty string in the db. For me that is equivalent to inserting NULL for a column's value, and I do not want to allow that.
I need some guidance on how to handle Default values in this situation where I cannot allow empty strings as data in the db. I want to mention that I am planning to put validations in the code to check if the incoming data is an empty string or NULL. But just in case that validation is not working etc, I want to make sure the DB can refuse to add such data.
Any help is really appreciated.
If the column can be then null, then either default or null are the same.
So Allowed Null, Default null is effectively irrelevant except when doing say
Insert (name,city,type) Values ('Fred',DEFAULT,'Caucasian')
Null isn't an empty string. Given you are allowing null in the table but interpreting it as empty string in your application, you have an irritating flaw in your design.
If you don't want empty strings in there, normally you'd use a check constraint which as far as I know still isn't implemented in mysql. Apparently this lack is usually solved with an insert trigger.
So you'd check the value in the trigger and then fail the insert for empty strings.
PS it doesn't solve the integrity problem, but if you did want a way to put nulls in when a straing was empty so you would not have to distinguish between empty string and null.
Then have a look at the nullif function.