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

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'

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.

Remove record from sys.objects?

I ran a ALTER SCHEMA ... on a table (SQL Server 2012 SP1) but the sys.objects record is still there. When I run the ETL I get an error that the table doesn't exist anymore because it's trying to remove all constraints. This one is of type PK Primary Key Constraint. How can I safely remove the record from the sys.objects table?
I managed to fix this issue by scripting the database to a new one, ALTER SCHEMA ... again on that table to bring it back to dbo., then DROP and recreate the table in the different table schema.

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

Invisible Foreign Key in SQL Server 2005

I have a script to update a database by checking for a foreign key's existence and, if it doesn't exist, creating it. It was generated by SQL Management Studio.
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_tblChangeRequestForecast_tblCostingCenter]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblChangeRequestForecast]'))
ALTER TABLE [dbo].[tblChangeRequestForecast] WITH CHECK ADD CONSTRAINT [FK_tblChangeRequestForecast_tblCostingCenter] FOREIGN KEY([CostingCenterID])
REFERENCES [dbo].[tblCostingCenter] ([CostingCenterID])
GO
IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_tblChangeRequestForecast_tblCostingCenter]') AND parent_object_id = OBJECT_ID(N'[dbo].[tblChangeRequestForecast]'))
ALTER TABLE [dbo].[tblChangeRequestForecast] CHECK CONSTRAINT [FK_tblChangeRequestForecast_tblCostingCenter]
GO
The script raises an error when it's run:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_tblChangeRequestForecast_tblCostingCenter". The conflict occurred in database "mydatabase", table "dbo.tblCostingCenter", column 'CostingCenterID'.
This is very mysterious. There is no trace of the foreign key as far as the queries can tell, but the create script fails with the above error. The server is running SQL Server 2005 SP3 (9.00.4035.00).
[Update]: I've just reproduced the issue on a SQL Server 2012 instance. So the version doesn't appear to be too important.
Any idea what could cause this?
It is complaining about the FK constraint that you are trying to add because there is existing data in the table that would no longer be valid if the constraint was successfully applied.
Probably means there is one or more ChangeRequestForecast records where CostingCenterID has a value that doesn't correspond with CostingCenter.CostingCenterID.
Open up SSMS, go to that database, expand that table, and look in the Constraints section. There likely is one in there called "FK_tblChangeRequestForecast_tblCostingCenter" that you need to figure out if you need, and if not, delete it.

Resources