SQL Server : Relationships - sql-server

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.

Related

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.

Legacy tables grails mapping issue with SQL Server

I have 2 legacy tables into SQL Server:
This is the first one --> http://ur1.ca/hl1mi
This the second table --> http://ur1.ca/hl1mr
An these are my domain classes for these tables
Taladro.groovy --> http://ur1.ca/hl1mw
Proyecto.groovy --> http://ur1.ca/hl1n8
When I try to insert a Taladro into the database using the dinamic scaffolding fails with these error:
ERROR util.JDBCExceptionReporter - Cannot insert the value NULL into column 'proyecto_id', table 'Portal.ORECONTROL.TALADROS'; column does not allow nulls. INSERT fails.
I change the name of the foreign key from id_proyecto to IdMalla as are in the legacy tables (See http://ur1.ca/hl1mw).
I'm using a SQL Server 2008 as Database server
Seems that now you have column proyecto_id in TALADROS table with not-null constraint. In domain object Taladro you have specified association column mapping as 'IdMalla' instead of default 'proyecto_id'.
Obviously that means that column proyecto_id is not filled and since it has not-null constraint, you get this error.
I can resolve the problem renaming the domain classes with name of the tables in the database and changing the name of the foreign key to "IdMalla". Thanks all for the answers

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

How do you identify a foreign key constraint from its name?

Using Hibernate ORM against SQL Server 11 on ColdFusion 10.
I'm getting the error:
[Macromedia][SQLServer JDBC Driver][SQLServer]The ALTER TABLE
statement conflicted with the FOREIGN KEY constraint
"FK9E8E5409B9A80FE4". The conflict occurred in database "dbname", table
"dbo.TableName", column 'TableId'.
Which seems self-explanatory, except I have checked the relevant table in SQL Server Management Studio and don't find a foreign key constraint with that ID for that table, nor any related tables.
Is there a way to locate the constraint, e.g. by querying the database system tables?
Update: I was looking in the wrong system views - looking inside dbname.sys.foreign_keys I do see a bunch of foreign keys, but checking for the one mentioned in the error returns nothing. Is that the right place to look?
Take a look at the sys.all_objects view, it's easier to query
select
sa.name as FKName
,so.name as TableName
from
sys.all_objects sa
inner join sys.objects so on sa.parent_object_id = so.[object_id]
where
sa.[type] = 'F'

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.

Resources