Foreign key reference problem, SQL Server
up vote
0
down vote
favorite
I'm having trouble understanding how to setup my foreign key reference. I'm trying to make a reference between tables Lägenheter and Arbetslista so that one row in Lägenheter belong to many rows in Arbetslista.
I have created tables likes this:
CREATE TABLE Deltagare (
Deltagarnr int PRIMARY KEY,
Namn varchar(30),
Typ varchar(30)
);
CREATE TABLE Uppgifter (
Uppgift varchar(30) PRIMARY KEY,
Typ varchar(30),
Pris money
);
CREATE TABLE Arbetslista (
Deltagarnr int not null,
Datum date not null,
Uppgift varchar(30) not null,
Lägenhetsnr int not null,
PRIMARY KEY (Deltagarnr, Datum, Uppgift),
FOREIGN KEY (Deltagarnr) REFERENCES Deltagare(Deltagarnr),
FOREIGN KEY (Uppgift) REFERENCES Uppgifter(Uppgift)
);
Then I import some data from a .mdb file to all three tables.
After that I add another table:
CREATE TABLE Lägenheter(
lägenhetsnummer int PRIMARY KEY,
gatuadress varchar(30),
kvadrater int
);
It all works fine until this last step:
ALTER TABLE Arbetslista
ADD FOREIGN KEY (Lägenhetsnr) REFERENCES Lägenheter(lägenhetsnummer)
;
Here I get this error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__403A8C7D". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'
.
After that I have also tried to delete all data from Arbetslista and then execute the mentioned ALTER TABLE
again. It works, but then I get an error trying to import the data again:
Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__412EB0B6". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.".
(SQL Server Import and Export Wizard)
Why is this not possible when the other foreign keys seems to work fine?
sql-server ssms
add a comment |
up vote
0
down vote
favorite
I'm having trouble understanding how to setup my foreign key reference. I'm trying to make a reference between tables Lägenheter and Arbetslista so that one row in Lägenheter belong to many rows in Arbetslista.
I have created tables likes this:
CREATE TABLE Deltagare (
Deltagarnr int PRIMARY KEY,
Namn varchar(30),
Typ varchar(30)
);
CREATE TABLE Uppgifter (
Uppgift varchar(30) PRIMARY KEY,
Typ varchar(30),
Pris money
);
CREATE TABLE Arbetslista (
Deltagarnr int not null,
Datum date not null,
Uppgift varchar(30) not null,
Lägenhetsnr int not null,
PRIMARY KEY (Deltagarnr, Datum, Uppgift),
FOREIGN KEY (Deltagarnr) REFERENCES Deltagare(Deltagarnr),
FOREIGN KEY (Uppgift) REFERENCES Uppgifter(Uppgift)
);
Then I import some data from a .mdb file to all three tables.
After that I add another table:
CREATE TABLE Lägenheter(
lägenhetsnummer int PRIMARY KEY,
gatuadress varchar(30),
kvadrater int
);
It all works fine until this last step:
ALTER TABLE Arbetslista
ADD FOREIGN KEY (Lägenhetsnr) REFERENCES Lägenheter(lägenhetsnummer)
;
Here I get this error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__403A8C7D". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'
.
After that I have also tried to delete all data from Arbetslista and then execute the mentioned ALTER TABLE
again. It works, but then I get an error trying to import the data again:
Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__412EB0B6". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.".
(SQL Server Import and Export Wizard)
Why is this not possible when the other foreign keys seems to work fine?
sql-server ssms
1
Are you putting the required data inLägenheter
?
– Mat
Nov 13 at 9:48
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm having trouble understanding how to setup my foreign key reference. I'm trying to make a reference between tables Lägenheter and Arbetslista so that one row in Lägenheter belong to many rows in Arbetslista.
I have created tables likes this:
CREATE TABLE Deltagare (
Deltagarnr int PRIMARY KEY,
Namn varchar(30),
Typ varchar(30)
);
CREATE TABLE Uppgifter (
Uppgift varchar(30) PRIMARY KEY,
Typ varchar(30),
Pris money
);
CREATE TABLE Arbetslista (
Deltagarnr int not null,
Datum date not null,
Uppgift varchar(30) not null,
Lägenhetsnr int not null,
PRIMARY KEY (Deltagarnr, Datum, Uppgift),
FOREIGN KEY (Deltagarnr) REFERENCES Deltagare(Deltagarnr),
FOREIGN KEY (Uppgift) REFERENCES Uppgifter(Uppgift)
);
Then I import some data from a .mdb file to all three tables.
After that I add another table:
CREATE TABLE Lägenheter(
lägenhetsnummer int PRIMARY KEY,
gatuadress varchar(30),
kvadrater int
);
It all works fine until this last step:
ALTER TABLE Arbetslista
ADD FOREIGN KEY (Lägenhetsnr) REFERENCES Lägenheter(lägenhetsnummer)
;
Here I get this error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__403A8C7D". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'
.
After that I have also tried to delete all data from Arbetslista and then execute the mentioned ALTER TABLE
again. It works, but then I get an error trying to import the data again:
Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__412EB0B6". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.".
(SQL Server Import and Export Wizard)
Why is this not possible when the other foreign keys seems to work fine?
sql-server ssms
I'm having trouble understanding how to setup my foreign key reference. I'm trying to make a reference between tables Lägenheter and Arbetslista so that one row in Lägenheter belong to many rows in Arbetslista.
I have created tables likes this:
CREATE TABLE Deltagare (
Deltagarnr int PRIMARY KEY,
Namn varchar(30),
Typ varchar(30)
);
CREATE TABLE Uppgifter (
Uppgift varchar(30) PRIMARY KEY,
Typ varchar(30),
Pris money
);
CREATE TABLE Arbetslista (
Deltagarnr int not null,
Datum date not null,
Uppgift varchar(30) not null,
Lägenhetsnr int not null,
PRIMARY KEY (Deltagarnr, Datum, Uppgift),
FOREIGN KEY (Deltagarnr) REFERENCES Deltagare(Deltagarnr),
FOREIGN KEY (Uppgift) REFERENCES Uppgifter(Uppgift)
);
Then I import some data from a .mdb file to all three tables.
After that I add another table:
CREATE TABLE Lägenheter(
lägenhetsnummer int PRIMARY KEY,
gatuadress varchar(30),
kvadrater int
);
It all works fine until this last step:
ALTER TABLE Arbetslista
ADD FOREIGN KEY (Lägenhetsnr) REFERENCES Lägenheter(lägenhetsnummer)
;
Here I get this error message:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__403A8C7D". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'
.
After that I have also tried to delete all data from Arbetslista and then execute the mentioned ALTER TABLE
again. It works, but then I get an error trying to import the data again:
Hresult: 0x80004005 Description: "The INSERT statement conflicted with the FOREIGN KEY constraint "FK__Arbetslis__Lägen__412EB0B6". The conflict occurred in database "Hushåll", table "dbo.Lägenheter", column 'lägenhetsnummer'.".
(SQL Server Import and Export Wizard)
Why is this not possible when the other foreign keys seems to work fine?
sql-server ssms
sql-server ssms
edited Nov 13 at 11:48
Birel
15410
15410
asked Nov 13 at 9:44
Crocky
12
12
1
Are you putting the required data inLägenheter
?
– Mat
Nov 13 at 9:48
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24
add a comment |
1
Are you putting the required data inLägenheter
?
– Mat
Nov 13 at 9:48
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24
1
1
Are you putting the required data in
Lägenheter
?– Mat
Nov 13 at 9:48
Are you putting the required data in
Lägenheter
?– Mat
Nov 13 at 9:48
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
There are rows in Arbetslista
table, for which the value of Lägenhetsnr
doesn't exists in field lägenhetsnummer
of table Lägenheter
. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
add a comment |
up vote
0
down vote
After importing the data into Arbetslista
, the column Lägenhetsnr
was initialize to some value ( say XYZ
).
later, when trying to add the FOREIGN KEY
to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ
and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
There are rows in Arbetslista
table, for which the value of Lägenhetsnr
doesn't exists in field lägenhetsnummer
of table Lägenheter
. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
add a comment |
up vote
0
down vote
There are rows in Arbetslista
table, for which the value of Lägenhetsnr
doesn't exists in field lägenhetsnummer
of table Lägenheter
. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
add a comment |
up vote
0
down vote
up vote
0
down vote
There are rows in Arbetslista
table, for which the value of Lägenhetsnr
doesn't exists in field lägenhetsnummer
of table Lägenheter
. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
There are rows in Arbetslista
table, for which the value of Lägenhetsnr
doesn't exists in field lägenhetsnummer
of table Lägenheter
. You must clean the existing data to be able to create the foreign key like that.
Check your data by executing this script:
select * from Arbetslista a where not exists (select * from Lägenheter l where l.lägenhetsnummer = a.Lägenhetsnr)
answered Nov 13 at 9:49
Andrey Nikolov
1,566139
1,566139
add a comment |
add a comment |
up vote
0
down vote
After importing the data into Arbetslista
, the column Lägenhetsnr
was initialize to some value ( say XYZ
).
later, when trying to add the FOREIGN KEY
to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ
and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.
add a comment |
up vote
0
down vote
After importing the data into Arbetslista
, the column Lägenhetsnr
was initialize to some value ( say XYZ
).
later, when trying to add the FOREIGN KEY
to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ
and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.
add a comment |
up vote
0
down vote
up vote
0
down vote
After importing the data into Arbetslista
, the column Lägenhetsnr
was initialize to some value ( say XYZ
).
later, when trying to add the FOREIGN KEY
to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ
and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.
After importing the data into Arbetslista
, the column Lägenhetsnr
was initialize to some value ( say XYZ
).
later, when trying to add the FOREIGN KEY
to Arbetslista, SQL try to meet this, however it cannot find row with primary key lägenhetsnummer = XYZ
and the statement would be terminated.
I think the best is to create the 4 table first then clean the data and try to import it.
edited Nov 13 at 15:16
Birel
15410
15410
answered Nov 13 at 10:20
Ahmed Bahtity
17925
17925
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53278067%2fforeign-key-reference-problem-sql-server%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Are you putting the required data in
Lägenheter
?– Mat
Nov 13 at 9:48
And are you inserting the data inside a transaction into both tables?
– Sebastian S.
Nov 13 at 9:50
Side note: Please learn to name your constraints. It makes things a lot more pleasant if you later need to e.g. drop some of them.
– Damien_The_Unbeliever
Nov 13 at 10:24