473,396 Members | 2,010 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,396 software developers and data experts.

Unique Index for two columns in a table

Hi,

I would like to add a unique index that consists of two fields in a
table.
e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination
must be Unique.
Can anyone tell me the actual sql syntax to create this index?

Thanks,
June.
Jul 20 '05 #1
3 27446
Depending on whether you want a clustered or non-clustered index:

CREATE UNIQUE CLUSTERED INDEX index_name ON tbl_A (col1,col2)

or

CREATE UNIQUE NONCLUSTERED INDEX index_name ON tbl_A (col1,col2)
--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2
You might consider specifying a unique constraint instead of a unique
index since uniqueness is a characteristic of you data. SQL Server will
create a unique index to support a unique constraint so these are
functionally identical in terms of performance.

Unique constraint syntax:

ALTER TABLE tbl_A
ADD CONSTRAINT UN_tbl_A_1
UNIQUE (field1,field2)

Unique index syntax:

CREATE UNIQUE INDEX Index_tbl_A_1
ON tbl_A (field1,field2)

Also, either of the above indexes can be created as clustered or
non-clustered. See the Books Online for details.

--
Hope this helps.

Dan Guzman
SQL Server MVP

-----------------------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index....partmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

"June Moore" <ju******@yahoo.com.au> wrote in message
news:e5**************************@posting.google.c om...
Hi,

I would like to add a unique index that consists of two fields in a
table.
e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination
must be Unique.
Can anyone tell me the actual sql syntax to create this index?

Thanks,
June.

Jul 20 '05 #3
oj
create unique index _ix on tb_name(col1,col2)
--
-oj
Rac v2.2 & QALite!
http://www.rac4sql.net
"June Moore" <ju******@yahoo.com.au> wrote in message
news:e5**************************@posting.google.c om...
Hi,

I would like to add a unique index that consists of two fields in a
table.
e.g. tbl_A (field1,field2) -- field1 & field2 Indexed and combination
must be Unique.
Can anyone tell me the actual sql syntax to create this index?

Thanks,
June.

Jul 20 '05 #4

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

Similar topics

5
by: Kamil | last post by:
Hello What should I use for better perfomance since unique constraint always use index ? Thanks Kamil
2
by: Laurent | last post by:
DB2 8.1 ------- db2 => create table test (COL1 VARCHAR(10)) db2 => insert into test values ('A') db2 => insert into test values ('A ') db2 => insert into test values ('B') db2 => insert into...
4
by: deko | last post by:
I have a Make Table query that creates a fairly large table. The Make Table query populates the new table with one AutoNumber field (which is taken form another unrelated table as part of the...
10
by: deko | last post by:
I understand it's possible to make a composite Primary Key by holding down the control key and selecting multiple fields, then right-clicking and selecting Primary Key. But I've heard that's not a...
10
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
3
by: vj_dba | last post by:
Hi Group, I have a Primary key in my table. It's clear Primary key wont allow duplicates, this primary key creates one index for retrival. Suppose if my table is having a Unique index also....
4
by: p175 | last post by:
What are the advantages / disadvantages of having primary keys in lieu of ordinary indexes ? If there are no foreign relationships defined, is it better to have a two column index allowing reverse...
7
by: rAinDeEr | last post by:
I have a Parts Table with the following structure.. I need to decide which makes the primary key of the table. I cant have any sequences nor can I add any new fields in this Table. Table...
1
by: rdsandy | last post by:
Hi, I have a table Risk, a table Mitigation and a table RiskArchive. I am taking the RiskID, Criticality and MitigationPlan fields from Risk, and MitigationActionID from Mitigation and inserting...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.