I got the following table:
CREATE TABLE `unsub_counts` (
`count_id` int(11) NOT NULL AUTO_INCREMENT,
`unsub_date` date DEFAULT NULL,
`unsub_count` int(11) DEFAULT NULL,
`store_id` smallint(5) DEFAULT NULL,
PRIMARY KEY (`count_id`),
UNIQUE KEY `uc_unsub_date` (`unsub_date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
where column unsub_date is unique. Now I want to drop that uniqueness, because I need to have a unique index on unsub_date + store_id.
I found suggestions on the net, but is failing:
ALTER TABLE `unsub_counts` DROP CONSTRAINT `unsub_date`
gives me: 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 'CONSTRAINT unsub_date' at line 1
Is this related to MyISAM? Any other suggestions?
Use drop index with constraint name:
ALTER TABLE unsub_counts DROP INDEX uc_unsub_date;
or just:
drop index uc_unsub_date on unsub_counts;
Related
I have this scenario: I have those two tables:
CREATE TABLE sample_A (
ID bigint(20) UNSIGNED NOT NULL,
product varchar(60) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE sample_B (
ID bigint(20) UNSIGNED NOT NULL,
ref_id_sample_A_id bigint(20) UNSIGNED NULL,
ref_id_sample_A_ref_id bigint(20) UNSIGNED NULL,
document_name varchar(300) COLLATE utf8mb4_unicode_ci DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
and I was trying to restore a foreign key between ref_id_sample_A_id of table sample_B and ID of table sample_A but executing this instruction:
ALTER TABLE sample_B
ADD CONSTRAINT fk_sample_B_ref_sample_A_id
FOREIGN KEY (ref_id_sample_A_id)
REFERENCES sample_A(ID);
I obtain this error:
#1823 - Failed to add the foreign key constraint 'k3/fk_sample_B_ref_sample_A_id' to system tables
but I have no other foreign keys settled, neither informations if I query this:
SELECT * FROM information_schema.TABLE_CONSTRAINTS
WHERE CONSTRAINT_TYPE = 'FOREIGN KEY'
AND TABLE_NAME = 'sample_B';
I get empty result, and table simple_A is not a system table.... what I should do? Thanks in advance to all!
Cheers
I tested your sample using MySQL 8.0.26, and got this error:
ERROR 1822 (HY000): Failed to add the foreign key constraint. Missing index for constraint 'fk_sample_B_ref_sample_A_id' in the referenced table 'sample_A'
This means the referenced column, sample_A.ID, is not valid as the referenced column of a foreign key, because it is not part of a key in that table.
The referenced column must be part of a key, and ideally should be a PRIMARY KEY or UNIQUE KEY.
You did not define ID or any other column as the PRIMARY KEY for the table sample_A.
Solved right 2 sec ago anyway ... had to export dump data of the table, drop the table and recreate the table with all constraints in CREATE TABLE instruction then re-import all dump data and it worked. Thanks anyway to everyone #nbk and #Bill Karwin! :-)
I try to add a generated stored column to existing table running the following SQL command:
ALTER TABLE routes ADD routes_hash char(32)
AS (MD5(CONCAT(`traveler_id`, `track`))) stored;
MySQL returns the error message:
ERROR 1025 (HY000): Error on rename of './travelers/#sql-558_69' to './travelers/routes'
(errno: 150 - Foreign key constraint is incorrectly formed)
Mysql version is 5.7.20. Here is a database structure:
CREATE TABLE `travelers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(128) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `routes` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`traveler_id` int(11) NOT NULL,
`track` multilinestring NOT NULL,
PRIMARY KEY (`id`),
KEY `traveler_id` (`traveler_id`),
CONSTRAINT `routes_ibfk_1` FOREIGN KEY (`traveler_id`)
REFERENCES `travelers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
I have not found any restrictions in MySQL documentation that forbid usage of the column that participates in foreign key constraint as a part of expression used for build a generated column. However, if I exclude traveler_id from the generated column expression for routes_hash the column is created fine. Is this a bug of MySQL or I am missing something about generated column usage?
Unfortunately, it is a bug of MySQL that affects even current 5.7 and 8.0 versions. I have found description on MySQL's bug tracker https://bugs.mysql.com/bug.php?id=88111 . Please be aware.
I am trying to set a DEFAULT value and NOT NULL for type_id column which is a foreign key to instance table but it is complaining about MySQL syntax.
CREATE TABLE `type` (
`id` int(10) unsigned NOT NULL PRIMARY KEY AUTO_INCREMENT,
`city` varchar(256)
);
ALTER TABLE instance ADD COLUMN type_id int(10) unsigned NOT NULL;
ALTER TABLE instance ADD CONSTRAINT instance_xbf1 DEFAULT 1
FOREIGN KEY (type_id)
REFERENCES type(id) int(10) unsigned NOT NULL;
It is complaining about the syntax of the last statement:
Error: \"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 1\nFOREIGN KEY (type_id)\nREFERENCES type(id) int(10) unsi' at line 1\")", "stdout": "", "stdout_lines": [], "warnings": []}
You're trying to do too many things in your ADD CONSTRAINT statement for the foreign key.
You can add the default value in two ways. Either as part of the column definition:
ALTER TABLE instance ADD COLUMN type_id int(10) unsigned NOT NULL DEFAULT 1;
Or later in it's own statement:
ALTER TABLE instance ALTER type_id SET DEFAULT 1;
In the foreign key you cannot define the properties of the column such as data type, nullability, default values etc...
The error message is telling you it's not expecting the DEFAULT keyword as part of an ADD CONSTRAINT. If you remove that, it will then complain about the data type & nullability part: int(10) unsigned NOT NULL because it's not expecting those either. But you have already defined them anyway.
So, your ADD CONSTRAINT can be simplified to:
ALTER TABLE instance ADD CONSTRAINT instance_xbf1
FOREIGN KEY (type_id) REFERENCES type(id);
I have tried the following query (it was not written by myself, but was included in a source code from the Internet)
CREATE TABLE `city_list_count` (
`city_created` date NOT NULL,
`count_created` int(8) unsigned NOT NULL,
PRIMARY KEY `city_created`
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
It gave me the following error:
1064 - 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 ') ENGINE=InnoDB DEFAULT CHARSET=utf8' at line 5
According to MySQL CREATE TABLE syntax, you need parentheses before and after a column list for a constraint:
[CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
Trying different things I've pointed out that the reason of the error is that primary key field name should be wrapped in parentheses:
CREATE TABLE `city_list_count` (
`city_created` date NOT NULL,
`count_created` int(8) unsigned NOT NULL,
PRIMARY KEY (`city_created`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I couldn't find the explanation for this anywhere. However, it worked for me.
Just wanted to share this with others if someone came across the same trouble.
I am trying to create a self-referential FK:
DROP TABLE IF EXISTS `Company`;
CREATE TABLE `Company` (
`company_id` INTEGER(32) UNSIGNED AUTO_INCREMENT,
`parent_company_id` INTEGER(32),
PRIMARY KEY (`company_id`)
) ENGINE=InnoDB;
ALTER TABLE `Company`
ADD FOREIGN KEY `parent_company_id` REFERENCES `Company`(`company_id`);
I am getting the following error:
1064 - 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 'REFERENCES Company(company_id)' at line 1
Got it.
You gotta change the definition of
parent_company_id INTEGER(32)
To match company_id except for the autoincrement and then use this statement
ALTER TABLE `Company`
ADD CONSTRAINT fk_parent_company_id FOREIGN KEY (`parent_company_id`) REFERENCES `Company`(`company_id`)
So basically remember to put unsigned on the column you are using as FK so it matches the definition of the referenced key