Adding foreign key constrain fails on subcriber - sql-server

I have a replicated database using SQL Server 2008. Here is what I am trying to do.
Create a new table (MyNewTable)
Create a column in an existing Table (MyExistingTable)
Create a FK constraint on this column (in #2) as it points to PK to new table from #1.
However when data get replicated to subscriber, i get following error in replication monitor.
The schema script "script for adding foreign key constraint" could not be propagated to the subscriber. (Source: MSSQL_REPL, Error number: MSSQL_REPL-2147201001)
Get help: http://help/MSSQL_REPL-2147201001
Foreign key "MyFKConstraint" references invalid table "MyNewTable" (Source: MSSQLServer, Error number: 1767)
This script runs fine on publisher though. Some people seem to suggest that adding foreign key constraint cannot be done in a replicated environment when its created between an existing table and a new table. This is kind of hard to buy for me although I am kind of new to SQL Server replication. Any thoughts?

Foreign key "MyFKConstraint" references invalid table "MyNewTable
Is "MyNewTable" in subscriber database?
if not can you use "NOT FOR REPLICATION" while creating FK

Related

SQL Server : Relationships

I'm using SQL Server 2019 for my project that is a Library management.
I was going to make a relationship from tblServices to tblCustomers that would get CustomerName from tblCustomer and set it into tblServices field CustomerName.
However when I was going to save the tables SQL Server prevent that and shown this error:
Unable to create relationship 'FK_tblServices_tblCustomers'.
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tblServices_tblCustomers". The conflict occurred in database "Library_DB", table "dbo.tblCustomers", column 'CustomerName'.
I guess it tells I'm making a duplicate relationship on a same property. But I've checked it and it was just one relationship on CustomerName.

Cannot create a relation between two tables with three primary keys

I recently used Microsoft SQL Server Migration Assistant for Oracle to convert an Oracle database to a SQL Server database through a two-pass approach.
There is are two tables, BILL_INFO and BILL_INFO_DETAIL, that are supposed to have a master-detail relation through composite PK. However, when I try to create that relation, I get this error:
'BILL_INFO' table saved successfully 'BILL_INFO_DETAIL' table
- Unable to create relationship 'FK_BILL_INFO_DETAIL_BILL_INFO'. The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"FK_BILL_INFO_DETAIL_BILL_INFO". The conflict occurred in database
"MyDatabase", table "dbo.BILL_INFO".
The database is plagued with bad data. So I did a basic search in the detail table to find BILL_NUMBER, PAY_MODE_ID, and CASHIER_ID that may not exist in master (one by one) and found two records when searching on BILL_NUMBER. I fixed them and also verified that PAY_MODE_ID and CASHIER_ID were in order.
Still, I cannot create the relation. Same error. Now I wonder if the Tuple is invalid between tables. How do I find a composite key that exists only in details table?
You could check for non-existing values using:
SELECT bill_number, pay_mode_id, cashier_id
FROM Bill_Info_Detail
EXCEPT
SELECT bill_number, pay_mode_id, cashier_id
FROM Bill_Info;
-- and then fix missing data
When using composite key, you need to check all columns as tuple.

Connecting the tables in a database

I have created a simple database in SQL Server Express which consists of three tables: Inventory, Customers, Orders.
I try to connect them in db diagram forcing the primary keys of Inventory and Customers (CarID and CustID) as foreign keys to Orders. However, when I try to save the diagram, I receive an error that does not allow me to save the diagram and link the tables.
The error indicates:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
"FK_Orders_Inventory". The conflict occurred in database "AutoLot",
table "dbo.Inventory", column 'CarID'.
FK_Orders_Inventory is the connection between Inventory and Orders. What could be a potential cause of the error?
The data currently in the table is probably not conforming to the constraints you have defined.
Make sure the data is consistent with the constraints before adding them.
In this case, one of the foreign keys you are defining fails because the column you are defining it on (in the Inventory table) contains values that do not exist on the referenced column (CarId) in the foreign table.
You have a CarID value in the child table that does not exist in the parent table.

SQL Server merge replication causes foreign key failures

In my application, using SQL Server 2005, I'm having two tables, let's call them Table A and Table B; a foreign key constraint is defined on Table B, referencing the primary-key column in Table A, which is an auto-generated integer ID. I'm running the following simple transaction:
Start transaction
Insert a row to table A
Retrieve the last-generated ID ("SELECT ##IDENTITY ... ")
Insert data to table B, using this ID
Commit
It all works well, until I'm trying to create merge replication (continuous) with another SQL Server 2005. Both publisher and subscriber now fail this transaction when trying to insert data to table B, because of foreign key constraint failure:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_TableB_TableA". The conflict occurred in database "MyDB", table "TableA", column 'ID'.
I was not able to make it work by committing after inserting data to table A. However, after removing the merge replication, everything worked. The database code is written in C++, using ADO.
Is the replication interfering with the transaction anyhow? Any other possible explanation?
Are the Primary Key values on Table A at both server nodes discrete from one another (In other words are you using Identity Range management at each node)?
Also, has the Foreign Key constraint been configured with the Not For Replication property?
I would assume that because your Foreign Key constraint has already been enforced locally at the Publisher, that you do not need to re-check it when merging with the Subscriber.
Looks like the issue is related to the scope of the ##IDENTITY function. When I used LAST_IDENT('TableB') instead, things seem to work.
As described in MSDN:
IDENT_CURRENT returns the last identity value generated for a specific table in any session and any scope.
##IDENTITY returns the last identity value generated for any table in the current session, across all scopes.
SCOPE_IDENTITY returns the last identity value generated for any table in the current session and the current scope.

SQL Server Create multiple foreign keys

The table contains a PRIMARY KEY column and another column which is FOREIGN KEY. This works fine. When I attempt to add another column as a FOREIGN KEY I get the following message:
Link to pic
This is unrelated to the PKs or FKs.
There is a setting in SQL Server 2008 SSSM to prevent table rebuilds via the designer.
Tools..Options..Designers... clear the option “Prevent saving changes that require table re-creation”.

Resources