MySql入门教学

基本概念

  1. 数据库:database ,是按照数据结构来组织、存储和管理数据的仓库。
  2. 数据库的特点:
  • 数据结构化:据库系统实现了整体数据的结构化,这是数据库的最主要的特征之一。
  • 实现了数据共享:因为数据是面向整体的,所以数据可以被多个用户、多个应用程序共享使用。
  • 数据独立性高:数据的独立性包含逻辑独立性和物理独立性,其中,逻辑独立性是指数据库中数据的逻辑结构和应用程序相互独立,物理独立性是指数据物理结构的变化不影响数据的逻辑结构。
  • 数据统一管理与控制:数据的统一控制包含安全控制、完整控制和并发控制。简单来说就是防止数据丢失、确保数据的正确有效,并且在同一时间内,允许用户对数据进行多路存取,防止用户之间的异常交互。
    3.数据库分类:
  • 关系型数据库:这种类型的数据库是最古老的数据库类型,关系型数据库模型是把复杂的数据结构归结为简单的二元关系(即二维表格形式)。
  • 非关系型数据库:关系型数据库以外的统称为非关系型数据库,简称NoSQL(not only SQL)。从存储结构可以划分键值存储数据库(Redis)、列存储数据库(HBase)、面向文档数据库(MongoDB)、图形数据库(Neo4J)、搜索引擎存储(Elasticsearch)。
    4.常见数据库
  • mysql:开源免费的数据库,小型的数据库.

命令行打开跟关闭服务
- net start mysql:启动MySQL的服务
- net stop mysql:关闭MySQL服务
MySQL的登录和退出
- 命令行
明文登录:mysql -uroot -proot,两个root分别表示用户名和密码。
密文登录:mysql -uroot -p 。回车后输入密码。
远程登录:mysql -h[远程电脑IP] -u用户名 -p密码。
SQL
- 结构化查询语言(Structured Query Language)简称SQL,SQL语句就是对数据库进行操作的一种语言。
- SQL语句可以分单行或者多行书写,以分号结尾。
- 可使用空格跟缩进增强语句的可读性。
- MySQL不区分大小写。
- 注释有3种:

  • – 单行注释,–后面紧跟的空格不能省略

  • # 单行注释

  • /* 多行注释 */
    SQL分类

    • DDL(Data Definition Language)数据定义语言
      -用来定义数据库对象:数据库,表,列等。关键字:create,drop,alter 等
    • DML(Data Manipulation Language)数据操作语言
      -用来对数据库中表的数据进行增删改。关键字:insert,delete,update 等
    • DQL(Data Query Language)数据查询语言
      -用来查询数据库中表的记录(数据)。关键字:select等
    • DCL(Data Control Language)数据控制语言(了解)
      -用来定义数据库的访问权限和安全级别,及创建用户。关键字:GRANT,REVOKE等
数据库操作

1,创建数据库

-- 直接创建数据库
create database 库名;
-- 如果数据库不存在则创建,存在则不创建。
create database if not exists 库名;
-- 创建数据库,不存在则创建,并制定指定字符集
create database if not exists 库名 character set utf8;
-- 数据库的字符集是直接utf8

2,查询数据库

-- 显示所有数据库
show databases;
-- 查看某个数据库的创建以及字符集
show create database 库名;

-- 修改数据库字符集
alter database 库名 character set utf8 ;

-- 查询当前正在使用的数据库
select database();
-- 使用数据库
use 库名;
表结构操作

1, 创建表

-- 语法
create table 表名 (
	列名 类型 [约束] [comment '备注'],
	列名 类型 [约束] [comment '备注'],
	列名 类型 [约束] [comment '备注'],
	......
	-- 最后一列后面不要加逗号
	列名 类型 [约束] [comment '备注']
)
-- 例如
create table student(
	id varchar(10),
	name varchar(10),
	sex varchar(10),
	age int ,
	s_date datetime
)

2,查询表

-- 查询表的数据
select * from 表名;
-- 查询某个数据中所有的表
show tables;
-- 查询表结构
desc 表名;
-- 查询表的创建SQL
show create table 表名;

3,修改表

--修改表名
alter table 表名 rename to 新的表名;
alter table student rename to stud;

-- 修改表的字符集
alter table 表名 character set 字符集的名称;
alter table student character set utf8;

-- 添加一列
 alter table 表名 add column 列名 列类型 [约束];
 -- 添加多列
 alter table 表名 add (列名 列类型 [约束],列名 列类型 [约束], ....);
 
 -- 添加到第一列或某一列后
 alter table 表名 add column 列名 列类型 [] [first]/[after 列名];
 alter table stud add column stu_height int not null first;
 alter table stud add column stu_height int not null after age;

-- 修改列名称跟类型
alter table 表名 change 原列名 新列名 新数据类型;
alter table stud change stu_weight stu_weight1 double;

-- 删除列
alter table 表名 drop 列名;
alter table stud drop column stu_weight;

4,删除表

drop table 表名;
drop table if exists 表名;
drop table student;
drop table if exists student;
表数据操作

1,新增数据

-- 语法
insert into 表名[(字段名1,字段名2.....)] values (字段值1,字段值2,....);
insert into student values ('001','zhangsan','男',19,str_date('%d.%m.%Y %h:%i:%s'));
-- 插入多个值的话可以一次性插入
insert into stud values ('001','zhangsan','男',19,'2020-07-03 12:11:12'),
('002','lisi','男',20,'2020-07-03 12:11:12'),
('003','wangwu','男',18,'2020-07-03 12:11:12');

