服务启动与登录

1
2
3
4
5
# Linux上mysql安装后都能在/etc/init.d/下找到mysqld,安装的方法不同会导致命令行中不一定能直接找到mysql命令,可以添加到别名中去
/etc/init.d/mysqld start

# 登录,回车后输入密码
mysql -h localhost -P part -u username -p

SQL(Structured Query Language 结构化查询语言)语句

DDL(Data Definition Language)数据定义语言

查看所有数据库

1
show database;

切换数据库

1
use database;

创建数据库

1
create database[if not exists] newDBName;

删除数据库

1
drop database dbName;

修改数据库编码

1
alter database dbName character set utf8;

查看所有数据表

1
show tables;

创建表

1
2
3
4
5
6
create table tableName(
id int primary key auto_increment,
name varchar(8) NOT NULL DEFAULT '' COMMENT '用户名',
create_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_at timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
);

修改表

  • 添加字段
1
2
alter table tableName add(rowName varchar(8));
alter table cloud_pipe_idc add(config varchar(128) not null default '' comment '接口配置');
  • 修改字段类型
1
alter table tableName modify rowName varchar(8);
  • 修改字段名
1
alter table tableName change oldName newName varchar(8);
  • 删除字段
1
alter table tableName drop rowName;
  • 修改表名称
1
alter table oldName rename to newName;

DML(Data Manipulation Language)数据操作语言

插入数据

1
insert into tableName(row1,row2) values(data1,data2);

更新数据

1
update tableName set row = 'data' where row1 = 'data1';

删除数据

  • 部分数据
    1
    delete from tableName where row = 'data';
  • 清空表:逐行删除表中所有的数据
    1
    delete from tableName;
  • 重置表:直接删除表然后再重新创建一个新表,属于DDL操作,速度快但是无法回滚
    1
    truncate table tableName;

DCL(Data Control Language)数据控制语言

  • 创建用户
    1
    create user username@localhost identified by 'password';
  • 用户授权
    1
    2
    # dbName是指要对那个数据进行授权
    grant create,alter,drop on dbName.* to username@localhost;
  • 撤销授权
    1
    revoke create,alter,drop on dbName.* from username@localhost;
  • 用户权限查看
    1
    show grants for username@localhost;
  • 修改用户密码
    1
    # 这个貌似已经随着MySQL的版本发生了变更,用到的时候再更新过来

DQL(Data Query Language)数据查询语言

基础

  • 查询所有列
    1
    select * from tableName;
  • 查询指定列
    1
    select rowName1,rowName2 from tableName;

单表查询

  • 普通查询
    1
    2
    3
    4
    5
    6
    select * from tableName where factorOne and factorTwo;
    select * from tableName where factorOne or factorTwo;
    select * from tableName where rowName in ('valueOne','valueTwo','valueThree');
    # 关于值为空的判断,is null有的时候并不生效可以用 = ''来做判断
    select * from tableName where rowName is null;
    select * from tableName where rowName is not null;
  • 模糊查询
    1
    2
    3
    4
    # 这里的一个_表示任意匹配一个字符(经过测试至少可以匹配字母、数字和中文),这里要抛去字段的类型,即使是int类型的像是id之类的数据也是要写到''中的
    select * from tableName where rowName like '__';
    # %模糊匹配0-n个字符,意思说这个占位符可以代表任意几个字符
    select * from tableName where rowName like '%haha%';
  • 去重
    1
    select distinct rowName from tableName;
  • 排序
    1
    2
    3
    4
    5
    6
    # 升序
    select * from tableName order by rowName desc;
    # 降序
    select * from tableName order by rowName asc;
    # 多重排序,先按照rowOne的值进行升序排列,如果有相同的值再按照rowTwo降序排列
    select * from tableName order by rowOne deac,rowTwo asc;
  • 聚合函数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 计数
    select count(*) from tableName where factor;

    # 求和
    select sum(rowName) from tableName where factor;

    # 平均数
    select avg(rowName) from tableName where factor;

    # 最大最小值
    select max(rowName),min(rowName) from tableName;
  • 分组查询
    1
    2
    3
    select row1,sum(row2) from tableName group by row1;
    # 二次过滤
    select row1,sum(row2) from tableName group by row1 having sum(row2) > 10;

多表查询

  • 内连接
    1
    2
    select * from tableA,tableB;
    select * from tableA inner join tableB
  • 外连接
    • 左连接
    • 右连接