User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 403,407 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,654 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 371 | Replies: 3
Reply
Join Date: Feb 2008
Posts: 9
Reputation: firebirds98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Multiple Identity Columns In SQL

  #1  
Feb 18th, 2008
I am new to SQL and I am making an asp.net page in which is a small airline service for a project for scool. For one of my tables I have:

AccountID, CustomerName, FrequentFlierMiles, FrequentFliernum for my column names. The acount AccountID column can not have duplicated values and FrequentFliernum can not have duplicated values and the only thing I could think from stopping that is through identity specification, the problem is you can only set one of the columns to have identity specification.

The AccountID and FrequentFliernum I would like to automatically increment by 1.

Right now my sql statement is
sql = "Insert Into Air_Customers VALUES(','" & txtname.Text & "','0','0')"
I would like that final '0' to automatically be incremented in the database and not have to manually put it in.


Anyhelp is appreciated. If this does not make sense I will try re-writing it.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Multiple Identity Columns In SQL

  #2  
Feb 18th, 2008
You cannot have two, however you can get around it three ways:

1. Make a separate table and relate the two. Each will have a unique identity column.

2. Do an update statement after inserting the values into the database, but this requires two hits to the database, which you should try to avoid.

3. Solution: Do a subquery that selects the highest/last entry for frequireflyernum, then increment it by one.
Example:
sql = "Insert Into Air_Customers (CustomerName, FrequentFlierMiles, FrequentFliernum) VALUES (@CustomerName, 0, COALESCE((SELECT TOP 1 FrequentFlierNum FROM Air_Customers ORDER BY FrequentFlierNum DESC), 0)+1)"
commandname.Parameters.AddWithValue( "@CustomerName", Trim(txtname.Text) )
Now what this will do is subquery the same database for the highest frequentfliernum and then add 1 to it. Now if it doesn't return anything, it will return the value "0", which when you add 1 to it, it becomes "1".

Learn to use parameters. they are very easy. The code I gave you uses parameters. It helps prevent sql injection, and poor formatting.
Reply With Quote  
Join Date: Feb 2008
Posts: 9
Reputation: firebirds98 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
firebirds98 firebirds98 is offline Offline
Newbie Poster

Re: Multiple Identity Columns In SQL

  #3  
Feb 18th, 2008
Thank you for your help!
Reply With Quote  
Join Date: Sep 2007
Posts: 1,057
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Multiple Identity Columns In SQL

  #4  
Feb 18th, 2008
No problem, let me know if you need further assistance.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:58 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC