0

In my database mysql table, there is one field ,It was split by auto_increment id and other str , For example:

insert into tableName (id, title, link, keyword) values (NULL, 'Title', 'http://www.domain.com/id', 'keyword');

How could that work? http://www.domain.com/id, this 'id' is the auto_increment id.

jianbing Ma
  • 355
  • 1
  • 3
  • 15
  • then just leave id,, insert into tableName (title, link, keyword) values ('Title', 'http://www.domain.com/id', 'keyword'); – Sruit A.Suk Jul 25 '15 at 14:18
  • You can use the DEFAULT when inserting into a primary key – Mihai Jul 25 '15 at 14:20
  • possible duplicate of [PHP mySQL - Insert new record into table with auto-increment on primary key](http://stackoverflow.com/questions/7492145/php-mysql-insert-new-record-into-table-with-auto-increment-on-primary-key) – Sruit A.Suk Jul 25 '15 at 14:22

2 Answers2

0

If id is auto_increment you don't need to set it on insert, just like this:

insert into tableName (title, link, keyword) values ('Title', 'http://www.domain.com/id', 'keyword');

In general if it auto incremented field, it can be your primary key and add other indexes as you need.

Eyal H
  • 991
  • 5
  • 22
0

Unfortunately, you can't make it automatically since mysql for auto increment fields, doesn't know beforehand the inserted id. You could update the row after it is inserted:

INSERT INTO tableName (title, keyword) VALUES('Title', 'keyword');

UPDATE tableName 
SET link = CONCAT('http://www.domain.com/', LAST_INSERT_ID())
WHERE id = LAST_INSERT_ID();
JuniorCompressor
  • 19,631
  • 4
  • 30
  • 57