Db2 proper syntax for adding a constraint and a primary key - sql

I got the following SQL command from a DDL of a table in SQL Server. I need to set the same in a db2 database:
ALTER TABLE "schema"."table"
ADD CONSTRAINT "PK_BLA_DIM" PRIMARY KEY "FRR_BLUR_ID"
I have been trying to set it up but I don't know the right syntax and it show different errors. Can someone help me with the right syntax here? Appreciate it.

ALTER TABLE <schema.table_name>
ADD CONSTRAINT <constraint_name>
PRIMARY KEY (<column_name>)

Related

Trouble Altering Table SQL

Hey guys just wondering what i'm doing wrong in this script.
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5),
ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#)
);
When i do the ADD commands individually the table alters, so I'm not really sure what I'm doing wrong. Thanks in advance
In Oracle, alter table only allows one modification at a time. You can see this in the syntax diagram in the documentation: There are no back arrows.
So:
ALTER TABLE SSV_TOURS ADD CRUISE_ID# CHAR(5);
ALTER TABLE SSV_TOURS ADD CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#) REFERENCES SSV_CRUISES(CRUISE_ID#);
I think you can't mix column_clause and constraint_clause in one alter table sentence. However in your case you either need to split your alter table to two (one with add column and other with add constraint) or slightly rewrite your statement as
ALTER TABLE SSV_TOURS (
ADD CRUISE_ID# CHAR(5)
CONSTRAINT TOURS_CRUISEID#_FK FOREIGN KEY (CRUISE_ID#)
REFERENCES SSV_CRUISES(CRUISE_ID#)
);

Incorrect Syntax near '.' (Microsoft SQL Server Native Client 11.0)

GO
ALTER TABLE [dbo].[CustomerInsuranceNumber]
  ADD CONSTRAINT pk_myConstraint PRIMARY KEY ([dbo].[CustomerStateProvince].[CustomerHQStateProvinceAbbreviation], [CustomerInsuranceNumber])
GO
I am trying to create a composite key made up of a column from the table above and a column from another table.
I get an incorrect syntax near '.' for the periods in [dbo].[CustomerStateProvince].[CustomerHQStateProvinceAbbreviation]
How do I fix this error? Thanks.
You cannot create a composite primary key from columns on another table. You need to have that value present on your table. Create a column named [CustomerHQStateProvinceAbbreviation]on your [CustomerInsuranceNumber] table and make the key as this:
ADD CONSTRAINT pk_myConstraint PRIMARY KEY ([CustomerHQStateProvinceAbbreviation], [CustomerInsuranceNumber])
You are trying to add a column from another tablo to primary key. You cannot do this.

SQL Alter table failure on constraint

My SQL is very out old, looking for some help with this error.
The alter table statement conflicted with the check constraint "ZYZ". The Conflict occurred in table A column DESCRIPTION
ALTER TABLE A_A
ADD CONSTRAINT ZYZ
CHECK (((LTRIM(RTRIM([DESCRIPTION)))+'A'=([DESCRIPTION+'A') AND [DESCRIPTION]<>''));
I get this is a data issue, but not sure what to look at...

Unable to add foreign key constraint due to obscure conflict

Trying to run :
ALTER TABLE [dbo].[Table1] ADD
CONSTRAINT [FK_Table1_ScenarioResult]
FOREIGN KEY ([ScenarioResultID]) REFERENCES [dbo].[ScenarioResult] ([ScenarioResultID]) ON DELETE CASCADE
Getting this error :
Msg 547, Level 16, State 0, Line 1
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Table1_ScenarioResult". The conflict occurred in database "8362", table "dbo.ScenarioResult", column 'ScenarioResultID'.
I have checked :
Constraint does not already exist, and no other exists on same column
The values in the column match in both tables
Types of columns are the same
Tried a different name, also fails
On SQL Server 2008 R2
Any ideas what I could try?
In theory this might work:
ALTER TABLE [dbo].[Table1] WITH NOCHECK ADD
CONSTRAINT [FK_Table1_ScenarioResult]
FOREIGN KEY ([ScenarioResultID]) REFERENCES [dbo].[ScenarioResult] ([ScenarioResultID])
ON DELETE CASCADE
Not sure how you checked for integrity of existing values, but it should be:
SELECT COUNT(*) as Orphans FROM [dbo].[Table1] t
WHERE NOT EXISTS
(SELECT * FROM [dbo].[ScenarioResult] WHERE ScenarioResultID = t.ScenarioResultID)
If "Orphans" is greater then zero you need to clean the data before adding a constraint.
This one was baffling me aswell, and then the penny dropped.
I was trying to create a Foreign Key using "Database Diagrams" in SQL Server 2012, but it refused to let me as it claimed to clash with the foreign key I was trying to create. Huh ?
But, I had accepted the defaults to "Enforce foreign key constraint". But I already had data in the two tables I was attempting to create a foreign key for, and it was breaking the foreign key rule I was trying to make, so SQL Server was rejecting the new key.
The solution (in this particular case) was to change "Enforce foreign key constraint" to "No", or at least until I had cleaned up my data.
Hope this helps.
I had the same issue
I clear the data of the tables where i want to edit the content and add a new foreign key constraint and retry.
It works for me.
I hope it help you.

SQL conflicted with the FOREIGN KEY constraint

I am trying to run some update scripts on my database and I am getting the following error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_UPSELL_DT_AMRNO_AFMKTG_REF". The conflict occurred in database "ECOMVER", table "dbo.AFFILIATE_MKTG_REF", column 'AMRNO'.
I am running the following script:
ALTER TABLE [dbo].[UPSELL_DATA] WITH CHECK ADD
CONSTRAINT [FK_UPSELL_DT_AMRNO_AFMKTG_REF] FOREIGN KEY
(
[AMRNO]
) REFERENCES [dbo].[AFFILIATE_MKTG_REF] (
[AMRNO]
)
GO
AMRNO is a PK in table AFFILIATE_MKTG_REF.
Also, I tried to create the foreign key relation using the modify table option in SQL Management studio and I got the same error. I am not sure what I should be looking for?
Any suggestions would be greatly appreciated.
You probably have records in your [dbo].[UPSELL_DATA] table with values in the [AMRNO] column that don't exist in the [dbo].[AFFILIATE_MKTG_REF] table, [AMRNO] column. Try a query like this to find those that don't have matching records:
select *
from [dbo].[UPSELL_DATA] u
left join [dbo].[AFFILIATE_MKTG_REF] m
on u.AMRNO = m.AMRNO
where m.AMRNO is null
I think you have data restricted by foreign key try to check the data on both tables before assigning a foreign key, whether there are restrictions on both the tables.