MySQL数据库常用的指令 -- 持续更新

  • 启动服务
net start mysql80
或
net start mysql
  • 停止服务
net stop mysql80
或
net stop mysql

启动服务和关闭服务指令在以管理员的身份运行的DOS命令窗口起作用。

  • 查看当前数据库的字符集
show variables like "character%";

|character_set_client      | gbk                              | 
| character_set_connection | gbk                              | 
| character_set_database   | utf8mb4                          | 
| character_set_filesystem | binary                           | 
| character_set_results    | gbk                              |
| character_set_server     | utf8mb4                          |
| character_set_system     | utf8mb3                          |
| character_sets_dir       | D:\MySQL install\share\charsets\ |
  • 创建数据库
create datadase <数据库名称>;
  • 查看数据库
查看所有数据库
show databases;

查看指定数据库

show create database <指定的数据库名>;show create database test;
  • 选择当前操作的数据库
use <数据库名称>;

use test;
  • 删除数据库
drop database <数据库名>;

drop database test;
  • 导出数据
select * from <表名> into outfile "导出的位置";

select * from student into outfile "D:/data/student.txt";

导出数据时可能遇到权限的错误,需要修改原来的配置

  • 导入数据
load data infile <文件名> into table <表名>;

load data infile  "D:/data/studend.txt" into table student;
  • 查看表结构
describe <表名>
  • 修改指定字段的数据类型
alter table <表名> modified <字段名> 数据类型 ;
alter table <表名> modified <字段名> 数据类型 not null;alter table student modified name char(8) not null; 
  • 添加一条数据
alter table <表名> add <字段名> <数据类型>;alter table teacher add name char(4) not null;
  • 更改表名
reanme table <原表名> to <新表名>;

rename table student to person;
  • 删除表
drop table <表名>;

drop table if exists <表名>;   # 如果表存在则删除

drop table if exists student;

表记录的操作

  • 插入数据
方式一
insert into <表名> values(<每一个字段对应的值>);insert into student values("2020100",'小明','男'.'2020-04-3',null,nulll,null);
方式二
insert into <表名> student(<需要插入的字段名>) values(<需要插入字段的值>);insert into student(stu_id,name,sex,birth) values("2020100",'小明','男'.'2020-04-3');
方式三
insert into student
  stu_id="2020100",
  name = "小明",
  sex = '男',
  birth = '2020-04-3';
  • 替换旧纪录
如果使用insert into 插入一条主键已经存在的数据,会报错

replace into <表名> values(<插入数据的数据>)  # 用法和insert 一样
  • 修改数据
修改一个表中的数据
updata <表名> set 列名1=exper1,列名2=exper2...   where 条件   # 修改符合条件的行,如果省略 where 则修改所有的行 
updata student set name="小红",sex="女"   where stu_id='2020100';

修改多个表的数据

updata <表名1>,<表名2> set 表名1.列名1=exper1,表名2.列名2=exper2...   where 条件  # 修改符合条件的行,如果省略 where 则修改所有的行 
例
updata <表名1>,<表名2> set 表名1.列名1=exper1,表名2.列名2=exper2...   where 条件  

updata student,teather set student.name='小明' teather.name="小红"   where   student.stu_id == '2020100' and teather.tea_id == '1223';
  • 删除记录
# 删除表中的行
delete from <表名> where 条件;   # 如果没有条件则会直接删除所有的行delete from student where student name == '李四';
  • 清空数据
truncate table 表名  # 清除表中的所有数据

查询数据

  • 查询表中的若干列
查询列名1,列名2 所有的数据
select 列名1,列名2 from student;
  • 查询表中的所有数据
select * from <表名>;

查询某些列的所有数据

select <列名>,<列名2> from  <表名>;select name,age from  user;   # 查询user表中的name和age

查询某些表中符合条件的数据

select * from <表名> where 条件 
条件可以是多个用
例
select * from user where aeg>10;   查询年龄大于10

select * from user where aeg>10 and sex="女";   查询年龄大于10的女生

过滤重复元素

select distinct name from user;  # 查询user表中的姓名,去除重复值

多表查询

内连接
内连接也称自然连接,是组合两个表的常用方法,内连接根据每一个表共有的列的值进行匹配两个表中的行,只是每一个表都存在相匹配的列值的记录才会出现在结果集中,在内连接中所有表都是平等的没有主次之分,
连接

