MySQL 基础指令

介绍了MySQL一些基础指令,增删改查

MySQL

准备工作

  • 添加环境变量

  • 初始化MySQL

    1
    mysqld --initialize-insecure

    该命令在安全性方面存在缺陷,主要用于开发和测试环境

  • 注册MySQL服务

    1
    mysqld -install
  • 启动/停止 MySQL服务

    1
    2
    3
    net start mysql

    net stop mysql
  • 修改默认账户密码

    1
    mysqladmin -u root password xxxx
  • 登录MySQL

    1
    mysql -uroot -pxxxx [-h数据库服务器IP地址 -P端口号]
  • 卸载MySQL

    1
    2
    3
    net stop mysql

    mysqld -remove mysql

SQL分类

  • DDL:Data Definition Language 定义数据库对象

  • DML :Data Manipulation Language 操作数据,进行增删改

  • DQL:Data Query Language 查询数据库中的记录

  • DCL:Data Control Language 创建数据库的用户,控制访问权限

基础语法

以分号结尾,不区分大小写

DDL
  • 查询数据库

1
show databases;
  • 创建数据库

1
2
3
create database filename;

create database if not exists filename; #如果不存在名为filename的数据库就进行创建
  • 切换数据库

1
use filename;
  • 查看当前正在使用的数据库

1
select database();
  • 删除数据库

1
2
3
drop database filename;

drop database if exists filename;
表操作
1
2
3
4
create table 表名(
字段1 字段类型 [约束] [comment 字段1注解],
字段2 字段类型 [约束] [comment 字段2注解]
)[comment 表注解];
约束 描述 关键字
非空约束 限制该字段值不能为null not null
唯一约束 保证字段的所有数据都是唯一的 unique
主键约束 主键是一行数据的唯一标识,要求非空且唯一 primary key (auto_increment自增)
默认约束 保存数据时,如果未指定该字段值,则采用默认值 default
外键约束 让两张表的数据建立连接,保证数据的一致性和完整性 foreign key
1
2
3
4
5
6
7
creat table tb_user(
id int primary key auto_increment comment 'ID,唯一标识',
username varchar(20) not null unique comment '用户名',
name varchar(10) not null comment '姓名',
age int comment '年龄',
gender char(1) default '男' comment '性别'
)comment '用户表'
  • 查询

1
2
3
4
5
6
7
8
-- 查看当下数据库的表
show tables;

-- 查看指定表结构
desc tablename;

-- 查看数据库的建表语句
show create table tablename;
  • 修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
-- 为表example 添加字段 qq varchar(11)
alter table example add qq varchar(11) comment 'QQ';

-- 修改example 字段类型 qq varchar(13)
alter table example modify qq varchar(13) comment 'QQ';

-- 修改example 字段名qq为qq_num
alter table example change qq qq_num varchar(13) comment 'QQ';

-- 删除qq_num字段
alter table example drop column qq_num;

-- 将example 表名修改为hello
rename table example to hello;
  • 删除

1
2
-- 删除 example 表
drop table if exists eample;
DML
  • insert语句

1
2
3
4
5
6
7
8
--  为 example 表的 username,name,gender 字段插入值
insert into example(username, name, gender) values ('tinali',' 提纳里',1);

-- 为 example 表的 所有字段 插入值
insert into example values(null,null,null,'tinali',' 提纳里',1);

-- 批量为 example 表的 username,name,gender字段插入数据
insert into example(username, name, gender) values ('tinali','提纳里',1),('审判官','那维莱特',1);
  • update语句

1
2
3
4
5
-- 将 example 中ID为1的 name 字段 更新为 张三,username 字段更新为 hello
update example set name = '张三', username = 'hello' where id = 1;

-- 将example 中所有 gender 字段改为 1
update example set gender = 1;
  • delete语句

1
2
3
4
5
6
delete from 表名 [where 条件];
-- 删除 example 中ID为1的信息
delete from example where id = 1;

-- 删除表中所有信息
delete from example;
DQL
  • 基本查询

1
2
3
4
5
6
7
8
9
10
11
-- 查询特定字段 name,entrydate 并返回
select name,entrydate from example;

-- 查询返回所有字段 (通配符)
select * from example;

-- 查询特定字段 name,entrydate 并起别名
select name as 姓名,entrydate as 入职日期 from example;

-- 查询job字段的记录(不重复)
select distinct job from example;
  • 条件查询(where

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- select 字段列表 from 表名 where 条件列表

-- 查询 name 为 三子曰 的记录
select * from example where name = '三子曰';

-- 查询 id 小于等于5 的记录
select * from example where id <= 5;

-- 查询 job 为空值的记录
select * from example where job is null;

-- 查询 job 非空值的记录
select * from example where job is not null;

-- 查询 password 不是 123456
select * from example where password != '123456';

-- 查询 age 在 20 到 30 之间的记录
select * from example where age >= 20 && age <= 30;

-- 查询id 为1,2,3的记录
select * from example where id in (1,2,3)l;

-- 查询 name 为两个字的记录 (两条下划线)
select * from example where name like '__';

-- 查询 name 字段中第一个字为 张 的记录
select * from example where name like '张%'

_可替代一个字符 %可替代随机数目的字符
  • 分组查询(groud by

    • 聚合函数 count max min avg sum
1
2
3
4
-- select 聚合函数 from 表名

-- 统计 id 字段数的数目
select count(id) from example;
1
2
3
4
5
6
7
-- select 字段列表 from 表名 [where 条件] group by 分组字段名[having 分组后的过滤的条件];

-- 根据 gender 分组,统计男女员工的数量
select gender,count(*) from example group by gender;

-- 查询 age 在30(包含)以前的员工,并对job进行分组,获得数量大于等于2的job数据
select job,count(*) from example where age <= 30 group by job having count(*) >= 2;
  • 排序查询(order by

1
2
3
4
5
6
--select 字段列表 from 表名 [where 条件列表][group by 分组字段] order by 字段1 排序方法1

--根据年龄进行升序排序 (降序desc)
select * from example order by age asc;


  • 分页查询(limit

1
2
3
4
5
6
7
8
9
10
-- select 字段列表 from 表名 limit 起始索引,查询记录数;

-- 从 起始索引为0,开始查询,每夜展示五条数据
select * from example limit 0,5;

-- 查询第一页数据,每页展示五条数据
select * from example limit 0,5;

-- 查询第二,每页展示五条数据
select * from example limit 5,5;