论述题 9.  有如下学生信息:
    学生表student(stu_id,stu_name);
    课程表course(c_id,c_name);
    成绩表score(stu_id,c_id score);
    1)写出向学生表中插入一条数据的SQL语句。
    2)查询名字为James的学生所选的课程。
    3)查询stu id为4的学生所学课程的成绩。
【正确答案】1)向数据库中插入一条记录用的是insert语句,可以采用如下两种写法:
   ①insert into student(stu_id,stu_name)values(1,'james')
   ②insert into student values(1,'james')
   如果这个表的主键为stu_id,并且采用数据库中自增的方式生成,那么在插入的时候就不能显式地指定stu_id这一列,在这种情况下,添加记录的写法为:
   insert into student(stu_name)values('james')
   2)在数据库中查询用到的关键字为select,由于student表中只存放了学生相关的信息,course表中只存放了课程相关的信息,学生与课程是通过score表来建立关系的,一种思路为:首先找到名字为Tom的学生的stu_id,然后在成绩表(score)中根据stu_id找出这个学生所选课程的c_id,最后就可以根据c_id找出这个学生所选的课程。
   ①可以使用下面的select语句来查询:
   select c_name from course where c_id in(select c_id from score where stu_id in(select std_id from student
   where stu_name='Tom'))
   ②当然也可以根据题目要求,根据三张表的关系,直接执行select操作,写法如下:
   select c_name from student st,course c,score sc where st.stu_id=sc.stu_id and sc.c_id=c.c_id and st.
   stu_name='Tom'
   ③当然也可以把②的写法改为对三个表做join操作。
   3)成绩都存在表score中,而课程名存储在表course中,因此,需要访问这两张表来找出课程与成绩,实现方法如下:
   select c.c_name,s.score from course c,score s where s.stu_id=4 and c.c_id=s.c_id
【答案解析】