473,473 Members | 1,419 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multiple auto increments in one column?

Is it possible to have multiple auto increments in one column? Say I
have two tables...

1. table 'messageboards' with fields ('id' , 'name')

2. table 'messagethreads' with fields ('id' , 'messagethread' ,
'messageboard_id')

The 'messagethreads.id' column could have multiple auto increments.
Then I could easily query a messageboard with ("SELECT * FROM
messagethreads WHERE messageboard_id = 1 ORDER BY id"). Each thread
would return an id starting from number 1. If MySQL does not support
multiple auto increments, I guess the only other solutions are...

- Create a new table for every 'messageboard' with an auto increment
column and join them to 'messagethreads' for a query. Id like to use
as few tables as possible though.

- Use transactions or table locking to generate the auto increment
value. Id like to not use either for performance reasons.

Anyone have a better solution? Im sure its been done? Thanks!

-Nick
Jul 19 '05 #1
12 5618
Nick wrote:
Is it possible to have multiple auto increments in one column? Say I
have two tables...

1. table 'messageboards' with fields ('id' , 'name')

2. table 'messagethreads' with fields ('id' , 'messagethread' ,
'messageboard_id')

The 'messagethreads.id' column could have multiple auto increments.
Then I could easily query a messageboard with ("SELECT * FROM
messagethreads WHERE messageboard_id = 1 ORDER BY id"). Each thread
would return an id starting from number 1. If MySQL does not support
multiple auto increments, I guess the only other solutions are...


Why would they need to start from number 1? What difference does it make
if they start from 543 or 43645? They will still be in correct order and
you will only get messagethreads for the correct messageboard, since you
use 'where messageboard_id = 1'.
Jul 19 '05 #2
Nick wrote:
Is it possible to have multiple auto increments in one column? Say I
have two tables...

1. table 'messageboards' with fields ('id' , 'name')

2. table 'messagethreads' with fields ('id' , 'messagethread' ,
'messageboard_id')

The 'messagethreads.id' column could have multiple auto increments.
Then I could easily query a messageboard with ("SELECT * FROM
messagethreads WHERE messageboard_id = 1 ORDER BY id"). Each thread
would return an id starting from number 1. If MySQL does not support
multiple auto increments, I guess the only other solutions are...


Why would they need to start from number 1? What difference does it make
if they start from 543 or 43645? They will still be in correct order and
you will only get messagethreads for the correct messageboard, since you
use 'where messageboard_id = 1'.
Jul 19 '05 #3
Nick wrote:
Is it possible to have multiple auto increments in one column? Say I
have two tables...

1. table 'messageboards' with fields ('id' , 'name')

2. table 'messagethreads' with fields ('id' , 'messagethread' ,
'messageboard_id')

The 'messagethreads.id' column could have multiple auto increments.
Then I could easily query a messageboard with ("SELECT * FROM
messagethreads WHERE messageboard_id = 1 ORDER BY id"). Each thread
would return an id starting from number 1. If MySQL does not support
multiple auto increments, I guess the only other solutions are...


Why would they need to start from number 1? What difference does it make
if they start from 543 or 43645? They will still be in correct order and
you will only get messagethreads for the correct messageboard, since you
use 'where messageboard_id = 1'.
Jul 19 '05 #4
Well (8 hours later) it turns out you can have multiple auto
increments in one column. Heres how...

Create a table with two or more columns. One column will be the auto
increment and the other column will designate which auto increment to
join. These two columns must be primary keys. Another caveat is that
you MUST create the auto increment column AFTER the designater column.
Heres an example...

--> create table test(a int not null, b int auto_increment not null,
primary key(a,b));

--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(3);
--> insert into test values(3);
--> insert into test values(3);

--> select * from test;

+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
+---+---+
Jul 19 '05 #5
Well (8 hours later) it turns out you can have multiple auto
increments in one column. Heres how...

Create a table with two or more columns. One column will be the auto
increment and the other column will designate which auto increment to
join. These two columns must be primary keys. Another caveat is that
you MUST create the auto increment column AFTER the designater column.
Heres an example...

--> create table test(a int not null, b int auto_increment not null,
primary key(a,b));

--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(3);
--> insert into test values(3);
--> insert into test values(3);

--> select * from test;

+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
+---+---+
Jul 19 '05 #6
Well (8 hours later) it turns out you can have multiple auto
increments in one column. Heres how...

Create a table with two or more columns. One column will be the auto
increment and the other column will designate which auto increment to
join. These two columns must be primary keys. Another caveat is that
you MUST create the auto increment column AFTER the designater column.
Heres an example...

--> create table test(a int not null, b int auto_increment not null,
primary key(a,b));

--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(1);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(2);
--> insert into test values(3);
--> insert into test values(3);
--> insert into test values(3);

--> select * from test;

+---+---+
| a | b |
+---+---+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
| 2 | 3 |
| 3 | 1 |
| 3 | 2 |
| 3 | 3 |
+---+---+
Jul 19 '05 #7
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick
Jul 19 '05 #8
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick
Jul 19 '05 #9
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick
Jul 19 '05 #10
Nick wrote:
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick


What ever works for you ;)

But if you print the numbers using a programming language, you don't
have to print the number you have in your variable. You can always take
something of it, or even use complitely different numbers.

So instead of printing the id value, you could just do something like:
for( i = 1; i < amount_of_messages; i++ )
{
print "messagenumber: " + i + "\n";
}

But yes, your solution it good also, perhaps even better if it doesn't
cause too much delay.
Jul 19 '05 #11
Nick wrote:
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick


What ever works for you ;)

But if you print the numbers using a programming language, you don't
have to print the number you have in your variable. You can always take
something of it, or even use complitely different numbers.

So instead of printing the id value, you could just do something like:
for( i = 1; i < amount_of_messages; i++ )
{
print "messagenumber: " + i + "\n";
}

But yes, your solution it good also, perhaps even better if it doesn't
cause too much delay.
Jul 19 '05 #12
Nick wrote:
Thanks for responding, I actually needed them to start from 1 so that
when someone views the first message of a message board, the message
starts at 1. It might be a little weird to start a new message board
with a thread starting from 15,234. The solution I posted seems to be
what I needed. What do you think? -Nick


What ever works for you ;)

But if you print the numbers using a programming language, you don't
have to print the number you have in your variable. You can always take
something of it, or even use complitely different numbers.

So instead of printing the id value, you could just do something like:
for( i = 1; i < amount_of_messages; i++ )
{
print "messagenumber: " + i + "\n";
}

But yes, your solution it good also, perhaps even better if it doesn't
cause too much delay.
Jul 19 '05 #13

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

Similar topics

0
by: Nick | last post by:
Is it possible to have multiple auto increments in one column? Say I have two tables... 1. table 'messageboards' with fields ('id' , 'name') 2. table 'messagethreads' with fields ('id' ,...
4
by: Olafur | last post by:
Hello all, I am a beginner with mysql, however installation was fine no problems and I have been trying the Consol Center trying to create new tables. I get two errors I can't get rid of. ...
3
by: Smitro | last post by:
Hey, I have a site that is about to go into production and it has a couple of tables that it uses that could get quite large once in full swing, most of them have a column called "id" and it...
2
by: Mike N. | last post by:
Hello- I have a database that uses an auto number field type that goes out of sync periodically. My customer gets a "cannot add record, number already in use" error message. I dump the records...
1
by: .Net Newbie | last post by:
I am relatively new to .Net and have been coding an intranet site for my employer for a couple of months. I am currently stuck coding in a text-editor called EditPlus without access to the VS.Net...
6
by: Alpha | last post by:
I retrieve a table with only 2 columns. One is a auto-generated primary key column and the 2nd is a string. When I add a new row to the dataset to be updated back to the database. What should I...
0
by: .Net Newbie | last post by:
I am relatively new to .Net and have been coding an intranet site for my employer for a couple of months. I am currently stuck coding in a text-editor called EditPlus without access to the VS.Net...
3
by: cmk128 | last post by:
Hi I want to select a single row called "row1" but let mysql auto append a NON-exist column call "row2" of it. And i want that non-exist column be auto-increment. how to? If i "select row1...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
2
by: paulyXvpf | last post by:
Hi All, FACTS: > I've created a VB.NET 2005 form, with a SQL Server 2000 backend > The form has about 30 fields that populate 30 columns in the SQL database > The form has mostly text feilds,...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.