MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old March 8th, 2004, 07:56 PM
Bothware:Hawk's Avatar
Bothware:Hawk Bothware:Hawk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Soton, UK
Posts: 43 Bothware:Hawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to Bothware:Hawk
Best way to implement auto-increment column that can take duplicates ?

I have a table that has 2 columns composing the primary key - 1 of them I need to autoincrement if not specified, else make a duplicate entry - the other will always be specified.

example:

MainId : int
Id2 : int
SomeText : nvarchar

Primary Key is (MainId,Id2)

Examples to demonstarte functionality I need:

INSERT INTO ExampleTable (Id2,SomeText) VALUES (2414,"Hello")

should give
1 | 2414 | Hello

INSERT INTO ExampleTable (Id2,SomeText) VALUES (93284,"Something")

should then result in:
1 | 2414 | Hello
2 | 93284 | Something

INSERT INTO ExampleTable (MainId,Id2,SomeText) VALUES (1,2429,"DifText")

should give
1 | 2414 | Hello
1 | 2429 | DifText
2 | 93284 | Something

INSERT INTO ExampleTable (Id2,SomeText) VALUES (7474747,"Random")

should give
1 | 2414 | Hello
1 | 2429 | DifText
2 | 93284 | Something
3 | 7474747 |Random

Right- thats enough examples, how shall I implement it?

( the database table in question is unlikely to grow beyond 6000 rows in next 6 months & will not be quiried too often - most records will probably only be read back max 10 times each ever ! )

The database is being used by some webservices written in C#.

Cheers in advance.

Reply With Quote
  #2  
Old March 8th, 2004, 09:23 PM
mat41 mat41 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sydney Australia
Posts: 17 mat41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m
Reputation Power: 0
Send a message via AIM to mat41 Send a message via Yahoo to mat41
mmmm, you have a abnormal looking DB structure. A table can only have one primary key.

you can concantenate two fields in a table to create a unique identifyer (is this what you mean?)

An auto incremeted primary key can not under any circumstances have a duplicate entry

your intended statement:
INSERT INTO ExampleTable (MainId,Id2,SomeText) VALUES (1,2429,"DifText")

Which ever field is your primary key (can only be one remember) does not need to be referenced in the statement. Auto incrementation means just that -you can not asign a value, this happens automaticly. If MainId was your incrementing primary key, you would:
INSERT INTO ExampleTable (Id2,SomeText) VALUES (2429,"DifText")

I hope this has been helpful.

Reply With Quote
  #3  
Old March 8th, 2004, 10:14 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,344 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 26 m 34 sec
Reputation Power: 891
the primary key can be composite, i.e. more than one column

Bothware:Hawk, the way you would implement it is exactly the way you wrote it

there is a wee trick to this, though:
Code:
create table ExampleTable 
( MainId  int identity not null
, Id2     int not null
, SomeText  varchar(11)
, Primary Key (MainId,Id2)
)
INSERT INTO ExampleTable (Id2,SomeText) VALUES (2414,'Hello')
INSERT INTO ExampleTable (Id2,SomeText) VALUES (93284,'Something')

select * from ExampleTable

MainId Id2   SomeText
1     2414   Hello
2     93284  Something

INSERT INTO ExampleTable (MainId,Id2,SomeText) VALUES (1,2429,'DifText')

Error: Cannot insert explicit value for identity column 
in table 'ExampleTable' when IDENTITY_INSERT is set to OFF. 
(State:23000, Native Code: 220)

okay, thanks, that was actually a very helpful error message, so let's try setting it...
Code:
set identity_insert ExampleTable on

INSERT INTO ExampleTable (MainId,Id2,SomeText) VALUES (1,2429,'DifText')

select * from ExampleTable

MainId Id2   SomeText
1     2414   Hello
1     2429   DifText
2     93284  Something

it works!
__________________
r937.com | rudy.ca

Reply With Quote
  #4  
Old March 8th, 2004, 10:58 PM
mat41 mat41 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Location: Sydney Australia
Posts: 17 mat41 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 m
Reputation Power: 0
Send a message via AIM to mat41 Send a message via Yahoo to mat41
Bothware:Hawk
r937

My apologies for mis informing you on your post. I am amazed at the ability to physicaly create a composite primary key. I have always created tables/relationships etc using the enterprize manager GUI. Whe you select a field as a primary key then try to select another it re asigns and moves the little yellow key (visually telling me the first selected key has been replaced by the most recent selection)

However running your create script, then viewing in enterprize manager show a key against each of the fields (in design view) achieving what I claimed as 'not possible'

Once again sorry bout that
Matt

Reply With Quote
  #5  
Old March 9th, 2004, 10:13 AM
Bothware:Hawk's Avatar
Bothware:Hawk Bothware:Hawk is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Soton, UK
Posts: 43 Bothware:Hawk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via ICQ to Bothware:Hawk
Thumbs up

Thanks.

I've been using Enterprise Manager to setup all my tables etc. you can create composite primary keys by right clicking and choosing "Indexes/Keys...".

Hmmm .. Enterprise Manager seems to provide absolutey no way to set identity_insert -- does anyone know where the option is hiding? -- it would make life easier if I could do it graphically in the future.

EDIT : just read this :
Quote:
Remarks
At any time, only one table in a session can have the IDENTITY_INSERT property set to ON. If a table already has this property set to ON, and a SET IDENTITY_INSERT ON statement is issued for another table, Microsoft® SQL Server™ returns an error message that states SET IDENTITY_INSERT is already ON and reports the table it is set ON for.

If the value inserted is larger than the current identity value for the table, SQL Server automatically uses the new inserted value as the current identity value.

The setting of SET IDENTITY_INSERT is set at execute or run time and not at parse time.

Permissions
Execute permissions default to the sysadmin fixed server role, and the db_owner and db_ddladmin fixed database roles, and the object owner.
-- seems like a flawed implementation - surely it should be permanantly either set on or off ?, this is really not what I would expect - unless I am misunderstanding?????

Last edited by Bothware:Hawk : March 9th, 2004 at 10:44 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Best way to implement auto-increment column that can take duplicates ?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway