Unix Technical Forum

Alter table add autoincrement

This is a discussion on Alter table add autoincrement within the MySQL forums, part of the Database Server Software category; --> Hello, I've an old very simple table that was only used to save datas. Now this table needs a ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 11:30 AM
Bob Bedford
 
Posts: n/a
Default Alter table add autoincrement

Hello,

I've an old very simple table that was only used to save datas. Now this
table needs a primary key. In order to add one, I've to sort the results by
savedate.

How to do so ? Actually when I do a select the order is taken on the first
field that I absolutely don't want to use for sorting.

Thanks for helping.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 11:30 AM
Captain Paralytic
 
Posts: n/a
Default Re: Alter table add autoincrement

On 26 Nov, 10:42, Willem Bogaerts
<w.bogae...@kratz.maardanzonderditstuk.nl> wrote:
> > I've an old very simple table that was only used to save datas. Now this
> > table needs a primary key. In order to add one, I've to sort the results by
> > savedate.

>
> Why? Is your date field the field that you want to "promote" to primary
> key? or do you want to add an autonumber column?
>
> Primary keys should have nothing to do with the values of the records
> and should not even be abused for sorting. The primary key exists to
> give your record a unique identity that exceeds its lifespan (that is
> why a primary key value is not reclaimed when you delete the record).
>
> If you want to change the default order of the table, you can change it:
>
> ALTER TABLE <table name> ORDER BY <the date field>;
>
> Best regards,
> --
> Willem Bogaerts
>
> Application smith
> Kratz B.V.http://www.kratz.nl/


Not quite default order:
"ORDER BY enables you to create the new table with the rows in a
specific order. Note that the table does not remain in this order
after inserts and deletes."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 11:30 AM
Bob Bedford
 
Posts: n/a
Default Re: Alter table add autoincrement


"Willem Bogaerts" <w.bogaerts@kratz.maardanzonderditstuk.nl> a écrit dans le
message de news: 474aa330$0$240$e4fe514c@news.xs4all.nl...
>> I've an old very simple table that was only used to save datas. Now this
>> table needs a primary key. In order to add one, I've to sort the results
>> by
>> savedate.

>
> Why? Is your date field the field that you want to "promote" to primary
> key? or do you want to add an autonumber column?
>
> Primary keys should have nothing to do with the values of the records
> and should not even be abused for sorting. The primary key exists to
> give your record a unique identity that exceeds its lifespan (that is
> why a primary key value is not reclaimed when you delete the record).
>
> If you want to change the default order of the table, you can change it:
>
> ALTER TABLE <table name> ORDER BY <the date field>;
>
> Best regards,
> --
> Willem Bogaerts

Hello Willem, thanks for replying.

I want to add an autonumber column but set the actual record autonumber
based on date field order.
I mean actually I've this when I do a select * from historytable:
user date
1 2006-10-10
2 2006-10-15
3 2006-09-01
3 2006-09-10

if I do a select, the order is based on the user number. So adding an
auto_increment field to this table will certainly add like show in the
example above. Instead I'd like the sorting by date to add the new
autoincrement values:
AutoIncrement user date
1 3 2006-09-01
2 3 2006-09-10
3 1 2006-10-10
4 2 2006-10-15

So I've to sort by date to set the auto_increment value.
Like: alter table historytable add auto_increment on (select * from
historytable order by date asc)
How to do so ?
Bob


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-28-2008, 11:30 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Alter table add autoincrement

Bob Bedford wrote:
> "Willem Bogaerts" <w.bogaerts@kratz.maardanzonderditstuk.nl> a écrit dans le
> message de news: 474aa330$0$240$e4fe514c@news.xs4all.nl...
>>> I've an old very simple table that was only used to save datas. Now this
>>> table needs a primary key. In order to add one, I've to sort the results
>>> by
>>> savedate.

>> Why? Is your date field the field that you want to "promote" to primary
>> key? or do you want to add an autonumber column?
>>
>> Primary keys should have nothing to do with the values of the records
>> and should not even be abused for sorting. The primary key exists to
>> give your record a unique identity that exceeds its lifespan (that is
>> why a primary key value is not reclaimed when you delete the record).
>>
>> If you want to change the default order of the table, you can change it:
>>
>> ALTER TABLE <table name> ORDER BY <the date field>;
>>
>> Best regards,
>> --
>> Willem Bogaerts

> Hello Willem, thanks for replying.
>
> I want to add an autonumber column but set the actual record autonumber
> based on date field order.
> I mean actually I've this when I do a select * from historytable:
> user date
> 1 2006-10-10
> 2 2006-10-15
> 3 2006-09-01
> 3 2006-09-10
>
> if I do a select, the order is based on the user number. So adding an
> auto_increment field to this table will certainly add like show in the
> example above. Instead I'd like the sorting by date to add the new
> autoincrement values:
> AutoIncrement user date
> 1 3 2006-09-01
> 2 3 2006-09-10
> 3 1 2006-10-10
> 4 2 2006-10-15
>
> So I've to sort by date to set the auto_increment value.
> Like: alter table historytable add auto_increment on (select * from
> historytable order by date asc)
> How to do so ?
> Bob
>
>
>


