Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 11th, 2004, 05:10 PM
Authorized User
 
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hlchuah77
Default Composite key question

Hi, everybody, does anyone know whether composite key is allowed in MS SQL Server 2000 not?

Normally the database table requires one and only one key, i.e. primary key. For my case, by using only one primary key does not make my tuple unique.

Pls look at the small example below:

Table : ApartmentBlock
Field01 : BlockName
Field02 : ApartmentName

BlockName ApartmentName
M1 Court 7
M2 Court 7
M1 Court 8
M2 Court 8

So, it is very clear that by using only BlockName as primary key is not sufficient to make the tuple unique. Thus, I have to enforce both BlockName and ApartmentName so to make the tuple unique, but I don't know how it is done in SQL Server 2000.

Can anyone pls help me on this, thank you.


 
Old October 11th, 2004, 10:03 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to jemacc
Default

you must create a unique index constraint ;since you already have your table created use the following:

alter apartmentblock
add constraint
ck_aptblock
check(blockname,apartmentname)

From Books online
UNIQUE Constraints
You can use UNIQUE constraints to ensure that no duplicate values are entered in specific columns that do not participate in a primary key. Although both a UNIQUE constraint and a PRIMARY KEY constraint enforce uniqueness, use a UNIQUE constraint instead of a PRIMARY KEY constraint when you want to enforce the uniqueness of a column, or combination of columns, that is not the primary key.

Multiple UNIQUE constraints can be defined on a table, whereas only one PRIMARY KEY constraint can be defined on a table.

Also, unlike PRIMARY KEY constraints, UNIQUE constraints allow the value NULL. However, as with any value participating in a UNIQUE constraint, only one NULL value is allowed per column.

A UNIQUE constraint can also be referenced by a FOREIGN KEY constraint


Jaime E. Maccou
Applications Analyst
 
Old October 13th, 2004, 11:58 AM
Authorized User
 
Join Date: Jun 2004
Posts: 99
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to hlchuah77
Default

Thank you very much, jemacc.

 
Old October 19th, 2004, 05:24 AM
Friend of Wrox
 
Join Date: Sep 2004
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Anantsharma Send a message via Yahoo to Anantsharma
Default

HI,

Yes SQL Server supports composite keys. U can create a single Primary Key composite upto 16 colums max.

Offcourse u should create a Primary key combining both the colums u want.

As the easiesr way In Enterprise Manage, Rclik on the Table u want and cick Design. Hold the control Key pressed and click one by one the colums u want to invlove in composite key. Now Click on the Set Primary key button on tool bar ( u can see an icon of Key).

U can also use alter table T-SQL Statement.

Hope this helps.

B. Anant





Similar Threads
Thread Thread Starter Forum Replies Last Post
Composite Foreign Key bijgupt SQL Server 2005 6 May 4th, 2007 08:46 AM
delete a composite key record from database abhi_loveu2002 ASP.NET 2.0 Basics 0 December 18th, 2006 02:12 AM
delete a composite key record abhi_loveu2002 ASP.NET 2.0 Basics 0 December 18th, 2006 02:09 AM
XSD composite key constraint aldwinenriquez XML 2 August 15th, 2005 04:20 AM
Composite primary key evandromoreno Dreamweaver (all versions) 0 July 16th, 2004 02:12 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.