1.not null
2. check(age > 18 and age <50) 检察约束 mysql 不支持
3.default 值
4.unique
5.primary key auto_increment
6.constrint 外键名 foreign key(自己表的字段) references 另一个表(另一个表的主键);
SQL: 结构化查询语言
1.DDL: 数据库定义语言
创建数据库, 表, 相当于 盖房子
create table person(
pid int primary key auto_increment,
pname not null);
alter table person modify pname varchar(13);
add address varchar(22)
change pname personName varchar(22)
drop pname;
2.DML: 数据库操纵语言
向数据库中添加数据,删除, 修改 ,
update person set pname = '' , age = where id = ?;
update person set pname ='' , age = 3 where id in(3,4);
delete from person where id = 1
delete from person
主键:
复合主键:
如何创建一个复合主键:
create table person(
pid int,
pname varchar(22),
age int,
primary key(pname, age));
外键存储的位置: 作用:
表和表之间的关系:
1. 1vs1 外键可以在任意表中, 但是要 结合 生活中的实际情况;
2. 1 vs n 外键 在 多方, 防止数据冗余
3. n vs n 要创建新表 中间 , 关联两个表 ,实现两个一对多 ,
3.DQL: Database Query Language 数据库查询语言:
1.有条件查询 select * from 表名 where ,,,,having Object ,ArrayList
2.无条件查询 select * from 表名 没有where limit 0,5 ArrayList
1)select * from 表名; * 是代表数据表中的所有字段,
select pid , name , age from 表名;
select name from 表名 from
employee :雇员表
empno 雇员编号
ename 雇员名称
job 职位
mgr 管理者编号
hiredate 入职日期
sal 工资
comm 佣金 提成
deptno 部门编号
department :部门表
deptno 部门编号
dname 部门名称
loc 地址
ResultSet: 结果集
select * from emp; 多条 ---> List ArrayList
关键字:
and
or
in(1,2)
where 过滤条件
is not null 不是null
is null 是null
between and 取区间
比较运算符:
< , >, <=, >= ,= ,!=
运算符: + , - ,* ,
'null' 代表 0
null 代表 空
1+1 2
1+null null
1+'null' 1
3*'null' 0
'3'+'4' 7
-------------------------------------------------------------------
模糊查询: like 关键字 % 通配符 万能符 , _ 占位符 1位
查找姓名中 包含字母A
select * from emp
where ename like '%A%';
-------------------------------------------------------------------
排序: order by
order by 一般情况下指定单个字段排序,
如果工作相同, 就按姓名排序
select * from emp
order by sal,ename;
-------------------------------------------------------------------
***
分页: limit
按类型划分 2中:
1.按钮式 60 B/S java
2.加载式 加载慢
编程中有3中分页实现方式:
假分页
1.ArrayList: subList(fromIndex,EndIndex); 14
subList(5,10);
2.物理分页: limit startRow,PageSize;
3.hibernate框架 分页
where 过滤
order by 排序
limit 0,3 分页
like 模糊查询
分组: group by 字段名
必须 结合分组函数(聚合函数使用)
分组:
sum(price); 求和
avg(price); 求平均值
max(sal);
min(sal);
count(*); 检索所有的字段
count("张三");
count(1); 查个数 推荐
count(ename); 这个字段必须是 not null
---------------------------------------------------------------------
having子句: where 和 having 可以连用 先过滤,分组, 过滤
where: 在group by 分组前使用
having: 在group by 分组后使用
查询 员工人数多于 5的部门是哪个?
------------------------------------------------------------------------
子查询: 什么是 子查询 一个sql 语句中嵌套sql 语句
子语句 可以放在 where 后
having 后
from 后
select () from
查询 在 new York 办公的人的 信息
------------------------------------------------------------------------
联合查询:
案例: 1.chinese 中国 id name score
American 美国 id name score
两个表 字段数 一致
字段的 顺序必须 一致
union 自动去重
union all 查询所有 不去重
-----------------------------------------------------------------------
distinct :去重
DISTINCT sal 去重关键字
明天将 多表查询 (级联查询)
------------------------------------------------------------------
行变列:
1.分组
2.分函数
3.case when 条件句练习
select stuName,
sum(case `subject` when '数学' then score else 0 end) as 数学,
sum(case `subject` when '语文' then score else 0 end) as 语文,
sum(case `subject` when '英语' then score ELSE 0 END) as 英语
from student group by stuName;
select 语句的格式 是固定的:
select distinct[字段列表 | 聚合函数]
from 表名
where [条件句]
group by 分组字段
having [条件句]
order by 排序字段[asc | desc]
limit 开始行,页大小;