select 列名 from1 [inner] join 表名2 on 表名1.列名 = 表名2.列名;
或者
select 列名列表 from 表名1,表名2 where 表名1.列名=表名2.列名;select A.id,A.name,B.class_name from student as A ,class as B 
where A.class_id = B.class_id

外连接
可以只限制一个表,而对另一个表不加限制,将另一表中的所有行都展现在结果集中(没有匹配到的数据称为悬浮元组)

参与外连接的表有主次之分,以主表中的每一行数据进行匹配,符合条件的数据返回到结果集中,不符合的,在另一个表相应列的位置填上 null

语法格式

左连接
主表在连接符的左边,从左侧引用左表的所有行后向外连接
select 列名列表 from1 as A left [outer] join 表名2 as B on A.列名=B.列名
右连接
主表在连接符的左边,从左侧引用左表的所有行后向外连接
select 列名列表 from1 as A right [outer] join 表名2 as B on A.列名=B.列名
列
select A.class_no,A.class_name,A.dep_name,B.stu_no,B.stu_name
from class as A left join student as B on A.class_no=B.class_no;as 可以省略
select A.class_no,A.class_name,A.dep_name,B.stu_no,B.stu_name
from class A left join student B on A.class_no=B.class_no;

自连接
如果在连接查询中,涉及的都是一个表,这种查询就是内连接查询

select 列名列表 from 表名1 as A ,表名1 as B 

where语句常用的一些查询条件

比较表达式
=(等于)、<(小于)>(大于) <>(不等于)、!>(不大于)、 !<(不小于)、 >=(大于等于)、 <=(小于等于)、!=(不等于)
例
查询年龄在20岁以的学生的姓名性别年龄
select stu_name,stu_sex,year(now())-year(stu_birth) as age 
  from student
  where year(now())-year(stu_birth) <20;

逻辑表达式
and,or, not

查询年龄在20岁以的女学生的姓名性别年龄
select stu_name,stu_sex,year(now())-year(stu_birth) as age 
  from student
  where year(now())-year(stu_birth) <20 and stu_sex ='女';

between… and … 关键字
查询 属性值在在指定范围内的元组,其中between后是范围的下限,and后面是上限

查询年龄在19-20岁的女生的姓名和年龄
select stu_name,year(now())-year(stu_birth) as age 
  from student
  where year(now())-year(stu_birth) <20 and stu_sex ='女' and ;

not between… and … 关键字
不在某个范围用法同上
in,not in
同between关键字一样,简化的检索的范围

表达式 [not] in (表达式1,表达式2)

select * from user 
where aeg in ('18','20')  
查询年纪为1820的人

like关键字
模糊查询,可以根据不确切的线索进行搜索信息

表达式 not like <匹配串>

常用的查询条件 只有在lik语句中才有意义

  • %百分号:表示0-n个任意字符
  • _下划线:表示单个任意字符
  • []封闭方括号:表示括号里面列出的任意一个字符
  • [^] :任意一个没有在方括号里面列出的字符

查询“王”姓学生的学号及姓名

select stu_no,stu_name 
  from student
  where stu_name like '王%';

涉及空值的查询
is null(为空),is not null(不为空)

查询选修课程却没有成绩的同学

select * 
	from score
	where score is null;
第一个 score 是表名,第二个是 列名

分组查询

聚合函数
count() max(),min() 用于数值,日期,字符
sum(),avg() 只能用于数值
聚合函数一般忽略null值

函数名 功能
sum() 对数值型或计算列求和
avg() 对数值型或计算列求平均值
max() 返回一个数值列或数值列或数值表达式的最大值
min() 返回一个数值列或数值列或数值表达式的最大值
count() 返回满足select语句中指定条件的记录个数
count() 返回查到的行

查询学生总人数
select count(*)
 from student;

查询选修020001课程的学生人数

select count(distinct stu_no) 
 from score
 where soure_no = '020001';

查询选修020001课程的分数最高的学生

select max(score)
 from score
 where soure_no = '020001';

group by
能够按照指定列对查询结果进行分组统计,该字句写在,where 后面


将学生表中按成绩统计出男生和女生的平均年龄及人数

select class_no,stu_sex ,avg(year(now())-year(stu_birth)) as 平均年龄,count(*) as 人数
 from student
 group by class_no,stu_sex;

未完待续

猜你喜欢

转载自blog.csdn.net/qq_52007481/article/details/124066976
今日推荐