I want to modify the update option of one foreign key.
For this I executed this command:
alter table testusers.ORDERS
DROP CONSTRAINT ORDER_FK_2,
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY(FK_PRODUCER_ID) REFERENCES testuser.PRODUCER (producer_id)
ON UPDATE CASCADE ON DELETE CASCADE;
If I execute this, there is the following error:
SQL-Fehler: ORA-01735: Ungültige Option ALTER TABLE
01735. 00000 - "invalid ALTER TABLE option"
There is no comma separated list for the alter table according to documentation syntax diagram http://docs.oracle.com/cd/B28359_01/server.111/b28286/clauses002.htm#CJAEDFIB
create table orders(order_id number, fk_producer_id number, CONSTRAINT order_pk PRIMARY KEY (order_id));
create table producer(producer_id number, CONSTRAINT producer_pk PRIMARY KEY (producer_id));
alter table orders
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY( FK_PRODUCER_ID)
REFERENCES PRODUCER (producer_id) ;
alter table orders
DROP CONSTRAINT ORDER_FK_2;
alter table orders
ADD CONSTRAINT ORDER_FK_2 FOREIGN KEY( FK_PRODUCER_ID)
REFERENCES PRODUCER (producer_id) ;
Ahm, yes, and I could not find any ON UPDATE CASCADE syntax either. But I am sure you can work it out now. Otherwise drop a little comment or post a new question.
Related
I keep getting an error "Incorrect index name 'f7'" using MySQL and I've narrowed it down to the following:
First I create the table,
CREATE TABLE testTable (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
f7 INTEGER NOT NULL,
FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
And then elsewhere,
ALTER TABLE testTable ADD UNIQUE f7;
This has led me to believe that this has to do with a duplicate index (?) I just can't figure out how to fix it. Many thanks.
Give it a name, so it doesn't conflict with the foreign Key index
ALTER TABLE `testtable` ADD UNIQUE INDEX `foo` (`f7`);
An incorrect index name error is given when you're attempting to create a new index with the same name as an existing index.
In MySQL, when you create a foreign key, as you're doing with FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE, an index is auto-created as well. In this case, the name is defaulted to f7.
The foreign key is created as a non-unique index; your second command: ALTER TABLE testTable ADD UNIQUE (f7); will make this index unique - not add a second one.
To verify what indexes already exist on the table, you can use the following:
SHOW INDEXES FROM testTable;
If you're receiving this error, there is likely additional code elsewhere that is attempting to create an index named f7. You can attempt to find it, or change your CREATE TABLE syntax to name the key something different so that it doesn't cause conflicts:
FOREIGN KEY fk_testTable_f7 (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE
In this example, I used fk_testTable_f7 and you should now have a non-unique index on the table named fk_testTable_f7. To make it unique, you can use your existing ALTER command as you want the column to be unique - not the foreign key itself.
The SQLite doc says:
It is not possible to use the "ALTER TABLE ... ADD COLUMN" syntax to
add a column that includes a REFERENCES clause, unless the default
value of the new column is NULL.
I can't get my ALTER TABLE statement to work, it looks like this:
ALTER TABLE Customers ADD COLUMN AddressID INTEGER DEFAULT NULL FOREIGN KEY REFERENCES MasterAddress (AddressID);
I can see references to workarounds that create a constraint at CREATE TABLE time and bouncing the data through a new table but I would prefer altering my existing table. Is this possible?
I have set PRAGMA foreign_keys = ON.
DEFAULT NULL is the default; you do not need to specifiy it.
FOREIGN KEY is used to introduce a table constraint, but ALTER TABLE supports only column constraints. The syntax for a column constraint does not have the FOREIGN KEY:
ALTER TABLE Customers ADD AddressID INTEGER REFERENCES MasterAddress(AddressID);
what'S wrong with the following statement?
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
The error message I am getting is
Can't create table `fcdemo`.`#sql-664_b` (errno: 150 "Foreign key constraint is incorrectly formed")
This works for me on MariaDB 10.1.8:
CREATE TABLE `submittedforecast` (
`id` INT(11) NOT NULL,
`data` INT(11) NOT NULL,
PRIMARY KEY (`id`),
INDEX `data` (`data`)
) ENGINE=InnoDB;
CREATE TABLE `blobs` (
`id` INT(11) NOT NULL,
`content` BLOB NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
ALTER TABLE submittedForecast
ADD CONSTRAINT FOREIGN KEY (data) REFERENCES blobs (id);
Can you give your MariaDB version number and a complete example including the CREATE TABLE statements for submittedForecast and blobs?
No idea if you already solved this but make sure both engines and collations match between tables (e.g: latin1 to latin1 and InnoDB to InnoDB).
I was having the same issue and by switching these I managed to get it working.
I was getting the same issue and after looking at my table structure, found out that my child table's foreign key clause was not referencing the primary key of parent table. Once i changed it to reference to primary key of parent table, the error was gone.
I had the same error and is actually pretty easy to solve, you have name the constraint, something like this should do:
ALTER TABLE submittedForecast ADD CONSTRAINT `fk_submittedForecast`
FOREIGN KEY (data) REFERENCES blobs (id)
If you would like more cohesion also add at the end of the query
ON DELETE CASCADE ON UPDATE RESTRICT
This could also be a different fields error so check if the table key and the foreign key are of the same type and have the same atributes.
I had the same problem, too. When checking the definition of the fields, I noticed that one field was defined as INT and the other as BIGINT. After changing the BIGINT type to INT, I was able to create my foreign key.
Recently I started using SQLite (as required for my study) and I came accross a couple of restrictions of SQLite and I was wondering: can't SQLite create foreign keys on the same table? E.g. this is my code:
CREATE TABLE Categories
(
name varchar(20),
parent_category varchar(20) NULL,
PRIMARY KEY(name),
FOREIGN KEY parent_category_fk(parent_category) REFERENCES Categories(name)
)
But it gives me an error for the foreign key when I try to execute the SQL in SQLiteStudio.
Does anyone know why this isn't working?
The problem is that you have the wrong syntax for the FK clause. It should be:
FOREIGN KEY (parent_category) REFERENCES Categories(name)
If you want to name the FK constraint, you do that with a prefix of the CONSTRAINT keyword, like this:
CONSTRAINT parent_category_fk FOREIGN KEY (parent_category) REFERENCES Categories(name)
I am trying to change the type in two tables of an innoDB. The problem is that the values are a key and a foreign key. When I try to make the change I get the following error
#1025 error on rename
Do I need to drop the foreign keys and then make the changes and then reapply the foreign key?
Since u can use the name to drop the foreign key first and the column then:
ALTER TABLE categories DROP FOREIGN KEY categories_ibfk_1;
ALTER TABLE categories DROP COLUMN assets_id;
To find out which table caused the error you can run
SHOW ENGINE INNODB STATUS\G
and then look at the "LATEST FOREIGN KEY ERROR" section.
Yeah, you have to drop the foreign key. Try SHOW INNODB STATUS to see if there's a more elaborated explanation of what's going on.