格式:
update 表名 set 字段名1=值1,字段名2=值2...where 条件
例:
修改id为5的学生数据,姓名改为小红,年龄改为20岁
update students set name ='小红',age=20where id =5
7.删除数据
deletefrom 表名 where 条件(物理删除,不用)
例:deletefrom students where id=1;--删除第一行
常用逻辑删除:通过设定一个字段来标识当前记录已经删除
truncatetable 表名--(只删数据)droptable 表名 --(删除所有数据和表结构)
说明:
delete--删除数据时,若新增数据,新增数据的id是删除的id号的后一个truncate--删除数据后,若新增数据,是从id=1开始的
例:
1、查询小豪年龄:
select age from 表名 where name ='小豪';2、查询20岁以下学生:
select*from 表名 where age <20;3、查询家乡不在新疆的学生:
select*from 表名 where hometown !='新疆';
逻辑运算
例 :
1、查询年龄小于20的女同学:
select*from 表名 where age <20and sex ='女';2、查询女学生或1班的学生:
select*from 表名 where class=‘1班’ or sex ='女';3、查询非新疆的学生:
select*from 表名 wherenot hometown='新疆';
模糊查询
例:
1、查询姓孙的学生:
select*from 表名 where name like'孙%';2、查询姓孙且名字是一个字的学生:
select*from 表名 where name like'孙_';3、查询姓名以乔结尾的学生:
select*from 表名 where name like'%乔';4、查询姓名中包含白的学生:
select*from 表名 where name like'%白%';
范围查询
例:
1、查询家乡是北京/南京/新疆的学生:
select*from 表名 where hometown in('北京','南京','新疆');2、查询年龄为18-20岁的学生:
select*from 表名 where age between18and20;
空判断
例:
1、查询没有填写身份证的学生:
select*from 表名 where cad isnull;2、查询填写了身份证的学生:
select*from 表名 where cad isnotnull;
内连接:连接两个表时,取得是两个表中都存在的数据
格式:
select*from 表1innerjoin 表2on 表1.例1=表2.例2;
或
select*from 表1 ,表2where 表1.例1=表2.例2;
补充:写表名时可以改名,如students可以写成:
students as stu,之后就可用stu代替表名(as可省略)
例:
1、查询学生信息及学生的课程对应的成绩:
select*from students innerjoin scores on stu.studentNo=sco.studentsNo
innerjoin courses on sco.courseNo=courses.courseNo;2、查询小豪的数据库成绩,要求显示姓名、课程号、成绩:
select students.name,courses.name,scores.score
from studentsstu
innerjoin scoressco
on stu.studentNo=sco.studentsN
innerjoin courses
on sco.courseNo=courses.courseNo
where students.name='小豪';3、查询所有学生的数据库成绩,要求显示姓名、课程号、成绩:
select students.name,courses.name,scores.score
from students stu
innerjoin scoressco
on stu.studentNo=sco.studentsN
innerjoin courses
on sco.courseNo=courses.courseNo;4、查询男生中最高成绩,要求显示姓名、课程号、成绩:
select students.name,courses.name,scores.score
from students stu
innerjoin scoressco
on stu.studentNo=sco.studentsN
innerjoin courses
on sco.courseNo=courses.courseNo
where students .sex='男'orderby scores.score
desclimit0,1;