当前位置:网站首页>[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
[MySQL database] a collection of basic MySQL operations - the basis of seeing (adding, deleting, modifying, and querying)
2022-07-26 09:39:00 【a box of Sweets】
1、examdb There are two tables in the library, as shown in the following figure :


2、 Operate on these two tables , Including building tables
user ExamDB;
#mysql If the self increasing key is a primary key
CREATE TABLE ExamDB.stuinfo(
StuName VARCHAR(8) NOT NULL ,
StuNo VARCHAR(20) NOT NULL ,
StuSex VARCHAR(4) NOT NULL DEFAULT ' male ',
StuAge INT(4) NOT NULL,
StuSeat INT(4) NOT NULL AUTO_INCREMENT ,
StuAddress varchar(100) DEFAULT ' The address is unknown ',
PRIMARY KEY (`StuSeat`)
);
# surface 2 StuMarks
CREATE TABLE ExamDB.StuMarks(
ID INT(4) NOT NULL AUTO_INCREMENT,
ExamNo VARCHAR(22) NOT NULL,
StuNo VARCHAR(20) ,
WrittenExam INT(4) NOT NULL DEFAULT 0,
LabExam INT(4) NOT NULL NULL DEFAULT 0,
PRIMARY KEY (`ID`)
);
# Add data
INSERT INTO ExamDB.stuinfo (
StuName,StuNo,StuSex,StuAge,StuSeat,StuAddress
)
VALUE(
' Zhang Qiuli ',
'2013000001',
' male ',
18,
1,
' Beijing haidian '
);
INSERT INTO ExamDB.stuinfo (
StuName,StuNo,StuSex,StuAge,StuSeat,StuAddress
)
VALUE(
' Lisbon ',
'2013000002',
' Woman ',
22,
2,
' Luoyang, Henan '
);
INSERT INTO ExamDB.stuinfo (
StuName,StuNo,StuSex,StuAge,StuSeat
)
VALUE(
' Li Wencai ',
'2013000003',
' male ',
21,
3
);
INSERT INTO ExamDB.stuinfo (
StuName,StuNo,StuSex,StuAge,StuSeat,StuAddress
)
VALUE(
' Ouyang Junxiong ',
'2013000004',
' male ',
18,
4,
' Karamay, Xinjiang '
);
# insert data League tables
INSERT INTO ExamDB.StuMarks (
ExamNo,
StuNo,
WrittenExam ,
LabExam
)
VALUE(
'E2013000001',
'2013000001',
80,
58
);
INSERT INTO ExamDB.StuMarks (
ExamNo,
StuNo,
WrittenExam
)
VALUE(
'E2013000002',
'2013000002',
50
);
INSERT INTO ExamDB.StuMarks (
ExamNo,
StuNo,
WrittenExam ,
LabExam
)
VALUE(
'E2013000003',
'2013000003',
97,
82
);
# 3、 ... and 、 Data addition, deletion, modification and query
-- #( One ) Query operation
-- 1) Query the data of two tables
SELECT * FROM ExamDB.stumarks;
SELECT * FROM ExamDB.stuinfo;
-- 2) Check the list of male students
SELECT * FROM stuinfo WHERE StuSex=' male ' ;
-- 3) Inquire about the students with excellent written examination results (75~100 branch )
SELECT * FROM ExamDB.stumarks WHERE WrittenExam BETWEEN 75 AND 100;
-- 4) Query the student's grades for reference , Include student name , Written test results , Machine test results
SELECT a.StuName,b.WrittenExam,b.LabExam FROM stuinfo a LEFT JOIN StuMarks b ON a.StuNo=b.StuNo ;
-- 5) Count the average scores of written test and machine test
SELECT AVG(WrittenExam) as WrittenExam,AVG(LabExam) as LabExam FROM ExamDB.stumarks;
-- 6) Count the number of students participating in this exam
SELECT count(*) from ExamDB.stumarks;
-- 7) Check the number of people who failed the exam ( The written test or machine test is less than 60 branch )
SELECT count(*) from ExamDB.stumarks where WrittenExam<60 or LabExam>60 ;
-- 8) Query students' scores , Show student number , Written test results , Machine test results , average
SELECT StuNo,WrittenExam, LabExam,(WrittenExam+LabExam)/2 as avg FROM ExamDB.stumarks GROUP BY StuNo;
-- 9) Ranking ( Sort by average from high to low ), Show student number 、 average
SELECT StuNo,(WrittenExam+LabExam)/2 as avg FROM ExamDB.stumarks GROUP BY StuNo order by avg DESC;
-- 10) a key : Ranking ( Sort by average from high to low ), Show name , Written test results , Machine test results , average
SELECT a.StuName,b.WrittenExam,b.LabExam,(b.WrittenExam+b.LabExam)/2 as avgsScore FROM ExamDB.stuinfo a
LEFT JOIN ExamDB.stumarks b on a.StuNo=b.StuNo order by avgsScore DESC ;
-- 11) a key : According to the average score , Display the top two messages , Including name, 、 Written test results 、 Machine test results 、 average limit
SELECT a.StuName,b.WrittenExam,b.LabExam,(b.WrittenExam+b.LabExam)/2 as avgsScore FROM ExamDB.stuinfo a
LEFT JOIN ExamDB.stumarks b on a.StuNo=b.StuNo order by avgsScore DESC LIMIT 2;
-- ( Two )DML operation
-- 1) a key : Add all written examination results 5 branch , But not more than 100 branch .
UPDATE stuMarks SET WrittenExam= if(WrittenExam>95,100,WrittenExam + 5 );
-- 2) Name is “ Ouyang Junxiong ” The student's name was changed to “ Ouyang Maimaiti ”
update ExamDB.stuinfo set StuName= ' Ouyang Maimaiti ' WHERE StuName =' Ouyang Junxiong ';
边栏推荐
猜你喜欢

小白搞一波深拷贝 浅拷贝

matlab simulink实现模糊pid对中央空调时延温度控制系统控制

Qt随手笔记(三)在vs中使用QtCharts画折线图

A new paradigm of distributed deep learning programming: Global tensor

登录模块用例编写

【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)

Server and client dual authentication (2)

面试题目大赏

CSV data file settings of JMeter configuration components

2022年中科磐云——服务器内部信息获取 解析flag
随机推荐
phpexcel导出emoji符号报错
Mo team learning summary (II)
学习笔记之常用数组api 改变原数组和不改变原数组的有哪些?
QT随手笔记(六)——更新界面、截图、文件对话框
添加dll
R language ggpubr package ggsummarystats function visualizes the grouping box diagram (custom grouping color) and adds the statistical values corresponding to the grouping under the x-axis label (samp
ie7设置overflow属性失效解决方法
【Mysql数据库】mysql基本操作集锦-看得会的基础(增删改查)
Add DLL
[MySQL] understand the important architecture of MySQL (I)
Gauss elimination
JS continuous assignment operation
Great reward for interview questions
解决npm -v突然失效 无反应
EOJ 2020 1月月赛 E数的变换
服务器、客户端双认证
R language ggplot2 visualization: align the legend title to the middle of the legend box in ggplot2 (default left alignment, align legend title to middle of legend)
面试突击68:为什么 TCP 需要 3 次握手?
(2) Hand eye calibration of face scanner and manipulator (eye out of hand: nine point calibration)
keepalived 实现mysql自动故障切换