I'm planning to make a very simple program using php and mySQL. The main page will take information and make a new row in the database with that information. However, I need a number to put in for the primary key. Unfortunately, I have no idea about the normal way to determine what umber to use. Preferably, if I delete a row, that row's key won't ever be reused.
我打算用php和mySQL制作一个非常简单的程序。主页面将获取信息并使用该信息在数据库中创建一个新行。但是,我需要一个数字来输入主键。不幸的是,我不知道确定使用什么的常用方法。优选地,如果我删除一行,则该行的密钥将不会被重用。
A preliminary search has turned up the AUTOINCREMENT keyword in mySQL. However, I'd still like to know if that will work for what I want and what the common solution to this issue is.
初步搜索已经在mySQL中调出了AUTOINCREMENT关键字。但是,我仍然想知道这是否适合我想要的以及这个问题的常见解决方案。
7 个解决方案
#1
19
In MySQL that's the standard solution.
在MySQL中,这是标准解决方案。
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
#2
6
Unless you have an overriding reason to generate your own PK then using the autoincrement would be good enough. That way the database manages the keys. When you are inserting a row you have to leave out the primary key column.
除非你有一个压倒一切的理由来生成自己的PK,否则使用自动增量就足够了。这样,数据库就可以管理密钥。插入行时,必须省略主键列。
Say you have a table table = (a, b, c) where a is the primary key then the insert statement would be
假设你有一个表table =(a,b,c),其中a是主键,然后是insert语句
insert into table (b, c) values ('bbb', 'ccc')
插入表(b,c)值('bbb','ccc')
and the primary key will be auto inserted by the databse.
并且数据库将自动插入主键。
#3
4
AUTOINCREMENT is what you want. As long as you don't change the table's settings, AUTOINCREMENT will continue to grow.
AUTOINCREMENT是你想要的。只要您不更改表格的设置,AUTOINCREMENT将继续增长。
#4
1
AUTOINCREMENT is the standard way to automatically create a unique key. It will start at 1 (or 0, I can't remember and it doesn't matter) then increment with each new record added to the table. If a record is deleted, its key will not be reused.
AUTOINCREMENT是自动创建唯一键的标准方法。它将从1开始(或0,我不记得并且没关系)然后随着添加到表中的每个新记录的增加而增加。如果删除记录,则不会重用其密钥。
#5
1
Auto increment primary keys are relatively standard depending on which DBA you're talking to which week.
自动增量主键是相对标准的,具体取决于您与哪个DBA进行通话。
I believe the basic identity integer will hit about 2 billion rows(is this right for mySQL?) before running out of room so you don't have to worry about hitting the cap.
我相信基本身份整数将会耗尽大约20亿行(这对于mySQL来说是正确的吗?),然后用完房间,这样你就不用担心会出现这个问题。
#6
1
AUTO_INCREMENT is the common choice, it sets a number starting from 1 to every new row you insert. All the work of figuring out which number to use is done by the db, you just ask it back after inserting if you need to ( in php you get it by callin mysql_last_insertid I think )
AUTO_INCREMENT是常见的选择,它设置从1开始到您插入的每个新行的数字。确定要使用哪个数字的所有工作都是由db完成的,如果你需要,你只需要在插入后再问一下(在php中你通过callin mysql_last_insertid得到它我认为)
#7
0
For something simple auto increment is best. For something more complicated that will ultimately have a lot of entries I generate a GUID and insert that as the key.
对于简单的自动增量最好。对于最终会有很多条目的更复杂的东西,我会生成一个GUID并将其作为键插入。
#1
19
In MySQL that's the standard solution.
在MySQL中,这是标准解决方案。
CREATE TABLE animals (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL,
PRIMARY KEY (id)
);
#2
6
Unless you have an overriding reason to generate your own PK then using the autoincrement would be good enough. That way the database manages the keys. When you are inserting a row you have to leave out the primary key column.
除非你有一个压倒一切的理由来生成自己的PK,否则使用自动增量就足够了。这样,数据库就可以管理密钥。插入行时,必须省略主键列。
Say you have a table table = (a, b, c) where a is the primary key then the insert statement would be
假设你有一个表table =(a,b,c),其中a是主键,然后是insert语句
insert into table (b, c) values ('bbb', 'ccc')
插入表(b,c)值('bbb','ccc')
and the primary key will be auto inserted by the databse.
并且数据库将自动插入主键。
#3
4
AUTOINCREMENT is what you want. As long as you don't change the table's settings, AUTOINCREMENT will continue to grow.
AUTOINCREMENT是你想要的。只要您不更改表格的设置,AUTOINCREMENT将继续增长。
#4
1
AUTOINCREMENT is the standard way to automatically create a unique key. It will start at 1 (or 0, I can't remember and it doesn't matter) then increment with each new record added to the table. If a record is deleted, its key will not be reused.
AUTOINCREMENT是自动创建唯一键的标准方法。它将从1开始(或0,我不记得并且没关系)然后随着添加到表中的每个新记录的增加而增加。如果删除记录,则不会重用其密钥。
#5
1
Auto increment primary keys are relatively standard depending on which DBA you're talking to which week.
自动增量主键是相对标准的,具体取决于您与哪个DBA进行通话。
I believe the basic identity integer will hit about 2 billion rows(is this right for mySQL?) before running out of room so you don't have to worry about hitting the cap.
我相信基本身份整数将会耗尽大约20亿行(这对于mySQL来说是正确的吗?),然后用完房间,这样你就不用担心会出现这个问题。
#6
1
AUTO_INCREMENT is the common choice, it sets a number starting from 1 to every new row you insert. All the work of figuring out which number to use is done by the db, you just ask it back after inserting if you need to ( in php you get it by callin mysql_last_insertid I think )
AUTO_INCREMENT是常见的选择,它设置从1开始到您插入的每个新行的数字。确定要使用哪个数字的所有工作都是由db完成的,如果你需要,你只需要在插入后再问一下(在php中你通过callin mysql_last_insertid得到它我认为)
#7
0
For something simple auto increment is best. For something more complicated that will ultimately have a lot of entries I generate a GUID and insert that as the key.
对于简单的自动增量最好。对于最终会有很多条目的更复杂的东西,我会生成一个GUID并将其作为键插入。