一、MySQL数据库表的数据插入、修改、删除和查询
1 CREATE DATABASE db0504;
2
3 USE db0504;
4
5 CREATE TABLE student (
6 sno VARCHAR (10) NOT NULL UNIQUE PRIMARY KEY,
7 sname VARCHAR (20) NOT NULL,
8 ssex enum ('男', '女') NOT NULL DEFAULT '男',
9 sbirth date,
10 sclass VARCHAR (10),
11 zno CHAR (4)
12 ) DEFAULT charset = gb2312;
13 #添加操作
14 INSERT INTO student
15 VALUES('2012010713','丁暖','女','1993-9-9','物联网12-1','z002');
16
17 INSERT INTO student (sno,sname,ssex,sbirth,sclass,zno)
18 VALUES('2011010503','孔超','男','1993-9-19','自动化11-1','z006');
19
20 INSERT INTO student (sno, sname, ssex, zno)
21 VALUES('2011010521','韩梅','女','z006');
22
23 INSERT INTO student (sname, zno, sno)
24 VALUES('韩梅2','z006','2011010522');
25
26 INSERT INTO student VALUES
27 ('2011010501','孔超1','男','1993-9-19','自动化11-1','z006');
28 ('2011010505','孔超2','男','1993-9-19','自动化11-1','z006');
29 ('2011010504','孔超3','男','1993-9-19','自动化11-1','z006');
30 ('2011010505','孔超4','男','1993-9-19','自动化11-1','z006');
31 #修改操作
32 UPDATE student
33 SET sname = '张三',sbirth = '1993-6-19'
34 WHERE sname = '丁暖';
35
36 UPDATE student SET zno = LEFT (zno, 3);
37 #删除操作
38 delete from student
39 where sname='张三';
40
42 #查询操作
43 SELECT * FROM student;#查询student表的所有记录
44 SELECT sno,sname,ssex,sbirth FROM student;#查询部分字段信息
45 SELECT * FROM student WHERE age BETWEEN 25 AND 30;
46
47 #清空表操作
48 truncate student;#高效
49 delete from student;
操作SQL语法格式:
插入:insert
修改:update
删除:delete
查询:select
二、设计型实验三
1.创建表
1 CREATE DATABASE homework;
2
3 USE homework;
4
5 CREATE TABLE student (
6 Num INT (10) NOT NULL UNIQUE PRIMARY KEY,
7 name VARCHAR(20) NOT NULL,
8 Sex VARCHAR(4) NOT NULL,
9 birthday DATETIME,
10 bumen VARCHAR(20) NOT NULL,
11 address VARCHAR(50)
12 ) DEFAULT charset = gb2312;
13
14 CREATE TABLE score (
15 Id INT(10) NOT NULL UNIQUE PRIMARY KEY,
16 C_name VARCHAR(20),
17 Stu_id INT(10) NOT NULL,
18 grade INT(10),
19 CONSTRAINT score_fk FOREIGN KEY(Stu_id) REFERENCES student(Num)
20 ) DEFAULT charset = gb2312;
21
22 INSERT INTO student VALUES
23 ('901','张军','男','1985-1-1','计算机系','北京市海淀区'),
24 ('902','张超','男','1986-2-2','中文系','北京市昌平区'),
25 ('903','张美','女','1990-2-1','中文系','湖南省永州市'),
26 ('904','李五一','男','1990-2-3','英语系','辽宁省阜新市'),
27 ('905','王芳','女','1991-4-6','英语系','福建省厦门市'),
28 ('906','王桂','男','1988-6-7','计算机系','湖南省衡阳市');
29
30 INSERT INTO score VALUES
31 (1001,'计算机','901','98'),
32 (1002,'英语','901','80'),
33 (1003,'计算机','902','65'),
34 (1004,'中文','902','88'),
35 (1005,'中文','903','95'),
36 (1006,'计算机','904','70'),
37 (1007,'英语','904','92'),
38 (1008,'英语','905','92'),
39 (1009,'计算机','906','90'),
40 (1010,'英语','906','85');
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='英语');