Forum Moderators: coopster

Message Too Old, No Replies

primary key and another field the same.... how to?

mysql question

         

HeadBut

4:13 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Maybe this is the wrong forum, but maybe the answer is in php.
I want to add a record to an mysql table and I want one of the fields to be the same as the auto_increment field. Do I need to do two sql statements?

Salsa

4:36 am on Dec 2, 2004 (gmt 0)

10+ Year Member



Make an insert into your table that has the auto_increment field. Then call:

$auto_increment_id = mysql_insert_id();

That will hold the auto_increment value from your most recent insert, which you can then insert into other tables for indexing.

ergophobe

9:40 am on Dec 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Make sure to read the manual on this as it may be better to use the native mysql command to get this value.

[us2.php.net...]

coopster

12:30 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Storing duplicate data is a waste of resource. Why would you want to store duplicate data in a single row anyway?

HeadBut

3:52 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



The duplicate field is called the ADID field. We use it to push items to the top in a sort that is already using the prime key to find the newest items. The ADID field is editable (Not like the prime key) and this allows me to push an item to the top of the "Newest" list... kinda a cheat of the newest items. If you have another sugestion I'm game. I'd really like to have two auto_increment fields and make one the prime key and the other one the editable ADID.

coopster

4:50 pm on Dec 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK. That's what I figured it might be, but still had to ask and make sure ;)

Can I ask another question though? I understand the "cheating" part of this, at least I understand exactly the usage you are describing and I often call them "sequence" fields. Basically they allow you to change the value so that it can be displayed in a certain order outside of any other logic except that a human being wants it sorted that way. Most often these "sequence" fields exist as another key column in the row. Something along the lines of...

+-------+--------+-----+----------+ 
¦itemID ¦ iTypes ¦ Seq ¦ itemName ¦
+-------+--------+-----+----------+
¦100001 ¦ Fruits ¦ 100 ¦ Banana ¦
¦100002 ¦ Fruits ¦ 200 ¦ Apple ¦
¦100003 ¦ Fruits ¦ 300 ¦ Cherry ¦
¦100004 ¦ Veggie ¦ 200 ¦ Turnip ¦
¦100005 ¦ Veggie ¦ 300 ¦ Potato ¦
¦100006 ¦ Veggie ¦ 100 ¦ Carrot ¦
+-------+--------+-----+----------+
...where a
SELECT * FROM table WHERE iTypes = 'Fruits' ORDER BY itemName;
would give us an alphabetical listing of the 'Fruits' items but the "maintainer-of-order" has the option of sorting it as they see fit, obviously enjoying bananas much more than apples -- and cherries even less, in this case.

It's fairly easy to resequence rows in this case because we deal with a particular subset of information, Fruits or Veggie for example. But if there were no secondary key and this table gets large, that is quite a bit of "resequencing" going on when the "maintainer-of-order" decides the rows are going to be different.

How are you approaching that, or am I way off target here?

HeadBut

5:00 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



We use this field to display items people have for sale. The 10 newest items first on our main.php page. The ADID is just used to cheat a little now and then. The ADID containing the same number as the prime key also allows us to maintain a fair mix of "newest' and ADID pushed items. Seems to have worked good in the past. We have just rewritten this site from CDML and have several more to convert. I'd be glad to show you the site, but I think we are not supposed to put the urls in posts for security reasons. Message me if you want to see it.

HeadBut

5:02 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



I think to more accuratly answer your question... All I ever have to worry about is the last ten new items. When I want to push an item I just change the ADID to push it to the top. I never have to resequence the numbers cause I only ever care about the top 10.

coopster

2:55 am on Dec 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, I understand now. And sorry for 20 questions but I just wanted to make sure that I understood where you were headed.

You cannot write a row out and AUTO_INCREMENT more than one column at a time because MySQL allows only one AUTO_INCREMENT column per table. It must be indexed and it cannot have a DEFAULT value. This all happens when the table is created [dev.mysql.com]. That's simply a restriction on the feature itself.

So the next question might be, as it seems you are asking here, "can I apply that AUTO_INCREMENT column value to another column?" The answer is yes, of course, but not until after the AUTO_INCREMENT value has been set, which occurs after the row has been created with an INSERT statement. You see, that is when the AUTO_INCREMENT value gets updated. You can see this if you were to

SHOW TABLE STATUS LIKE 'table';
and look at the
Auto_increment
column that MySQL maintains for the table.

How then would you get the same value in the other column that we have been discussing here? Just like Salsa and ergophobe have directed, and exactly as you suspected, an additional query statement. What's nice is that MySQL has made it easy for you though with LAST_INSERT_ID() [dev.mysql.com], as mentioned by ergophobe. Here's a sample to show you how you can use it:

CREATE TABLE sequence ( 
sID INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
sequence INTEGER UNSIGNED NOT NULL,
value CHAR(10),
PRIMARY KEY (sID)
);
--
INSERT INTO sequence (value) VALUES ('Value1');
UPDATE sequence SET sequence = LAST_INSERT_ID() WHERE sID = LAST_INSERT_ID();
INSERT INTO sequence (value) VALUES ('Value2');
UPDATE sequence SET sequence = LAST_INSERT_ID() WHERE sID = LAST_INSERT_ID();
INSERT INTO sequence (value) VALUES ('Value3');
UPDATE sequence SET sequence = LAST_INSERT_ID() WHERE sID = LAST_INSERT_ID();
INSERT INTO sequence (value) VALUES ('Value4');
UPDATE sequence SET sequence = LAST_INSERT_ID() WHERE sID = LAST_INSERT_ID();
INSERT INTO sequence (value) VALUES ('Value5');
UPDATE sequence SET sequence = LAST_INSERT_ID() WHERE sID = LAST_INSERT_ID();
--
SELECT * FROM sequence;
--
+-----+----------+--------+
¦ sID ¦ sequence ¦ value ¦
+-----+----------+--------+
¦ 1 ¦ 1 ¦ Value1 ¦
¦ 2 ¦ 2 ¦ Value2 ¦
¦ 3 ¦ 3 ¦ Value3 ¦
¦ 4 ¦ 4 ¦ Value4 ¦
¦ 5 ¦ 5 ¦ Value5 ¦
+-----+----------+--------+