Mysql defines the primary key as the automatic growth identifier type and the mysql identifier
This section describes how to define a primary key as an Automatically increasing identifier type in mysql.
1. Define the primary key as the automatic growth identifier type
In mysql, if the table's primary key is set to auto_increment type, the database automatically assigns a value to the primary key. For example:
create table customers(id int auto_increment primary key notnull, name varchar(15));insert into customers(name) values("name1"),("name2");
Once the id is set to auto_increment type, mysql database automatically assigns a value to the primary key in ascending mode.
In MS SQLServer, if the table's primary key is set to the identity type, the database automatically assigns a value to the primary key. For example:
create table customers(id int identity(1,1) primary key notnull, name varchar(15));-- www.jbxue.cominsert into customers(name) values("name1"),("name2");select id from customers;
The query result is the same as that of mysql. It can be seen that, once the id is set to the identity type, the MSSQLServer database automatically assigns a value to the primary key in ascending mode.
How to Set auto-increment int-type primary keys in mysql
Refer to the code
--
-- Table structure for table 'user'
--
Drop table if exists 'user ';
Create table 'user '(
'Uid' int (11) not null auto_increment,
'Gid' int (11) default NULL,
'Username' varchar (15) not null,
'Password' varchar (15) not null,
Primary key ('uid '),
) ENGINE = InnoDB default charset = gb2312;
In MySQL, can I set non-auto-increment field B as the primary key when field A is auto-incrementing?
Yes!
It is common practice to create a primary key for automatically growing fields. However, it is also possible to define other fields as the primary key. when inserting data, be sure not to repeat the fields to avoid errors.
Hope to help.
Your ID field should also be set as the primary key, which is the default! Haha