Unable to create diagram - sql-server

I'm getting this error:
'Category' table saved successfully
'Author' table saved successfully
'Product' table -
Unable to create relationship 'FK_Product_Author'. The ALTER TABLE
statement conflicted with the FOREIGN KEY constraint
"FK_Product_Author". The conflict occurred in database "Sample_1",
table "dbo.Author", column 'AuthorID'.
How can I fix this ??

I suppose you have information in your database and you try to create a diagram.
You have an inconsistence between Product and Author tables, I mean, you have an AuthorId inside Product Table that dooes not exist in Author Table, so when you try to create a Foreign Key, you can't create it.

Related

Table with multiple relations to a single primary key

Is it possible to create multiple relations from one table, to another table?
I have a table containing purchases, each of these purchases have a origin_country and a destination_country.
I would like to have relations (as foreign keys) to a single PK on a table from these two columns from the same table.
i have tried the following queries:
alter table Purchases
add constraint FK_Purchases_OriginCountries
foreign key (FK_OriginCountryCode) references dbo.countries
go
alter table Purchases
add constraint FK_Purchases_DestinationCountries
foreign key (FK_DestinationCountryCode) references dbo.countries
go
But end up getting a conflict, I can't however find documentation that this is not possible...
[23000][547] The ALTER TABLE statement conflicted with the FOREIGN KEY
constraint "FK_Purchases_DestinationCountries". The conflict occurred
in database "Market", table "dbo.countries", column 'ID'.
Is this relationship intentionally not possible, or did i just make a mistake?
Thank you
Yes you can.
The error is not a result of trying to create two foreign keys back to a single table, that's perfectly fine. Try running this to see it work:
create table t(i int primary key);
create table u
(
j int foreign key references t(i),
k int foreign key references t(i)
);
The problem you have is that you have some data in your Purchases table where the value in the column on which you are trying to create the foreign key does not exist in the countries table's ID column.
To find them run a query like this:
select p.*
from dbo.purchases p
where not exists
(
select *
from dbo.countries
where ID = p.FK_DestinationCountryCode
)
Note that I think your column names are a little weird here, You shouldn't call a column FK_DestinationCountryCode just because it has a foreign key on it, and a "code" is not the same kind of thing as an "ID". Your purchases table's columns should probably be called DestinationCountryID and OriginCountryID.

INSERT statement conflict with the foreign key

INSERT statement conflicts with the foreign key. I may be entering the foreign key incorrectly.
Error:
No row was updated.
The data in row 1 was not committed.
Error Source: .Net SqlClient Data Provider.
Error Message: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Movie_Director". The conflict occurred in database table Director column Director ID.
Foreign Key
The Project Tables
ExecuteInsertMovie
ExecuteInsertDirector
ErrorMessage
Since You don't have any data in director table you can't perform entry to movie table(because of foreign key reference) so first enter some data into the director table then enter data into movie table
For more details you can refer to this link
INSERT statement conflicted with the FOREIGN KEY constraint - SQL Server
Can you provide the executed insert query ? make sure that the value you are inserting into a column that references another table exists in that table. i.e in your example table, if you are inserting to a movie table the DirctorID should exist in the Director tale.

Postgresql problems with postgres_fdw module

I'm trying, using the PostgreSQL Maestro tool, to reference a foreign key coming from a "local" DB to an other primary key inside an other DB (actually, they're both on the same remote machine).
I've heard about the postgres_fdw module to create a foreign table that act like a copy of the table inside the remote DB, but when I try to execute my query I have this error:
"SQL Error: ERROR: referenced relation "foreign_olo" is not a table".
This is my sql code:
CREATE TABLE edb.olo_config (
primary_key integer NOT NULL PRIMARY KEY,
puntamento varchar,
mail_contatto_to varchar,
mail_contatto_cc varchar,
/* Foreign keys */
CONSTRAINT olo_code
FOREIGN KEY (olo_code)
REFERENCES edb.foreign_olo(codice_operatore)
) WITH (
OIDS = FALSE
);
foreign_olo is my foreign table created with postgres_fdw.
I have tried to commit an INSERT or a simple SELECT on the foreign_olo table, and all went well, so I can't understand why for the foreign key case it can't be recognized as a table.
Thank you to everyone would give me an hand!
There are two parts to enforcing a foreign key constraint:
An INSERT (or UPDATE of the FK field) on the child table must check that a parent record exists.
A DELETE (or UPDATE of the PK field) on the parent table must check that no child record exists.
Foreign tables can't be referenced by FK constraints, because this second condition is impossible to enforce - there's no way for the remote server to automatically check for records in your local table.
You can implement the first check relatively easily with a trigger:
CREATE FUNCTION edb.olo_config_fk_trg() RETURNS TRIGGER AS
$$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM edb.foreign_olo
WHERE codice_operatore = new.olo_code
FOR KEY SHARE
)
THEN
RAISE foreign_key_violation
USING MESSAGE = 'edb.foreign_olo.codice_operatore="' || new.olo_code || '" not found';
END IF;
RETURN NULL;
END
$$
LANGUAGE plpgsql;
CREATE TRIGGER olo_config_fk_trg
AFTER INSERT OR UPDATE OF olo_code ON edb.olo_config
FOR EACH ROW EXECUTE PROCEDURE edb.olo_config_fk_trg();
You can create a similar trigger on the PK table to do the update/delete check, but it will require another foreign table in your other database which points back to your local olo_config.

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: how to reference a foreign key to another table where the key is named differently

Trying to write the script to reference a foreign key in another table that is named differently. Here's part of my script, see if you can figure out what im trying to do, I don't know a better way to explain it:
ALTER TABLE journal
ADD CONSTRAINT journal_authorid_FK FOREIGN KEY(author_id) REFERENCES employee.emp_id;
as you can see, the author_id in one table references the emp_id primary key in another table. Reason being is that there is already a key in this table called emp_id.The emp_id FK in this table will be used to identify who this journal entry pertains to. The author_id is the person that made the entry. Obviously they are both located on the employee table. How do I make this relationship?
I believe the syntax is:
ALTER TABLE journal
ADD CONSTRAINT journal_authorid_FK FOREIGN KEY(author_id) REFERENCES employee(emp_id);

Resources