当前位置:网站首页>[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 ';
边栏推荐
猜你喜欢
开发转测试:从0开始的6年自动化之路...
asp.net 使用redis缓存
在Blazor 中自定义权限验证
The problem of accessing certsrv after configuring ADCs
Fuzzy PID control of motor speed
正则表达式
Source code analysis of object wait notify notifyAll
QT handy notes (III) use qtcharts to draw a line chart in VS
Drawing shadow error diagram with MATLAB
Redis sentinel mode setup under Windows
随机推荐
2020-12-29
JS 连等赋值操作
After attaching to the process, the breakpoint displays "currently will not hit the breakpoint, and no symbols have been loaded for this document"
PHP一次请求生命周期
Mo team learning notes (I)
v-for动态设置img的src
asp.net 使用redis缓存
Process32First返回false,错误x信息24
网站设计需要的基本知识
WARNING: [pool www] server reached pm.max_children setting (5), consider raising it
QT随手笔记(六)——更新界面、截图、文件对话框
IIS网站配置
配置ADCS后访问certsrv的问题
matlab simulink实现模糊pid对中央空调时延温度控制系统控制
IIS error prompt after installing Serv-U: hresult:0x80070020
Redis sentinel mode setup under Windows
Global variables in DLL
一种分布式深度学习编程新范式:Global Tensor
Qt随手笔记(三)在vs中使用QtCharts画折线图
Fiddler download and installation