hi i want to know that is is possible to make primarykey in one table in mysql. if yes please tell me the concept behind this. because i have seen a table in which two primary key is there with no auto increment set
嗨,我想知道可以在mysql中的一个表中生成primarykey。如果是,请告诉我这背后的概念。因为我看过一张表,其中有两个主键没有设置自动增量
6 个解决方案
#1
8
you can only have 1 primary key, but:
你只能有一个主键,但是:
- you can combine more than one column to be the primary key (maybe it's this what you have seen)
- the primary key don't needs to be an auto-increment, it just has to be unique
- you can add more than one index to one or more colums to speed up SELECT-statements (but slow down INSERT / UPDATE)
- those indexes can be marked as unique, wich means they don't let you insert a second row with the same content in the index-fields (just like a primary key)
你可以组合多个列作为主键(也许这就是你所看到的)
主键不需要是自动增量,它必须是唯一的
您可以向一个或多个列添加多个索引以加速SELECT语句(但减慢INSERT / UPDATE)
这些索引可以标记为唯一,这意味着它们不允许您在索引字段中插入具有相同内容的第二行(就像主键一样)
#2
4
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
[...] A table can have only one PRIMARY KEY. [...]
[...]一个表只能有一个PRIMARY KEY。 [...]
#3
3
No, but you can have other UNIQUE indexes on the table, in addition to the PRIMARY KEY. UNIQUE + NOT NULL is basically the same as a primary key.
不,但除了PRIMARY KEY之外,您还可以在表上使用其他UNIQUE索引。 UNIQUE + NOT NULL与主键基本相同。
What you have seen is probably a composite primary key (more than one column making up the unique key).
您所看到的可能是复合主键(构成唯一键的多个列)。
#4
2
Use a Composite Primary Key...
使用复合主键...
e.g.
CREATE TABLE table1 (
first_id int unsigned not null,
second_id int unsigned not null auto_increment,
user_id int unsigned not null,
desc text not null,
PRIMARY KEY(first_id, second_id));
Also, check out the example here
另外,请查看此处的示例
#5
1
You can use multiple columns for your primary key in this way:
您可以通过以下方式为主键使用多个列:
CREATE TABLE
newTable
( field1 INT(11)
, field2 INT(11)
, field3 VARCHAR(5)
, field4 BLOB
, PRIMARY KEY (field2, field1, field3) <====
)
#6
1
A table can have a single PRIMARY key, which may consist of one or more columns. A table can also have a number of additional keys defined on it, as UNIQUE KEY constraints.
一个表可以有一个PRIMARY键,它可以包含一个或多个列。表也可以在其上定义许多附加键,作为UNIQUE KEY约束。
It's not clear from your description whether you were looking at a table with multiple keys defined, or a table with a multi-column PRIMARY KEY.
从您的描述中不清楚您是在查看定义了多个键的表,还是具有多列PRIMARY KEY的表。
#1
8
you can only have 1 primary key, but:
你只能有一个主键,但是:
- you can combine more than one column to be the primary key (maybe it's this what you have seen)
- the primary key don't needs to be an auto-increment, it just has to be unique
- you can add more than one index to one or more colums to speed up SELECT-statements (but slow down INSERT / UPDATE)
- those indexes can be marked as unique, wich means they don't let you insert a second row with the same content in the index-fields (just like a primary key)
你可以组合多个列作为主键(也许这就是你所看到的)
主键不需要是自动增量,它必须是唯一的
您可以向一个或多个列添加多个索引以加速SELECT语句(但减慢INSERT / UPDATE)
这些索引可以标记为唯一,这意味着它们不允许您在索引字段中插入具有相同内容的第二行(就像主键一样)
#2
4
http://dev.mysql.com/doc/refman/5.1/en/create-table.html
[...] A table can have only one PRIMARY KEY. [...]
[...]一个表只能有一个PRIMARY KEY。 [...]
#3
3
No, but you can have other UNIQUE indexes on the table, in addition to the PRIMARY KEY. UNIQUE + NOT NULL is basically the same as a primary key.
不,但除了PRIMARY KEY之外,您还可以在表上使用其他UNIQUE索引。 UNIQUE + NOT NULL与主键基本相同。
What you have seen is probably a composite primary key (more than one column making up the unique key).
您所看到的可能是复合主键(构成唯一键的多个列)。
#4
2
Use a Composite Primary Key...
使用复合主键...
e.g.
CREATE TABLE table1 (
first_id int unsigned not null,
second_id int unsigned not null auto_increment,
user_id int unsigned not null,
desc text not null,
PRIMARY KEY(first_id, second_id));
Also, check out the example here
另外,请查看此处的示例
#5
1
You can use multiple columns for your primary key in this way:
您可以通过以下方式为主键使用多个列:
CREATE TABLE
newTable
( field1 INT(11)
, field2 INT(11)
, field3 VARCHAR(5)
, field4 BLOB
, PRIMARY KEY (field2, field1, field3) <====
)
#6
1
A table can have a single PRIMARY key, which may consist of one or more columns. A table can also have a number of additional keys defined on it, as UNIQUE KEY constraints.
一个表可以有一个PRIMARY键,它可以包含一个或多个列。表也可以在其上定义许多附加键,作为UNIQUE KEY约束。
It's not clear from your description whether you were looking at a table with multiple keys defined, or a table with a multi-column PRIMARY KEY.
从您的描述中不清楚您是在查看定义了多个键的表,还是具有多列PRIMARY KEY的表。