MySQL数据库表的数据插入、修改、删除、查询操作及实例应用

时间:2022-08-21 17:02:53

一、MySQL数据库表的数据插入、修改、删除和查询

 CREATE DATABASE db0504;

 USE db0504;

 CREATE TABLE student (
sno VARCHAR (10) NOT NULL UNIQUE PRIMARY KEY,
sname VARCHAR (20) NOT NULL,
ssex enum ('男', '女') NOT NULL DEFAULT '男',
sbirth date,
sclass VARCHAR (10),
zno CHAR (4)
) DEFAULT charset = gb2312;
#添加操作
INSERT INTO student
VALUES('','丁暖','女','1993-9-9','物联网12-1','z002'); INSERT INTO student (sno,sname,ssex,sbirth,sclass,zno)
VALUES('','孔超','男','1993-9-19','自动化11-1','z006'); INSERT INTO student (sno, sname, ssex, zno)
VALUES('','韩梅','女','z006'); INSERT INTO student (sname, zno, sno)
VALUES('韩梅2','z006',''); INSERT INTO student VALUES
('','孔超1','男','1993-9-19','自动化11-1','z006');
('','孔超2','男','1993-9-19','自动化11-1','z006');
('','孔超3','男','1993-9-19','自动化11-1','z006');
('','孔超4','男','1993-9-19','自动化11-1','z006');
#修改操作
UPDATE student
SET sname = '张三',sbirth = '1993-6-19'
WHERE sname = '丁暖'; UPDATE student SET zno = LEFT (zno, 3);
#删除操作
delete from student
where sname='张三'; #查询操作
SELECT * FROM student;#查询student表的所有记录
SELECT sno,sname,ssex,sbirth FROM student;#查询部分字段信息
SELECT * FROM student WHERE age BETWEEN 25 AND 30; #清空表操作
truncate student;#高效
delete from student;

操作SQL语法格式:

插入:insert

修改:update

删除:delete

查询:select

二、设计型实验三

1.创建表

 CREATE DATABASE homework;

 USE homework;

 CREATE TABLE student (
Num INT (10) NOT NULL UNIQUE PRIMARY KEY,
name VARCHAR(20) NOT NULL,
Sex VARCHAR(4) NOT NULL,
birthday DATETIME,
bumen VARCHAR(20) NOT NULL,
address VARCHAR(50)
) DEFAULT charset = gb2312; CREATE TABLE score (
Id INT(10) NOT NULL UNIQUE PRIMARY KEY,
C_name VARCHAR(20),
Stu_id INT(10) NOT NULL,
grade INT(10),
CONSTRAINT score_fk FOREIGN KEY(Stu_id) REFERENCES student(Num)
) DEFAULT charset = gb2312; INSERT INTO student VALUES
('','张军','男','1985-1-1','计算机系','北京市海淀区'),
('','张超','男','1986-2-2','中文系','北京市昌平区'),
('','张美','女','1990-2-1','中文系','湖南省永州市'),
('','李五一','男','1990-2-3','英语系','辽宁省阜新市'),
('','王芳','女','1991-4-6','英语系','福建省厦门市'),
('','王桂','男','1988-6-7','计算机系','湖南省衡阳市'); INSERT INTO score VALUES
(1001,'计算机','',''),
(1002,'英语','',''),
(1003,'计算机','',''),
(1004,'中文','',''),
(1005,'中文','',''),
(1006,'计算机','',''),
(1007,'英语','',''),
(1008,'英语','',''),
(1009,'计算机','',''),
(1010,'英语','','');

2.查询student表的操作

#查询student表的所有记录
SELECT * FROM student;
SELECT Num,name,Sex,birthday,bumen,address FROM student;
#查询student表的第2条到第4条记录
SELECT Num,name,Sex,birthday,bumen,address FROM student LIMIT 1,3;
#从student表中查询所有学生的学号、姓名和院系的信息
SELECT Num,name,bumen FROM student;
#查询计算机系和英语系学生的信息
SELECT * FROM student WHERE bumen IN('计算机系','英语系');
SELECT * FROM student WHERE bumen='计算机系' OR bumen='英语系';
#从student表中查询年龄为18-22岁学生的信息
ALTER TABLE student ADD age INT(4);
ALTER TABLE student MODIFY age INT(4) AFTER Sex;
UPDATE student SET age=24 WHERE Num=901;
UPDATE student SET age=23 WHERE Num=902;
UPDATE student SET age=19 WHERE Num=903;
UPDATE student SET age=19 WHERE Num=904;
UPDATE student SET age=18 WHERE Num=905;
UPDATE student SET age=21 WHERE Num=906;
SELECT * FROM student WHERE age BETWEEN 18 AND 22;
SELECT * FROM student WHERE age>=18 AND age<=22;
#从student表中查询每个院系有多少人,为统计的人数列取别名sum_of_bumen
SELECT bumen,COUNT(bumen) AS sum_of_bumen FROM student GROUP BY bumen;

3.两表综合查询

#从score表中查询每个科目的最高分
SELECT C_name,MAX(grade) FROM score GROUP BY C_name;
#查询李五一的考试科目(C_name)和考试成绩(grade)
select C_name,grade from student left join score on student.Num=score.Stu_id where name='李五一';
#用连接查询的方式查询所有学生的信息和考试信息
select * from student left join score on student.Num=score.Stu_id;
#计算每个学生的总成绩(需显示学生姓名)
select name '姓名',SUM(grade) '总成绩' from student left join score on student.Num=score.Stu_id group by name;
#计算每个考试科目的平均成绩
select C_name '考试科目',AVG(grade) '平均成绩' from student left join score on student.Num=score.Stu_id group by C_name;
#查询计算机成绩低于95分的学生信息
select * from student left join score on student.Num=score.Stu_id where grade<95 AND C_name='计算机';
#将计算机成绩按从高到低排序
select grade '计算机成绩' from score where C_name='计算机' order by grade desc;#asc升序,desc降序
#从student表和score表中查询学生的学号,然后合并查询结果
select Num '学号' from student union select Stu_id from score;
#查询姓张或者姓王的同学的姓名、院系、考试科目和成绩
select name,bumen,C_name,grade from student left join score on student.Num=score.Stu_id where name like '张%' union
select name,bumen,C_name,grade from student left join score on student.Num=score.Stu_id where name like '王%';
#查询都是湖南的学生的姓名、年龄、院系、考试科目和成绩
select name,age,bumen,C_name,grade from student left join score on student.Num=score.Stu_id where address like '湖南%';

这个查询同时参加计算机和英语考试的学生信息难了我好久,下面提供三种做法:

#查询同时参加计算机和英语考试的学生的信息做法一
select * from student where Num in (
select Stu_id from score where C_name ='计算机' and Stu_id in(select Stu_id from score where C_name ='英语'));
#查询同时参加计算机和英语考试的学生的信息做法二
SELECT a.* FROM student a,score b,score c WHERE a.Num=b.Stu_id AND b.C_name='计算机'
AND a.Num=c.Stu_id AND c.C_name='英语';
#查询同时参加计算机和英语考试的学生的信息做法三
SELECT * FROM student WHERE Num =ANY( SELECT Stu_id FROM score WHERE Stu_id IN (
SELECT Stu_id FROM score WHERE C_name='计算机') AND C_name='英语');