当前位置:网站首页>常用SQL语句(完整范例)
常用SQL语句(完整范例)
2022-07-02 15:00:00 【游戏编程】
本文基于学校的班级、学生、学科、成绩等场景,实践SQL语句的使用
创建表
- 创建班级表
CREATE TABLE class(class_id INT auto_increment PRIMARY key,class_name VARCHAR(10));alter table class AUTO_INCREMENT =1;- 创建学生表
CREATE TABLE student(stu_id INT AUTO_INCREMENT PRIMARY KEY,stu_name VARCHAR(10),stu_sex VARCHAR(1),stu_age INT,class_id INT,foreign key(class_id) references class(class_id));alter table student AUTO_INCREMENT =1;- 创建科目表
CREATE TABLE course(course_id INT auto_increment PRIMARY key,course_name VARCHAR(10));alter table course AUTO_INCREMENT =1;- 创建成绩表
drop table score;CREATE TABLE score(id INT auto_increment PRIMARY key,stu_id INT,course_id INT,mark DECIMAL(3,1),foreign key(stu_id) references student(stu_id),foreign key(course_id) references course(course_id));alter table score AUTO_INCREMENT =1;desc score;向表里插入数据
操作表为class表
insert into class values(NULL, '一二')insert into class (`class_id`,`class_name`) values (NULL, '一3')insert into class (`class_name`) values ('一4')存储过程的使用:批量插入数据
- 创建存储过程
CREATE PROCEDURE search_sex ( #search_user_name为存储过程的名字 IN search_sex VARCHAR (20), #传入的参数 OUT count_number INT #返回的参数) READS SQL DATA #程序中包含读数据的语句BEGIN SELECT COUNT(*) INTO count_number FROM student WHERE stu_sex LIKE CONCAT('%', search_sex, '%');END- 调用存储过程
CALL search_sex('女', @nameCount);SELECT @nameCount;- 使用存储过程向学生表里插入大量数据
#创建存储过程创建大量数据drop PROCEDURE if EXISTS insert_students;CREATE PROCEDURE insert_students(IN loop_times INT,IN stu_name CHAR,IN stu_sex CHAR,IN stu_age INT,IN class_id INT)BEGIN DECLARE var INT DEFAULT 0; WHILE var < loop_times DO INSERT INTO student VALUES (NULL,concat(stu_name,CONVERT(var,CHAR)),stu_sex,stu_age,class_id); SET var = var + 1; END WHILE;ENDCALL insert_students(10,'钱','男',16,4); 查找语句
- 查找所有成绩都大于95分的同学姓名
SELECT student.stu_name FROM score,studentwhere score.mark>95and student.stu_id=score.stu_idGROUP BY score.stu_idhaving count(*)>1- 查询平均分大于90分的同学名单
SELECT score.stu_id,student.stu_name,AVG(score.mark)FROM score,student where student.stu_id=score.stu_idGROUP BY score.stu_id HAVING AVG(score.mark)>90- 查出平均分最高的同学 (如果最高有两个呢??)
SELECT score.stu_id,student.stu_name,AVG(score.mark)FROM score,student where student.stu_id=score.stu_idGROUP BY score.stu_idORDER BY AVG(score.mark) desc LIMIT 1作者:测试进阶
游戏编程,一个游戏开发收藏夹~
如果图片长时间未显示,请使用Chrome内核浏览器。
边栏推荐
- 将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus
- Tech talk activity preview | building intelligent visual products based on Amazon kVs
- 宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
- Smart trash can (V) - light up OLED
- 2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst
- executescalar mysql_ExecuteScalar()
- 选择 SAP Spartacus 作为 SAP Commerce Cloud Storefront 实现框架的五个理由
- Schoolbag novel multithreaded crawler [easy to understand]
- CEPH principle
- How to quickly distinguish controlled components from uncontrolled components?
猜你喜欢

默认浏览器设置不了怎么办?

2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst

例题 非线性整数规划
![[leetcode] 14. Préfixe public le plus long](/img/70/e5be1a7c2e10776a040bfc8d7711a0.png)
[leetcode] 14. Préfixe public le plus long

Microservice architecture practice: using Jenkins to realize automatic construction

剑指 Offer 25. 合并两个排序的链表

Smart trash can (V) - light up OLED

Tech talk activity preview | building intelligent visual products based on Amazon kVs

Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia

关于我
随机推荐
ssb门限_SSB调制「建议收藏」
si446使用记录(一):基本资料获取
What if the default browser cannot be set?
Smart trash can (V) - light up OLED
Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
Experience home office, feel the completion of the project | community essay solicitation
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
13、Darknet YOLO3
【Leetcode】13. Roman numeral to integer
From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
class和getClass()的区别
The computer comes with software to make the background color of the picture transparent (matting white background)
Visibilitychange – refresh the page data when the specified tab is visible
将您的基于 Accelerator 的 SAP Commerce Cloud Storefront 迁移到 Spartacus
Eth data set download and related problems
ROS知识点——消息过滤器 ( message_filters)
Meanings of SNAT, DNAT and masquerade in iptables
Briefly introduce the use of base64encoder
2、 Expansion of mock platform
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面