当前位置:网站首页>Common SQL statements (complete example)
Common SQL statements (complete example)
2022-07-02 17:28:00 【Game programming】
This article is based on the school class 、 Student 、 Discipline 、 Performance and other scenes , practice SQL Use of statements
Create table
- Create class table
CREATE TABLE class(class_id INT auto_increment PRIMARY key,class_name VARCHAR(10));alter table class AUTO_INCREMENT =1;- Create student table
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 chart of accounts
CREATE TABLE course(course_id INT auto_increment PRIMARY key,course_name VARCHAR(10));alter table course AUTO_INCREMENT =1;- Create a score sheet
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;Insert data into the table
The operation table is class surface
insert into class values(NULL, ' 12 ')insert into class (`class_id`,`class_name`) values (NULL, ' One 3')insert into class (`class_name`) values (' One 4')Use of stored procedures : Bulk insert data
- Create stored procedure
CREATE PROCEDURE search_sex ( #search_user_name Is the name of the stored procedure IN search_sex VARCHAR (20), # Incoming parameter OUT count_number INT # Parameters returned ) READS SQL DATA # The program contains statements to read data BEGIN SELECT COUNT(*) INTO count_number FROM student WHERE stu_sex LIKE CONCAT('%', search_sex, '%');END- Calling stored procedure
CALL search_sex(' Woman ', @nameCount);SELECT @nameCount;- Use stored procedures to insert a large amount of data into the student table
# Create stored procedures to create large amounts of data 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,' money ',' male ',16,4); Look for statements
- Find all grades greater than 95 The name of the classmate
SELECT student.stu_name FROM score,studentwhere score.mark>95and student.stu_id=score.stu_idGROUP BY score.stu_idhaving count(*)>1- The average query score is greater than 90 The list of students divided
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- Find out the students with the highest average score ( What if there are two at most ??)
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 1author : Test advanced
Game programming , A game development favorite ~
If the picture is not displayed for a long time , Please use Chrome Kernel browser .
边栏推荐
- [fluent] dart data type map type (create map set | initialize map set | traverse map set)
- Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
- SSB threshold_ SSB modulation "suggestions collection"
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- Example nonlinear integer programming
- Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
- Qwebengineview crash and alternatives
- Vscode + eslint configuration
- JS delete substring in string
- 伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
猜你喜欢

13、Darknet YOLO3

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

Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity

ThreadLocal

例题 非线性整数规划

ETH数据集下载及相关问题

Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders

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

Microservice architecture practice: using Jenkins to realize automatic construction

si446使用记录(一):基本资料获取
随机推荐
Idea2021.1 installation tutorial
SAP Commerce Cloud 架构概述
剑指 Offer 27. 二叉树的镜像
The construction of scalable distributed database cluster and the partition design of oneproxy sub database
QStyle实现自绘界面项目实战(二)
Vscode knowledge points - Common Errors
OpenHarmony如何启动FA(本地和远程)
uniapp H5页面调用微信支付
visibilitychange – 指定标签页可见时,刷新页面数据
使用知行之桥的API端口,提供资源供合作伙伴访问
几行代码搞定RPC服务注册和发现
Eye of depth (III) -- determinant of matrix
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
Connect Porsche and 3PL EDI cases
The computer comes with software to make the background color of the picture transparent (matting white background)
Exploration of mobile application performance tools
Niuke js3 separator
dstat使用[通俗易懂]
Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
体验居家办公完成项目有感 | 社区征文