2

I'm not sure how to formulate the title correctly. I'll try to elaborate, and make it understandable here.

I have a table with an auto_incremented ID (Primary Key). In the same table I have a guid-column. This guid is going to be an URL. So the guid will typically be something like http://www.example.com/path/path/ID

My question is this: Because the ID is auto-generated on INSERT, and I don't manually assign this, I dont know beforehand what the ID is going to be. How can I then assign the guid-value in the insert-statement, if I'm dependant on having the ID?

The solution I have at the moment is doing it with two statements:

INSERT INTO table (ID, guid) VALUES (null, null); 

I will now have a row that looks something like this:

------------- | ID | GUID | ------------- | 1 | null | ------------- 

I can then in another statement update this row with a statement like this;

UPDATE table SET guid = ID WHERE ID = 1; 

(Might be off-syntax, but you get the point)

Is there any way to do this in "one go"? Where I can set the GUID based off of what the auto_incremented ID is going to be? So it would be something like this

INSERT INTO table (ID, guid) VALUES (null, ID); 

I hope my question is understandable! I will clarify if it's still unclear. I don't know any better way to explain my question.

3
  • If the id has some kind of meaning beyond the scope of the database, then you shouldn't really be using auto_increment for this. AUTO_INCREMENT is great for surrogate keys, but that's about it. That said, if we have the path and the PK, what do we need the GUID for? Commented Dec 3, 2013 at 17:42
  • The table is a pre-made wordpress table (wp_posts). The ID is being used as a unique identifier for each row, and it's also being used as part of the URL that's being stored in the GUID. Apart from this, the ID does not have a usage, or a meaning. Commented Dec 3, 2013 at 17:45
  • 1
    As you have defined it, ID will always be equal to GUID. Therefore the GUID field will always be redundant and unnecessary. Wouldn't it be better to develop a path, (assuming PHP) like $guid = "http://www.example.com/path/path/".$row['id']; where $row['id'] is the result from your query? Commented Dec 3, 2013 at 17:46

1 Answer 1

1

If the PK is autogenerated you don't need it in your insert query.

insert into table (guid) values (your guid) 

You of course have to generate the guid on your own and there are lots of ways to do that.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.