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);
Related
I'm trying to create a table with this sql:
CREATE TABLE angestellte (
PersonalNr int(11) NOT NULL AUTO_INCREMENT,
Vorname varchar(50) NOT NULL,
Nachname varchar(50) NOT NULL,
Beruf varchar(50) NOT NULL,
Gehalt int(11) NOT NULL,
arbeitetInAbteilung int(11) NOT NULL,
PRIMARY KEY (PersonalNr),
FOREIGN KEY abteilung (AbteilungID)
)
ENGINE=InnoDB;
But I get only the message
#1064 - 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 ')
ENGINE=InnoDB' at line 10
I looked really can't find my mistake but think it's probably something obvious I just don't see.
Supposing you have a table where a field (arbeitetInAbteilung) references a row in another table (say arbeiten), you need to define it like this:
CREATE TABLE `angestellte` (
`PersonalNR` INT(11) NOT NULL AUTO_INCREMENT,
...other fields...
`arbeitetInAbteilung` INT(11) NULL DEFAULT NULL,
PRIMARY KEY (`PersonalNR`),
INDEX `FK__arbeiten` (`arbeitetInAbteilung`),
CONSTRAINT `FK__arbeiten` FOREIGN KEY (`arbeitetInAbteilung`)
REFERENCES `arbeiten` (`arbeitID`)
ON UPDATE CASCADE
ON DELETE CASCADE
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
This means that an index will be created to index the field you specify (arbeitetInAbteilung). Then a constraint will be set in place so that this index is linked to the values of another field in a different table, which could be defined like this:
CREATE TABLE `arbeiten` (
`arbeitID` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`arbeitID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
Note that the two fields must be absolutely identical; if they are text fields, they need to have the same collation; they must be both either NULL or NOT NULL; et cetera. The slightest difference will yield a "Foreign key constraint is incorrectly formed" error, and you'll need to show the engine status for InnoDB and parse its output to understand why.
The ON UPDATE and ON DELETE define what happens when you change a value in the master (arbeiten) table, or you delete it. If the ID 123 becomes 456, CASCADE means that all the rows that referred 123 will now refer 456. Another possibility would be to prevent the operation (RESTRICT), or set the mismatched rows to NULL (SET NULL).
As already commented, your FK syntax is total wrong.
FOREIGN KEY abteilung (AbteilungID)
should be
FOREIGN KEY some_column REFERENCES abteilung (AbteilungID)
Here, some_column should be replaced by the column which you want to designate as the referencing key and the definition of this column should exactly match with column AbteilungID of table abteilung
I get the following error:
ERROR 1215 (HY000) at line 13: Cannot add foreign key constraint
for the following query:
ALTER TABLE client_generique
ADD CONSTRAINT client_generique_boutique_id_boutique_id
FOREIGN KEY (boutique_id) REFERENCES boutique (id)
ON DELETE SET NULL;
The boutique.id is a primary key unique not null.
The phpmyadmin structure export of the boutique table is this:
--
-- Table structure for table `boutique`
--
CREATE TABLE `boutique` (
`id` bigint(20) NOT NULL,
`nom` varchar(255) NOT NULL,
`identifiant_site` varchar(8) NOT NULL COMMENT 'the eight number identifier from the bank',
`certificate` varchar(255) NOT NULL COMMENT 'changes according to the mode, this will be used as salt in the sha1 that will be sent to the bank as the ''signature''',
`mode` varchar(10) NOT NULL COMMENT 'is the ''vads_ctx_mode'': TEST or PRODUCTION',
`payment_system` varchar(10) NOT NULL COMMENT 'CYBERPLUS OR PAYZEN for the new payment system added in end 2014'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Indexes for table `boutique`
--
ALTER TABLE `boutique`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `unique_mode_per_identifiant_idx` (`identifiant_site`,`mode`);
I do not understand. What is the reason? How to solve this problem?
Could it be that your table client_generique stores values in boutique_id which do not exist in your table boutique?
Since you try to establish a reference to boutique the values must exist in that table.
Thanks for Philipp Dietl for the solution.
It's not possible to add a foreign key constraint with a set null on delete on a not null column.
I've just modified the boutique_id column to a BIGINT (but no BIGINT NOT NULL).
I need to change a bunch of PK column types, and use MySQL's Workbench to design my database.
Is it possible to change the PK's type in a given table, and have all the FK types automatically change?
I performed to check the workbench. I created the table employees and employees_hobbies. The PK of tableemployees is id_employees the type INT(11). The employees_hobbies table has a FK id_employees the type INT(11). I tried to change the type of the field in id_employees of table employees for CHAR(5) and the error is as follows:
ERROR 1025: Error on rename of './teste/#sql-4aa_10875' to
'./teste/employees' (errno: 150) SQL Statement: ALTER TABLE
teste.employees CHANGE COLUMN id_employees id_employees
CHAR(5) NOT NULL
ERROR: Error when running failback script.
The error means (errno: 150)
MySQL error code 150: Foreign key constraint is incorrectly formed
My tables:
CREATE TABLE `employees` (
`id_employees` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) DEFAULT NULL,
`url_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id_employees`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `employees_hobbies` (
`id_employees_hobbies` int(11) NOT NULL AUTO_INCREMENT,
`id_employees` int(11) NOT NULL,
`id_hobbies` int(11) NOT NULL,
PRIMARY KEY (`id_employees_hobbies`),
KEY `fk_employees_hobbies_1_idx` (`id_employees`),
KEY `fk_employees_hobbies_2_idx` (`id_hobbies`),
CONSTRAINT `fk_employees_hobbies_1` FOREIGN KEY (`id_employees`) REFERENCES `employees` (`id_employees`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_employees_hobbies_2` FOREIGN KEY (`id_hobbies`) REFERENCES `hobbies` (`id_hobbies`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
You can not do this in Workbench.
The solution is to generate the script and parse all FK performing the exchange type.
Actually I was used this sql script to create the table scheme on mysql 5.0.24, and it was just working fine, the problem is when i tried same script on mysql 5.5.16 i get below error message:
general error message from server: "can't create table 'amdb.am_wta_methodtree' (errno: 150)"
and here it is my sql:
create table AM_WTA_MethodInstance(
"ID" BIGINT NOT NULL,
"TRANSACTIONID" BIGINT NOT NULL,
"INVOCATIONTIME" BIGINT NOT NULL,
"METHODIDENTIFIERID" BIGINT NOT NULL,
"THREADID" VARCHAR(255) NOT NULL,
"INCLUSIVETIME" BIGINT NOT NULL DEFAULT 0,
"EXCLUSIVETIME" BIGINT NOT NULL DEFAULT 0,
"STATUS" INTEGER(1) NOT NULL DEFAULT 0,
"EXCEPTIONMESSAGE" TEXT,
PRIMARY KEY ("ID"),
FOREIGN KEY(METHODIDENTIFIERID) REFERENCES AM_WTA_MethodIdentifier(ID),
FOREIGN KEY(TRANSACTIONID) REFERENCES AM_WTA_Transaction(TRANSACTIONID) ON DELETE CASCADE)
create table AM_WTA_MethodTree(
"PARENTID" BIGINT NOT NULL,
"CHILDID" BIGINT NOT NULL,
"INVOCATIONTIME" BIGINT NOT NULL,
PRIMARY KEY ("PARENTID","CHILDID"),
FOREIGN KEY(PARENTID,CHILDID) REFERENCES AM_WTA_MethodInstance(ID,ID) ON DELETE CASCADE)
If you create the tables commit and after create the foreign key it should work.
Thanks
The method instance table should be created first before before it can be referenced. I suggest that you create the Tables first.
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;