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.
Related
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())
I've create a table called Students.
It has 4 fields: Name, Age, Class, Years.
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Name | varchar(50) | NO | PRI | NULL | |
| Age | smallint | YES | | 18 | |
| Class | varchar(10) | YES | | NULL | |
| Years | smallint | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
I have added a CHECK CONSTRAINT on Years field that means that the value in Years column can't be lower than 3.
I did it by
mysql> alter table Students
-> add constraint CK_Years check (Years >= 3);
My goal here is to insert a query with a record which has a Years value of 2, which means it won't pass the CK_Years check constraint.
I want to do it without changing the constraint or the fields in the insert statement.
Is it possible?
mysql> insert into Students
-> (Name, Age, Class, Years) values ("Eric","25","110","2");
I have tried:
mysql> alter table Students
-> NOCHECK CONSTRAINT CK_Years;
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 'NOCHECK CONSTRAINT CK_Years' at line 2
I want to disable it just for this insert, and then bring it back (the check constraint).
As you can see, I got this error.
I tried to write it in couple ways. Wit 'CK_Years' or CK_Years or
`CK_Years`
got the same error for all of these ways.
I have verified that I have the constraint in the table:
mysql> select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
-> where table_name='Students';
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| CONSTRAINT_CATALOG | CONSTRAINT_SCHEMA | CONSTRAINT_NAME | TABLE_SCHEMA | TABLE_NAME | CONSTRAINT_TYPE | ENFORCED |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
| def | School | PRIMARY | School | Students | PRIMARY KEY | YES |
| def | School | CK_Years | School | Students | CHECK | YES |
+--------------------+-------------------+-----------------+--------------+------------+-----------------+----------+
So how can I do it without changing the constraint or the fields in the insert statement?
How can I fix the syntax error?
And what is the reason for it?
Thanks.
MySQL does support syntax to alter a check constraint so it is not enforced, but it's different from the Microsoft SQL Server syntax.
mysql> alter table students alter check CK_Years NOT ENFORCED;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into students values ('Eric',25,'110',2);
Query OK, 1 row affected (0.00 sec)
It's documented here: https://dev.mysql.com/doc/refman/8.0/en/alter-table.html
If you re-enable it, the constraint is enforced, and if you had inserted any rows that violate the constraint, you get an error trying to re-enable it.
mysql> alter table students alter check CK_Years ENFORCED;
ERROR 3819 (HY000): Check constraint 'CK_Years' is violated.
Also once you make the constraint not enforced, it would take effect for all clients, not just for one INSERT.
So I don't think you can do what you intend with a CHECK constraint. You may have to write a trigger that enforces a similar condition on that column, but has logic to enforce it conditionally.
Or do what most developers did before MySQL supported CHECK constraints: enforce it in the client app, before doing the INSERT. Then you can apply any conditions you want.
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
After accidentally running the server using Django 1.10 and possibly run a syncdb/migrate I am getting the following message when I try to access the admin
OperationalError at /admin/
(1054, "Unknown column 'django_content_type.name' in 'field list'")
Request Method: GET
Request URL: http://xx.com:8000/admin/
Django Version: 1.7
Exception Type: OperationalError Exception Value:
(1054, "Unknown column 'django_content_type.name' in 'field list'")
Exception Location: build/bdist.linux-x86_64/egg/MySQLdb/connections.py in defaulterrorhandler, line 36
Python Executable: /tools/python/2.7.3/linux64/bin/python
Python Version: 2.7.3
this seems to be the same issue as the one below:
django admin error - Unknown column 'django_content_type.name' in 'field list'
However when I look at the Django_content_type table I do not see a "name" column.
mysql> show columns from django_content_type;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| app_label | varchar(100) | NO | MUL | NULL | |
| model | varchar(100) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
I tried to drop the table but got the following message:
mysql> drop table if exists django_content_type ;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
I am fairly new to Django and MYSQL and I am at a loss here as to what to do next.
Thanks in advance for your help.
Eric.
I am very very confused right now, but somewhere along the lines phpMyAdmin has got confused.
SELECT tracking_active FROM `hutber`.`pma__tracking` WHERE db_name = 'hutber' AND table_name = 'exp_accessories' ORDER BY version DESC
#1146 - Table 'hutber.pma__tracking' doesn't exist
I cannot select db hutber, when I click on any of the tables i got the above error.
[Edit]
I then added the pma__tracking to the table and now I get this error:
SELECT tracking_active FROM `hutber`.`pma__tracking` WHERE db_name = 'hutber' AND table_name = 'exp_accessories' ORDER BY version DESC
#1054 - Unknown column 'db_name' in 'where clause'
How can I fix my DB?
mysql> SHOW TABLES; DESC pma__tracking;
+-------------------------------+
| Tables_in_hutber |
+-------------------------------+
| exp_accessories |
| exp_actions |
| exp_ajw_datagrab |
| exp_assets_files |
| pma__tracking |
+-------------------------------+
147 rows in set (0.00 sec)
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| idpma__tracking | int(11) | NO | PRI | NULL | |
| tracking_active | varchar(45) | YES | | NULL | |
| pma__trackingcol | varchar(45) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
Note: I created pma__tracking... Just so you know why the structure is like that.
1. #1146 - Table 'hutber.pma__tracking' doesn't exist
Since it is listed in SHOW TABLES; this could be a mysql permission issue (chmod, chown) in /var/lib/mysql. Ensure pma_tracking has the right permissions
chown mysql:mysql /path/to/mysql
sudo chmod -R 755 /var/lib/mysql/
2. #1054 - Unknown column 'db_name' in 'where clause'
DESC pma__tracking; clearly shows that there is no column as 'db_name'
, 'version', 'table_name' as mentioned select query