MySQL中可以创建多个数据库用于存储不同场景的表结构,学习MySQL之前,我们要先理清如下的关系:
数据库 --> 数据表 --> 字段
抛开数据库存储上限去考虑,每个数据库中可以包含无数个数据表,而每个数据表又可以包含无数个字段,因此我们的学习也应该从创建数据库开始。
create database 数据库名 charset=utf8; 例: create database mydata charset=utf8; show databases;show databases; 
use 数据库名; select database(); drop database 数据库名; 例: drop database mydata; 创建好了数据库,我们就可以在某个数据库中进行数据表的创建与操作了,数据表是MySQL语法中的核心部分,数据工作者的多数时间都在进行数据表中各个字段的“增删改查”。
show tables; desc 表名; 给定字段名和数据类型时,我们就可以进行数据表的创建了,当然创建表的时候也有很多特殊的可选项,具体如下:
数据表创建示例(简版):
MySQL常用数据类型介绍:点击此处
CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype ); create table Score( name varchar(20), age tinyint, score int ); create table students( id int unsigned primary key auto_increment not null, name varchar(20) default '', age tinyint unsigned default 0, height decimal(5,2), gender enum('男','女','保密'), cls_id int unsigned default 0 ); 上述代码块创建的时候使用了许多特殊语句,解释如下:
- unsigned:限制整数类型为无符号数据(没有负数)。
- primary key:主键约束,在表中定义一个主键来唯一确定表中每一行数据的标识符。主键可以是表中的某一列或者多列的组合,其中由多列组合的主键称为复合主键。
- auto_increment:主键自动增长,从1开始,每多插入一条数据,主键值+1。
- default:为该字段设定默认值。
- decimal:保留准确精确度的列,例如decimal(5,2)标识一共能存储5位数,小数点后可以有2位,因此可保存数据的范围是
-999.99-999.99。- enum:只能从其中选择一个值插入到数据库。
数据库中创建好的内容不是一成不变的,我们可以随时对表结构进行增删改的操作。
alter table 表名 add 列名 类型; 例: alter table students add birthday datetime; alter table 表名 change 原名 新名 类型及约束; 例: alter table students change birthday birth datetime not null; alter table 表名 modify 列名 类型及约束; 例: alter table students modify birth date not null; alter table 表名 drop 列名; 例: alter table students drop birthday; 建议慎用所有的删除语句。
drop table 表名; 例: drop table students; show create table 表名; 例: show create table classes; 表创建好之后,我们可以使用SQL语句添加数据,但并不是所有场景都会用到该语句,工作中数据添加的方式多是:端口实时同步,表格导入等。
insert into 表名 ('字段名1', '字段名2') values(值1, 值2); values中填写所有字段对应的值,否则将会报错。insert into 表名 values(值1, 值2); insert into students(id, name, age, height, gender, cls_id) values (10001, 'xiaoming', 22, 180, '男', 1); 
insert into students values (10002, 'xiaolv', 23, 165, '女', 1); 
insert into students(id, name, age, height, gender, cls_id) values (10003, 'xiaohu', 22, 175, '男', 2), (10004, 'xiaoli', 24, 160, '女', 1), (10005, 'xiaoxi', 23, 167, '女', 2); 
表查询操作是数据库中最重要的操作(没有之一),可以说对于数据库,每天的工作就是组合各种各样的查询语句进行不同场景的数据查询。
select * from 表名; 例: select * from students; 
select 列1,列2,... from 表名; 例: select id, name from students; 例(使用别名): select id as id2, name from students; 
使用where子句对表中的数据筛选,结果为true的行会出现在结果集中
select * from 表名 where 条件; 例: select * from students where id=10001; 
select * from students where id > 10003; 
select * from students where id <= 10004; 
select * from students where name != 'xiaoming'; 
select * from students where id > 3 and gender='女'; 
select * from students where id < 10002 or age > 24; 
like语句进行模糊查询,模糊查询常用的符号如下:%表示任意多个任意字符_表示一个任意字符select * from students where name like 'xiaol_'; 
select * from students where name like '%hu'; 
select * from students where id in(10002,10004); 
select * from students where id between 10002 and 10004; 
# 插入带有空值的数据 insert into students(id, name, age) values (10006, 'xiaomei', 30); # 查询没有填写身高的学生 select * from students where height is null; 
select * from students where gender is not null; 
为了方便查看数据,可以对数据进行排序
语法:
select * from 表名 order by 列1 asc|desc ,列2 asc|desc,...; 说明
示例
select * from students where gender='女' and cls_id=1 order by id desc; 
select * from students where cls_id=1 order by height; 
select * from students order by age desc,height desc; 
操作数据的时候,我们经常需要进行统计计算,SQL中提供了多种聚合函数可以让我们快速计算。
计数
select count(*) from students; 
最大值/最小值
select max(id) from students where gender='女'; 
求和
select sum(age) from students where gender='男'; 
平均值
select avg(age) from students where gender='女'; 
聚合函数组合
count和sum来求得平均值。select sum(age)/count(*) as avg_age from students where gender='女'; 
group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组group by的方法如下select gender from students group by gender; 
上段代码根据gender字段来分组,gender字段的全部值有3个’男’,‘女’,NULL,所以出现了三个数据,但是单独使用groupby是没有过多意义的(可以作为去重来使用),通常分组函数要配合聚合函数/concat函数一同使用。
group by + group_concat()
select gender,group_concat(name) from students group by gender; 
group by + 集合函数
值的集合做一些操作select gender,avg(height) from students group by gender; 
group by + having
select cls_id, count(*) from students group by cls_id having count(*)>2; 
当数据量过大时,在一页中查看数据是一件非常麻烦的事情,我们可以使用limit进行限制进行分页查询。
语法
select * from 表名 limit start,end select * from students where gender='女' limit 2; 
select * from students where gender='女' limit 0,2; 
在很多工作中我们需要面对成百上千个不同的数据表,当进行查询的时候,我们需要的结果也会来自于不同的表,这种情况下查询时我们需要将多张表连接成一个大的数据集,再选择合适的列返回
mysql支持三种类型的连接查询,分别为:
内连接查询
查询的结果为匹配字段相同时两个表匹配到的数据

