去下划线?
一.知识点
△用数据库的原因
1文件操作的复杂度 2同步 3并发处理 4安全
△数据库管理系统-DBM
# 网络应用服务端 # 我们要使用服务端的数据 - 需要有一个客户端 # 客户端可以自己写 : 未来写代码的时候 # 也可以用别人写好的 : 第三方的工具 数据库管理软件的公司出版的官方客户端 # 数据库管理系统本质上也是管理一堆文件 # 只不过人家的管理方式比我们更高效 更安全
△数据库管理员-DBA
# 搭建数据库服务环境 # 用户的创建 权限的管理 # 性能\语句的优化 # 数据库的二次开发 : 让数据库具有公司的特质
△软件
# mysql : 小公司 # 甲骨文 oracle : 事业单位 金融企业 # 微软 sql server # sqllite
数据库的分类
# 关系型数据库 mysql oracle sqlserver sqllite # 非关系型数据库 redis mongodb memcache hbase
关系型数据库
优点:
1、易于维护:都是使用表结构,格式一致
2、使用方便:SQL语言通用,可用于复杂查询
3、复杂操作:支持SQL,可用于一个表以及多个表之间非常复杂的查询
缺点:
1、读写性能比较差,尤其是海量数据的高效率读写
2、固定的表结构,灵活度稍欠
3、高并发读写需求,传统关系型数据库来说,硬盘I/O是一个很大的瓶颈
非关系型数据库
优点:
1、格式灵活:存储数据的格式可以是key,value形式、文档形式、图片形式等等
2、速度快,成本低:nosql数据库部署简单,基本都是开源软件
缺点:
1、不提供sql支持,学习和使用成本较高
2、无事务处理
3、数据结构相对复杂,复杂查询方面稍欠
> Markdown 是一种轻量级标记语言,它允许人们使用易读易写的纯文本格式编写文档,然后转换成格式丰富的HTML页面。 —— 维基百科 正如您在阅读的这份文档,它使用简单的符号标识不同的标题,将某些文字标记为***粗体****或者斜体*,创建一个链接或一个脚注1。下面列举了几个高级功能,更多语法请按的
查看帮助。
△名词
# DB 数据库 - 文件夹 # table 表 - 文件 # data 一条数据-每一行数据
△服务端-安装启动关闭
# mysqld install 安装 # net start mysql 启动 以管理员身份在cmd中输入 # net stop mysql 关闭
creat user 'alex'@'%'identified by'123'; #创建用户alex 在所有网段允许 设置密码为123
△三种方法操作数据库
终端,python,第三方工具SQlyog
△二.mysql编码问题
编码问题
1.临时解决问题在客户端执行set XXXX = utf8;
2.永久解决问题fimy. ini添加set xXxx = utf8;
3.实时解决问题create table表名() charset=utf8;
△SQL语言的5个部分(重要性从高到低):
数据查询语言(DQL:Data Query Language):select
其语句,也称为“数据检索语句”,用以从表中获得数据,确定数据怎样在应用程序给出
数据操作语言(DML:Data Manipulation Language):inster,updata,delete
其语句包括动词INSERT,UPDATE和DELETE。它们分别用于添加,修改和删除表中的行。也称为动作查询语言
数据定义语言(DDL):create,alter,drop
在数据库中创建新表或删除表;为表加入索引等
数据控制语言(DCL):grant,revoke
它的语句确定单个用户和用户组对数据库对象的访问
事务处理语言(TPL):
它的语句能确保被DML语句影响的表的所有行及时得以更新
前三个部分必须掌握,后两个部分可以现用现查
同时注意:SQL 对大小写不敏感
△
1,先跟数据库进行连接(用账号和密码,默认3306端口)
2,创建数据库:
create database 数据库名:
删除数据库(慎用!):
DROP DATABASE 数据库名称;
显示所有数据库:
SHOW DATABASES;
切换数据库:
USE 数据库名称 ;
3.创建表:
4.查看表结构
二.管理员账户设置密码?√
# 默认用户登陆之后并没有实际操作的权限 # 需要使用管理员root用户登陆 # mysql -uroot -p -h mysql5.6默认是没有密码的 遇到password直接按回车键 # mysql> set password = password('root'); 给当前数据库设置密码 # mysql> select user(); #查看当前用户 # 输入“ipconfig”即可查看到本机的ip信息。 # ;表示sql语句的结束 # \c放弃当前要执行的sql语句 # mysql> exit # 也可以用\q quit退出
1.创建用户账号并授权
mysql> grant all on *.* to 'eva'@'%' identified by '123' 5.6及以下版本 mysql> grant all on *.* to 'eva'@'localhost' identified by '123'
①细节
创建用户
mysql> create user 'eva'@'192.168.10.%' IDENTIFIED BY '123';# 指示网段 mysql> create user 'eva'@'192.168.10.5' # 指示某机器可以连接 mysql> create user 'eva'@'%' #指示所有机器都可以连接 mysql> show grants for 'eva'@'192.168.10.5';查看某个用户的权限
给账号授权
mysql> grant all on *.* to 'eva'@'%'; #grant 权限类型 on 数据库名称 . * (或表名) to 'shang'@'%'; mysql> flush privileges; # 刷新使授权立即生效
三.用户端-操作
1. 操作文件夹(库) 数据备份: mysqldump –uroot –p test101 > C :\t1.sql (如果有警告可以忽略) 数据恢复: mysql –uroot –p 数据库名 < d:\t1.sql 增:create database db1 charset utf8; charset utf8可省略,创建数据库 查:show databases; 显示所有数据库 改:alter database db1 charset latin1;(改成欧洲的) 删除: drop database db1; 删除数据库(慎用!) 切换::use db1; 切换数据库
2. 操作文件(表) 先切换到文件夹下:use db1 增:create table t1(id int,name char); #ALTER TABLE t_person ADD age INT; 增加列名 类型 查: select database(); 查看当前所在库 show tables; 查看所有的表 desc 表; 查看表结构 show creat table 表名; 查看建表语句和搜索引擎,显示的信息更全面 show engines; 查看MySQL重要引擎 改: #RENAME TABLE t_person TO t_user; 更改表名称 #alter table t1 modify name char(3); 修改列的数据类型 #alter table t1 change name name1 char(2); 修改列名和数据类型 #ALTER TABLE t_person DROP ageNum; 删除列 删:drop table t1
3. 操作文件中的内容(记录) 增:insert into t1 values(1,'egon1'),(2,'egon2'),(3,'egon3'); # insert into t1(id,name) values (5,'wusir'), (6,'wusir'); # insert into t2 select * from t1; 先执行执行select,把查到的内容导入到t2 # insert into t1(id,name) select id,name from t1; 查:# select * from 表; 查询表的信息 # select emp_name,salary from employee; 指定列查询 # select emp_name,salary*12 from employee; 在列中使用四则运算 # select emp_name,salary*12 as annul_salary from employee; 重命名 # select emp_name,salary*12 annul_salary from employee; 重命名 # select distinct post from employee; 去重 # select distinct sex,post from employee; 双向去重 改:# update 表 set 字段1=值1,字段2=值2 where 条件; 删:delete from t1 where id=1;
1.函数 concat() 拼接
# select concat('姓名 :',emp_name),concat('年薪:',salary*12) from employee; # select concat_ws('|','a','b','c')
2. case when语句 == if条件判断句
3SELECT ( CASE WHEN emp_name = 'jingliyang' THEN emp_name WHEN emp_name = 'alex' THEN CONCAT(emp_name,'_BIGSB') ELSE concat(emp_name, 'SB') END ) as new_name FROM employee;
silf语句
四.基础数据类型
数字类型
整数 :
tinyint(m) 1个字节 -128~127
smalint(m) 1个字节 -32768~32767
==mediumint== 3个字节 -8388608~8388608
==int(m)== 4个字节 -2147483648~2147483647
bigint(m) 8个字节 -9223372036854775808~9223372036854775807
m: 显示宽度,和数据类型的取值范围是无关的
(int unsigned) : unsigned 标上表示无符号
小数 :
MySQL中使用浮点数和定点数来表示小数。它们都可以用(m,n)来表示,其中m称为精度,表示总共的位数;n称为标度,是表示小数的位数(会四舍五入)
==float==
==double==
时间类型
year 年 酒的产期
time 时:分:秒 计时软件
date 年-月-日 入职日期 出账日期 还款日期
datetime 年-月-日 时:分:秒 1000-01-01 00:00:00~9999-12-31 23:59:59 日志,消费记录,上下班打卡
timestamp 年-月-日 时:分:秒 不能为空,自动写当前时间,级联更新,范围小
now() 函数表示当前时间
让datetime 自动写当前时间,级联更新 create table 表( 列 datetime NOT NULL #非空 DEFAULT CURRENT_TIMESTAMP #自动写当前时间 ON UPDATE CURRENT_TIMESTAMP #级联更新当前时间 )
字符串类型
char(255) :定长的 节省时间 浪费空间 手机号码、身份证号
varchar(65535) : 变长的 节省空间 浪费时间 评论
枚举和集合
enum 单选
set 多选,自动去重
例如建表时定义字段
爱好 set(“游泳”,“篮球”,“下棋”,“音乐”,“旅游”)
insert into t_1 values("游泳,篮球,音乐")
添加时会自动去重和去掉不存在的
1.举例-数字 字符串
# create table t1(i1 tinyint,i2 int); 默认创建的数据类型都是有符号的 i1-表头 # create table t2(i1 tinyint unsigned,i2 int unsigned); 给字段添加一个unsigned表示无符号 # create table t3(f1 float,f2 double); # mysql> create table t1(id int,name varchar(20),sex enum('man','wuman'),age int,hire_date date,post varchar(20),post_comment varchar(20),salary double(7,2),office int,depart_id int); 一般在用char varchar时要限制字符
2.举例-小数
# create table t3(f1 float,f2 double); # create table t4(f1 float(7,2)); 小数点后2位,小数点前5位
3.举例-时间
# create table t7(dt datetime NOT NULL DEFAULT CURRENT_ TIMESTAMP ON UPDATE CURRENT_ TIMESTAMP,y year); NOT NULL DEFAULT CURRENT_ TIMESTAMP ON UPDATE CURRENT_ TIMESTAMP 让datetime的时间在无数据填充的情况下会自动更新到此次修该的时间
4.举例-枚举和集合 enum和set
# create table t8(username cha(12),gender enum('male','female')); # create table t8(username cha(12),hobby set('喝酒','抽烟','烫头','洗脚')); 填充的时候也必须时字符串格式
五.查
使用算数表达式查询某几列:年龄增加50
SELECT uname,country,age+50 FROM t1;
as
SELECT age+50 AS '年龄' FROM t_user; #修改列名的显示(起别名)注意不要用 关键字,as可以省略
distinct 查询时去重
SELECT DISTINCT country FROM t_user; SELECT DISTINCT country,uname FROM t_user; #联合去重 查询每个国家都有谁,注意:distinct后面的字段用逗号分隔,逗号两边不能有空格
""" asad sa """
比较运算
= > < >= <= != / <>
# select * from employee where age>18; # select * from employee where salary<10000; # select * from employee where salary=20000;
between
a and b [a,b]
# select * from employee where salary between 10000 and 20000;
in
# select * from employee where salary in (17000,19000);
like 模糊查询
_ 通配符 表示一个字符长度的任意内容
select * from employee where emp_name like 'jin___'
% 通配符 表示任意字符长度的任意内容
select * from employee where emp_name like 'jin%' select * from employee where emp_name like '%g' select * from employee where emp_name like '%n%'
regexp 正则匹配
# select * from employee where emp_name regexp '^jin' 查看所有员工中名字是jin开头,n或者g结果的员工信息 select * from employee where emp_name regexp '^jin.*[gn]$';
关键字IS NULL
(判断某个字段是否为NULL不能用等号,需要用IS)
# SELECT emp_name,post_comment FROM employee WHERE post_comment IS NULL; # SELECT emp_name,post_comment FROM employee WHERE post_comment IS NOT NULL; # SELECT emp_name,post_comment FROM employee WHERE post_comment=''; 注意''是空字符串,不是null ps: 执行 update employee set post_comment='' where id=2; 再用上条查看,就会有结果了
逻辑运算
and # select * from employee where age>18 and post='teacher'; or # select * from employee where salary<10000 or salary>30000; not # select * from employee where salary not in (10000,17000,18000);
分页
在查询时可以只检索前几条或者中间某几行数据(数据量很大时,几百万条)
SELECT * FROM t_user LIMIT 0,3;
limit 后面的第一个数字设置从哪里开始检索(偏移量,从0开始)
limit 后面的第二个数字是设置显示多少条
5个聚合函数
常用函数
count 统计值
select concat ('<名字:',name, '>' , '<薪资:',salary,'>') from 表;
max 最大值
min 最小值
avg 平均值
sum 最大值
# 分组聚合 group by # 查询岗位名以及岗位包含的所有员工名字 # select post,group_concat(emp_name) from employee group by post; # 查询各部门年龄在20岁以上的人的平均薪资 # select post,avg(salary) from employee where age>20 group by post; # select * from 表 where 条件 group by 分组
ascii(str)
查看字符的ASCII码值,str是空时返回0
SELECT ASCII('a')
char(数字)
查看ASCII码值对应的字符
SELECT CHAR(97)
concat(str1,str2,...)拼接字符串
SELECT CONCAT(12,34,'ab')
SELECT uname,CONCAT(age,'岁') FROM t_user;
过滤 having (group by + 聚合函数)
# 查询平均薪资大于1w的部门 # select avg(salary) from employee group by post having avg(salary) > 10000 # 1. 查询各岗位内包含的员工个数小于2的岗位名、岗位内包含员工名字、个数 # select post,emp_name,count(id) from employee group by post having count(id)<2 # 2. 查询各岗位平均薪资大于10000的岗位名、平均工资 # select post,avg(salary) from employee group by post having avg(salary) > 10000 # 3. 查询各岗位平均薪资大于10000且小于20000的岗位名、平均工资 # select post,avg(salary) from employee group by post having avg(salary) between 10000 and 20000;
order by 排序
# asc 升序 # select * from employee order by salary; # select * from employee order by salary asc; # desc 降序 # select * from employee order by salary desc; # select * from employee order by age,salary; # select * from employee order by age,salary desc; # select * from employee order by age desc,salary;
排列顺序从高到低 DESC
limit
# select * from 表 order by 列 limit n; 取前n条 # select * from 表 order by 列 limit m,n; 从m+1开始,取n条 # select * from 表 order by 列 limit n offset m; 从m+1开始,取n条
公式
select * from 表 where 条件 group by 分组 having 过滤 order by 排序 limit n;
一.引擎
# show create table books; 查看表引擎 # show engines;查看MySQL重要引擎 # 什么是存储方式、存储机制(存储引擎) # 表结构 存在一个文件中 : 硬盘上 # 表数据 存在另一个文件中、内存中 # 索引(目录) 为了方便查找设计的一个机制 : # 存储引擎的种类 # innodb : 索引+数据 表结构 数据的持久化存储 # 事务 :一致性 n条语句的执行状态是一致的 # begin; # 开启事务 # select id from innot where id =1 for update; # update innot set id = 2 where id = 1; # commit; # 提交事务 解锁被锁住的数据,让他们能够被修改 # 行级锁 :只对涉及到修改的行加锁,利于并发的修改,但是对于一次性大量修改效率低下 # 表级锁 :一次性加一把锁就锁住了整张表,不利于并发的修改,但是加锁速度比行锁的效率要高 # 外键约束 :被约束表中的数据不能随意的修改/删除 约束字段据要根据被约束表来使用数据 # myisam : 索引 数据 表结构 数据的持久化存储 # 表级锁 # memory : 表结构 # 数据断电消失 # create table innot(id int) engine = innodb; # create table myist(id int) engine = myisam; # create table memot(id int) engine = memory;
二.约束
not null 非空
# create table t1(id int not null,name char(12)); # 默认插入0 # create table t2(id int,name char(12) not null); # 默认插入空字符串 ``` 设置严格模式: 不支持对not null字段插入null值 不支持对自增长字段插入" ”值-空字符串 不支持text字段有默认值 直接在mysql中生效(重启失效): mysql>set sql_mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"; 配置文件添加(永久失效): sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION" ```
unique唯一 (不能重复)
# create table t4(id int unique,name char(12)); # 联合唯一约束 # create table t5(family char(12),name char(12),unique(family,name)); # 约束各自不能为空 且联合唯一 # create table t5(family char(12) not null,name char(12) not null,unique(family,name)); # 唯一+非空 id name # create table t6(id int not null unique, name char(12) not null unique); # pri 是怎么产生的? 第一个被设置了非空+唯一约束会被定义成主键 primary key # 主键在整张表中只能有一个
主键 =非空+唯一
# create table t6(id int primary key, name char(12) not null unique); # create table t5(family char(12) ,name char(12),primary key(family,name)); # 约束各自不能为空 且联合唯一 还占用了整张表的主键
自增
自增的必须是主键
# create table t6(id int auto_increment, name char(12)); # 报错 # create table t8(id int primary key auto_increment, name char(12)) ; # create table t9(id int unique auto_increment, name char(12)) auto_increment=100000; 自增=>非空 非空+唯一约束会被定义成主键 # delete from t7; 清空表数据但不能重置auto_increment # truncate table t7; # 清空表并且重置auto_increment # 所有的操作都无法改变auto_increment的自动计数。但是我们也没有必要去改变它。 # 1.至少要看到自增的效果 # 2.至少写3条数据 4,5,6 # 3.删掉第5条,再看结果 # 4.再insert一条数据 # 5.删掉第5条,再看结果 # 6.再insert一条数据 # 7.清空整张表 # 8.再insert一条数据,再看结果 # 修改auto_increment # alter table 表名 auto_increment = n; 修改表的auto_increment # alter table t7 auto_increment = 1000; 修改表的auto_increment
default 默认值
# create table t3(id int,name char(12),sex enum('male','female') default 'male'); # 非空约束 和 默认值 # create table t3(id int not null,name char(12) not null,sex enum('male','female') not null default 'male');
6.外键
foreign key(class_id) references class3(cid) on update cascade
# 没有建立外键: # create table stu(id int,name char(12),class_id int); # create table class(cid int,cname char(12)); # insert into stu values (1,'日魔',1),(2,'炮手',1) # insert into class values(1,'py27'); # insert into class values(2,'py28'); # select * from stu,class where class_id = cid; # delete from stu where id = 1; # delete from class where cid = 1; # stu2 class2 # create table class2(cid int unique,cname char(12)); # create table stu2(id int,name char(12),class_id int,foreign key(class_id) references class2(cid)); # insert into class2 values(1,'py27'); # insert into stu2 values (1,'日魔',1),(2,'炮手',1) # delete from class2 where cid = 1; # insert into class2 values(2,'py28'); # update class2 set cid = 1 where cid = 2; 不能修改 # stu3 class3 级联更新 # create table class3(cid int primary key,cname char(12)); # create table stu3(id int,name char(12),class_id int,foreign key(class_id) references class3(cid) on update cascade); # insert into class3 values(1,'py27'); # insert into stu3 values (1,'日魔',1),(2,'炮手',1) # update class3 set cid = 2; 修改了class3中的cid,stu3中相关的数据也会跟着变化,是on update cascade设置导致的