目录
⼀、数据库基本概念
常⻅的数据库:
MySQL简介
mysql的安装
linux下
windows下安装
⼆、 SQL
三、操作数据库
四、数据库表
例子:
五、数据类型
数值型
字符型
⽇期时间型
枚举enum
集合set
六、数据操作
1. insert
2.update
3. delete
七、数据查询
1 基础查询
2 条件查询(where)
3. 排序(order by)
4.限制结果集(limit)
5.集合函数
6.分组(group by)
⼋ 存储引擎
数据库是存放数据的仓库。它的存储空间很⼤,可以存放百万条、千万条、上亿条数据。但是数据库并不是随意地将数据进⾏存放,是有⼀定的规则的,否则查询的效率会很低。
MySQL最早是由瑞典的MySQL AB公司开发的⼀个开放源码的关系数据库管理系统,该公司于2008年被昇阳微系统公司(Sun Microsystems)收购。在2009年,甲⻣⽂公司(Oracle)收购昇阳微系统公司,因此在这之后MySQL成为了Oracle旗下产品。
MySQL在过去由于性能⾼、成本低、可靠性好,已经成为最流⾏的开源数据库,因此被⼴泛地应⽤于中⼩型⽹站开发。随着MySQL的不断成熟,它也逐渐被应⽤于更多⼤规模⽹站和应⽤,⽐如维基百科、⾕歌(Google)、脸书(Facebook)、淘宝⽹等⽹站都使⽤了MySQL来提供数据持久化服务。
甲⻣⽂公司收购后昇阳微系统公司,⼤幅调涨MySQL商业版的售价,且甲⻣⽂公司不再⽀持另⼀个⾃由软件项⽬OpenSolaris的发展,因此导致⾃由软件社区对于Oracle是否还会持续⽀持MySQL社区版(MySQL的各个发⾏版本中唯⼀免费的版本)有所担忧, MySQL的创始⼈⻨克尔·维德纽斯以MySQL为基础,成⽴分⽀计划MariaDB(以他⼥⼉的名字命名的数据库)。有许多原来使⽤MySQL数据库的公司(例如:维基百科)已经陆续完成了从MySQL数据库到MariaDB数据库的迁移。
参考https://blog.csdn.net/qq_41080850/article/details/97495643
在实际开发中,为了⽅便⽤户操作,可以选择图形化的客户端⼯具来连接MySQL
服务器,包括:
MySQL Workbench(官⽅提供的⼯具)
Navicat for MySQL(界⾯简单优雅,功能直观强⼤)
SQLyog for MySQL(强⼤的MySQL数据库管理员⼯具)
基本可分为:
数据定义语⾔DDL (create、 drop)
数据操作语⾔DML(insert、 delete、 update)
数据查询语⾔DQL(select、 where、 group by、 order by 、 limit)
数据控制语⾔DCL(grant、 revoke)
事务处理语⾔TPL(commit、 rollback)
数据库操作命令
注意:
每条命令结束必须使⽤; 或者 \g 结束
退出mysql使⽤命令quit或exit
drop table 表名;
create table 表名 like 其他表名
desc 表名;
show create table 表名;
#修改字段类型
alter table 表名 modify 字段名 类型 [限制]
#增加字段
alter table 表名 add [column] 字段名 类型 [限制];
#删除字段
alter table 表名 drop [column] 字段名;
修改字段名和类型
alter table 表名 change [column] 旧字段名 新字段名 类型 [限制];
#修改表名
alter table 表名 rename 新表名
alter table 表名 [engine=myisam] [default charset=utf8];
# 可以通过first、 after指定插⼊位置
alter table student add sno varchar(3) not null after sid; //在sid列后插⼊
alter table student add sid int primary key auto_increment first;//在第⼀列插⼊
primary key 不允许空值,唯⼀
not null ⾮空
unique 唯⼀
default 缺省,默认值
注意:
(1) char 和 varchar 的区别:
update 表名 set 字段1=值1,字段2=值2... where 条件 #不加where修改的是所有的记录
基本结构: select 字段名列表 from 表名
关系运算符: > 、 >=、 <、 <=、 =、 !=、 <>、 between and
select username,password from user where uid <10
select username,password from user where uid != 10
select username,password from user where uid between 10 and 20
逻辑运算符: and 、 or、 not
select username,password from user where uid < 100 and uid > 20;
select username,password from user where uid > 100 or uid < 20;
集合运算符: in、 not in
select username,password form user where uid in (2,3,4)
select username,password form user where uid not in (2,3,4)
判空运算: is null、 is not null
select username,password from user where username is null
字符串的模糊查询(like)
通配符 _代表⼀个字符, %代表任意⻓度字符串
select * from user where username like '王_';
select * from user where username like '王%';
asc 升序(默认)、 desc 降序、
select * from user order by age asc;
select * from user order by age desc;
//"#多字段排序"
select name,age from php_user_history order by age desc,name;
//"#如果在第⼀列上有相同的值,在具有相同的age的记录上再按name升序排列"
limit n #取前n条记录
limit offset,n #从第offset条开始取,取n条
select * from php_user_history limit 3;
select * from php_user_history limit 4,2;
//注意结果集中记录从0开始数数, offset相对于0开始
//实现分⻚必须的技术点
limit (page-1)*num,num
select count(*) num from user;
select count(distinct age) num from user; //去除重复记录
select * from student where sno = max(sno);//错误
将结果集分组统计,规则:
select uid, count(*) num from php_forum group by uid;
select uid,title, count(*) num from forum group by uid having count(*) >=2;
having和where的区别:
where针对原始表进⾏过滤
having 是针对分组进⾏过滤
查询⼩结:
可以使⽤show engines命令查看数据库引擎
show engines \G
通过上⾯的⽐较我们可以了解到, InnoDB是唯⼀能够⽀持外键、事务以及⾏锁的存储引擎,所以我们之前说它更适合互联⽹应⽤,⽽且它也是较新的MySQL版本中默认使⽤的存储引擎。
myisam和innodb的区别: