0

For example,
there is a table articles,it has these fields:

id
title
content
slug

id field is auto-increment, the value starts from 1.

Question:
I want to set slug field to be auto-increment, and the value starts from 10000,how to do it?

zwl1619
  • 4,002
  • 14
  • 54
  • 110
  • 7
    Your `articles` table can have only one auto increment column. If we make `slug` auto increment, then `id` would lose that condition. – Tim Biegeleisen Sep 03 '17 at 10:29
  • 4
    You can use a trigger , to update slug field to ID + 10000 – sagi Sep 03 '17 at 10:35
  • 2
    If slug is simply ID+10000, whats the point in duplicating the data in the database? – Nigel Ren Sep 03 '17 at 10:41
  • you can have separate table with storing last autoincrement id for all your tables you want to have separate autoincrement field and use it to substitute in before insert trigger and set new value of autoincrement id. But honestly - I doubt you need it. What do you want to achieve ? What's the problem you are trying to resolve ? – Alex Kapustin Sep 03 '17 at 11:23
  • Please check this thread: https://stackoverflow.com/questions/469009/can-you-access-the-auto-increment-value-in-mysql-within-one-statement – jorgesimoes Sep 03 '17 at 11:53

1 Answers1

1

Why do you want 2 fields to be auto incremented? They are going to have the same values, even if one starts from 10000..

Example:

id | title | content | slug
---| ------|---------|------
 1 |                  10000
 2 |                  10001
 3 |                  10001
 . |                  .
 . |                  .
 n |                  n + 10000 - 1   

All you have to do is:

UPDATE articles SET slug = id + (10000 - 1) WHERE id = 'any id here..'
Vural
  • 8,666
  • 11
  • 40
  • 57