No, if you just do a SELECT, the returned order is not defined. It's
just how the rows happen to occur in the database. If you want a
specific order you must use the ORDER BY clause.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-28-2008, 11:30 AM
Peter H. Coffin
 
Posts: n/a
Default Re: Alter table add autoincrement

On Mon, 26 Nov 2007 13:45:45 +0100, Bob Bedford wrote:

> "Willem Bogaerts" <w.bogaerts@kratz.maardanzonderditstuk.nl> a écrit
> dans le message de news: 474aa330$0$240$e4fe514c@news.xs4all.nl...
>
>>> I've an old very simple table that was only used to save datas. Now
>>> this table needs a primary key. In order to add one, I've to sort
>>> the results by savedate.

>>
>> Why? Is your date field the field that you want to "promote" to
>> primary key? or do you want to add an autonumber column?
>>
>> Primary keys should have nothing to do with the values of the records
>> and should not even be abused for sorting. The primary key exists to
>> give your record a unique identity that exceeds its lifespan (that is
>> why a primary key value is not reclaimed when you delete the record).
>>
>> If you want to change the default order of the table, you can change
>> it:
>>
>> ALTER TABLE <table name> ORDER BY <the date field>;
>>
>> Best regards,
>> --
>> Willem Bogaerts

> Hello Willem, thanks for replying.
>
> I want to add an autonumber column but set the actual record autonumber
> based on date field order.


Again, why? Specifically, what do you feel you would gain by doing this?

--
Crowds want to beat, journalists deserve to be beaten. Where lies
the problem?
-- Lars Syrstad
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-28-2008, 11:30 AM
Bob Bedford
 
Posts: n/a
Default Re: Alter table add autoincrement

> No, if you just do a SELECT, the returned order is not defined. It's just
> how the rows happen to occur in the database. If you want a specific
> order you must use the ORDER BY clause.


I know but my original question is:

how to add an autoincrement value to a table assigning the new autoincrement
values in date order on existing records.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-28-2008, 11:30 AM
Bob Bedford
 
Posts: n/a
Default Re: Alter table add autoincrement

>> I want to add an autonumber column but set the actual record autonumber
>> based on date field order.

>
> Again, why? Specifically, what do you feel you would gain by doing this?
>

The datas have been created using the date of the day (date field). Since
this is the natural order they have been created and since we will use the
new AutoIncrement field for retrieving datas (where autonumber > ...) we
will it this way.

Bob


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-28-2008, 11:30 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Alter table add autoincrement

Bob Bedford wrote:
>> No, if you just do a SELECT, the returned order is not defined. It's just
>> how the rows happen to occur in the database. If you want a specific
>> order you must use the ORDER BY clause.

>
> I know but my original question is:
>
> how to add an autoincrement value to a table assigning the new autoincrement
> values in date order on existing records.
>
>
>


And the point is - you can't guarantee the order unless you use ORDER BY.

But your entire premise is flawed. The autoincremement column is to
uniquely identify the row. It show have no dependence on any data.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-28-2008, 11:30 AM
Jerry Stuckle
 
Posts: n/a
Default Re: Alter table add autoincrement

Bob Bedford wrote:
>>> I want to add an autonumber column but set the actual record autonumber
>>> based on date field order.

>> Again, why? Specifically, what do you feel you would gain by doing this?
>>

> The datas have been created using the date of the day (date field). Since
> this is the natural order they have been created and since we will use the
> new AutoIncrement field for retrieving datas (where autonumber > ...) we
> will it this way.
>
> Bob
>
>
>


Bob.

Your assumption that that will work is incorrect. If you need to
retrieve data based on a date, then compare on the date field.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-28-2008, 11:30 AM
Peter H. Coffin
 
Posts: n/a
Default Re: Alter table add autoincrement

On Mon, 26 Nov 2007 15:47:26 +0100, Bob Bedford wrote:
>>> I want to add an autonumber column but set the actual record autonumber
>>> based on date field order.

>>
>> Again, why? Specifically, what do you feel you would gain by doing this?
>>

> The datas have been created using the date of the day (date field). Since
> this is the natural order they have been created and since we will use the
> new AutoIncrement field for retrieving datas (where autonumber > ...) we
> will it this way.


So you want to autoincrement by date order in order to retrieve in date
order. Why not select and order data by date?

--
67. No matter how many shorts we have in the system, my guards will be
instructed to treat every surveillance camera malfunction as a
full-scale emergency.
--Peter Anspach's list of things to do as an Evil Overlord
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 04:04 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com