一、库操作
1、查看当前在哪个库
select database();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
+------------+ | database() | +------------+ | userinfo | +------------+
2、创建库
create database 库名 default character set=字符集; # 此处如果不指定,则使用系统默认字符集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> create database userinfo default character set=utf8mb4; Query OK, 1 row affected mysql> show create database userinfo; +----------+----------------------------------------------------------------------+ | Database | Create Database | +----------+----------------------------------------------------------------------+ | userinfo | CREATE DATABASE `userinfo` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ | +----------+----------------------------------------------------------------------+
3、查看当前系统所有库
show databases;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
+--------------------+ | Database | +--------------------+ | information_schema | | info | | mysql | | performance_schema | | test | | userinfo | +--------------------+
4、使用库
use 库名;
5、删除库
drop database 库名; # 此操作很危险,删除库的同时删除库下的所有表
二、数据类型
1 、数值型
''' unsigned:表示无符号的 只针对数值型 float(M,D) 浮点型 decimal(M,D) 定点型 比float更加准确 `money` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '充值金额', M:精度(总位数) D:标度(小数位) '''
2、 字符串类型
3、 时间类型
4、 特殊的NULL类型
''' NULL不是假,也不是真,而是空 NULL的判断只能用is null, is not null NULL影响查询速度,一般避免其值为NULL '''
三、表操作
1、查看当前数据库中所有表
show full tables;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
2、创建表
create table student( id int not null auto_increment primary key, name varchar(10) character set utf8mb4 not null default '' comment '姓名' ) default character set='utf8mb4';
# auto_increment 自增
# default '' 默认是空
# comment '注释'
# primary key 主键 一种特殊的唯一索引,不允许有空值,如果主键使用单个列,则它的值必须唯一,如果是多列,则其组合必须唯一
3、修改表名
alter table current_table_name rename to new_table_name;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> alter table student rename to accountinfo; Query OK, 0 rows affected mysql> show tables; +--------------------+ | Tables_in_userinfo | +--------------------+ | accountinfo | +--------------------+ 1 row in set
3、清空表
3.1 delete from 表名; # delete 清空表,如果遇到自增列,会从delete前最后一行的自增列开始计数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> select * from accountinfo; +----+-------+ | id | name | +----+-------+ | 1 | li | | 2 | fred | | 3 | fred2 | | 4 | fred3 | | 5 | fred4 | +----+-------+ 5 rows in set mysql> delete from accountinfo; Query OK, 5 rows affected mysql> insert into accountinfo(name) values('fred4'); Query OK, 1 row affected mysql> select * from accountinfo; +----+-------+ | id | name | +----+-------+ | 6 | fred4 | +----+-------+ 1 row in set
3.2 truncate table 表名; # truncate 清空表 如果遇到自增列,会从头开始
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> truncate table accountinfo; Query OK, 0 rows affected mysql> insert into accountinfo(name) values('fred4'); Query OK, 1 row affected mysql> insert into accountinfo(name) values('fred4'); Query OK, 1 row affected mysql> select * from accountinfo; +----+-------+ | id | name | +----+-------+ | 1 | fred4 | | 2 | fred4 | +----+-------+ 2 rows in set
4、删除表
drop table 表名;
5、修改表
5.1 添加字段
alter table student add 字段名 tinyint(4) not null default '0' comment '地址'; # 默认是add到表的最后一列
alter table student add phone int(11) not null default '0' comment '电话' after `name`; # 在指定字段后面添加
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> alter table student add addr tinyint(4) not null default '0' comment '地址 '; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | sex | tinyint(4) | NO | | 0 | | | addr | tinyint(4) | NO | | 0 | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set mysql> alter table student add phone int(11) not null default '0' comment '电话' after `name`; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> mysql> mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | | addr | tinyint(4) | NO | | 0 | | +-------+-------------+------+-----+---------+----------------+ 5 rows in set
5.2 修改字段
alter table student modify addr varchar(32) not null default '' comment '地址';
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | | addr | tinyint(4) | NO | | 0 | | +-------+-------------+------+-----+---------+----------------+ mysql> alter table student modify addr varchar(32) not null default '' comment '地址'; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | | addr | varchar(32) | NO | | | | +-------+-------------+------+-----+---------+----------------+ 5 rows in set
5.3 删除字段
alter table student drop addr;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | | addr | varchar(32) | NO | | | | +-------+-------------+------+-----+---------+----------------+ 5 rows in set mysql> alter table student drop addr; Query OK, 0 rows affected Records: 0 Duplicates: 0 Warnings: 0 mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set
四、表数据的增删改
1、增加数据 insert into
1.1 按规矩指定所有列名,并且每列都插入数据 insert into table_name(字段1,字段2,....) values(值1,值2,....) # 注意此处字段和值必须一一对应
insert into account(user_id,game_id,open_id,union_id,access_token) values(729672,7296723,'oY8lLwjg3jVc','oAxDHvxOabV','14_KV2LYNVY7Ak0d');
1.2 对表中所有列插入数据,table_name后可以不用跟字段名 insert into table_name values(值1,值2......) # 此处表中有几个字段,必须对应几个值
insert into account values(729672,7296723,'oY8lLwjg3jVc','oAxDHvxOabV','14_KV2LYNVY7Ak0d');
1.3 表中如果有字段是自增的,insert时,可以不用指定此列
insert into student(name,phone,sex) values('fred','187','2');
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> desc student; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(10) | NO | | | | | phone | int(11) | NO | | 0 | | | sex | tinyint(4) | NO | | 0 | | +-------+-------------+------+-----+---------+----------------+ 4 rows in set mysql> insert into student(name,phone,sex) values('fred','187','2'); Query OK, 1 row affected mysql> select * from student; +----+------+-------+-----+ | id | name | phone | sex | +----+------+-------+-----+ | 1 | fred | 187 | 2 | +----+------+-------+-----+ 1 row in set
1.4 将其他表中的的数据插入到指定表 insert into table_name(字段1,字段2..) select from table_name1
insert into guild_member(user_id,guild_id) select user_id,687490 from accountinfo limit 5;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> desc guild_member; +------------+---------------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default | Extra | +------------+---------------------+------+-----+-------------------+-------+ | user_id | int(11) unsigned | NO | PRI | NULL | | | guild_id | int(11) unsigned | NO | PRI | NULL | | | admin | tinyint(4) | NO | | 0 | | | is_union | tinyint(4) unsigned | NO | | 0 | | | score | int(11) | NO | | 0 | | | win | int(11) | NO | | 0 | | | s100 | int(11) | NO | | 0 | | | s300 | int(11) | NO | | 0 | | | s500 | int(11) | NO | | 0 | | | join_time | timestamp | NO | | CURRENT_TIMESTAMP | | | guild_data | varchar(2048) | NO | | | | +------------+---------------------+------+-----+-------------------+-------+ 11 rows in set (0.01 sec) mysql> insert into guild_member(user_id,guild_id) select user_id,687490 from accountinfo limit 5; Query OK, 5 rows affected (0.01 sec) Records: 5 Duplicates: 0 Warnings: 0 mysql> select * from guild_member where guild_id=687490; +---------+----------+-------+----------+-------+-----+------+------+------+---------------------+------------+ | user_id | guild_id | admin | is_union | score | win | s100 | s300 | s500 | join_time | guild_data | +---------+----------+-------+----------+-------+-----+------+------+------+---------------------+------------+ | 279899 | 687490 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2018-10-09 16:17:02 | | | 321714 | 687490 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2018-10-09 16:17:02 | | | 611391 | 687490 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2018-10-09 16:17:02 | | | 961411 | 687490 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2018-10-09 16:17:02 | | | 994470 | 687490 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2018-10-09 16:17:02 | | +---------+----------+-------+----------+-------+-----+------+------+------+---------------------+------------+
2、修改数据 update table_name set 字段1=values; # 注意 线上数据最好不要update 如果非要update 必须加where 条件
update accountinfo set room_card=room_card-10000 where user_id=729672; # 如果不加where条件,表中这个列都会被修改,很危险
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> select user_id,nick,room_card from accountinfo where user_id=729672; +---------+--------------+-----------+ | user_id | nick | room_card | +---------+--------------+-----------+ | 729672 | 随梦而飞 | 11108 | +---------+--------------+-----------+ 1 row in set (0.00 sec) mysql> update accountinfo set room_card=room_card-10000 where user_id=729672; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select user_id,nick,room_card from accountinfo where user_id=729672; +---------+--------------+-----------+ | user_id | nick | room_card | +---------+--------------+-----------+ | 729672 | 随梦而飞 | 1108 | +---------+--------------+-----------+ 1 row in set (0.00 sec)
update accountinfo set room_card=room_card-10000,nick='碎梦' where user_id=729672; # 同时修改多列
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> select user_id,nick,room_card from accountinfo where user_id=729672; +---------+--------------+-----------+ | user_id | nick | room_card | +---------+--------------+-----------+ | 729672 | 随梦而飞 | 1108 | +---------+--------------+-----------+ 1 row in set (0.00 sec) mysql> update accountinfo set room_card=room_card-10000,nick='碎梦' where user_id=729672; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select user_id,nick,room_card from accountinfo where user_id=729672; +---------+--------+-----------+ | user_id | nick | room_card | +---------+--------+-----------+ | 729672 | 碎梦 | -8892 | +---------+--------+-----------+ 1 row in set (0.00 sec)
3、删除数据(最好不要,可以用update一个标记字段代替,还可以实现sql审计)
delete from accountinfo where user_id=729672;
PS: mysql -U 阻止update 或 delete 不加条件执行 -U, --safe-updates Only allow UPDATE and DELETE that uses keys. 加-U后,update和delete where条件的值结果必须唯一
否则会报1175的错误,key值不唯一
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> delete from account where user_id=102607; ERROR 1175 (HY000): You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column mysql> select * from account where user_id=102607; +---------+---------+------------+----------+--------------+ | user_id | game_id | open_id | union_id | access_token | +---------+---------+------------+----------+--------------+ | 102607 | 1026075 | 1524023526 | visitor | visitor | | 102607 | 1026075 | 1524023526 | visitor | visitor | +---------+---------+------------+----------+--------------+ 2 rows in set (0.00 sec)
五、用户管理
1、查看指定用户的权限
show grants for root@'172.16.16.%';
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> show grants for root@'172.16.16.%'; +-----------------------------------------------------+ | Grants for root@172.16.16.% | +-----------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.16.16.%' | +-----------------------------------------------------+ 1 row in set (0.00 sec)
2、查看当前所有用户
select user,host from mysql.user;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> select user,host from mysql.user; +------+-----------+ | user | host | +------+-----------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | +------+-----------+
3、创建用户并授权
grant 权限 on 库.表 to user@'host' identified by '明文密码';
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> grant all on *.* to fred@'127.0.0.1' identified by '835y1354'; Query OK, 0 rows affected mysql> flush privileges; Query OK, 0 rows affected mysql> mysql> show grants for fred@'127.0.0.1'; +----------------------------------------------------------------------------------------------------------------------+ | Grants for fred@127.0.0.1 | +----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'fred'@'127.0.0.1' IDENTIFIED BY PASSWORD '*7DF241C2E2C1916417BD7B281C55FF4084F66C35' | +----------------------------------------------------------------------------------------------------------------------+ 1 row in set
flush privileges; 强制刷新到权限表
4、修改已存在账号的密码
set password for 'user'@'host ' = password('明文密码');
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> set password for 'fred'@'127 ' = password('ko2K24Cjga'); 1133 - Can't find any matching row in the user table mysql> set password for 'fred'@'127.0.0.1' = password('ko2K24Cjga'); Query OK, 0 rows affected
5、创建并授权超级管理员账号
grant all on *.* to fred_li@'127.0.0.1' identified by '835y1354' with grant option;
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> grant all on *.* to fred_li@'127.0.0.1' identified by '835y1354' with grant option; Query OK, 0 rows affected mysql> show grants for fred_li@'127.0.0.1'; +-------------------------------------------------------------------------------------------------------------------------------------------+ | Grants for fred_li@127.0.0.1 | +-------------------------------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO 'fred_li'@'127.0.0.1' IDENTIFIED BY PASSWORD '*7DF241C2E2C1916417BD7B281C55FF4084F66C35' WITH GRANT OPTION | +---------------------------
6、查看当前登录的用户
select user();
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
mysql> select user(); +--------------------+ | user() | +--------------------+ | root@172.16.16.247 | +--------------------+ 1 row in set (0.00 sec)