当前位置:网站首页>常用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内核浏览器。
边栏推荐
- 2022 interview questions
- executescalar mysql_ExecuteScalar()
- Interpretation of key parameters in MOSFET device manual
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- How to quickly distinguish controlled components from uncontrolled components?
- SSB threshold_ SSB modulation "suggestions collection"
- Eye of depth (II) -- matrix and its basic operations
- 博客主题 “Text“ 夏日清新特别版
- QStyle实现自绘界面项目实战(二)
- Ocio V2 reverse LUT
猜你喜欢

Exploration of mobile application performance tools

几行代码搞定RPC服务注册和发现

Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights

Qstype implementation of self drawing interface project practice (II)

Blog theme "text" summer fresh Special Edition

福元医药上交所上市:市值105亿 胡柏藩身价超40亿

剑指 Offer 21. 调整数组顺序使奇数位于偶数前面

绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东

ThreadLocal

ETH数据集下载及相关问题
随机推荐
A case study of college entrance examination prediction based on multivariate time series
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
剑指 Offer 25. 合并两个排序的链表
2022 interview questions
Sword finger offer 22 The penultimate node in the linked list
几行代码搞定RPC服务注册和发现
[shutter] dart data type (dynamic data type)
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
ROS知识点——消息过滤器 ( message_filters)
class和getClass()的区别
Linux Installation PostgreSQL + Patroni cluster problem
871. 最低加油次数
Vscode + eslint configuration
ThreadLocal
si446使用记录(二):使用WDS3生成头文件
Idea2021.1 installation tutorial
Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei
Microservice architecture practice: Construction of scalable distributed database cluster
Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
Blog theme "text" summer fresh Special Edition