php phpmyadmin 中的自动增量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5665571/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:08:32  来源:igfitidea点击:

Auto increment in phpmyadmin

phpphpmyadmin

提问by JaYwzx Wong

I have an existing database using PHP, MySQL and phpMyAdmin.

我有一个使用 PHP、MySQL 和 phpMyAdmin 的现有数据库。

When users become a member on my website, I need the system to create a unique membership number for them using a five digit number. for e.g 83773. I guess it is like generating a random password except I only want numbers for my members. This ID number has to be unique to each member.

当用户成为我网站的会员时,我需要系统使用五位数字为他们创建一个唯一的会员编号。例如 83773。我想这就像生成一个随机密码,除了我只想要我的会员号码。此 ID 号对于每个成员必须是唯一的。

Would it be possible for me to set the primary key to auto_increment in my user table and set it to start at 10000 and then auto increment every time a member registers?

我是否可以在我的用户表中将主键设置为 auto_increment 并将其设置为从 10000 开始,然后在每次成员注册时自动递增?

Also, is there a maximum number that the primary key ID number would go up to?

另外,主键 ID 号是否有上限?

Is this a reliable and safe way to use the primary key ID number as a membership number?

这是将主键 ID 号用作会员号的可靠且安全的方法吗?

回答by Amit Bhagat

There are possible steps to enable auto increment for a column. I guess the phpMyAdmin version is 3.5.5 but not sure.

有一些可能的步骤可以为列启用自动增量。我猜 phpMyAdmin 版本是 3.5.5 但不确定。

Click on Table> Structuretab > Under ActionClick Primary (set as primary), click on Changeon the pop-up window, scroll left and check A_I. Also make sure you have selected Nonefor Defaultenter image description here

单击> 结构选项卡 > 在操作下单击主要(设置为主要),单击弹出窗口中的更改,向左滚动并选中A_I。还要确保您为默认选择了在此处输入图片说明

回答by Mala

In phpMyAdmin, navigate to the table in question and click the "Operations" tab. On the left under Table Options you will be allowed to set the current AUTO_INCREMENT value.

在 phpMyAdmin 中,导航到相关表并单击“操作”选项卡。在左侧的表选项下,您将被允许设置当前的 AUTO_INCREMENT 值。

回答by lethalMango

Just run a simple MySQL query and set the auto increment number to whatever you want.

只需运行一个简单的 MySQL 查询并将自动增量编号设置为您想要的任何值。

ALTER TABLE `table_name` AUTO_INCREMENT=10000

In terms of a maximum, as far as I am aware there is not one, nor is there any way to limit such number.

就最大值而言,据我所知,没有一个,也没有任何方法可以限制这样的数量。

It is perfectly safe, and common practice to set an id number as a primiary key, auto incrementing int. There are alternatives such as using PHP to generate membership numbers for you in a specific format and then checking the number does not exist prior to inserting, however for me personallyI'd go with the primary id auto_inc value.

将 id 号设置为主键,自动递增 int 是完全安全的,也是常见的做法。还有其他替代方法,例如使用 PHP 以特定格式为您生成会员编号,然后在插入之前检查该编号不存在,但是就我个人而言,我会使用主 ID auto_inc 值。

回答by Phil

  1. In "Structure" tab of your table
  2. Click on the pencil of the variable you want auto_increment
  3. under "Extra" tab choose "auto_increment"
  4. then go to "Operations" tab of your table
  5. Under "Table options" -> auto_increment type -> 10000
  1. 在表格的“结构”选项卡中
  2. 单击您想要 auto_increment 的变量的铅笔
  3. 在“额外”选项卡下选择“自动增量”
  4. 然后转到表的“操作”选项卡
  5. 在“表选项”下 -> auto_increment 类型 -> 10000

回答by KE Keronei

@AmitKB, Your procedure is correct. Although this error

@AmitKB,您的程序是正确的。虽然这个错误

Query error: #1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key

查询错误:#1075 - 表定义不正确;只能有一个自动列,并且必须将其定义为键

can be solved by first marking the field as key(using the key icon with label primary),unless you have other key then it may not work.

可以通过首先将字段标记为键来解决(使用带有标签主键的键图标),除非您有其他键,否则它可能无法工作。

回答by Tim

In phpMyAdmin, if you set up a field in your table to auto increment, and then insert a row and set that field's value to 10000, it will continue from there.

在 phpMyAdmin 中,如果您将表中的字段设置为自动递增,然后插入一行并将该字段的值设置为 10000,它将从那里继续。

回答by Emil Vikstr?m

You cannot set a maximum value (other than choosing a datatype which cannot hold large numbers, but there are none that have the limit you're asking for). You can check that with LAST_INSERT_ID() after inserting to get the id of the newly created member, and if it is too big handle it in your application code (e.g., delete and reject the member).

您不能设置最大值(除了选择不能容纳大数字的数据类型,但没有您要求的限制)。您可以在插入后使用 LAST_INSERT_ID() 检查以获取新创建成员的 id,如果它太大,请在您的应用程序代码中处理它(例如,删除并拒绝该成员)。

Why do you want an upper limit?

为什么要设置上限?

回答by Faith Olanrewaju

This is due to the wp_terms, wp_termmetaand wp_term_taxonomytables, which had all their ID's not set to AUTO_INCREMENT

这是由于wp_terms,wp_termmetawp_term_taxonomy表的所​​有 ID 未设置为AUTO_INCREMENT

To do this go to phpmyadmin, click on the concern database, wp_termstable, click on structure Tab, at right side you will see a tab named A_I(AUTO_INCREMENT), check it and save (You are only doing this for the first option, in the case wp_termyou are only doing it for term_id).

要做到这一点去的phpmyadmin,单击关系数据库,上wp_terms表中,单击结构选项卡,在右边你会看到一个标签命名A_I(AUTO_INCREMENT),检查和保存(你只能做这行的第一个选项,如是wp_term你是只做它term_id)。

Do the same for wp_termmetaand wp_term_taxonomythat will fix the issue.

用同样的方法wp_termmeta,并wp_term_taxonomy会解决这个问题。

回答by Done_Ke

(a)Simply click on your database, select your table. Click on 'Operations'. Under the 'table options' section change the AUTO_INCREMENT value to your desired value, in this case: 10000 the click 'Go'. (See the image attached)

(a) 只需单击您的数据库,选择您的表。单击“操作”。在“表选项”部分下,将 AUTO_INCREMENT 值更改为您想要的值,在本例中为:10000,单击“执行”。(见附图)

(b)Alternatively, you can run a SQL command under the SQL tab after selecting your table. Simply type 'ALTER TABLE table_name AUTO_INCREMENT = 10000;' then click 'Go'. That's it!! SETTING AUTO INCREMENT VALUE image(a)

(b) 或者,您可以在选择表后在 SQL 选项卡下运行 SQL 命令。只需输入'ALTER TABLE table_name AUTO_INCREMENT = 10000;' 然后单击“开始”。就是这样!! 设置自动增量值 image(a)

SETTING AUTO INCREMENT VALUE image(B)

设置自动增量值图像(B)