MySQL学习小结
一、数据库:存储、维护和管理数据的集合。
DB、DBMS
二、SQL:结构化查询语言
SQL的分类
(1) DDL:数据定义语言,定义数据库对象,对对象进行操作的。
create alter drop
(2) DML:数据管理语言,对数据库表中的数据进行操作的。
insert update delete
(3) DQL:数据查询语言,对数据进行查询的。
select
(4) DCL:数据控制语言,对数据进行权限访问和安全级别的控制。
grant(授权) revoke(收回)
示例:
DDL:
create database mydb1;
use mydb1;
create table student (
sid int primary key auto_increment,
sname varchar(50) not null unique,
sex varchar(10) default '男'
);
alter table student add birthday date;
alter table student drop birthday;
alter table student add grade double(3, 1);
alter table student modify sex varchar(20);
alter table student change sname stuname varchar(50);
DML:
insert into student (sname, sex, grade) values ('tom', default, 98);
insert into student (sname, sex, grade) values ('lucy', '女', 95);
insert into student (sname, sex, grade) values ('bruce', '女', 59);
update student set sex = default, grade = 99 where sid = 3; -- 或者
update student set sex = default, grade = 99 where sname = 'bruce';
delete from student where sid = 1;
delete from student;
truncate table student;
DQL:
-- 查询分数与tom分数一样的学生信息
select * from student where grade = (
select grade from student where sname = 'tom');
select xxx from xxx where xxx group by xxx having xxx order by xxx limit; -- 书写顺序
from xxx where xxx group by xxx having xxx select xxx order by xxx limit; -- 执行顺序
where xxx like / between and / in / not in / is null / is not null / and / or / not /
sum() avg() max() min() count()
ifnull(comm, 0)
三、数据完整性
实体完整性:行级约束。
primary key
unique
auto_increment
域完整性:列级约束。
数据类型
not null
default
check()
引用完整性:参照/关联完整性。
foreign key
表与表之间的关系:
一对多/多对一
多对多
一对一
四、多表查询
合并查询:union union all
连接查询:
内连接查询:[inner] join xxx on xxx
外连接查询:[outer] join xxx on xxx
left join xxx on xxx
right join xxx on xxx
自然连接查询:natural join
natural left join
natural right join
子查询:
自连接查询:
五、MySQL中的函数
时间、日期相关函数
字符串相关函数
数学相关函数
六、MySQL数据库的备份与恢复
生成SQL脚本 导出数据
执行SQL脚本 恢复数据