列名跟值需要一一对应。
如果表名后没有跟指定字段则必须将全部所有字段值给出。

2,修改数据

-- 语法
update 表名 set 字段名1 = 新字段值1,字段名2=新字段值2 ...[where 条件];
update stud set age = 22,sex='女' where name='lisi';

如果不加任何条件,则会将表中所有记录全部修改。

3,删除数据

delete from 表名 [where 条件];
delete from stud where name 'zhangsan';

如果不加条件,则删除表中所有记录。
当需要删除所有数据(表结构保留)的时候也可以使用truncate table 表名。
delete from 属于DML,所以操作可以回关。而truncate table 表名属于DDL,所以操作不能回滚。
从效率上讲truncate table 表名高于delete from.

查询数据
  • 语法
    select 字段列表 from 表名列表
    [where 条件列表]
    [group by 分组字段列表]
    [having 分组后的条件列表]
    [order by 排序字段]
    [limit 分页限定]

1,基础查询

-- 多个字段的查询 
select 字段名1,字段名2... from 表名;
-- 如果查询表中的所有字段可以使用*号select * from 表名

-- 去重
select distinct 字段列表 from 表名

-- 列计算
select age+1 from student;
-- 如果有null参与的运算,计算结果都会为null,可以使用IFNULL函数解决
-- IFNULL:如果第一个参数为null的时候按照第2个参数值代替处理。
select ifnull(age,0)+1 from stud;

-- 取别名
select 字段名1 as 别名1,字段名2 as 别名2 ... from 别名;
select id as stuid,name as "stu name" from stud;
-- 如果别名字段有空格需要用引号
-- as 可以省略
 

2,条件查询

  • < >= <= <> !=

-- 查找stud表中age大于18的
select * from stud where age > 18;
-- <>和!=一样
select * from stud where age <>20;
  • BETWEEN AND
-- 18跟28都包括在内
select * from stud where age between 18 and 28;
  • is null , is not null
-- 对表中的字段进行空或者非空判断
select * from stud where sex is null;
select * from stud where sex is not null;
  • in , not in
select * from stud where age in(18,20,22);
select * from stud where age not in(18,20,22);
-- 使用not in 的时候如果表中有null的值的话,则查询无结果,需要先把null值过滤掉,这是MySQL的bug
select * from stud where age is not null and age not in(18,20,22);

in ,not in 后面的集合可以是具体值也可以是个字查询。
not in 在MySQL中也是有bug的,需要把null值给排除掉。

and ,or, not

select * from stud where name is not null and age >18;
select * from stud where age > 18 or sex = '男';
select * from stud where not age > 18;

and效果同&&、or效果同||、not效果同!

  • like
    用来进行模糊查询
    占位符**_表示任意一个字符
    占位符
    %**表示任意N个字符
-- 查询姓名第2个字符是h的学生信息
select * from stud where name like '_h%';

所有null值不会参与运算,如果需要参与可以使IFNULL函数

-- 查询所有年龄小于18岁的学生信息,如果年龄字段为null按照0处理。
select * from stud where ifnull(age,0)< 18;

3,排序查询
语法order by 排序字段1 排序方式1,排序字段2 排序方式2…
ASC升序(默认),DESC降序
多字段排序时,只有前面字段值相同时才把相同字段值的数据按照后面的排序字段跟方式排序。

-- 先按照年龄进行升序排序,年龄相同的再按照id降序排序
select * from stud order by age asc ,id desc;

4,聚合函数
将一列数据作为一个整体,进行纵向的计算。

  • count
    计算个数,一般选择非空的列,比如主键。计算表数据总条数一般使用count(*)
select count(*) as total_num from stud;
  • max 、min 、avg 、sum
    分别表示计算列的最大值、最小值、平均值、总和。
select max(age) as max_age from stud;
select min(age) as min_age from stud;
select avg(age) as avg_age from stud;
select sum(age) as sum_age from stud;
-- 查询stud表中最大年龄、最小年龄、平均年龄、总年龄
select max(age) max_age,min(age),avg(age),sum(age) from stud;

使用聚合函数后不能查询一般字段,如果使用了分组可以使用分组字段。
where后面不能跟聚合函数

5,分组查询
语法:group by 分组字段 [having 条件]

-- 按照性别进行分组
select sex from stud group by sex ;
-- 按照性别分组,分组后查询平均年龄>18的分组信息
select avg(age),sex from stud group by sex having avg(age)>18

分组后查询字段只能是分组字段跟聚合函数,非分组共有字段则无意义。
-where跟having的区别:
where跟having都表示条件过滤,where是先过滤在分组,having是先分组在过滤。
where后面不能跟聚合函数,having后可以跟聚合函数。

6.分页查询

  • limit X,Y;
    X为起始位置的索引,Y为取多少条数据。
-- 从索引为0的位置开始,取5条数据
select * from stud limit 0, 5;

-- 假设每页数据为pageSize,当前页数为pageNum,则取出当页数据的sql公式为:
select * from stud limit (pageNum-1)*pageSize , pageSize

只有MySQL可以通过limit分页,其它数据库不能使用这种方式。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值