数据库操作
1 创建数据库 create database
2 修改数据库 alter database 数据库名 defaut character set 编码方式collate编码方式_bin;
3 删除数据库 drop database 数据库名字;
数据表
创建 创建之指明位置 类型
修改表名 alter table teavher rename 位置_表名(Windows的文件夹)
change
modify 改性的
删除字段 alter 表名 drop字段名
删除表名 drop 表名
#############################################################################################
主键 primary key constraint(单字段主键 复合主键)键唯一不为空
字段 类型 primary key
修改为主键 alter table 表名 modify 字段名 数据类型 primary key
删除主键 alter table 表名 drop primary key
复合主键 复合主键中的每个键都不为空值
primary key(字段名1,字段名2,......)
alter table表名 add primary key(字段名1,字段名2,......)
删除复合主键alter table 表名 drop primary key
外键
alter table 从表名 constraint 外键名(自定义) foreign key(从表的外键字段名) references 主表名(字段)
出现hy(000) cannot add foreign key constraint 的原因1 字段引擎不符合
给字段创建 添加 删除 各种约束
主键 primary key constraint(单字段主键 复合主键)键唯一不为空
字段 类型 primary key
修改为主键 alter table 表名 modify 字段名 数据类型 primary key
删除主键 alter table 表名 drop primary key
复合主键
primary key(字段名1,字段名2,......)
alter table表名 add primary key(字段名1,字段名2,......)
外键(表与表之间 构建 联系)注意 外
constract 名 foreign key reference 主表名(Java 子类 与父类))
alter table 从表名 constract 名 foreign key reference 主表名
删除 与主键类似
增
insert 表名
values( , , , , ), ( , , , , , );//增加的记录是字符串
删
deltete 表名
where
改
update 表名
set 字段名 =value[,...]
where ;
查
select 要查询的内容
from 数据表;
筛选
selart要查询的内容 运算符分类 运算符 说明
form 数据表 比较运算符
where 筛选内容
(时间格式 可以进行四则运算) 列表运算符 in , not in
模式匹配运算符 like , not like
空值运算符 is null, is not null
逻辑运算符 and ,or , not
order by
字句对查询结果集中的数据行依照指定字段的值重新排列?
select
where
order by[asc|desc] asc 升序 desc 降序
limit[始行 默认0] 止行;
集合函数
count distinct
sum
avg 平均值 distinct 去重值
max 最大值
min 最小值
多表查询
先合并表再选出符合要求的
select[表名.]目标字段表达式[as 别名], ...
from 左表名[as别名]连接类型 右表名[as 别名]
on 链接条件
[where 条件表达式];
连接类型
内连接 inner join或join(条件 输出符合要求 3种类型)//类似于乘法分配律
交叉连接 cross join(笛卡尔积 一般无意义)
外链接
外连接 left join
右连接 right join
完全连接 full join (先左后右)
自连接 (一个表的两个副本)
子查询
(select 位于另一个查询的where子句中 可多层嵌套 由内向外)
比较子查询 子查询于父查询用比较运算符连接 子查询的返回结果最多只有一个
in 子查询 父查询与子查询之间用in或not in进行连接并判断某个字段的值是否在子查询查找到的集合中
批量子查询 子查询的结果有多个 父查与子查用比较连接 在子查前加all any
any 只要有一个位TRUE则结果为TRUE 否则为FALSE
all 全部为TRUE .............TRUE
exists (布尔)
insert update delete中的子查询
合并结果集 union
非空
对数据类型进行进一步限制 not null
删除 alter table表名 modify 字段名 数据类型