MySQL数据库 mysql常用命令
什么是数据库
数据库:存储数据以及数据结构的仓库,称为DB
注意:数据结构比数据更为重要 数据结构不合理 数据库也会慢
① 数据库本身是透明的空的

image-20211007104529267.png
② 数据库是需要管理的 :数据库管理系统 作用就是 管理数据
什么是数据库管理系统
专门管理数据库的,称为DBMS phpmyadmin
谁去管理
用户
数据库系统包含:
** 用户 User
数据库管理系统 DBMS
数据** DB
数据库的作用
帮助 查询 存储 管理 数据的
数据库的本质
数据库的本质是一个二维数组的表格,和excel是一样的
1 表名: 对应的是每一个实体,按照对象的概念来划分,
① teacher_table
② student_table
③ message_table
④ course_table
2 属性: 字段
数据库的种类
1 关系型数据库:mysql sql Server oracle db2
2 非关系型 (现在不用了) redis
1 mysql 登录和退出
mysql [-h] -u -pmysql -u root -p 123456exit 退出quit
2 查看辅助信息
select now(); 查看 当前时间select curdate(); 当前日期select curtime(); 当前时间select version(); mysql版本select user(); 查看用户
3 数据库和数据类型
① show database:显示数据库② create database 数据库名称:创建数据库③ drop database 数据库名称:删除数据库④ use 数据库名称
4 数据表
create table 数据表名称(列定义列名称 类型 【默认值】【auto_increment自增长】【主索引 primany key】列名称 类型 【默认值】列名称 类型 【默认值】列名称 类型 【默认值】......)
5 Mysql 数据类型
1 整型 | |||
---|---|---|---|
tinyint | 1 字节 | 有符号 | -128===>127 |
无符号 | 0===>225 | ||
smallint | 2字节 | 有符号 | -32768===>32767 |
无符号 | |||
int | 4字节 | 有符号 | -2147483648===>2147483647 |
无符号 | 0===4294967295 | ||
bigint | 8字节 | ||
2 浮点 | |||
float | 4字节 | 会丢失字节 | |
double | 4字节 | 会丢失字节 | |
DECIMAL[m,d] | 精度小数 | ||
3 字符 | |||
char | 定长字符 | 255 只有一个字符也占255 | |
varchar | 变长字符 | 0-255 | |
text | 65535个字符 | 评论 文章 | |
MEDIUMBLOB | 2的24次方 | ||
enum(val1,val2,val3) | 列枚举 | 只能取其中一个 sex | |
4时间日期 | |||
DATE | 日期 | ||
DATETIME | 时间 | ||
time | 日期时间 |
6 创建表
create table stu(id int(6) auto_increment primany key,stuNum varchar(6),stuName varchar(20),stuAge tinyint(2),stuSex enum("1","2"),stuTel varchar(20))查看表 show table 查看表结构 desc 表名称
SQL 命令的DDL操作
1 表添加字段
alter table 表名称 add 列定义alter table stu add email varchar(20);
2 修改字段
alter table 表名称 change 旧字段名称 新字段定义alter table stu change email stuEmail varchar(20);
3 删除字段
alter table 表名 drop 字段名alter table stu drop student
4 修改表名称
alter table 表名 rename 新名字alter table stu rename student
5 删除表
drop table 表名称drop table student
SQL命令的DML操作(增删改查)
1 增
insert into 表名称(字段1,字段2,字段3,...)values(val1,val2,val3);
2 删
delete from 表名称 where 条件
3 改
update 表名称 set 字段=值,字段=值,字段=值,... where id=3;
4 查
select * from 表名
完整格式
select 字段列表 form 表名称[where 条件][order by 字段 asc/desc][limit 起始位置,长度][group by 字段名称(分组)]
查找多个字段
select 字段名,字段名,字段名...from 表名称(查询指定字段)
别名
可以给字段名或表名起别名,别名的作用 字段简单 方便调用select StuNum as Stn, StuName as Sn from student as Sd;
order by
asc 升序
desc 降序select * from student order by id desc;
select * from student order by pid asc;
limit
limit 起始位置,长度 用于分页 首页select * from student limit 1,3(从第二条开始截取 截取3条);
select * from student limit 4,4 (从第五条开始截取 截取4条)倒序截取select * from student order by id desc limit 0,4;
group by 分组
select * from books group by TypeId 按类型id分组
分组之后,每组种的记录都会取1条
where 条件
1 比较 < > <= >= =
select * from books where id=100;
select * from books where pid>100;
2 逻辑运算 and or
select * from books where bid>100 and bid<110;
3 模糊搜索
字符种含有某个关键词就能找到 like %关键词% 表示任意字符_关键字_ 表示一个字符select * from books where bName like "%入门%";
4 范围
1 连续范围[not]between...and (not 表示不是这个范围的其他)select * from books where bid between 10 and 50;
等同于
select * from books where bid>=10 and bid<=50;2 不连续范围 [not] in()select * from books where bid in(105,108,110);
等同于
select * from books where bid=100 or bid=103 or bid=108;
5 子查询
在select语句中又出现查询语句,称为子查询
Btype表 | Books表 | |||
---|---|---|---|---|
bTypeId | bTypeName | bid | bName | bTypeId |
1 | windows应用 | 93 | 书名1 | 1 |
2 | <u>网站</u> | 94 | 书名2 | 1 |
3 | 3D动画 | 95 | 书名3 | <u>2</u> |
4 | Delphi学习 | 96 | 书名4 | 3 |
5 | 黑客 | 97 | 书名1 | 8 |
6 | 网络技术 | 98 | 书名5 | 8 |
7 | 网络安全 | 99 | 书名6 | 5 |
8 | Linux学习 | 100 | 书名7 | 5 |
9 | AutoCAD | 101 | 书名8 | 5 |
10 | Photoshop | 102 | 书名9 | 3 |
select bTypeId from bType where bTypeName="网站"select * from books where bTypeId=2;
上面语句的结果,作为了下面语句的开始,结合在一起
select * from books where bTypeId=(select bTypeId from bType where bTypeName="网站");括号里面的 select bTypeId from bType where bTypeName="网站" 结果是2
加上前面的
select * from books where bTypeId=2
这段代码的含义是,查询在所有图书中查询 类型是 网站 的
所以查询是网站 这个条件是主要条件 那么 select bTypeId from bType where bTypeName="网站" 这句是主表
被查询的表是 从表
主表:后面需求中涉及到的表
从表:前面需求中涉及到的表
此种方法效率比较低,一般使用链接查询
6 链接查询
通过多张表的共有字段,查找多张表构成的 合并的表 至少两张表以上
1)内连接
**共同字段相等(没有地位之分,都是一样的)**
语法select * from 表1,表2 where 表1.共有字段=表2.共有字段select * from book,bType where books.bTypeId=bType.bTypeIdselect * from book as bs,bType as bt where bs.bTypeId=bt.bTypeId
2) 外连接
**有主次之分 表分为 主表 和 从表**主表的数据 全部展示从表的数据 有和主表对应的数据展示不和主表对应的数据不展示保证主要内容都要显示left join 查主表
语法:
select * from 主表 left join 从表 on 主表.共有字段=从表.共有字段 where [其他条件]select * from books left join bType on books.bTypeId=bType.bTypeId
select * from books as bs left join bType as bt on bs.bTypeId=bt.bTypeId
right join 查从表
语法:
select * from 从表 right join 主表 on 主表.共有字段=从表.共有字段 where[其他条件]select * from bType right join books on bType.bTypeId=books.bTypeId
select * from bType as bt right join books as bs on bType.bTypeId=books.bTypeId
7 mysql中的聚合函数
**什么是聚合函数:**mysql提供的系统实现特定功能的函数1) 算数函数:sum()求和 select sum(bTypeId) from booksavg()求平均数 select avg(bTypeId) from bookscount()求个数 select count(bTypeId) from books
2) 拼接字符串 (将多个字符串拼接成一个字符串)
concat(str1,str2,str3,...);select contat(bName,bTypeId) from books;select contat(bName,bTypeId) as p from books;//将两个字段拼接为 p 这个字段
8 mysql中的索引
索引:相当于图书的目录,类似于查询目录 是以文件的形式存储的特点:数据的类型和索引的更新是一样的(类似于 书的内容更新了 ,目录也需要更新)作用:提高查询效率索引的类型: 1 主索引: primary key 确定唯一记录的 where id=???2 普通索引 ?3 唯一索引 ?4 全文索引 ?
9 mysql中的外键
作用:让用户不更新数据,或者是用户删除数据的时候,让订单也同步删除
mysql的编码: utf8-general_ci
© 著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务

喜欢的朋友记得点赞、收藏、关注哦!!!