Home > database >  Mysql automatic growth (auto_increment) constraint problems
Mysql automatic growth (auto_increment) constraint problems

Time:09-26

Excuse me the following two all SQL statements that right, why?
1, the mysql> The create table a1 (id int primary key auto_increment, name text);
2, mysql> The create table a2 (id int auto_increment, name text);


When I want to automatically increase the field after add the primary key constraint, you can compile success
mysql> The create table a1 (id int primary key auto_increment, name text);
Query OK, 0 rows affected (0.01 SEC)


When the field does not add the primary key constraint, direct from the growth would be an error and the
mysql> The create table a2 (id int auto_increment, name text);
ERROR 1075 (42000) : Incorrect table definition; There can be only one auto column and it must be defined as a key (to automatic growth must give him a key (key))

CodePudding user response:

The first right, if you want the growth is not the primary key, you can set the optional can be a key,
 create table tab_01 (
Id int auto_increment not null,
The test varchar (10),
The key (id)
)

CodePudding user response:

First create a statement is right

CodePudding user response:


Junction post rate: 0%

When you the solutions of the problems please post.
http://topic.csdn.net/u/20090501/15/7548d251-aec2-4975-a9bf-ca09a5551ba5.html
http://topic.csdn.net/u/20100428/09/BC9E0908-F250-42A6-8765-B50A82FE186A.html
http://topic.csdn.net/u/20100626/09/f35a4763-4b59-49c3-8061-d48fdbc29561.html

8, how to give points and knot stick?
http://bbs.csdn.net/help#post_dispose

CodePudding user response:

It should be in order to make the column does not repeat, and speed, are forced to use a key, namely to use indexes

CodePudding user response:

Auto_increment field is used to set the gain, the field must create indexes, if not create indexes, create a data table will be an error, but in MySQL, primary key is a primary key constraint, will create a unique index, the default. The default is called primary so auto_increment and primary ke is used together, don't need to create indexes can successfully create the data table, if you want to make, mysql> The create table a2 (id int auto_increment, name text); Successfully create a data table, adding indexes can, below I add a normal index:
The create table a2 (id int auto_increment, name text, index (id)); So after adding can successfully create data table//because the auto_increment attribute is used in the field ids, so to create indexes in this field, as to the type of the index, in this will not be explained
  • Related