mysql> create table user( -> id int primary key, -> name varchar(20) -> ); Query OK, 0 rows affected (0.05 sec)
mysql> show tables; +-------------------------+ | Tables_in_test_database | +-------------------------+ | test_table | | user | +-------------------------+ 2 rows in set (0.00 sec)
mysql> describe user; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
mysql> insert into user values(1,'kzero'); Query OK, 1 row affected (0.01 sec)
mysql> select * from user; +----+-------+ | id | name | +----+-------+ | 1 | kzero | +----+-------+ 1 row in set (0.01 sec)
mysql> insert into user values(2,'codekzero'); Query OK, 1 row affected (0.01 sec)
mysql> select * from user; +----+-----------+ | id | name | +----+-----------+ | 1 | kzero | | 2 | codekzero | +----+-----------+ 2 rows in set (0.00 sec)
mysql> describe user4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
使用drop,删除主键
1
alter table 数据表名 drop primary key;
例子:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> alter table user4 drop primary key; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> describe user4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int | NO | | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
使用modify,修改字段,添加主键
1
alter table 数据表名 modify id int primary key;
例子:
1 2 3 4 5 6 7 8 9 10 11 12
mysql> alter table user4 modify id int primary key; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0
mysql> describe user4; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int | NO | PRI | NULL | | | name | varchar(20) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
自增约束(PRI)
会自增id值
1 2 3 4
create table 数据表名( id int primary key auto_increment, 表头1 数据类型 );
mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | test_database | +--------------------+ 5 rows in set (0.00 sec)
选择要使用的数据库
1
use 数据库名
1 2 3 4 5
mysql> use test_database; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A
mysql> select * from test_table; +----------+-----------+ | name | passwored | +----------+-----------+ | kzero | mima | | admin | admin | | zoiiiiii | mima | +----------+-----------+ 3 rows in set (0.00 sec)
查询表中的数据
1
select * from 数据表名;
1 2 3 4 5 6 7 8
mysql> select * from test_table; +-------+-----------+ | name | passwored | +-------+-----------+ | kzero | mima | | admin | admin | +-------+-----------+ 2 rows in set (0.00 sec)
条件查询表中的数据
1
slect * from 数据表名 where 条件;
1
slect * from 数据表名 where id=1;
1 2 3 4 5 6 7
slect * from test_table where name='kzero'; +-------+-----------+ | name | passwored | +-------+-----------+ | kzero | mima | +-------+-----------+ 1 row in set (0.00 sec)
查看数据表的结构
1
describe 数据表名;
1 2 3 4 5 6 7
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | name | varchar(20) | YES | | NULL | | | passwored | varchar(20) | YES | | NULL | | +-----------+-------------+------+-----+---------+-------+ 2 rows in set (0.00 sec)
删除数据
1
delete from 数据表名 where 条件;
1
delete from test_table where name='admin';
1
Query OK, 1 row affected (0.01 sec)
再次查询
1 2 3 4 5 6 7 8
mysql> select * from test_table; +----------+-----------+ | name | passwored | +----------+-----------+ | kzero | mima | | zoiiiiii | mima | +----------+-----------+ 2 rows in set (0.00 sec)