MySQL 常用 SQL 语句
数据库:
SET FOREIGN_KEY_CHECKS=0;-- ------------------------------ Table structure for course-- ----------------------------DROP TABLE IF EXISTS `course`;CREATE TABLE `course` (`Cno` int(11) NOT NULL,`Cname` varchar(255) CHARACTER SET utf8 DEFAULT NULL,`Cpno` int(11) DEFAULT NULL,`Ccredit` int(11) DEFAULT NULL,PRIMARY KEY (`Cno`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;-- ------------------------------ Records of course-- ----------------------------INSERT INTO `course` VALUES ('1', '数据库', '5', '4');INSERT INTO `course` VALUES ('2', '数学', null, '2');INSERT INTO `course` VALUES ('3', '信息系统', '1', '4');INSERT INTO `course` VALUES ('4', '操作系统', '6', '3');INSERT INTO `course` VALUES ('5', '数据结构', '7', '4');INSERT INTO `course` VALUES ('6', '数据处理', null, '2');INSERT INTO `course` VALUES ('7', 'PASCAL语言', '6', '4');-- ------------------------------ Table structure for sc-- ----------------------------DROP TABLE IF EXISTS `sc`;CREATE TABLE `sc` (`Sno` int(11) NOT NULL,`Cno` int(11) NOT NULL,`Grade` int(11) DEFAULT NULL,KEY `FK_cno` (`Cno`),KEY `FK_sno` (`Sno`),CONSTRAINT `FK_cno` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `FK_sno` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`) ON DELETE CASCADE ON UPDATE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=latin1;-- ------------------------------ Records of sc-- ----------------------------INSERT INTO `sc` VALUES ('201215121', '1', '97');INSERT INTO `sc` VALUES ('201215121', '2', '90');INSERT INTO `sc` VALUES ('201215121', '3', '93');INSERT INTO `sc` VALUES ('201215122', '2', '95');INSERT INTO `sc` VALUES ('201215122', '3', '85');-- ------------------------------ Table structure for student-- ----------------------------DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (`Sno` int(11) NOT NULL,`Sname` varchar(255) CHARACTER SET utf8 DEFAULT NULL,`Ssex` varchar(255) CHARACTER SET utf8 DEFAULT NULL,`Sage` int(11) DEFAULT NULL,`Sdept` varchar(255) CHARACTER SET utf8 DEFAULT NULL,PRIMARY KEY (`Sno`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;-- ------------------------------ Records of student-- ----------------------------INSERT INTO `student` VALUES ('201215121', '刘晨', '女', '19', 'CS');INSERT INTO `student` VALUES ('201215122', '罗琪', '女', '17', 'CS');INSERT INTO `student` VALUES ('201215123', '刘中江', '男', '18', 'MA');INSERT INTO `student` VALUES ('201215125', '曹志雄', '男', '18', 'CS');INSERT INTO `student` VALUES ('201215126', '刘琰', '男', '19', 'CS');INSERT INTO `student` VALUES ('201215127', '刘言曌', '男', '19', 'CS');INSERT INTO `student` VALUES ('201215128', '曹志雄', '男', '19', 'CS');INSERT INTO `student` VALUES ('201215207', '曹阿瞒', '男', '19', 'IS');INSERT INTO `student` VALUES ('201215208', '春日风', '女', '18', null);
#### SELECT 常用语句#####查询年龄在18-19的学生#select * from student where Sage between 18 and 19;#查询年龄不在18-19的学生#select * from student where Sage not between 18 and 19;#查询计算机科学系(CS系),数学系(MA系)和信息系(IS)学生#select * from student where Sdept in('CS','MA','IS');#查询既不是计算机系(IS系),也不是信息系(MA系)和信息系(IS系)学生#select * from student where Sdept not in('CS','MA','IS');#查询姓刘的学生#select * from student where Sname like '刘%';#查询姓欧阳,且全名为三个汉字的学生#select *from student where Sname like '欧阳_';#查询名字中第二个字为晨的学生#select * from student where Sname like '_晨';#查询不姓刘的学生#select * from student where Sname not like '刘%';#查询缺少成绩的学生的学号和相对的课程号#select sno,cno from sc where grade is null;#查询所有有成绩的学生的学号和课程号#select sno,cno from sc where grade is not null;#查询选修了3号课程的学生的学号,姓名和分数,按成绩降序排序#select sc.sno,student.sname,sc.grade from student,sc where cno=3 and student.sno=sc.sno order by grade desc;#查询全体学生,按所在系升序排序,同一个系按年龄降序#select * from student order by sdept asc,sage desc ;#查询学生总人数#select count(*) from student;#查询选修了课程的学生总人数#select count(distinct sno) from sc ;#计算3号课程的平均分#select avg(grade) from sc where cno=2;#查询学号201215121 选修课程的总分数#select sum(grade) from sc where sno='201215121';#查询每个学生及其选修课的情况#select * from student, sc where student.sno = sc.sno;#查询选修了2号课程且成绩在90分以上的所有学生#select * from sc where cno=2 and grade>90;#左外连接#select student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from student left outer join sc on (student.sno = sc.sno);#多表连接#select student.sno,sname,cname,grade from student, sc, course where student.sno = sc.sno and course.cno = sc.cno#嵌套查询-查询选修了2号课程的学习姓名#select sname from student where sno in (select sno from sc where cno='2');#查询与刘晨在同一系学习的学生#select * from student where sdept = (select sdept from student where sname = '刘晨');#select * from student where sdept in (select sdept from student where sname = '刘晨');#select * from student s1,student s2 where s1.sdept = s2.sdept and s1.sname = '刘晨';#查询选修了信息系统的学生#select * from student,sc,course where sc.sno = student.sno and sc.cno = course.cno and course.cname = '信息系统'; ;#select * from student where sno in (select sno from sc where cno = ( select cno from course where cname = '信息系统' ));#找出每个学生超过其选修平均成绩的课程号#select * from sc where grade > (select avg(grade) from sc where sno = sc.Sno);#查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生#select * from student where sage < any (select sage from student) and sdept != 'cs';#select * from student where sage < (select max(sage) from student) and sdept != 'cs';#查询非计算机系中比计算机系所有年龄都要小的学生#select * from student where sage < all (select sage from student ) and sdept != 'cs';#select * from student where sage < (select min(sage) from student);#查询选修了2号课程的学生#select * from student where exists (select * from sc where cno = 2 and sc.sno = student.sno);#select * from student where sno in (select sno from sc where cno = 2);#查询没有选修2号课程的学生#select * from student where not exists (select * from sc where cno = 2 and sc.sno = student.sno);#select * from student where student.sno not in (select sc.sno from sc where sc.sno = 2);#查询选修了全部课程的学生-- select * from student where not exists -- (-- select * from course where not exists -- (-- select * from sc where sc.sno = student.sno and cno = course.cno-- )-- ) -- select sno from -- (-- select sno,count(*) as sum from sc group by sno having sum = -- (-- select count(*) from course-- )-- ) as A # 集合查询 UNION (或,并集)#select * from student where Sname = '罗琪' UNION select * from student where Sname = '刘言曌';#select * from student where Sname = '罗琪' or Sname = '刘言曌';#找出每个学生超过自己选修课程平均成绩的课程号-- select * from sc,-- (-- select sno,avg(grade) as avg_grade from sc group by sno-- ) as A-- where A.sno = sc.sno and sc.grade > A.avg_grade;#### INSERT 常用语句 #####将一个新学生插入到 Student 表中#insert into Student value(201215128, '曹志雄', '男', 20, 'MA');#insert into student(sno,sname,ssex,sage,sdept) values (201215207, '曹阿瞒','男',19, 'IS');#### UPDATE 常用语句 #####修改某一个元组的值#将罗琪的年龄-1#update student set Sage = '18' where Sname = '罗琪'#修改多个元组的值#将所有的学生年龄-1#update Student set Sage = Sage - 1;#带子查询的修改语句#将计算机系的学生成绩+5-- update sc -- set grade = grade + 5-- where sno in-- (select sno from student where sdept = 'CS');#### DELETE 常用语句 #####先添加一条学生记录,然后删除他#insert into student value(201215130,'张三','男',19,'IS');#delete from student where sno = '201215130';#删除多条记录##delete from sc;#### 空值的处理 #####空值产生#添加一个学生,学院为NULL#insert into student value(201215208, '春日风','女',18,NULL);#将曹志雄的学院设置为NULL#update student set sdept = NULL where sname = '曹志雄';#空值判断#查询没有先行课的课程#select * from course where cpno is null;#查询有先行课的课程#select * from course where cpno is not null#### 视图常用语句 #####创建一个男生视图-- create view is_boy -- as -- select * from student where ssex = '男';#查询视图#select * from is_boy where sage < 19#更新视图#update is_boy set sage = sage +1 where sname = '刘中江'#删除视图#drop view is_boy;
转自:https://liuyanzhao.com/7131.html
MySQL 常用 SQL 语句相关推荐
- php面试专题---MySQL常用SQL语句优化
php面试专题---MySQL常用SQL语句优化 一.总结 一句话总结: 原理,万变不离其宗:其实SQL语句优化的过程中,无非就是对mysql的执行计划理解,以及B+树索引的理解,其实只要我们理解执行 ...
- Mysql常用sql语句(11)- between and 范围查询
测试必备的Mysql常用sql语句系列 https://www.cnblogs.com/poloyy/category/1683347.html 前言 between and可以判断值是否在指定范围内 ...
- MySQL—常用SQL语句整理总结
关注微信公众号:CodingTechWork,一起学习进步. 引言 记录并整理常用的SQL语句使用笔记. 创建数据库和表 创建库(CREATE DATABASE) CREATE DATABASE ...
- MYSQL常用SQL语句分享
转自:微点阅读 https://www.weidianyuedu.com SQL分类: DDL-----数据定义语言(CREATE--创建,ALTER--修改. DROP--删除表,DECLARE- ...
- mysql常用sql语句优化
转载自:http://www.cnblogs.com/gomysql/p/3632209.html 在数据库日常维护中,最常做的事情就是SQL语句优化,因为这个才是影响性能的最主要因素.当然还有其他方 ...
- mysql 常用sql语句 简介
目录 零.用户管理 一.数据库操作 二.创建表 三.修改表 四.插入数据 五.更新数据 六.删除数据 七.条件控制 八.MySQL的正则表达式 九.MySQL的一些函数 十.分组查询 十一.UNION ...
- mysql 常用sql语句
增加字段: alter table 表X add province_id int(11) COMMENT '省份id' AFTER province DEFAULT 1; 删除字段: A ...
- MySQL常用SQL语句(CURD,建表,加字段,查改参数)
查询: FROM_UNIXTIME():时间戳转日期 sum:合计 SELECT*,FROM_UNIXTIME(a.add_time) AS add_time2,SUM(b.goods_number) ...
- 软件测试--------数据库MySQL 常用sql语句
1 --选择: select * from table1 where 范围 2 --插入: insert into table1(field1,field2) values(valu ...
最新文章
- RedHat 7.0及CentOS 7.0禁止Ping的三种方法
- 第十八课.动态图模型
- List,Set,Collection,Collections比较
- 下面不属于python第三方库的安装方法的是-python第三方库的pip安装方法
- 深度学习模型大合集:GitHub趋势榜第一,两天斩获2000星
- 一个内核网络漏洞详解|容器逃逸
- java final 初始化_在Java中,可以从构造函数助手中初始化final字段吗?
- Java开发人员的十大戒律
- kafkatemplate无法注入_Spring-Kafka(三)-KafkaTemplate发送消息及结果回调
- 大理大学日常作业计算机基础知识,大理学院成人高等教育大学计算机基础课程作业.doc...
- 网件R8000路由器怎么云存储_给大姐姐换个“控制中心”——NETGEAR 网件 R7800 AC2600M 路由器 简晒_路由器...
- js制作随机抽奖,(指定数字范围内随机出现一个数字)
- SpringBoot_Vue实现电影院售票系统
- java-php-python-ssm在线装机报价系统计算机毕业设计
- 关于uniapp cheneckbox复选框不显示对号的问题
- inprivate浏览是什么意思_InPrivate浏览是什么?
- axure6.5汉化最新正式破解版本下载(有注册码
- ftp上传 防火墙的设置
- 第3关:匿名函数应用
- Sql server 2016数据库卸载