473,546 Members | 2,244 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SS 2005 Express- Allowing null values and Foriegn Key Constraints

Hello,

I have a foreign key constraint between two tables (Appointments and
MissedAppointme ntReasons) and I'd like to allow null values in Appointments
table for the field containing the MissedAppointme ntReason, but currently, I
get a Foreign Key Constraint Error when I try to add a record to the
Appointments table.

Any ideas how I can have the null values but still maintain the Foreign Key
Constraint?

Thanks!
Rick
Dec 28 '06 #1
3 5801
I would suggest adding a lookup value for "did not miss appointment"
and then storing the ID value of that in your appointments table. That
way you can keep the FK relationship and still have a valid value,
which you could even make the default value for the field for ease of
use.

On Dec 28, 1:57 pm, "Rico" <y...@me.comwro te:
Hello,

I have a foreign key constraint between two tables (Appointments and
MissedAppointme ntReasons) and I'd like to allow null values in Appointments
table for the field containing the MissedAppointme ntReason, but currently, I
get a Foreign Key Constraint Error when I try to add a record to the
Appointments table.

Any ideas how I can have the null values but still maintain the Foreign Key
Constraint?

Thanks!
Rick
Dec 28 '06 #2
Rico (yo*@me.com) writes:
I have a foreign key constraint between two tables (Appointments and
MissedAppointme ntReasons) and I'd like to allow null values in
Appointments table for the field containing the MissedAppointme ntReason,
but currently, I get a Foreign Key Constraint Error when I try to add a
record to the Appointments table.
Then you are writing a value to MissedAppointRe ason that does exist
in that table, for instance 0.

That is, NULL values do not cause FK violation. Try this:

CREATE TABLE first (a int NOT NULL,
CONSTRAINT pk_first PRIMARY KEY(a))
CREATE TABLE second (b int NOT NULL,
a int NULL,
CONSTRAINT pk_second PRIMARY KEY(b),
CONSTRAINT fk FOREIGN KEY (a) REFERENCES first(a))
go
INSERT first (a) VALUES (1)
go
INSERT second (b, a) VALUES( 1, 1) -- goes fine.
INSERT second (b, a) VALUES( 2, NULL) -- goes fine.
INSERT second (b, a) VALUES( 3, 2) -- error.
go
SELECT * FROM second
go
DROP TABLE second, first

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Dec 28 '06 #3
Doh! Thanks Erland. I have no idea how that happened since I never user
defaults except for booleans and dates.

I suppose I should have double checked that before posting here. (*blush*)

Thanks for your help!

Rick

"Erland Sommarskog" <es****@sommars kog.sewrote in message
news:Xn******** *************@1 27.0.0.1...
Rico (yo*@me.com) writes:
>I have a foreign key constraint between two tables (Appointments and
MissedAppointm entReasons) and I'd like to allow null values in
Appointments table for the field containing the MissedAppointme ntReason,
but currently, I get a Foreign Key Constraint Error when I try to add a
record to the Appointments table.

Then you are writing a value to MissedAppointRe ason that does exist
in that table, for instance 0.

That is, NULL values do not cause FK violation. Try this:

CREATE TABLE first (a int NOT NULL,
CONSTRAINT pk_first PRIMARY KEY(a))
CREATE TABLE second (b int NOT NULL,
a int NULL,
CONSTRAINT pk_second PRIMARY KEY(b),
CONSTRAINT fk FOREIGN KEY (a) REFERENCES first(a))
go
INSERT first (a) VALUES (1)
go
INSERT second (b, a) VALUES( 1, 1) -- goes fine.
INSERT second (b, a) VALUES( 2, NULL) -- goes fine.
INSERT second (b, a) VALUES( 3, 2) -- error.
go
SELECT * FROM second
go
DROP TABLE second, first

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Dec 29 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1587
by: Chris | last post by:
If i've installed SQL Server 2005 Developer, when I come to install VS 2005 is there any need to install SQL Server Express? I would have thought the install would have detected SQL Server Developer was installed and not select Express by default. Do any examples projects for example rely on SQL Express being present? Thanks, Chris
1
1873
by: Lubomir | last post by:
Hi, I would like to ask, how different are C# 2005 Express Edition and C# within MS Visual Studio 2005. Than as well Visual Web Developer 2005 Express Edition and ASP within MS VS 2005. Thanks, Lubomir
10
1958
by: Bonj | last post by:
Hi I installed .NET 2005 express edition, and am about to unnistall it again because it can't seem to be able to even insert a resource file into a project. Can anyone tell me is this right? Should it be able to? I know it's express edition, and therefore limited functionality, but surely resources are quite fundamental? Also, once it's...
6
2548
by: Brian Henry | last post by:
Visual Basic 2005 Express: http://download.microsoft.com/download/f/c/7/fc7debaf-4513-4300-9e6a-8fe27be88cd1/vbsetup.exe Visual C# 2005 Express: http://download.microsoft.com/download/2/d/2/2d221839-eaf9-4d91-899e-95a9af12202e/vcssetup.exe Visual C++ 2005 Express:...
8
1540
by: Peter Oliphant | last post by:
I have been using VS C++.NET 2005 Express as our company waits for the MSDN full version to arrive. It is a pretty amazing free tool! My question is I'd like to also do some VS C#.NET stuff too. So I'd like to download VS C#.NET 2005 Express while we are waiting as well. Problem is, although I've never tried, it says I have to uninstall all...
1
7136
by: QLD_AU | last post by:
Has anyone see the following error ? VS 2005 Installs ok, however the SQL Mobile Edition (part of a full install) fails with the following error ? With Thanks Jason
1
6601
by: Peter | last post by:
I've purchased VS.NET 2005 Standard and have tried to install SQL Server 2005 Express, but get the following error in the error log. Please could someone help me.... Microsoft SQL Server 2005 Express Edition x86: Component Microsoft SQL Server 2005 Express Edition x86 returned an unexpected value. ***EndOfSession***? Microsoft SQL Server...
10
1771
by: kmich | last post by:
I am planning on learning C#. Would it be best to learn on a full version of 2003 Pro or 2005 Express. What would be the advantages/disadvantages? Thank you for your time.
2
2083
by: kress1963nov22 | last post by:
I recently purchased a good MS book ("Build a Web Site Now") by Jim Buyens. It has the Express Edition of MS-Visual Web Developer 2005 on CD and also MS SQL Server 2005 Express Edition on the CD. A couple of questions for this newbie: Can MS SQL Server 2005 Express Edition be used in place of the older MSDE product for backend database work? I...
1
2494
by: Dr T | last post by:
Hi! I downloaded MS Visual Web Developer 2005 Express Edition, MS .NET Framework SDK v2.0, and MS SQL Server 2005. Subsequently, I bought MS Visual Studio 2005 Professional Edition. 1) Are both the MS Visual Web Developer 2005 Express Edition and the MS Visual Studio 2005 Professional Edition used to develop .ASP applications?
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6030
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3472
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.