基础题
数据准备
先创建student和score表,添加基本数据,用于查询演练
创建student表SQL代码如下
create table student(
id int(10) not null unique primary key,
name varchar(20) not null,
gender varchar(4),
birth year,
department varchar(20),
address varchar(50)
);
创建score表SQL代码如下:
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)
);
向student表插入记录的insert语句如下:
insert into student values
( 901,‘张老大’,‘男’,1985,‘计算机系’,‘北京市海淀区’),
( 902,‘张老二’, ‘男’,1986,‘中文系’, ‘北京市昌平区’),
( 903,‘张三’, ‘女’,1990,‘中文系’, ‘湖南省永州市’),
( 904,‘李四’, ‘男’,1990,‘英语系’, ‘辽宁省阜新市’),
( 905,‘王五’, ‘女’,1991,‘英语系’, ‘福建省厦门市’),
( 906,‘王六’, ‘男’,1988,‘计算机系’, ‘湖南省衡阳市’);
向score表插入记录的insert语句如下:
insert into score values
(NULL,901, ‘计算机’,98),
(NULL,901, ‘英语’, 80),
(NULL,902, ‘计算机’,65),
(NULL,902, ‘中文’,88),
(NULL,903, ‘中文’,95),
(NULL,904, ‘计算机’,70),
(NULL,904, ‘英语’,92),
(NULL,905, ‘英语’,94),
(NULL,906, ‘计算机’,90),
(NULL,906, ‘英语’,85);
mysql> select * from student;
+-----+-----------+--------+-------+--------------+--------------------+
| id | name | gender | birth | department | address |
+-----+-----------+--------+-------+--------------+--------------------+
| 901 | 张老大 | 男 | 1985 | 计算机系 | 北京市海淀区 |
| 902 | 张老二 | 男 | 1986 | 中文系 | 北京市昌平区 |
| 903 | 张三 | 女 | 1990 | 中文系 | 湖南省永州市 |
| 904 | 李四 | 男 | 1990 | 英语系 | 辽宁省阜新市 |
| 905 | 王五 | 女 | 1991 | 英语系 | 福建省厦门市 |
| 906 | 王六 | 男 | 1988 | 计算机系 | 湖南省衡阳市 |
+-----+-----------+--------+-------+--------------+--------------------+
6 rows in set (0.00 sec)
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 | 英语 | 94 |
| 9 | 906 | 计算机 | 90 |