微信公众号和网站建设方案,模板建站和定制建站,网站后台登陆不了,安徽网站推广营销设计目录 数据库常用命令
数据库的创建
数据表的操作
表数据的增删查改
分组与函数查询
运算符#xff1a;数学运算符
连接查询
多表查询
修改语句
删除语句
字符查询like
MySQL练习
总结感谢每一个认真阅读我文章的人#xff01;#xff01;#xff01;
重点数学运算符
连接查询
多表查询
修改语句
删除语句
字符查询like
MySQL练习
总结感谢每一个认真阅读我文章的人
重点配套学习资料和视频教学 数据库常用命令
进入数据库在win系统下打开cmd,切换用户权限进入root。 沒權限用root登錄mysql -uroot如果root有密碼mysql -uroot -p 数据库的创建
查询所有数据库show databases
创建数据库create database 数据库名
删除数据库drop database 数据库名
进入数据库use 数据库名
数据表的操作
1查询数据库下表show tables;
2创建表create table student(id int(4) primary key,name char(20));
注释 id为表的第一列
int数字类型
primary key主键的意思列不能重复。 Name为表的第二列名字。
char:类型; 创建表create table score(id int(4) not null,class int(2));
注释 not null字段不能为空。 创建表create table student1(id int(4) not null,name char(20)); Field (列名)Type字段类型null是否为空key主键 3查看表结构describe student; 或 desc student; 4修改表名alter table 表名 rename 表名; 5删除表drop table 表名; 6修改表字段信息alter table student change id id int(20); 7增加表字段信息alter table student1 add class int(4) not null after id; 8删除一个表字段alter table student1 drop number; 表数据的增删查改
提示在数据库导入表时要修改列的字段类型并设置主键
主键表中经常有一个列或多列的组合其值能唯一地标识表中的每一行。这样的一列或多列称为表的主键通过它可强制表的实体完整性。当创建或更改表时可通过定义 PRIMARY KEY 约束来创建主键。一个表只能有一个 PRIMARY KEY 约束而且 PRIMARY KEY 约束中的列不能接受空值。由于 PRIMARY KEY 约束确保唯一数据所以经常用来定义标识列。 表数据新增格式insert into 表格名列名 values值先导入student和score表表为Excel可以自己编写。
例子
mysql insert into student(id,class,number,name) values(81,4,19,stu81);
mysql insert into student(id,class,number) values(82,4,20);
mysql insert into student values(83,4,21,stu83);
mysql alter table student change id id int(2) auto_increment;
注释auto_increment以1为单位自增长的意思
mysql insert into student(class,number,name) values(4,22,stu84);
mysql alter table score change id id int(4) auto_increment;
注释auto_increment自增长的意思。1。输入该命令表格会在新输入自动新增长新的一行id也会成自增。
mysql insert into score(class,number,maths,chinese,english) values(4,19,80,78,98);
mysql insert into score(class,number,maths,chinese,english) values(4,20,98,88,68);
mysql insert into score(class,number,maths,chinese,english) values(4,21,91,83,78);
mysql insert into score(class,number,maths,chinese,english) values(4,22,67,83,88); 查询表数据格式select * from 表名 where
注释语句以逗号做分隔*通配符select是展示的意思where是条件
例子 查询学生信息表中所有信息select * from student; 查询成绩表中列idclasschinese的信息select id,class,chinese from score; 3表数据排序操作升序order by 降序升序语句末尾加 desc
例子查询成绩表中列idchinese的信息并且以列chinese排序
select id,chinese from score order by chinese;升序
select id,chinese from score order by chinese desc;降序 4表数据查询操作
(1)查询1班与2班的成绩信息mysql select * from score where class1 or class2;
(2)查询语文为77并且数学为88的成绩信息
mysql select * from score where chinese77 and maths88;
(3)查询123班的成绩信息mysql select * from score where class in (1,2,3);
查询不为4班的成绩信息: mysql select * from score where class not in (4);
(4)查询不为4班的成绩信息: mysql select * from score where class !4;
注释 !在数据库里面为否定的意思:
(5) 查询1班到3班的成绩信息: mysql select * from score where class between 1 and 3;
注释 between在之间中间的意思:
(6) 查询不为3班与4班的成绩信息mysql select * from score where class not in (3,4);
(7)查询语文成绩大于等于80小于等于90的成绩信息
mysqlselect * from score where chinese80 and chinese90;
(8) 统计成绩表的总数:mysql select count(*) from score;
(9) 按照英语去重显示英语成绩信息:mysql select distinct English from score;
注释 distinct 去除重复的意思
(10) 显示4到7行的数据:mysql select * from score limit 3,4;
注释数据库数据排列0123 3显示第4行 4567共有4行 34
3表示第4行4表示从第3行开始到第7行共有4行
(11) 按chinese排序显示4,5行数据: mysql select * from score order by chinese limit 3,2;
(12) 查询出学生姓名为stu10的学生信息mysql select * from student where namestu10;
注释只要不是数字有汉字数字字母多种组成的形式都要加单引号表示字符串。
(13) 查询出学生姓名为stu10或者stu15的学生信息:
mysql select * from student where name in (stu10,stu15);
(14) 分组查询每个班的人数mysql select class,count(*) from student group by class; 作业
1查询4班的成绩信息select * from score where class4
;
2查询4班语文成绩大于80小于90的成绩信息
select * from score where class in (4) and chinese80 and chinese90; 3查询学生表中5到10行的数据select * from student limit 4,6; 4显示3班语文成绩为90数学成绩为68的class与number信息,
select class, number from score where class3 and chinese90 and maths68; 5查询出4班成绩并且按语文成绩倒序排序
select * from score where class4 order by chinese desc; 6查询2班与3班语文成绩与数学成绩都大于80的class与number信息
select class, number from score where class in (2,3) and chinese80 and maths88; 7查询学生名不为stu18stu22stu35stu46stu54stu72班级与学号信息 分组与函数查询
温馨提示分组之后查询其他函数结果是不正确的 分组函数group by
按班级分组查询出每班数学最高分select class,max(maths) from score group by class;
不分班级查询总人数最高分: select max(maths) from score;
注释: max:最大值; 按班级分组查询出每班数学最低分select class,min(maths) from score group by class;
注释:最小值min 按班级分组查询出每班数学总分select class,sum(maths) from score group by class;
注释sum总分 按班级分组查询出每班数学平均分select class,avg(maths) from score group by class;
注释avg平均值 按班级分组查询出每班学生总数select class,count(*) from score group by class;
注释count:有价值的 语句执行顺序: from先执行后执行where, 再接着执行having,limit等。
例句
select class,max(maths) from score where group by(分组) class having(所有) order by(排序) limit
from后面可以加兹查询语句先执行后面再执行前面 运算符数学运算符
mysql select class,number,maths,maths5 from score; mysqlselect class,number,chinesemathsenglish from score; mysql select *,mathschineseenglish as total from score; mysql select *,mathschineseenglish as total from score order by total desc; mysql select class*2,number,mathschineseenglish as total from score order by total desc; 连接查询 左连接查询
mysql select stu.*,sc.*,mathssc.chinesesc.english from student stu left join score sc on stu.idsc.id;
注释stu:为别名。student stu left join scorestudent:为主表score为副表显示。 left join:为左连接。 两表关联其ID必须一一对应stu.idsc.id 右连接查询
mysql select stu.*,sc.*,mathssc.chinesesc.english from student stu right join score sc on stu.idsc.id; 内连接查询两个表同时都有的内容才会显示。
mysql select stu.*,sc.*,mathssc.chinesesc.english from student stu join score sc on stu.idsc.id; 显示查询数据连接把后表与前排合起来在一个表显示。
select id,name,class from student union select class,number,maths from score; 多表查询
select name,student.class,student.number,maths,chinese,english from student,score where student.idscore.id; 题目练习
显示总分大于200的学生信息
select stu.name,sc.maths,sc.chinese,sc.english,sc.mathssc.chinesesc.english from student stu,score sc where stu.idsc.id and sc.mathssc.englishsc.chinese200;
显示班级总数大于等于20的班级
select class,count(*) as total from student group by class having total20;
显示人总数大于等于20的班级的成绩信息
mysql select sc.class,sc.number,sc.maths from score sc,(select class,count(*) as total from student group by class having total20) s where sc.classs.class; 注释commit保存提交的意思一般文件删除修改都要做保存
Rollback撤回的意思命令执行后可以撤回为修改删除前的数据
truncate table score永久删除的意思尽量少用删除则无记录找回
select now()查询现在的时间 修改语句
update 表名 set where 条件
mysql update student set birth1988,department中文系 where id901 and name张老大;
把张老大的出生日期修改为1988院系修改成中文系
mysql update student set birthbirth-5;
把所有学生的年纪增加5岁 删除语句
mysql delete from student where id901;
删除901同学的学生信息
mysql delete from student where address like 湖南%;
删除湖南籍学生的信息
mysql delete from student;
清空学生表信息 字符查询like
mysql select * from student where address like 北京%;
查询地址为北京的学生信息
mysql select * from student where address like %北京%平%;
查询地址为北京市昌平区的学生信息
mysql select * from score where stu_id in (select id from student where address like 湖南%);
查询湖南籍学生的成绩信息 作业
1把张三的计算机成绩修改成60分
update score set grade60 where stu_id in(select id from student where name张三)and c_name计算机; 2把计算机科目的分数降低5分
update score set gradegrade-5 where c_name计算机; 3把湖南省学生计算机分数提高5分
update score set gradegrade5 where c_name计算机and stu_id in(select id from student where address like 湖南%); 4把学号为904的学生计算机成绩改为85
update score set grade85 where c_name计算机 and stu_id904; 5删除904学生的成绩
delete from score where stu_id904; 6删除湖南籍贯学生的成绩
delete from score where stu_id in(select id from student where address like 湖南%); 7删除王姓与张姓同学英语成绩
delete from score where stu_id in (select id from student where name like 王%or name like 张%) and c_name英语; 8删除年纪大于30的学生的计算机成绩 delete from score where stu_id in (select id from student where 2016-birth30); MySQL练习 创建student和score表 CREATE TABLE student (id INT(10) NOT NULL PRIMARY KEY ,name VARCHAR(20) NOT NULL ,sex VARCHAR(4) ,birth YEAR,department VARCHAR(20) ,address VARCHAR(50) ); 创建score表SQL代码如下 CREATE TABLE score (id INT(10) NOT NULL PRIMARY KEY AUTO_INCREMENT ,
stu_id INT(10) NOT NULL ,c_name VARCHAR(20) ,grade INT(10)); 为student表和score表增加记录 向student表插入记录的INSERT语句如下
INSERT INTO student VALUES( 901,张老大, 男,1984,计算机系, 北京市海淀区);
INSERT INTO student VALUES( 902,张老二, 男,1987,中文系, 北京市昌平区);
INSERT INTO student VALUES( 903,张三, 女,1991,中文系, 湖南省永州市);
INSERT INTO student VALUES( 904,李四, 男,1993,英语系, 辽宁省阜新市);
INSERT INTO student VALUES( 905,王五, 女,1990,英语系, 福建省厦门市);
INSERT INTO student VALUES( 906,王六, 男,1989,计算机系, 湖南省衡阳市);
INSERT INTO student VALUES( 907,老七, 男,1991,计算机系, 广东省深圳市);
INSERT INTO student VALUES( 908,老八, 女,1990,英语系, 山东省青岛市); 向score表插入记录的INSERT语句如下
INSERT INTO score VALUES(NULL,901, 计算机,98);
INSERT INTO score VALUES(NULL,901, 英语, 80);
INSERT INTO score VALUES(NULL,902, 计算机,65);
INSERT INTO score VALUES(NULL,902, 中文,88);
INSERT INTO score VALUES(NULL,903, 中文,95);
INSERT INTO score VALUES(NULL,904, 计算机,70);
INSERT INTO score VALUES(NULL,904, 英语,92);
INSERT INTO score VALUES(NULL,905, 英语,94);
INSERT INTO score VALUES(NULL,906, 计算机,90);
INSERT INTO score VALUES(NULL,906, 英语,85);
INSERT INTO score VALUES(NULL,907, 计算机,98); 1.查询student表的第2条到4条记录
select * from student limit 1,3; 2.从student表查询所有学生的学号id、姓名name和院系department的信息
mysql select id,name,department from student; 3.从student表中查询计算机系和英语系的学生的信息
select * from student where department in (计算机系 ,英语系); 4.从student表中查询年龄23~26岁的学生信息
select * from student where birth between 1990 and 1993; 2016-231993 2016-261990
select id,name,sex,2016-birth as age,department,address from student where 2016-birth; 5.从student表中查询每个院系有多少人
select department,count(id) from student group by department; 6.从score表中查询每个科目的最高分。
select c_name,max(grade) from score group by c_name; 7.查询李四的考试科目c_name和考试成绩grade
select c_name,grade from score,student where score. stu_idstudent.id and name李四;
select c_name,grade from score where stu_id(select id from student where name李四; 8.用连接的方式查询所有学生的信息和考试信息
select stu.*,sc.* from student stu left join score sc on stu.idsc.id; 9.计算每个学生的总成绩
select stu_id,sum(grade) from score group by stu_id; 10.计算每个考试科目的平均成绩
select c_name,avg(grade) from score group by c_name; 11.查询计算机成绩低于95分的学生信息
select student.*, grade from score,student where student.idscore.stu_id and c_name like 计算机 and grade95; 12.查询同时参加计算机和英语考试的学生的信息
select student.*,c_name from student,score where student.idscore.stu_id and student.
id any( select stu_id from score where stu_id in (select stu_id from score where c_name 计算机) and c_name 英语 ); select * from student where id in(select stu_id from score where stu_id in (select stu_id from
score where c_name计算机 )and c_name英语); select student.* from student,(select stu_id from score where stu_id in (select stu_id from score where c_name计算机 )and c_name英语) t1 where student.idt1.stu_id; select * from student where id in (select stu_id from score sc where sc.c_name计算机) and id in (select stu_id from score sc where sc.c_name英语); 13.将计算机考试成绩按从高到低进行排序
select c_name,grade from score where c_name计算机 order by grade; 14.从student表和score表中查询出学生的学号然后合并查询结果
select id from student union select id from score; 15.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name,department,c_name,grade from score sc,student st where st.idsc.stu_id and (name like张%or name like 王%); 16.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩中文系
select name,2016-birth age,department,address,c_name,grade from student,score where student.idscore.stu_id and address like湖南%; 17.查询每个科目的最高分的学生信息.
分解 scoret1, t2select c_name,max(grade) as grade from score group by c_name t1.stu_id注解
分解 select * from student where id in (select t1.stu_id from score t1,t2 t2 where t1.c_namet2.c_name and t1.gradet2.grade); select * from student where id in (select t1.stu_id from score t1,(select c_name,max(grade) as grade from score group by c_name) t2 where t1.c_namet2.c_name and t1.gradet2.grade); select student.* from student,(select score.* from score,(select max(grade) grade,c_name from score group by c_name) t1 where score.c_namet1.c_name and score.gradet1.grade) t2 where student.idt2.stu_id; 总结 感谢每一个认真阅读我文章的人
如果下面这些资料用得到的话可以直接拿走
1、自学开发或者测试必备的完整项目源码与环境
2、测试工作中所有模板测试计划、测试用例、测试报告等
3、软件测试经典面试题
4、Python/Java自动化测试实战.pdf
5、Jmeter/postman接口测试全套视频获取
6、Python学习路线图 重点配套学习资料和视频教学
那么在这里我也精心准备了上述大纲的详细资料包含电子书简历模块各种工作模板面试宝典自学项目等。如下需要的点击下方名片加我VX免费领取。