MySQL数据库
MySQL
结构化查询语言(操作关系型数据库的编程语言)
注释
单行注释: – 注释内容(或者 # 注释内容(MYSQL独有))
多行注释:/**/
SQL分类
- DDL(Data Definition Language)数据定义语言,用来定义数据库对象:数据库,表,列等
- DML(Data Manipulation Language)数据操作语言,用来对数据库中表的数据进行增删改
- DQL(Date Query Language)数据查询语言,用来查询数据库中表的记录(数据)
- DCL(Data Control Language)数据控制语言,用来定义数据库的访问权限和安全级别,及创建用户
DDL定义数据库对象:数据库,表,列等
DDL操作数据库
创建数据库
create database db1//创建数据库db1
create database if not exists db1;//如果不存在db1则创建
删除数据库
drop database db1;
drop database if exists db1;
查询正在使用的数据库
select database();
操作数据库
use db1;//db1为要使用数据库名称
DDL操作表
创建表
create table 表名(字段名 数据类型,字段名 数据类型);
对于常用的数据类型
int 整数型–age int
score double(总长度,小数点后保留位数)–score double(5,2)–110.25
brithday date–只有年月日
char–name char(10)–固定空间,name不够10则补空格
varchar–name varchar(10)–长度可变,只占用name的空间
查询表
show table;//查询当前数据库的表
desc 表名;//查询表结构信息(字段、约束等)
修改表
alter table 表名 rename 新表名;//修改表名
alter table 表名 add 字段名 数据类型;//新添一个字段
alter table 表名 modify 字段名 新数据类型;//修改字段数据类型
alter table 表名 change 字段名 新字段名 新字段数据类型;//修改字段
alter table aaa drop 字段名;//删除字段
删除表
drop table 表名;
drop table if exists 表名;
DML数据库中表的数据进行增删改
DML操作数据
添加数据
insert into 表名(字段1,字段2) values(数据1,数据2);//单行添加
insert into 表名(字段1,字段2) values(数据1数据2),(数据1,数据2).....//多行
修改数据
//没有条件,则所有数据都修改
update 表名 set 字段名1=值1,字段名2=值2【where 条件】;
删除数据
//没有条件,则所有数据都删除
delete from 表名 【where 条件】;
DQL查询数据库中表的记录(数据)
DQL操作数据
基础查询
select 字段1,字段2 from 表名;
select * from 表名;//查询所有数据
select distinct 字段名 from 表名;//去除重复数据
条件查询
select 字段列表 from 表名 where 条件列表;
多条件查询
//and..between and
select * from student where age>10 and age<20;
select * from student where age between 10 and 20;
//or... in()
select * from student where age=10 or age=20 or age=30;
select * from student where age in(10,20,30);
null值
//null值比较不可以使用=或者!=,要使用is或者is not
select * from teacher where age is null;
模糊查询
//like %代表任意个数字符 _代表单个任意字符
select * from teacher where name like '_张%';//查询姓名中第二个字是张的人
排序查询
select 字段列表 from 表名 order by 排序字段名1[排序方式],排序字段名2[排序方式];
//asc (默认)升序 , desc :降序
//当第一个条件相同,才会使用第二个条件
select * from teacher order by age desc;
select * from teacher order by age desc,score asc;
分组查询
聚合函数
//将一列数据作为一个整体,进行纵向计算
count(列名)//统计数量
max(列名)//最大值
min(列名)//最小值
avg(列名)//平均值
//语句
select 聚合函数名(列名) from 表名;
select 字段列表 from 表名 where 分组前条件限定 group by 分组字段名 having 分组后条件过滤;
//where 为分组前判断且不可对聚合函数判断,having 分组后判断,可对聚合函数判断
select sex,avg(math),count(*) from student group by sex;//多加sex可以容易看出分组的类型
select sex,avg(math),count(*) from student where math>70 group by sex;
分页查询
select 字段列表 from 表名 limit 起始索引,查询条目数;
//起始索引:从0开始 起始索引=(当前页码-1)*每页条数
//limit 为MYSQL特有,Oracle为rownumber,SQLserver 为top
select * from teacher limit 0,3;
约束
作用于表中列上的规则,限制加入表中的数据;保证了数据库的正确性、有效性以及完整性。
- 非空约束:保证数据不为NULL值(not null)
- 唯一约束:保证列中数据不相同(unique)
- 主键约束:非空且唯一(primary key)
- 检查约束:保证列中的值满足某一条件(check)–mysql不支持
- 默认约束:保存数据时,未指定值为默认值(default 值)
- 外键约束:两个表之间建立联系(foreign key)
自增:auto_increment
建完表后添加约束
//添加约束
alter table 表名 modify 字段名 数据类型 not null//unique;
alter table 表名 add primary key;
alter table 表名 alter 列名 set default 默认值;
//删除约束
alter table 表名 modify 字段名 数据类型;//删除非空约束
alter table 表名 drop index 字段名;//删除唯一约束
alter table 表名 drop primary key;
alter table 表名 alter 列名 drop default;
外键约束
create table 表名(
constraint 外键名称 foreign key(外键列名) references 主表(主表列名);
);
alter table 表名 add constraint 外键名称 foreign key(外键字段名称) references 主表名称(主表列名称);
//删除约束
alter table 表名 drop foreign key 外键名称;
数据库设计
- 一对多
- 一对一
- 多对多
一对多
在 多的一方建立 外键指向一方的主键
多对多
建立第三张中间表 ,至少要有两个外键,分别关联两方主键
一对一
多用于表的拆分,将一个实体中经常使用的字段放在一张表里,其他的放在另一张表里。
多表查询
我们知道,单表查询是直接select * from 表名;那么多表查询可不可以直接select * from 表1,表2;-------- 当然可以,但会产生笛卡尔积无效数据。
笛卡尔积:取A、B集合所有组合情况
多表查询:从多张表查询数据
- 连接查询
内连接:相当于查询AB交集数据
外阵接:
左外连接:相当于查询A表所有数据和交集部分数据
右外连接:相当于查询B表所有数据和交集部分数据
- 子查询
内连接
//隐式内连接
select * from 表1,表2 where 条件;
select A.name,B.age from A,B where A.Aage=B.Bage;
//显示内连接
select * from 表1 inner join 表2 on 条件;
select * from A join B on A.Aage=B.Bage;//inner可省略
外连接
//左外连接
select 字段列表 from 表1 left outer join 表2 on 条件;
select A.name,B.age from A left join B on A.Aage=B.Bage;
//右外连接
select 字段列表 from 表1 right outer join 表2 on 条件;
select * from A right join B on A.Aage=B.Bage;//outer可省略
子查询
在查询里嵌套查询
子查询结果不同,作用不同
//子查询为单行单列(作为条件,用<>=!=)
select 字段列表 from 表1 where 字段名= (子查询);
//子查询为多行单列(使用in关键字)
select 字段列表 from 表 where 字段名 in (子查询);
//子查询为多行多列(作为一张表)
select 字段列表 from (子查询)where 条件;
事务
事务是一种机制,一个操作序列,包含了 一组数据库操控命令
事务把所有命令作为一个 整体提交,要么 同时成功,要么 同时失败
//开启事务,只要不提交,外部查询不到事务影响
start;//或者begin;
//事务出错时,回滚事务
rollback;//数据回到初始状态
//提交事务
commit;//事务成功