MySQL generate varchar type primary key sort

Source: Internet
Author: User
Tags uuid

Generating a 20-bit primary key with a UUID

SELECT Left (REPLACE (UUID (), '-', '), and () from DUAL

Another way:

Because there are letters in the database

Remove letters when sorting, re-fetch maximum, and add 1 to new primary key

SELECT ifnull (dept_id,0) +1 from Base_dept ORDER by dept_id+0 DESC LIMIT 0,1

Or

Use the MySQL function Cast/convert:

MySQL provides us with two types of conversion functions: cast and convert, how can we let go of the ready-made stuff?

The CAST () and CONVERT () functions can be used to get a value of one type and produce a value of another type.
This type can be one of the following values:
binary[(N)]
char[(N)]
DATE
Datetime
DECIMAL
Signed [INTEGER]
Time
UNSIGNED [INTEGER]

So we can also use cast to solve the problem:

as signed) DESC LIMIT 0, 1;

You can also use convert to fix this problem:

SELECT ifnull (dept_id,0) +1 from Base_dept ORDER by CONVERT (dept_id,signed) DESC LIMIT 0, 1;

MySQL generate varchar type primary key sort

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.