yc2
July 31st, 2008, 01:42 PM
Starting to try out MySQL (using php) I find that I haven't understood the basics of it.
I have a database. I want to add a column to it. The column should contain unique integer numbers in ascending order according to alphabetical order of another column.
Sorry not clear maybe, I want to have integer numbers like this:
3 John Doe
1 Betty Boop
4 Carl Lewis
5 Ronald Reagan
2 Bill Clinton(Numbers assigned according to last name of person. 3 columns in this db.)
I haven't understood the indexing features of MySQL, maybe there is already a feature to do this.
I have found out how to LIST in alphabetical order, but not how to assign numbers.
$str= "SELECT * FROM person ORDER BY LastName";
mysql_query($str);
Is there a reasonably simple way of setting this rank/order?
Thanks :)
EDIT: If I skip the demand of alphabetical order, is there some simple way of introducing a new column with a unique key?
EDIT: Skipping the demand of alphabetical order, I found this on the net, it works:ALTER TABLE person ADD id int( 11 ) PRIMARY KEY AUTO_INCREMENT NOT NULL (However, if I delete data, the id that are no more used will not be reused by mysql. It will just keep incrementing and leave unused ID numbers. If I make a NEW column like this it will address the problem of leaving holes in the series, but the alphabetical problem I don't know how to solve. Maybe I'm old-fashioned and haven't understood the advantages of relational databases? I thought row-numbering was useful?)
I have a database. I want to add a column to it. The column should contain unique integer numbers in ascending order according to alphabetical order of another column.
Sorry not clear maybe, I want to have integer numbers like this:
3 John Doe
1 Betty Boop
4 Carl Lewis
5 Ronald Reagan
2 Bill Clinton(Numbers assigned according to last name of person. 3 columns in this db.)
I haven't understood the indexing features of MySQL, maybe there is already a feature to do this.
I have found out how to LIST in alphabetical order, but not how to assign numbers.
$str= "SELECT * FROM person ORDER BY LastName";
mysql_query($str);
Is there a reasonably simple way of setting this rank/order?
Thanks :)
EDIT: If I skip the demand of alphabetical order, is there some simple way of introducing a new column with a unique key?
EDIT: Skipping the demand of alphabetical order, I found this on the net, it works:ALTER TABLE person ADD id int( 11 ) PRIMARY KEY AUTO_INCREMENT NOT NULL (However, if I delete data, the id that are no more used will not be reused by mysql. It will just keep incrementing and leave unused ID numbers. If I make a NEW column like this it will address the problem of leaving holes in the series, but the alphabetical problem I don't know how to solve. Maybe I'm old-fashioned and haven't understood the advantages of relational databases? I thought row-numbering was useful?)