用于自己的复习
-
第一范式:原子性(表的字段满足不可再拆分)
-
第二范式:唯一性(表中必须至少存在一个字段,这个字段时唯一不能重复的)
-
第三方式:关联性(表中其他列必须与主键存在关系,不存在传递依赖(就是说其他字段都必须与主键存在直接的关联)
-
MySQL字段4个约束:主键约束primary key(不能为空) 非空约束not null 唯一约束unique(可以为空) 外键约束foreign key
-
show databases; # 查看逻辑空间
-
create database name; #创建名字为name的逻辑空间
-
drop database name; #删除名字为name的逻辑空间
-
use name; #对逻辑空间name进行操作
-
create table name( # 创建名字为name的数据表
# 名字 类型 约束, 约束可以不加
id int unsigned primary key auto_increment, # 数据名为id,是无符号int类型,约束为主键,auto_increment是让主 # 键值自动加一,可以不加
thename varchar(20) not null default “my” # varchar(20)是大小不定,最大为20的字符类型,not null约束为不能空
# default "my"是给字段一个默认值,可以不加
);
create table name2(
id2 int unsigned primary key,
thename varchar(20) notnull,
dnum int not null,
foreign key (thename外键字段名字) reference name父表名字(thename父表关联字段名字)
# index 名字 (字段)
index myindex (dnum) # 创建索引,名字可以没有,没有就用字段名字
);
-
主键自带索引机制,给表加索引的其他方法:
create index 索引名 on 表名 (字段);
alter table 表名 add index 索引名 (字段); # 索引名可以不加
-
show index from 表名; # 查看表的索引
-
drop index 索引名 on 表名; # 删除某个表的某个索引
-
insert into name values(1, “my”); #对名字为name的表插入数据
-
show tables; 查看数据表
-
desc name; 查看名字为name的数据表的结构
-
show create table name; 查看建名字为name的表时的SQL语句
-
drop table name; 删除名字为name的表
-
alter table name # 修改名为name的表的结构
add sex char(1) not null, # 往表里添加一行
add remark varchar(200);
-
alter table name
modify remark varchar(300) not null; # 可修改字段的类型和约束
-
alter table name
change remark newremark varchar(300) not null; # 修改字段名称和类型约束
-
alter table name
drop newremark; # 删除表的某个字段
-
select name1, name2 as “别名” from 表名; # 查询表的name1,name2字段,其中name2字段用别名显示
-
select name from tablename limit 起始位置, 偏移量;
#limit用于数据分页,指从起始位置取偏移量的数据 该语句的执行顺序是from > select > limit
-
select name from tablename order by 字段1 asc, 字段2 desc limit 0, 5;
#order by排序,默认是升序,desc是降序,按第一个字段优先排,若字段1值相等则派字段2排。顺序:from>select>order by>limit
-
select distinct name from tablename
#distinct去重,必须写在select后,而且一个select只能写一个distinct
-
与null值运算的结果都是null,条件查询时若想查询某字段是否为null,应该写where A is null 而不是where A = null (MySQL的等于比较运算符是=)
-
select name from tablename where 条件1 and 条件2 or(not(非) xor(异或)) 条件3;
#条件查询 where中条件执行顺序从左到右,所以先把有索引的字段先查询。执行顺序为:from > where > select > order by > limit
-
where语句中不可出现聚合(汇总)函数(sum(),count(),avg(),max(),min())因为where是对数据进行划定筛选范围,还未确定结果集,而聚合函数是针对结果集进行计算。
-
select name from tablename group by 字段, 字段;
#对数据进行分组查询,有group by时,select后出现的字段要么是group by后出现的字段,要么是聚合函数,不可出现其它字段,除非用group_concate(字段)函数将要显示的字段汇聚成一条。执行顺序为:from > where > group by > select > order by > limit
-
select name from tablename group by 1;
#group by 1是让MySQL依据select子句中的列进行分组
-
当有group by存在时,因为where的执行顺序比group by快,故where后的条件不可用聚合函数,所以用having代替where,having是针对结果集(已经划定好数据范围)的数据来筛选。having一定要搭配group by使用,因为在没有group by的情况下用having代替where只会导致数据库查询效率低下,毫无意义
select name from tablename where 条件1 group by name having 条件2;
#执行顺序:from > where > group by > having > select having语句后的条件可用聚合函数,但不可直接用聚合函数和字段作比较
-
内连接是结果集只保留符合连接条件的记录,以下三种写法等同
select name from 表1 别名 join 表2 别名 on 连接条件;
select name from 表1 别名 join 表2 别名 where 连接条件;
select name from 表1 别名, 表2 别名 where 连接条件;
#连接条件也不能出现聚合函数,与where后不能出现聚合函数同理
#join默认为Inner join, 内连接。内连接的数据表不一定有同名字段,只要字段之间符合逻辑关系就行。内连接的两个表可以是同一个表
-
在条件中嵌套子查询语句会导致查询效率下降,可以将子查询作为一张表连接与原表来代替原有的子查询:
select e.name from table1 e join (select avg(sal) avg from table1) t on e.sal>=t.avg;
-
外连接是除了符合条件的记录之外还会将保留不符合条件的记录
select name from table1 left join table2 on 条件;
#left join左连接,以左边的表为基础进行外连接,right join是右连接。可写多个left join,以from后面的表为主体。在外连接中,on与where效果是不一样的,where后的条件会将记录过滤掉,而不是保留下来
-
union可以将多个查询语句的结果集进行合并
(查询语句) union (查询语句) union (查询语句);
-
select后接的子查询只可出现单行,不可出现多行,多行子查询只可出现在where或者from中
-
insert into 表名 (字段1, 字段2) values(值1, 值2), (值1, 值2);
#表名后面加字段能让数据库查询效率更高。值可用子查询表示,但该子查询的结果集必须是一行一列的值
-
insert ignore into…;
#ignore能让insert语句只插入表中不存在的数据,即插入不冲突的数据,而冲突的数据忽略不报错
-
update ignore 表名 set 字段1=值1, 字段2=值2 where 条件 order by … limit …;
#limit后只可接一个数,即limit只能规定第一页数据
-
update 表1 join 表2 on 条件 set 字段1=值1, 字段2=值2…;
update 表1, 表2 where 条件 set 字段1=值1, 字段2=值2…;
#字段可以是表1的也可以是表2
-
update 表1 left join on 条件 set 字段1=值1, …;
-
delete ignore from 表名 where 条件1, 条件2 order by … limit…;
#delete用于删除数据
-
delete 表1 from 表1 join 表2 on 条件…;
#删除delete后所写的表1的记录
-
delete 表1, … from 表1 left join 表2 on 条件;#外连接
-
truncate table 表名;
#truncate用于删除表的所有记录内容。delete也能做到,但是由于事务机制会导致效率下降,而truncate是绕过事务机制(即删除前不会将数据先保存于日志中(先备份))直接删除,效率较高
-
MySQL通过事务区作为数据的缓冲地带