MySQL throwing syntax error while trying to alter a table - mysql

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())

Related

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.

I cannot change databases column

I cannot change databases column
My Env
MacOS Mojave, MySQL Server version: 10.1.39-MariaDB Source distribution
why
Making a CRUD app, but I want to change table column,
from text to desc, so I searched and used alter
command, but right SQL command returns error messages.
My table
MariaDB [cake_cms]> describe interns;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | | NULL | |
| name | varchar(64) | NO | | NULL | |
| text | varchar(255) | NO | | NULL | |
| location | varchar(64) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
MariaDB [cake_cms]> Alter Table interns Rename Column text to desc;
ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version
for the right syntax to use near 'Column text to desc' at line 1
Refered
https://www.dbonline.jp/mysql/table/index18.html
says to use
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;
Rename a column in MySQL
This site says:
ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);
So I write
alter table interns rename column "name" to "newname" varchar(255);
But returned syntax error message....
I do not know what to do. Please help me!
desc is a sql command so you can't name your table like this

Update only the time stamp in a MySql table

Given this table
mysql> describe last_user_activity;
+--------------+------------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------+------+-----+-------------------+-------+
| customer_id | int(11) | NO | | NULL | |
| token | text | NO | | NULL | |
| time_stamp | timestamp | YES | | CURRENT_TIMESTAMP | |
| is_logged_in | tinyint(1) | NO | | 0 | |
+--------------+------------+------+-----+-------------------+-------+
4 rows in set (0.00 sec)
I want to "touch" a row of the table, setting the time_stamp to its default, CURRENT_TIMESTAMP.
I thought that I could try
UPDATE last_user_activity WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
(that is a valid token in the table), but that resulted in
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
token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"' at line 1
Oh, well, it seemed clever, but NVM. So, then I tried
UPDATE last_user_activity SET token="40aed4d9-c9ac-471e-8d53-b2baa0d72523"
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
which said that it succeeded, BUT, the timestamp field was not updated.
What am I doing wrongly?
UPDATE last_user_activity
SET time_stamp = NULL
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
OR
UPDATE last_user_activity
SET time_stamp = NOW()
WHERE token="40aed4d9-c9ac-471e-8d53-b2baa0d72523";
It looks as if you have given default value of column time_stamp, but not on update.
You may considering this change :
ALTER TABLE last_user_activity
MODIFY COLUMN time_stamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;
I hope this works.After that you could try using your update commands.

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.

Compilation Error in MYSQL trigger?

Table structure is:
mysql> DESC groups;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| PKey | varchar(64) | NO | PRI | NULL | |
| group_name | varchar(64) | YES | | NULL | |
| Region | varchar(128) | NO | | NULL | |
| Role | varchar(128) | NO | | NULL | |
| parent_group | varchar(64) | NO | MUL | NULL | |
+--------------+--------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
When i am executing this Trigger , i'm having a compilation error
DELIMITER $$
CREATE
TRIGGER `group_before_delete` BEFORE DELETE
ON `groups`
FOR EACH ROW BEGIN
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!`;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.Pkey;
END IF;
END$$
DELIMITER ;
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 ';
ELSE
t_group=old.parent_group
t_group=old.PKey;
END IF;' at line 6
mysql> DELIMITER ;
Can you tell me what i'm missing here ??
You get an error in your code because the syntax of your UPDATE statement is not valid.
The links you give are 4 and 5 years old!
Since the SIGNAL statement is available in MySQL since version 5.5.0 (released 3 years ago), this is really not a good idea to use the hacks described in these 2 webpages. Instead, use the SIGNAL statement.
Note: From the comments we learn that the OP is not using MySQL 5.5, so SIGNAL is not available.
In your IF statement, the following is not a valid SQL statement:
UPDATE `Error: deletion RootGroup is prohibited!`;
This should be this:
IF old.parent_group=old.PKey THEN
UPDATE `Error: deletion RootGroup is prohibited!` set x=1;
ELSE
UPDATE groups
SET parent_group=old.parent_group
WHERE parent_group=old.PKey;
END IF;
I have never done things this way. It is a bit of an ugly way of doing it. But if it works, what the heck.