持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第8天
DML(数据操作语言)
insert(插入)
insert into 表格名 values(属性值);
-- 当行插入
insert into tb_student values(1001,'张三丰',1,'1982-2-2','湖北十堰');
-- 对应数值当行录入信息
insert into tb_student(student_id,student_name) values (1003,'张翠山');
insert into tb_student(sudent_sex,student_id,student_name) values (0,1004,'殷素素');
-- 批量加入
insert into tb_student(sudent_sex,student_id,student_name) values
(1,1005,'张无忌')
(1,1006,'谢逊')
(0,1007,'小龙女');
delete(删除)
-- 把整张表都删了
delete from tb_student(表名);
-- 截断表(删除全表)
truncate table tb_student(表名);
-- 删除某行(一般条件用主键)
delete from tb_student where student_id=1002;
-- 删除所有女生
delete from tb_student where student_sex = 0
update(更新)
-- 更新操作
-- 更新全表的家庭地址
update tb_student set student_address = '四川成都';
-- 更新某一行或者某多行
update tb_student set student_address = '四川成都' where student_id in (1005,1006);
-- 或者
-- 更新某一行或者某多行单个属性
update tb_student set student_address = '四川成都' where student_id=1005 or student_id=1006;
-- 改某一行的多项属性
update tb_student set student_address = '四川成都',studentbirth='1998-12-30' where student_id = 1007;
- 注意在SQL语法中,“=”不再是赋值,而是判断相等,所以想要赋值就得在属性前面加个set
多对一关系,外键约束,唯一性约束
- 示例
-- 创建学院表
create table tb_college
(
colid int auto_increment comment '编号',
colname varchar(31) not null comment '名称',
website varchar(1023) comment '网址',
primary key (colid)
);
-- 插入学院记录
insert into tb_college (colname) values
('计算机学院')
('外国语学院')
('经济管理学院');
alter table tb_studnet add column (colid) int;
update tb_studnet set colid=1 where student_id between 1001 and 1006;
update tb_studnet set colid=1 where student_id in (1007,1008);
update tb_studnet set colid=1 where student_id=1009;
- 实体:学生,学院
- 关系:从属,学生属于学院
- 重数:N:1(多对一的关系)
那么上述问题来了,两个表联立起来了吗?
显然没有,因为我们可以将colid改为任意不存在的学院id
所以我们通过外键约束来解决问题
外键约束
-- 修改学生表添加外键约束(参照完整性)
alter table tb_student add constraint fk_student_colid foreign key (colid) references tb_college(colid);
-
上述语句意思就是:修改tb_student表加一个约束名为fk_student_colid,外键参照tb_college(colid)
-
外键:顾名思义就是外来的主键(另一个表的主键)
唯一性约束
-- 一个学生选某个课程只能选一次
alter table tb_socre add constraint uni_score_stuid_couid unique(student_id,couid);
- 意思:修改名为tb_socre的表加一个名为uni_score_stuid_couid 的唯一约束(student_id,couid)
select(查询)
-- 查表的所有信息
select * from tb_student;
-- 查询所有的课程名称及学分(投影和别名)
select couname,coucredit from tb_course;
select couname as 课程名称, coucredit as 学分 from tb_course;
-- 查看学生姓名性别
select stuname as 姓名, case stusex when 1 then '男' else '女' end as 性别 from tb_student;
-- 查询所有女学生的姓名和出生日期(筛选)
select stuname,stubirth from tb_student where stusex=0;
-- 查询所有80后的学生的姓名、性别和出生日期(筛选)
select stuname,stusex,stubirth from tb_student where stubirth>='1980-1-1' and stubirth<='1989-12-31';
select stuname,stusex,stubirth from tb_student where stubirth between'1980-1-1' and '1989-12-31';
-- 查询姓为'李'的学生的姓名和性别(模糊)
select stuname, stusex from tb_student where stuname like '李%';
-- 查询姓为'李'的两个字学生的姓名和性别(模糊)
select stuname, stusex from tb_student where stuname like '李_';
-- 查询没有录入家庭住址的学生姓名(空值)
select stuname from tb_student where stuaddr is null;
-- 查询学生选课的所有日期(去重)
select distinct scdate from tb_score;
-- 查询男学生的姓名和生日按年龄从小到大(排序)
select stuname,stubirth from tb_student where stusex=1 order by stubirth desc;
-- 查询年龄最大的学生的出生日期(聚合函数)
select min(stubirth) from tb_student
-- 查询男生女生的人数(分组和聚合函数)
selecr stusex,count(stusex) from tb_student group by stusex;
- as:alias
- 通用:case ... when .. then .. else...end (查看值为..,当值为..时输出..否则输出..结束) mysql独有:if(属性,'输出1','输出2')真为输出1,假为输出2
- mysql中获取当前日期的函数为now()
- like:用于模糊查询,像的意思
- %:通配符(代表0个或者多个字符)
- _:通配符(代表往后匹配一个字符)
- <>:不等号(数据库,python2)
- distinct:去重
- order by:排序,默认从小到大(asc:升序,desc:降序)
- min和max:聚合函数
- group by:分组