sql oracle, ALTER TABLE error - sql

Here is ERD model of my DB:
Here is create table codes i used so far:
pastebin
I have created all tables and now I'm trying to alter them to create foreign keys, but this alter doesn't work.
Alter table uskladnena_surovina add foreign key (datum_spotreby,id_dodavatela,id_suroviny) references ponukana_surovina (datum_spotreby,id_dodavatela,id_suroviny)
/
It says:
"DATUM_SPOTREBY":
00904. 00000 - "%s: invalid identifier"

You need to tell Oracle that you want to create a constraint. To do this, you need to specify add constraint after alter table .... If you don't tell Oracle what you want to add to the table, it assumes you want to add a new column.
Try
Alter table uskladnena_surovina add constraint constraint_name foreign key (datum_spotreby,id_dodavatela,id_suroviny) references ponukana_surovina (datum_spotreby,id_dodavatela,id_suroviny)
/
You also have to give the constraint a name. Feel free to change constraint_name.

Oh sorry guys,
i forgot to add datum_Spotreby into uskladnena_surovina so obviously it couldnt work, now it works

Related

error when adding new entry to existing primary key sybase

I have encountered a problem in sybase.
By searching in stackoverflow, I found this link.
But I found that incorrect in sybase, which gave me a syntax error warning.
Are there any other ways that can change the primary key in sybase?
We can use sp_dropkey to drop the key and sp_primarykey to add the primary key.
sp_dropkey primary, MyTable
sp_primarykey MyTable, col1, col2,..,col8
1- Use :
sp_helpconstraint S87EntityFolderMap
To get your Primary Key system Generated name
2- Then use alter table to drop it :
alter table S87EntityFolderMap drop constraint name_from_first_step

Sql Server 2008 Create Foreign Key Manually

I have inherited an old database which wasn't designed very well. It is a Sql Server 2008 database which is missing quite a lot of Foreign Key relationships. Below shows two of the tables, and I am trying to manually create a FK relationship between dbo.app_status.status_id and dbo.app_additional_info.application_id
I am using SQL Server Management Studio when trying to create the relationship using the query below
USE myDatabase;
GO ALTER TABLE dbo.app_additional_info
ADD CONSTRAINT FK_AddInfo_AppStatus FOREIGN KEY (application_id)
REFERENCES dbo.app_status (status_id)
ON DELETE CASCADE
ON UPDATE CASCADE ;
GO
However, I receive this error when I run the query
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"FK_AddInfo_AppStatus". The conflict occurred in database
"myDatabase", table "dbo.app_status", column 'status_id'.
I am wondering if the query is failing because each table already contains approximately 130,000 records?
Please help.
Thanks.
The error is occuring because there is a value in dbo.app_additional_info.application_ID that is not in dbo.app_Status.Status_ID. Unless the naming convention is seriously messed up you are trying to add a relationship to unrelated columns, why would application_ID reference status_ID?
I expect that dbo.App_Additional_Info.Application_ID should be referencing dbo.Application.Application_ID (Guessing at the table and column names slightly) so you would want this:
USE MyDatabase
GO
ALTER TABLE dbo.App_Additional_Info
ADD CONSTRAINT FK_App_Additional_Info_Application_ID (Application_ID)
REFERENCES dbo.Application (Application_ID)
ON DELETE CASCADE
ON UPDATE CASCADE;

Please explain the syntax the SQLServer uses to create a check constraint

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]

Problem with an SQL statement

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!!

Modify unique constraint in Oracle

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