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; --> On 26 Nov, 10:04, "Bob Bedford" <b...@bedford.com> wrote: > Hello, > > I've an old very simple table that ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

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

On 26 Nov, 10:04, "Bob Bedford" <b...@bedford.com> wrote:
> 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.


SELECT
*
FROM simple_table
ORDER BY savedate
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 02-28-2008, 11:30 AM
Willem Bogaerts
 
Posts: n/a
Default Re: Alter table add autoincrement

> 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/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13 (permalink)  
Old 02-28-2008, 11:30 AM
Michael Martinek
 
Posts: n/a
Default Re: Alter table add autoincrement


> 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)


The idea that comes to mind is to create a new table like the old one,
but add your AUTO_INCREMENT column. Then write a script or something
to:

SELECT * FROM historytable ORDER BY date DESC

INSERT INTO newtable VALUES (NULL, $user, $date)

Shutdown MySQL, rename your old tables to something else.. rename the
new tables to historytable. Start MySQL. You should now have an
auto_increment column that's up to date, and the entries ordered.
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 03:02 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