mysql 練習題mysql
***建立student表sql
mysql> create table student (ide
-> id int(10) not null unique primary key,spa
-> name varchar(20) not null,it
-> sex varchar(4),io
-> brith year,table
-> deparment varchar(20),class
-> address varchar(50)select
-> );nio
Query OK, 0 rows affected (0.03 sec)
查看student表的結構
mysql> desc student
-> ;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL | |
| name | varchar(20) | NO | | NULL | |
| sex | varchar(4) | YES | | NULL | |
| brith | year(4) | YES | | NULL | |
| deparment | varchar(20) | YES | | NULL | |
| address | varchar(50) | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
***建立score表
mysql> create table score (
-> id int(10) not null unique primary key,
-> stu_id varchar(20),
-> c_name varchar(20),
-> grade int(10)
-> );
Query OK, 0 rows affected (0.02 sec)
查看score表的結構
mysql> desc score;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(10) | NO | PRI | NULL |
| stu_id | varchar(20) | YES | | NULL | |
| c_name | varchar(20) | YES | | NULL | |
| grade | int(10) | YES | NULL |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
****向student表中插入數據
mysql> select * from student;
+-----+--------+------+-------+-----------+--------------+
| id | name | sex | brith | deparment | address |
+-----+--------+------+-------+-----------+--------------+
| 901 | 張老大 | 男 | 1985 | 計算機系 | 北京市海淀區 |
| 902 | 張老二 | 男 | 1987 | 計算機系 | 北京市昌平區 |
| 903 | 張三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英語系 | 遼寧省阜新市 |
| 905 | 王五 | 女 | 1991 | 英語系 | 福建廈門市 |
| 906 | 王六 | 男 | 1998 | 計算機系 | 湖南省衡陽市 |
+-----+--------+------+-------+-----------+--------------+
6 rows in set (0.03 sec)
****向score表中插入數據
mysql> select * from score;
+----+--------+--------+-------+
| id | stu_id | c_name | grade |
+----+--------+--------+-------+
| 1 | 901 | 計算機 | 98 |
| 2 | 901 | 英語 | 80 |
| 3 | 902 | 計算機 | 65 |
| 4 | 902 | 中文 | 88 |
| 5 | 903 | 中文 | 95 |
| 6 | 904 | 計算機 | 70 |
| 7 | 904 | 英語 | 92 |
| 8 | 905 | 英語 | 90 |
+----+--------+--------+-------+
8 rows in set (0.01 sec)
==============================================================
****題目1
查詢student表中全部記錄
mysql> select * from student;
****題目2
查詢student表的第二條到第四條記錄
mysql> select * from student limit 1,3;
****題目3
從student表中查詢全部學生的學號(id)姓名(name)、和院系(deparment)的信息.
mysql> select id,name,deparment from student;
****題目4
從student 表中查詢計算機系和英語系的學生信息
mysql> select * from student where deparment='計算機系' or deparment='英語系';
****題目5
從student表中查詢出生年齡在26-30歲的學生信息
mysql> select id,name,sex,brith as age ,deparment,address
-> from student
-> where 2017-brith between 26 and 30;
****題目6
從student表中查詢每一個院系有多少人
mysql> select deparment,count(id)
-> from student
-> group by deparment;
****題目7
從score表中查詢每一個科目最高分
mysql> select c_name, max(grade)
-> from score
-> group by c_name;
****題目8
查詢李四的考試(c_name)科目和考試成績(grade)
mysql> select c_name,grade
-> from score
-> where stu_id=(
-> select id from student
-> where name='李四'
-> );
****題目9
用連接的形式查詢全部學生的信息和考試成績
mysql> select student.id,name,sex,brith,deparment,address,c_name,grade
-> from student, score
-> where student.id=score.stu_id;
****題目10
計算每一個學生的總成績降序排列
mysql> select student.id,name,sex,brith,deparment,address,sum(grade)
-> from student,score
-> where student.id=score.stu_id
-> group by name
-> order by sum(grade) desc;
****題目11
計算每一個科目的平均成績
mysql> select student.id,name,sex,brith,deparment,address,avg(grade)
-> from student,score
-> where student.id=score.stu_id
-> group by id;
****題目12
查詢計算機成績低於95的學生信息
mysql> select * from student
-> where id in (
-> select stu_id from score
-> where c_name='計算機' and grade<95);
****題目13
查詢同時參加計算機和英語考試的學生信息
mysql> select * from student
-> where id=any
-> (select stu_id from score
-> where stu_id in (
-> select stu_id from
-> score where c_name='計算機')
-> and c_name='英語');
****題目14
從student表和score表查詢出學生的學號,而後合併查詢結果
mysql> select id from student
-> union
-> select stu_id from score;
****題目15
查詢姓張或者姓王的同窗的姓名、院系和考試科目及成績
mysql> select student.id,name,sex,brith,deparment,address,c_name,grade
-> from student,score
-> where (name like '張%' or name like '王%')
-> and student.id=score.stu_id;
****題目16
查詢都是湖南的學生的姓名、年齡、院系和考試科目及成績
mysql> select student.id,name,sex,2017-brith as age,deparment,c_name,grade
-> from student,score
-> where address like '湖南%'
-> and
-> student.id=score.stu_id;