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
Related
I'm trying to create some tables in HeidiSQL, the main key allows me to create it without any problem but when trying to create a foreign key I get error (1064) I would like to know if they could help.
Table 1. Where the main key is located
Table 2. Where the foreign key should go
Log in with Root
Create the tables
fill the tables with the given information
Create the main key in id_clientes
Create the foreign key in the table t_clientes put in the cell with the same name bearing the main key (id_clientes)
Those are at least the steps I have been given for this task but I will appear error 1064, even if I try to switch to another cell does not allow me the foreign key.
try this, I believe it will work
ALTER TABLE t_clientes
ADD CONSTRAINT id_clientes_fkey FOREIGN KEY (id_clientes)
REFERENCES t_ventas(id_clientes) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
I have looked at other questions but their solutions have not worked for me. I have a table with 2 columns, and when attempting to run
ALTER TABLE tableName ADD PRIMARY KEY (col1,col2);
it gives the following error:
ERROR: multiple primary keys for table "tableName" are not allowed
All solutions I have found on SO so far have given me the same issue. Is this a result of a newer version of psql?
I have the following query which is adding contraint.
but in order to add, i want to check if this key has already been used or not?
ALTER TABLE HL7_MessageHierarchy
ADD CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType);
for example. if i have to add a column, i can easily check if the table exists in sysobjects and its respective column exists in syscolumns.
Is it possible to use the query multiple times without GO and without making any error indeed? if yes then how ???
[EDIT]
I don't know why my browser not allowing me to add comments so i am adding to Edit.
I want to check if there exists any foreign key with same name. so if there is no data even then the query can make problem because the key may already be existing. I want to run the above script clean (ofcourse resident data does matter but that is perhaps a straight forward check?)
[EDIT]
my bad, i must have known that version is important... I believe its 2005... (will love to know if someone can tell for other versions too)
I assume you mean
check the HL7_MessageHierarchy for values not inHL7_MessageType"
So, a query like this will tell you
SELECT *
FROM HL7_MessageHierarchy H
WHERE NOT EXISTS (SELECT *
FROM HL7_MessageType T
WHERE H.vMessageType = T.vMessageType)
Also, I'd recommend using WITH CHECK too
ALTER TABLE HL7_MessageHierarchy WITH CHECK ADD
CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType);
In SQL 2005, the recommended way of checking for the existence of objects is Catalog Views. The one you want is sys.foreign_keys:
IF NOT EXISTS ( SELECT * FROM sys.foreign_keys
WHERE name = 'fk_vMessageType' )
BEGIN
EXEC ('
ALTER TABLE HL7_MessageHierarchy
ADD CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType)
')
END
I have wrapped the creation in EXEC to avoid confusing the parser.
Using:
ALTER TABLE dbo.Table1Name
ADD FOREIGN KEY (colname)
REFERENCES dbo.Table2Name (colname)
I get a foreign key with a name like:
FK___colname__673F4B05
I want it to be named:
FK_Tabl1Name_Table2Name,
...so that it will be easy to read when browsing the DB structure in SSMS. I know I can go back into the GUI and do this, but I want to be able to script it.
So What's the SQL sytnax for adding a name to the FK? Nothing I've found online seems to bother with this.
Here's how you can assign your chosen name to the foreign key constraint:
ALTER TABLE dbo.Table1Name
ADD CONSTRAINT FK_Tabl1Name_Table2Name
FOREIGN KEY (colname) REFERENCES dbo.Table2Name (colname)
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!!