MySQL 5.7 using CURRENT_TIMESTAMP not working - mysql

I have a table existed. I want to alter the table so that it has a column storing Created date. I had looked up some articles insists that from Mysql ver 5.6, you can use DATETIME with CURRENT_TIMESTAMP. but I failed to implement.
~:$ mysql --version
mysql Ver 14.14 Distrib 5.7.22, for macos10.13 (x86_64) using EditLine wrapper
MYSQL VERSION 5.7
mysql> ALTER TABLE table_name MODIFY datecreated DEFAULT CURRENT_TIMESTAMP;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CURRENT_TIMESTAMP' at line 1
error occured
mysql> SHOW COLUMNS FROM table_name;
+---------------+---------------+------+-----+-------------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+---------------+------+-----+-------------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
...
...
..
.
.
.
| datecreated | datetime | YES | | NULL | |
+---------------+---------------+------+-----+-------------------------+----------------+
15 rows in set (0.00 sec)
table infos.

Thanks everyone, and especially #fifonik and #NICK
mysql> ALTER TABLE table_name MODIFY datecreated DATETIME NULL DEFAULT CURRENT_TIMESTAMP;
😀
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0

Related

MySQL throwing syntax error while trying to alter a table

I have been trying to alter a table to include a date column with default value of CURDATE() but MySQL is constantly throwing syntax error. Now, I have checked syntax for altering a table from several sources but I believe I do not have any syntax error. When I remove the default value part, the query runs fine but for some reason it cannot add a default value for the date column. I don't know why that is the case.
The code:
mysql> describe test;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| col1 | int | YES | | 0 | |
| col2 | varchar(100) | YES | | hello | |
| col3 | varchar(5) | YES | | T | |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.02 sec)
mysql> ALTER TABLE test ADD COLUMN col4 DATE DEFAULT CURDATE();
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURDATE()' at line 1
mysql>
Edit: My MySQL version: 8.0.31
I think it has to be like this now:
ALTER TABLE test ADD COLUMN col4 DATE DEFAULT (CURRENT_DATE);
Note the parenthesis, or (curdate())

MySQL Unknown column in CHECK

I have a MySQL database And I want to add a column:
MariaDB [(none)]> use myDatabase;
Database changed
MariaDB [myDatabase]>
ALTER TABLE material add new_column FLOAT;
But I get the following error:
ERROR 1054 (42S22): Unknown column '`myDatabase`.`m`.`existing_column`' in 'CHECK'
Sure enough, the existing_column is in the table material:
MariaDB [myDatabase]> describe material;
+------------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| existing_column | tinyint(1) | YES | | NULL | |
+------------------------------+--------------+------+-----+---------+----------------+
42 rows in set (0.003 sec)
(i've left out the other columns for clarity)
And there is a CHECK constraint in place:
MariaDB [myDatabase]> SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS WHERE `TABLE_NAME` = "material";
+--------------------+-------------------+------------+-----------------+-------------------------------------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | TABLE_NAME | CONSTRAINT_NAME | CHECK_CLAUSE |
+--------------------+-------------------+------------+-----------------+-------------------------------------+
| def | myDatabase | material | CONSTRAINT_1 | `existing_column` in (0,1) |
+--------------------+-------------------+------------+-----------------+-------------------------------------+
2 rows in set (0.007 sec)
I've tried:
Making sure all values in existing_column are either 0 or 1 -> No change
Dropping the CHECK -> I just get the same error when I try:
MariaDB [myDatabase]> alter table material drop constraint CONSTRAINT_1;
ERROR 1054 (42S22): Unknown column '`myDatabase`.`m`.`existing_column`' in 'CHECK'
making an sqldump and importing it on another system -> No error and I can add my column!
Context:
I'm using mysql 10.3.29 on Debian 10
I normally use flask-sqlalchemy and flask-migrate for managing migrations. That's where I got the error initially.
I don't really need the CHECK constraint. Sqlalchemy added it automatically
Linking this issue here because it's similar: MariaDB: ALTER TABLE command works on one table, but not the other
I was running MariaDB on Debian: 10.5.10-MariaDB-1:10.5.10+maria~buster
I could apply schema to other databases, but I was getting stuck on one table that kept raising the same error:
ERROR 1054 (42S22): Unknown column '`database`.`table`.`col`' in 'CHECK'
Updating MariaDB to 10.5.15 allowed me to apply the schema. It might have just needed a restart - but impossible to know now.

MySQL INSERT INTO failing at WHERE clause

Ok, I'm stumped on this one:
mysql> INSERT INTO item (col1) VALUES ('testing') WHERE item_name = 'server1';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE item_name = 'server1'' at line 1
Here's the table desc (slightly sanitized):
mysql> desc item;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| item_name | varchar(128) | YES | | | |
| active | int(11) | NO | | 0 | |
| col1 | varchar(512) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
]I've tried some variations on the INSERT, but I've gotten nowhere. Look forward to someone revealing whatever obvious thing I'm missing!
Running: mysql Ver 14.12 Distrib 5.0.95, for redhat-linux-gnu (x86_64) using readline 5.1 (I know it's older, but I can't upgrade this box at the moment).
If you're looking to update an existing row, it's this:
UPDATE item
SET col1 = 'testing'
WHERE item_name = 'server1';
You're not using the INSERTstatement correctly, if you use VALUES clause you can't apply a WHERE condition on it.
Here is the same query with the appropriate syntax:
INSERT INTO item (col1)
SELECT 'testing'
FROM yourTable
WHERE item_name = 'server1';
Hope this will help you.

