1,er图 entity -relationship 实体关系模型
属性 用椭圆 ,实体(可分) 用方形 , 联系 用菱形框
避免表中有表(避免实体重复出现)
3.范式 normal form 6级(一般到3级或bc范式)
1 NF 表中无表
2 满足1, 完全函数依赖, 通过俩个属性得到需要的值(有两个主键得到,通过一个主键直接得到某个属性)
3 id–>name (只能用id对应到其他的表,不可以用其他字段对应)
id—>age
name—>teacherName(x)
4. sql是什么 structured query language 结构化查询语言
ddl语言 data manipulation language
tcl tansaction control language
5.外键约束
主表是被参照表,外键在子表上
删除时 更新时(级联操作) 主表做什么,子表相应做什么 set null cascas(对应删除)
create table xxx(
… ,constraint fk_sno foreign key(sno) references student(sno)
)
alter table sc add constrain fk_no foreign key(sno) references student(sno) on delete
//restrict 就是子表有记录,那主表不能删除 === no
action(如果没有对应记录可以删除),也是一样的alter table sc drop foreign key fk_no;
6.删除非空约束
alter table user modify username varchar(20) NULL;
7.默认约束与非空约束配合
alter table xx set default 'xxx';alter table xx drop default;
8.自增必须为整型
create table xx()auto_increment
9.检查约束 8.0以上有效
create table xx(,check(age between 14 and 20 ))
10.插入数据
insert into aaa set id=1,name='999'insert into aaa values (),();insert into aaa select * from student where id=1;
#如果原表有就删除,再插入
replace into aaa value(11,'xx');
update xx set id=1, name=2 where xxx
12.单表查询
#去掉重复的记录,放在字段前
select distinct sex as 'sex1' from stu;
//只是预览,不是真的修改
select score+2 as '修改后预览' from stu;
//and or xor
select score from stu where score>=85 and score<=90;
#between and 去掉等于
select score from stu where score not between 85 and 90;
#in,代替or复杂 not in
select score from stu where score not in('aa','bb');
#is null 查询是否为空 ,score=null(x)
select score from stu where score is null;
//_一个字符 %匹配多个字符
select score from stu where score like 'a_%';
#特殊用法,本身带 _ %需要转义,定义一个转义符号 放在前面即可 转义
select score from stu where score like '#_#%' ESCAPE '#';
13.order by desc(上到下,从大到小排序) /asc 升序
#多个字段,如果字段的前一个排序相同就用后面这个排序
select * from user where pp=1 order by birthday desc,aaa desc;
14.限制查询条数(第一个是偏移量)
//查询5条数据
select * from user limit 5;
//偏移0条数据,全部显示2条数据(前面两条数据)
select * from user limit 0,2;
15.函数
count(*/字段)不包括null
select count(name) from user;sum(aaa)minmaxavg
16.分组查询 group by 只会显示每组的第一个数据(数据格式不相同)
!!!不可以写where在group by后(分组吗的必须要出现在前面)
select * from user group by id having score>85;
select classNo,count(*) from user group by classNo having count(*)>2;
17.union 求并集
mysql> select d_idfrom bumen unionselect d_id from yuangong;
上一篇:登录脚本下发