Cannot create a relation between two tables with three primary keys - sql-server

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.

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.

Inserting Records into ASE linked table results in single-row update/delete affected more than one row of a linked table

In Microsoft Access 2016 i have a link to an ASE table trough the Adaptative Server Enterprise ODBC driver.
The table is used only has a data pass table for database admins
On the server side the table has no indexes, primary keys or foreign keys
When linking the table in access, it always asks you to identify the primary key/keys in order to be able to insert records into it.
In Microsoft Access 2010 they had it working by identifying 10 fields as primary keys just so that records could be inserted from access
In Micrososft Access 2016 i am getting the error:
Single-row update/delete affected more than one row of a linked table.
Unique index contains duplicate values. (Error 3362)
Is there a way to allow access to insert records into this table without declaring a primary key, since the server table has none?
Thanks in advance

SQL Server - ALTER query can create FK relationships, but cannot view them?

I'm stumped on whether I have written the correct syntax to create a foreign key.
I used SQL Server 2012 Express.
If I run a ALTER query to set a foreign key relationship between two table, it works fine, no errors occured. However, if I right-click the table where the FK was created, I don't see any relationships.
This is the ALTER query I have wrote. It creates a relationship between Employers and Employees with EmployerID as a FK.
USE demodemo;
BEGIN TRAN t1
ALTER TABLE Employees
WITH check
ADD CONSTRAINT Employees_EmployerID_FK FOREIGN KEY
(EmployerID) REFERENCES Employers(ID);
GO
The command was executed 'successfully'.
However, if I right click the table, Employees, and select 'Relationships'.
No foreign keys relationships can be seen.
I thought writing the above ALTER query would be the equivalent of creating a FK relationship via the 'Relationships' gui.
Despite having no issues in creating foreign key relationships, I just cannot see them at all.
What could I be doing wrong?
Is my ALTER query correct?
What is the ALTER syntax equivalent to allow me to view the "selected relationships"?
Your DML is missing COMMIT. Also, right click and refresh after executing the SQL
Raj

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