473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

One column is nullable on the composite key

Hi All!

I would like to have a composite PK on 3 columns, one of them is null
CREATE TABLE TableA (
ColA int NOT NULL ,
ColB int NOT NULL ,
ColC char (3) NULL ,
......
)
GO

ALTER TABLE TableA ADD
CONSTRAINT TableA_PK PRIMARY KEY CLUSTERED
(
ColA,
ColB,
ColC
)
GO
SQL Server does not allow having a composite PK with one nullable column:

What is wrong to have values?
1,100,NULL
1,200,ABC
1,200,ABD
.....

Code in C applies to Values in B and for some values in B the code does not exist.

I can work out and define a special Code:
NEV(not existing value), but in general I do not understand this restriction.

Thanks
Jul 20 '05 #1
3 14514
It is simple: You cannot identify something, if a portion of the identifying
information is missing.

Considering your example, if you have another row, say ( 1, 100, XYZ ) would
it be sensible to have a value missing in the composite key? BTW, SQL has
screwed up this area royally with FKs allowing NULLs in composite key
references -- which is why surrogates are recommended in certain scenarios.

--
Anith
Jul 20 '05 #2
>> I can work out and define a special Code: NEV(not existing value),
but in general I do not understand this restriction. <<

Get a book on RDBMS; read the part about keys; read the definition of
NULL. That ought to answer your question. This is like thinking that
you can locate a point on earth without both Longitude and Latitude.
Jul 20 '05 #3
Andy (ne********@hotmail.com) writes:
I would like to have a composite PK on 3 columns, one of them is null
CREATE TABLE TableA (
ColA int NOT NULL ,
ColB int NOT NULL ,
ColC char (3) NULL ,
......
)
GO


All columns in a primary key must be NOT NULL. This is a basic concept
in relational databases.

However, you can define a UNIQUE constraint on the columns. SQL Server will
then allow exactly one row with NULL in ColC for any given value of ColA and
ColB.

If you have references to this table, you should probably throw in a
surrogate key, and use that as foreign key to other tables.

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

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4

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

Similar topics

20
by: Andy | last post by:
Hi All! General statement: FK should not be nullabe to avoid orphans in DB. Real life: Business rule says that not every record will have a parent. It is implemented as a child record has FK...
1
by: shiyuan | last post by:
Dear Sir: We have created a table with a column of composite type. how can we insert values in this table. for exemple: create table aa (a int); create tabel bb (b aa); how to insert data ...
5
by: Robert Stearns | last post by:
Either I missed something, or ALTER TABLE does not have this capability. Is there any way of doing it except DROPping all constraints which mention this table, EXPORTing the data, DROPping the...
30
by: dbuchanan | last post by:
ComboBox databindng Problem == How the ComboBox is setup and used: My comboBox is populated by a lookup table. The ValueMember is the lookup table's Id and the DisplayMember is the text from a...
7
by: Rajesh.............................. | last post by:
What is the impact of using a nullable column vs a not nullable column for partitioning a Union ALL View? I have a Union ALL View with ten underlying tables unioned based on different values for a...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
0
by: Jacques Vandensavel | last post by:
I have a legacy table which is composed out three key fields. It is representing a bank. I'm using NHibernate in my ASP.NET application to glue everything together. I'm already searching for an...
3
by: Eric | last post by:
I have created a fairly basic composite control consisting of a Label and a TextBox. In the overridden Render function, I'm creating a table with two rows and each row contains a cell (td). The...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.