Re: One column is nullable on the composite key

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Erland Sommarskog (esquel_at_sommarskog.se)
Date: 08/14/04


Date: Sat, 14 Aug 2004 22:11:02 +0000 (UTC)

Andy (net__space@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, esquel@sommarskog.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp


Relevant Pages

  • One column is nullable on the composite key
    ... ALTER TABLE TableA ADD ... SQL Server does not allow having a composite PK with one nullable column: ...
    (microsoft.public.sqlserver.programming)
  • Re: Most efficient way to process this set of records??? (Bill Vaughn)
    ... I thought Bill's books were strictly for SQL Server. ... What I mean by "Composite" record is a group of columns from several ... Use datarelation between a master to the new detail (as opposed to ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Target number of database reads per user
    ... Yeah, just to echo what Andy is saying, if you want to have the question ... If you can post code/database design we could all take a look. ... Pro SQL Server 2000 Database Design - ...
    (microsoft.public.sqlserver.programming)
  • Re: Most efficient way to process this set of records??? (Bill Vaughn)
    ... I am more a dotNet guy than a SQL server guy, this seems for me typical a ... What I mean by "Composite" record is a group of columns from several ... Use datarelation between a master to the new detail (as opposed to ... procedure to setup command parameters and retrieve a master record, ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: Orderby on multiple fields in a list
    ... Year Surname TotalCost Volume ... 2003 Jim $14,377.41 61377 ... 2004 Andy $119,609.07 798193 ... Business Intelligence and not writing a query for SQL Server? ...
    (microsoft.public.sqlserver.programming)