I'm having a problem with an SQL statement.
I want to activate a "ON UPDATE CASCADE" behavior for a foreign key in a table with this statement :
ALTER TABLE "DB"."RECORD" ADD CONSTRAINT "RECORD_PT_OUTIL_FK1" FOREIGN KEY ("CDE_PO")
REFERENCES "DB"."PT_OUTIL" ("CDE_PO") ON UPDATE CASCADE ENABLE;
But when i run the statement in Oracle Developer, i just get this error message : "ORA-00905 : missing keyword"
I can't find what could be this missing keyword, i tried several changes but always the same error occurs.
I reuse a code generated by Oracle Developer it self and just modify it with what i want.
This is the generated code :
ALTER TABLE "DB"."RECORD" ADD CONSTRAINT "RECORD_PT_OUTIL_FK1" FOREIGN KEY ("CDE_PO")
REFERENCES "DB"."PT_OUTIL" ("CDE_PO") ON DELETE CASCADE DISABLE;
See, i just change the end of it.
So what's the matter here ? Am i missing something ? (please don't bash if it's something obvious :) )
Thx !
Oracle does not support the ON UPDATE clause for foreign keys.
See the description of the REFERENCES clause in the manual:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/clauses002.htm#CJAIHHGC
Try by removing the DISABLE/ ENABLE at the end of your command
As per the ADD CONSTRAINT reference, there doesnt seem to be any "Enable / Disable" as part of the command.
I think it is something that your Oracle Developer is adding at the end (it being part of Oracle Syntax) and it might be causing the problem!!
Related
I have a Model class that has an attribute referencing another instance of the same Model class. Its basically a tree structure in one Model.
When I try to exectute MyModel.deleteAll() it fails because a foreign key constraint fails.
Is there someway to easily suspend this constraint for the deleteAll query?
The only workaround I've found, since I'm using mysql, is to issue a TRUNCATE statement, which mysql accepts straight away.
Thanks in advance,
Evan
Exception details:
org.javalite.activejdbc.DBException: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (visibledb_testing.accountabilities, CONSTRAINT accountabilities_prototype_id FOREIGN KEY (prototype_id) REFERENCES accountabilities (id)), query: DELETE FROM accountabilities
The Model#deleteAll() simply generates SQL DELETE FROM YOURTABLE.
If you can run this on MySQL console, it will work from the model. If you are getting constraint violation, maybe you want to:
Base.exec("TRUNCATE " + MyModel.getTableName());
Alternatively, you can try http://javalite.io/delete_cascade#method-deletecascade. BE CAREFUL- this is a powerful but dangerous method, only use after reading all docs.
I have two tables tblA and tblB. And a constraint called tblA_tblB_FK is created between these tables. I wanted to update both columns in tables chained with tblA_tblB_FK constraint. While reading different posts I thought the best way is to disable the constraint for a moment and enable again after the update. For that reason I executed these queries:
alter table tblA NOCHECK CONSTRAINT tblA_tblB_FK
After this step I did the update and till now everything was OK, but then I tried to enable again the constraint, so I executed this query:
ALTER TABLE tblA CHECK CONSTRAINT tblA_tblB_FK
and it says command successfully completed. But when I try to make update again it doesn't stop me from doing that, meaning there is a problem with the enabling process. I tried to execute another query:
ALTER TABLE tblA WITH CHECK CHECK CONSTRAINT tblA_tblB_FK
and it doesn't allow me complaining there is tblA_tblB_Fk constraint active. I don't understand why it allows me to make an update, while it doesn't allow me to execute this command?
I am using SQL Server 2005. Thanks in advance for any suggestions!
Check you insert and update specification for the foreign key in management studio under Table>Table_name>Keys folder. It might be set to "Cascade".
H2 does not appear to support the index/FK syntax generated by hibernate for the MySQL5 dialects when using ;MODE=MYSQL and hibernate with a dialect of org.hibernate.dialect.MySQL5Dialect.
My goal here is to have one set of SQL scripts and use hibernate for the ORM parts. Everything works fine in MySQL 5.5 but when I try to use H2 for things like unit tests and starting up a demo version of my app I get hundreds of failures from hibernate generated alter table statements as shown below. Unfortunately, I have not been able to find a way to get hibernate to change the way the statements are generated but that might be an option as well. I tried using org.hibernate.dialect.H2Dialect but that produces more severe errors so I don't think that will work.
alter table SAM_PUBLISHEDSECUREDIP_T
add index FK1EDEA25B9482C945 (ASSESSMENTID),
add constraint FK1EDEA25B9482C945 foreign key (ASSESSMENTID)
references SAM_PUBLISHEDASSESSMENT_T (ID)
Results in an error like this in H2:
org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "ALTER
TABLE SAM_PUBLISHEDSECUREDIP_T ADD INDEX FK1EDEA25B9482C945
(ASSESSMENTID),[*] ADD CONSTRAINT FK1EDEA25B9482C945 FOREIGN KEY
(ASSESSMENTID) REFERENCES SAM_PUBLISHEDASSESSMENT_T (ID) "; SQL
statement: alter table SAM_PUBLISHEDSECUREDIP_T add index
FK1EDEA25B9482C945 (ASSESSMENTID), add constraint FK1EDEA25B9482C945
foreign key (ASSESSMENTID) references SAM_PUBLISHEDASSESSMENT_T (ID)
[42000-172]
NOTE: I am open to writing and providing a patch for H2 but I could use some tips on where to look in that codebase.
H2 is not 100% compatible with MySQL, even when using the MySQL mode. It seems some of your SQL statements are not supported by H2.
Creating an index is not done using alter table (does MySQL really use this strange syntax? Every DBMS I know uses CREATE INDEX to create an index).
You have to split this up in two statements:
CREATE INDEX fk_assessment_id_index
on SAM_PUBLISHEDSECUREDIP_T (ASSESSMENTID);
alter table SAM_PUBLISHEDSECUREDIP_T
add constraint FK1EDEA25B9482C945 foreign key (ASSESSMENTID)
references SAM_PUBLISHEDASSESSMENT_T (ID);
Those two statements should also work in MySQL if I'm not mistaken.
When I right click on a default constraint and I ask SQL Server to create a CREATE script for it, it generates the following code:
ALTER TABLE [dbo].[tblEventTurnJudgeStartValues] WITH NOCHECK ADD CONSTRAINT [tblEventTurnJudgeStartValues_ExecutionToggle] CHECK (([ExecutionToggle]=(1) OR [ExecutionToggle]=(0) OR [ExecutionToggle]=(-1)))
GO
ALTER TABLE [dbo].[tblEventTurnJudgeStartValues] CHECK CONSTRAINT [tblEventTurnJudgeStartValues_ExecutionToggle]
For the record, I understand the first ALTER statement but I do not understand what the the second alter statement does. Tried to google the "CHECK CONSTRAINT" phrase but only got hits on the add constraint syntax.
Thanks.
Seth
update
Thanks Joe for your answer. Found this link which helps.
http://blog.sqlauthority.com/2009/11/12/sql-server-disable-check-constraint-enable-check-constraint/
I did not know that you could enable and disable constraints. Cool!
Seth
The first statement creates the constraint, but since it is created with NOCHECK, existing data is not validated at the time of creation.
The second statement simply turns the constraint on and is technically redundant.
Personally, I'd prefer the second statement be written with the WITH CHECK option, which will validate all existing data against the constraint and will prevent the constraint from becoming untrusted.
ALTER TABLE [dbo].[tblEventTurnJudgeStartValues] WITH CHECK CHECK CONSTRAINT [tblEventTurnJudgeStartValues_ExecutionToggle]
I need to update an existing constraint in Oracle database to add a new column there.
ALTER TABLE MY_PARTNER_DETAILS
MODIFY CONSTRAINT UQ_MY_PARTNER_DETAILS
UNIQUE(PARTNER_CODE,PGOOD_CODE,SITE_CODE,PARTNER_PLACEMENT,PARTNER_PARTICIPATION)
Gives the error:
Error at line 1
ORA-00933: SQL command not properly ended
What's the problem with that?
You should drop and recreate the constraint. modify constraint allows you to change constraint's state not definition.
See: Oracle Docs