ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL学习笔记

2021-06-16 20:02:19  阅读:203  来源: 互联网

标签:deptId ename 笔记 学习 dept emp MySQL where select


MySQL中的简单内容

一、常用管理命令

quit; ——退出服务器连接
show databases; ——显示当前数据库服务器下的所有数据库
use 数据库名称; ——进入指定数据库
show tables; ——显示当前数据库下所有的数据表
desc 数据表名称; ——描述指定数据表下有哪些列

二、SQL命令执行方式

脚本模式:mysql -uroot < 拖拽文本文件到此位置
交互模式

三、常用的SQL命令

# 设置客户端连接服务器端的编码
set names utf8;
# 丢弃数据库,如果存在
drop database if exists xuezi;
# 创建新的数据库
create database xuezi charset=utf8;
# 进入数据库
use xuezi;
# 创建数据表
create table laptop(
  lid int primary key, # 编号是为了体现数据的唯一性
  title varchar(64),
  price decimal(7,2),
  stockCount smallint not null,
  shelfTime date,
  isOnSale boolean # 1/0
);
# 插入数据
insert into laptop values('3', '手机', '4500.00', '12345', '2021-03-02', true);
insert into laptop values('2', '衣服', '145.99', '1000', '2020-01-20', true);
insert into laptop values(1, '鞋子', 99.99, 500, '2020-02-04', 120);
insert into laptop values(4, '帽子', 100.99, 100, null, 120);
# 修改数据表中的内容
update laptop set stockCount=0 where lid=3;
# 删除数据表中的行
delete from laptop where lid=2;

四、解决MySQL中文乱码

计算机存储字符:ASCII Latin-1 GB2312 GBK BIG5 Unicode
解决三步:1. 脚本文件另存为UTF-8;2. 客户端连接服务器的编码为UTF-8;3. 服务器端创建的数据库,设置存储的编码为UTF-8

五、列类型

1. 数值型

tinyint 微整形,占1个字节,范围-128~127
smallint 小整形,占2个字节,范围-32768-32767
int 整型,占4个字节,范围-2147483648-2147483647
bigint 大整型,占8个字节
float 单精度浮点型,占4个字节,3.4e38
double 双精度浮点型,占8个字节
decimal(M, D) 定点小数,M代表总的有效位数,D代表小数点后的有效位数
bool/boolean 布尔型,使用时自动转成tinyint

2. 日期时间型

date 日期型 2021-03-02
time 时间型 14:56:30
datetime 日期时间型 2021-03-02 14:56:30

3. 字符串型

varchar(M) 变长字符串,几乎不会产生空间浪费,M最大值为65535
char(M) 定长字符串,可能会产生空间浪费,但是操作速度相对快,M最大值为255
text(M) 大型变长字符串,M的最大值为2G

六、列约束

主键约束——primary key 自增列——auto_increment
非空约束——not null
唯一约束——unique
默认值约束——default
检查约束——check(MySQL)不支持
外键约束——foreign key(外键列) references 另一个表(主键列);

七、查询

给列起别名 as
select eid as 编号,ename as 姓名 from emp;
显示不同的记录 distinct
select distinct sex from emp;
查询结果排序 order by ... asc/desc
select * from emp order by sex,birthday desc;
条件查询 where,搭配比较运算符(> < >= <= = !=)、is 、is not、and、[not] between ... and...、or、in()、not in()
select * from emp where ename = 'lucy';
select * from emp where deptId is null;
select * from emp where deptId is not null;
select * from emp where salary>=5000 and salary<=8000;
select * from emp where salary between 5000 and 8000;
select * from emp where salary<5000 or salary>8000;
select * from emp where salary not between 5000 and 8000;
select * from emp where deptId in (2, 3, 1);
select * from emp where deptId not in (2, 3, 1);
模糊条件查询 like搭配%、_
select * from emp where ename like '%e%';
select * from emp where ename like '%e';
select * from emp where ename like '%e_';
分页查询 limit 当前页第一条数据编码, 每页数据量
select * from emp limit 0,5;
聚合查询 count()/sum()/avg()/max()/min();分组查询 group by
select count(eid) from emp;
select sum(salary) from emp;
select avg(salary) from emp where sex=0;
select min(salary) from emp where deptId=1;
select max(birthday) from emp;
select count(eid),max(salary),min(salary),sex from emp group by sex;
子查询
select * from emp where year(birthday)=year((select birthday from emp where ename='Tom')) and ename!='Tom';
多表查询,前提是在创建表的时候已经建立了关联,一个表外键对应另一个表主键
select emp.ename,dept.dname from emp,dept where emp.deptId=dept.did;
select ename,dname from emp inner join dept on deptId=did;
select ename,dname from emp left outer join dept on deptId=did;
select ename,dname from emp right outer join dept on deptId=did;
(select ename,dname from emp left join dept on deptId=did) union (select ename,dname from emp right join dept on deptId=did);
(select ename,dname from emp left join dept on deptId=did) union all (select ename,dname from emp right join dept on deptId=did);

标签:deptId,ename,笔记,学习,dept,emp,MySQL,where,select
来源: https://www.cnblogs.com/CarlaZhou/p/14890824.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有