Create a database and create a table.
SQL code
- mysql> Create database ssh2;
- Query OK, 1 row affected (0.04 sec)
-
- mysql> use SSH2;
- Database changed
- mysql> Create table user(
- -id integer primary key,
- - firstname varchar(+) not null,
- - lastname varchar(+) not null,
- -Age integer
- );
- Query OK, 0 rows Affected (0.46 sec)
Add a self-increment function to the primary key:
Java code
- mysql> ALTER TABLE user modify ID integer auto_increment;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
In this way, the ID of the primary key in the user table above can be self-increment.
add default values and auto-increment to the primary key ID above.
Java code
- mysql> ALTER TABLE user modify ID integer auto_increment;
- Query OK, 0 rows affected (0.39 sec)
- Records: 0 Duplicates: 0 Warnings: 0
-
- mysql> ALTER TABLE user Modify ID integer default ' 1 ';
- Query OK, 0 rows affected (0.16 sec)
- Records: 0 Duplicates: 0 Warnings: 0
-
- mysql> ALTER TABLE user modify ID integer auto_increment;
- Query OK, 1 row affected (0.28 sec)
- Records: 1 Duplicates: 0 Warnings: 0
MySQL Get system time:
Java code
- mysql> ALTER TABLE user add createtime timestamp default current_timestamp;
- Query OK, 2 rows affected (0.17 sec)
- Records: 2 Duplicates: 0 Warnings: 0
MySQL Settings primary key cannot be empty and will grow automatically (there is no default value set here, but the default is 1, starting from 1 growth.) ) and get the system default date:
If you want to create a table of the same data structure from a table:
CREATE TABLE Newtbname SELECT * from tab where 0;
This new table has the same data structure as the original table but does not set the primary key auto-growth
ALTER TABLE Modify ID integer primary key auto_increment
This ensures that the primary key ID is automatically increased in the database
MySQL Set primary key auto Grow