Error trying to convert tables to InnoDB

I have a Percona 5.1 server in production that uses MyISAM tables in our production database. To support DB transactions I need to update the tables to InnoDB. We're currently using MySQL 5.5 in development and the migration script runs fine with simple ALTER TABLE xyz ENGINE=InnoDB; queries. However in production tests (against a copy of the production database) we're getting an error:
mysql> ALTER TABLE `xyz` ENGINE=InnoDB;
ERROR 1005 (HY000): Can't create table 'InnoTest.#sql-644_dd133' (errno: 1478)
On our development server, using the same database dump as our production tests:
mysql> ALTER TABLE `xyz` ENGINE=InnoDB;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 2
mysql> show warnings;
+---------+------+------------------------------------------------------------+
| Level | Code | Message |
+---------+------+------------------------------------------------------------+
| Warning | 1478 | InnoDB: ROW_FORMAT=DYNAMIC requires innodb_file_per_table. |
| Warning | 1478 | InnoDB: assuming ROW_FORMAT=COMPACT. |
+---------+------+------------------------------------------------------------+
And the stats:
mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+-------------------------------------------+
| Variable_name | Value |
+-------------------------+-------------------------------------------+
| innodb_version | 5.1.73-14.11 |
| protocol_version | 10 |
| version | 5.1.73-rel14.11-log |
| version_comment | Percona Server (GPL), 14.11, Revision 603 |
| version_compile_machine | x86_64 |
| version_compile_os | unknown-linux-gnu |
+-------------------------+-------------------------------------------+
mysql> SHOW VARIABLES LIKE "%version%";
+-------------------------+-------------------------+
| Variable_name | Value |
+-------------------------+-------------------------+
| innodb_version | 5.5.38 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.5.38-0ubuntu0.12.04.1 |
| version_comment | (Ubuntu) |
| version_compile_machine | x86_64 |
| version_compile_os | debian-linux-gnu |
+-------------------------+-------------------------+
Debugging for gloomy.penguin:
mysql> ALTER TABLE `xyz` ENGINE=InnoDB;
ERROR 1005 (HY000): Can't create table 'InnoTest.#sql-644_df08c' (errno: 1478)
mysql> show errors;
+-------+------+------------------------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------------------------+
| Error | 1005 | Can't create table 'InnoTest.#sql-644_df08c' (errno: 1478) |
+-------+------+------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table visit;
| Table | Create Table | xyz | CREATE TABLE `xyz` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`some_field` int(11) DEFAULT NULL,
`some_field` tinyint(2) DEFAULT '0',
`some_field` enum('a','b') DEFAULT 'b',
`some_field` varchar(200) DEFAULT NULL,
`some_field` date DEFAULT NULL,
`some_field` time DEFAULT NULL,
`some_field` datetime DEFAULT NULL,
`some_field` text,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC |
1 row in set (0.02 sec)
mysql> SELECT ##GLOBAL.sql_mode;
+-------------------+
| ##GLOBAL.sql_mode |
+-------------------+
| |
+-------------------+
1 row in set (0.00 sec)
mysql> SELECT ##SESSION.sql_mode;
+--------------------+
| ##SESSION.sql_mode |
+--------------------+
| |
+--------------------+
1 row in set (0.00 sec)
mysql> ALTER TABLE `xyz` ENGINE=InnoDB ROW_FORMAT=COMPRESSED;
ERROR 1005 (HY000): Can't create table 'InnoTest.#sql-644_df08c' (errno: 1478)
mysql> ALTER TABLE `xyz` ENGINE=InnoDB ROW_FORMAT=COMPACT;
Query OK, 0 rows affected (0.13 sec)
Records: 0 Duplicates: 0 Warnings: 0
The development server doesn't have any InnoDB settings in my.cnf (default Ubuntu 12.04 mysql-server install), production has these:
innodb = FORCE
innodb_strict_mode = 1
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_log_file_size = 64M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_buffer_pool_size = 592M
k... so OP isn't responding. that's cool. this is the mysql documentation on that error... http://dev.mysql.com/doc/innodb-plugin/1.0/en/innodb-compression-syntax-warnings.html
run ALTER TABLE xyz ENGINE=InnoDB; in prod again.
then do show errors;.
do a show create table xyz;
see if you're in innodb strict mode... (i would do this in both prod and non-prod to see any difference) SELECT ##GLOBAL.sql_mode; SELECT ##SESSION.sql_mode;
from docs: If you are running in InnoDB strict mode, the combination of a KEY_BLOCK_SIZE with any ROW_FORMAT other than COMPRESSED generates an error, not a warning, and the table is not created.
use that info you got and the info at the link to determine how you should set your key_block_size and row_format to get it to take it.
the non-prod db works because:
Specifying a KEY_BLOCK_SIZE with any other ROW_FORMAT generates a warning that you can view with SHOW WARNINGS. However, the table is non-compressed; the specified KEY_BLOCK_SIZE is ignored). (in non-innodb-strict mode)
if you want more help, post the info you get and i can definitely suggest things... which would probably be alter table xyz engine=innodb ROW_FORMAT=COMPRESSED; and/or making the innodb mode setting equal to what your non-prod db is set with.
key_block_size
more than you ever wanted to know on row formats
for real, though... it sounds like prod is in innodb strict mode and non-prod isn't.
found this:
innodb_strict_mode:
The innodb_strict_mode option controls whether InnoDB operates in strict mode,
where conditions that are normally treated as warnings, cause errors instead
(and the underlying statements fail).
This mode is the default setting in MySQL 5.5.5 and higher.
One of your versions is above 5.5.5 and the other is below. That default might be the discrepancy....

database name in mysql seems valid but it is showing an error

I am trying to create a database with a '-' in between two words and it should work perfectly fine but it isn't. here is the log file:
mysql> create database test1;
Query OK, 1 row affected (0.00 sec)
mysql> show databases
-> ;
+-----------------------+
| Database |
+-----------------------+
| information_schema |
| mifos |
| mifosplatform_tenants |
| mifostenant |
| mifostenant_default |
| mysql |
| performance_schema |
| sakila |
| test |
| test1 |
| testing |
| world |
+-----------------------+
12 rows in set (0.00 sec)
mysql> create database test1-mohit;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near '-mohi
t' at line 1
you can use _(underscore)
create database test1_mohit;
or try this
create database `test1_mohit`;
These both will work
Use one of these character for naming database [0-9,a-z,A-Z$_]
Reference: http://dev.mysql.com/doc/refman/5.0/en/identifiers.html