|
-
Jul 29th, 2008, 01:31 AM
#1
Thread Starter
Fanatic Member
[2005] unique columns
Code:
create table mytable
(
recordId int identity(1,1) primary key,
col2 int,
col3 int,
col4 int
)
basically i created the primary key recordID which will be unique in value. now how will i be able to set col2 and col3 have unique values too.
-
Jul 29th, 2008, 08:59 AM
#2
Re: [2005] unique columns
unique in what way... composite key, clustered index on all 3 fields, all counting up in increments of 1? etc...
for example, with the primary key recordID being unique, you will have records starting with:
1...
2...
3... etc
as the other fields are INT, what would you expect them to count up the same way?
e.g.
1,1,1
2,2,2
3,3,3... etc
Could you give me a quick example of how the table would look.
ps. here is an example of the first field being an IDENTITY field, with the two other fields being part of the same primary key making up a composite key.
Code:
CREATE TABLE [dbo].[Table_1]
(
[recordId] [int] IDENTITY(1,1) NOT NULL,
[col2] [int] NOT NULL,
[col3] [int] NOT NULL,
[col4] [int] NULL,
CONSTRAINT [PK_Table_1] PRIMARY KEY CLUSTERED
(
[recordId] ASC,
[col2] ASC,
[col3] ASC
) ON [PRIMARY]
) ON [PRIMARY]
Last edited by kevchadders; Jul 29th, 2008 at 09:07 AM.
-
Jul 29th, 2008, 01:58 PM
#3
Thread Starter
Fanatic Member
Re: [2005] unique columns
hi, basically the col2 and col3 will accept input values from user. i can be a random int nos but again it must be unique.
-
Jul 30th, 2008, 03:01 AM
#4
Re: [2005] unique columns
Ok.
I have created a quick table, with a identity field on the first row, and a UNIQUE INDEX on col2 and col3.
Run this code in the query window and test to see if that's what your after
Code:
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[Table_1]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [dbo].[Table_1](
[col1] [int] IDENTITY(1,1) NOT NULL,
[col2] [int] NULL,
[col3] [int] NULL
) ON [PRIMARY]
END
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[dbo].[Table_1]') AND name = N'col2')
CREATE UNIQUE NONCLUSTERED INDEX [col2] ON [dbo].[Table_1]
(
[col2] ASC
) ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[dbo].[Table_1]') AND name = N'col3')
CREATE UNIQUE NONCLUSTERED INDEX [col3] ON [dbo].[Table_1]
(
[col3] ASC
) ON [PRIMARY]
-
Jul 31st, 2008, 06:52 AM
#5
Thread Starter
Fanatic Member
Re: [2005] unique columns
perfect! i never expect the tsql be very long... is there any way around to make this shorter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|