左连接查询
查询的结果为右表匹配字段中的值在左表的匹配字段中存在时匹配到的数据,左表特有的数据,对于左表中无法被右表匹配的数据使用null填充

右连接查询
查询的结果为左表匹配字段中的值在右表的匹配字段中存在时匹配到的数据,右表特有的数据,对于右表中无法被左表匹配的数据使用null填充

-- 分数表 create table scores ( id int, subject varchar(20), score int ); -- 学生表 create table students2 ( id int, name varchar(20) ); insert into students2 values (10001, '张三'), (10002, '李四'), (10003, '王五'), (10004, '赵六'); insert into scores values (10001, '数学', 80), (10001, '语文', 90), (10003, '数学', 70), (10004, '数学', 100), (10005, '语文', 70); 语法
select * from 表1 inner/left/right join 表2 on 表1.列 = 表2.列 select students2.id, name, subject, score from students2 inner join scores on students2.id = scores.id; 
select students2.id, name, subject, score from students2 left join scores on students2.id = scores.id; 
select scores.id, name, subject, score from students2 right join scores on students2.id = scores.id; 
子查询概念
我们在进行select查询的时候,查询的结果本质上也是一张数据表,我们可以使用自查询的方式继续对该表进行查询。
在一个 select 语句中,嵌入了另外一个 select 语句,那么被嵌入的 select 语句称之为子查询语句。
主查询
主要查询的对象,第一条 select 语句
主查询和子查询的关系
select * from scores where score >= (select avg(score) from scores); 
具有考试成绩的学生id
找出学生表中对应的名字
select name from students2 where id in (select id from scores); 
in () 括号内的内容表示查询范围,数据在括号中存在则满足。
select * from students2 inner join (select * from scores where subject='数学') a on students2.id = a.id; 
上述代码中,我们对一个查询到的结果表进行了join。在使用该方法操作时,我们使用
()将自查询扩起来,并在末尾对查询结果临时命名为a,当命名为a后,我们可以认为有数据表a可以供我们使用,a中的内容就是我们查询到的结果,接下来按照常规数据表连接查询方式进行操作即可。