CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY,name VARCHAR(20) NOT NULL,sex VARCHAR(4),birth YEAR,department VARCHAR(20),ddress VARCHAR(50));
CREATE TABLE score (id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,stu_id INT(10) NOT NULL,c_name VARCHAR(20),grade INT(10));
2、为student表和score表增加记录
INSERT INTO student VALUES( 1,'张三丰','女',1988,'上海戏剧学院','山东青岛');
INSERT INTO student VALUES( 2,'秦海璐','女',1978,'中央戏剧学院','辽宁营口市');
INSERT INTO student VALUES( 3,'霍建华','男',1979,'南强工商','中国台湾');
INSERT INTO student VALUES( 4,'杨洋','男',1991,'中国人民解放军艺术学院','上海');
INSERT INTO student VALUES( 5,'杰森斯坦森','男',1970,'国家跳水运动员','英国');
select * from student where department='计算机' or department='英语'
7、从student表中查询年龄18~22岁的学生信息
select * from student where ((2017 - birth) + 1) >=28and ((2017 - birth) + 1) <= 32;
8、从student表中查询每个院系有多少人
select department,COUNT(*) fromstudent GROUP BY department
10、查询李四的考试科目(c_name)和考试成绩(grade)
select * from score as s LEFT JOIN student as st ONs.stu_id = st.id where name = '李四';
11、用连接的方式查询所有学生的信息和考试信息
select * fromscore as s LEFT JOIN student as st ON s.stu_id = st.id;
12、计算每个学生的总成绩
select name,SUM(grade) from score LEFT JOIN student ONstu_id = student.id GROUP BY name;
13、计算每个考试科目的平均成绩
select c_name, AVG(grade) from score GROUP BY c_name;
14、查询计算机成绩低于95的学生信息
select name from score LEFT JOIN student on score.stu_id =student.id GROUP BY c_name HAVING AVG(grade) < 95;
15、查询同时参加计算机和英语考试的学生的信息
select name from (select * from score where c_name = '计算机') as s inner JOIN (select * from score where c_name = '英语') as st on s.stu_id = st.stu_id INNER JOIN student on s.stu_id =student.id;
16、将计算机考试成绩按从高到低进行排序
select * from score where c_name = '计算机' ORDER BY grade desc;
17、查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select * from score as s INNER JOIN student as st ONs.stu_id = st.id where name like '张%' or name like '王%';
18、查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select * from score as s INNER JOIN student as st ON s.stu_id= st.id where address like '湖南%';
19、把张三的姓名改为张三丰,课程改为java 分数100
UPDATE student set NAME='张三丰' WHERE id=1;UPDATE score set C_name='java' WHERE id =1;update score set grade=100 WHERE id=1;