用SQL创建学生成绩数据库

创建数据库school,这个数据库中包含四个表:分别是学生表、教师表、课程表和成绩表。
用SQL创建学生成绩数据库
语法:
create database school;(创建数据库school)

show databases;(查看是否已经创建好)

drop database school;(删除数据库school,这里不必删除)

2.设计创建学生表、教师表、课程表和成绩表。
用SQL创建学生成绩数据库

语法:
use school;

create table student(

sno varchar(20) not null,

sname varchar(20) not null,

ssex varchar(20) not null default’男’,

sbirthday datetime,

sclass varchar(20));
用SQL创建学生成绩数据库

语法:
use school;

create table teacher(

tno varchar(20) not null,

tname varchar(20) not null,

tsex varchar(20) not null default’男’,

tbirthday datetime,

prof varchar(20) not null,

depart varchar(20));

用SQL创建学生成绩数据库

语法:
use school;

create table course(

cno varchar(20) not null,

cname varchar(20) not null,

tno varchar(20) not null);

用SQL创建学生成绩数据库

语法:
use school;

create table score(

sno varchar(20) not null,

cno varchar(20) not null,

degree decimal(4,1) not null);

显示所有表是否已经创建,可用以下语句:

Show tables from school;
用SQL创建学生成绩数据库

3.添加主键和外键约束

用SQL创建学生成绩数据库

语法:
对学生表添加主键约束:

alter table student

add constraint primary key(sno);

对成绩表添加外键约束:

alter table student

add constraint foreign key(sno)

references student(sno);

alter table student

add constraint foreign key(cno)

references course(cno);

对成绩表添加主键约束:

Alter table student

Add constraint primary key(sno,cno);

用语法将以上的主键和外键都添加约束:

alter table student

add constraint primary key(sno);

alter table teacher

add constraint primary key(tno);

alter table course

add constraint primary key(cno);

alter table course

add constraint foreign key(tno)

references teacher(tno);

alter table score

add constraint primary key(sno,cno);

alter table score

add constraint foreign key(cno)

references course(cno);

4.将以下学生信息插入到学生表中:

用SQL创建学生成绩数据库

语法:
insert into student(sno,sname,ssex,sbirthday,sclass)

values(108,‘曾华’,‘男’,‘1997-09-01’,95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(105,‘匡明’,‘男’,‘1975-10-02’,95031);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(107,‘王丽’,‘女’,‘1976-01-23’,95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(101,‘李军’,‘男’,‘1976-02-20’,95033);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(109,‘王芳’,‘女’,‘1975-02-10’,95031);

insert into student(sno,sname,ssex,sbirthday,sclass)

values(103,‘陆君’,‘男’,‘1974-06-03’,95031);

select*from student;(查看是否所有信息都插入表中了)

结果如下:
用SQL创建学生成绩数据库

5.将以下信息插入到教师表中:

用SQL创建学生成绩数据库

语法:
insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(804,‘李诚’,‘男’,‘1958-12-02’,‘副教授’,‘计算机系’);

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(856,‘张旭’,‘男’,‘1969-03-12’,‘讲师’,‘电子工程系’);

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(825,‘王萍’,‘女’,‘1972-05-05’,‘助教’,‘计算机系’);

insert into teacher(tno,tname,tsex,tbirthday,prof,depart)

values(831,‘刘冰’,‘女’,‘1977-08-14’,‘助教’,‘电子工程系’);

6.以下信息输入课程表表中

用SQL创建学生成绩数据库

语法:
insert into course(cno,cname,tno)

values(‘3-105’,‘计算机导论’,825);

insert into course(cno,cname,tno)

values(‘3-245’,‘操作系统’,804);

insert into course(cno,cname,tno)

values(‘6-166’,‘数据电路’,856);

insert into course(cno,cname,tno)

values(‘19-888’,‘高等数学’,831);

7.将以下信息输入成绩表中:

用SQL创建学生成绩数据库

语法:
insert into score(sno,cno,degree)

values(103,‘3-245’,86);

insert into score(sno,cno,degree)

values(105,‘3-245’,75);

insert into score(sno,cno,degree)

values(109,‘3-245’,68);

insert into score(sno,cno,degree)

values(103,‘3-105’,92);

insert into score(sno,cno,degree)

values(105,‘3-105’,88);

insert into score(sno,cno,degree)

values(109,‘3-105’,76);

insert into score(sno,cno,degree)

values(101,‘3-105’,64);

insert into score(sno,cno,degree)

values(107,‘3-105’,91);

insert into score(sno,cno,degree)

values(108,‘3-105’,78);

insert into score(sno,cno,degree)

values(101,‘6-166’,85);

insert into score(sno,cno,degree)

values(107,‘6-166’,79);

insert into score(sno,cno,degree)

values(108,‘6-166’,81);

8.要求查询学生表中的sname、ssex和sclass列。

语法:
select sname,ssex,sclass

from student;
用SQL创建学生成绩数据库

9.查询学生表中的所有记录。

语法:

Select*from student;

结果如下:

用SQL创建学生成绩数据库

10.查询教师表中不同的系名,以及每个系有多少老师。(重点)

分析:

按系名分组(group by)
求每组有多少老师,即每组有几行?(count)
语法:
Select depart,count(*)

From teacher

Group by depart

用SQL创建学生成绩数据库

11.查询成绩表中分数在60分到80分之间的所有记录

分析:

查询成绩表中的某些记录
这些记录一定满足60分到80分
语法:
Select*from score

Where degree between 60 and 80;

用SQL创建学生成绩数据库

12.查询成绩表中分数为85,86或88的记录。

分析:

查询成绩表中的某些记录
这些记录应满足成绩为85,86或88
这里用where子句:

方法a:使用关键字in

方法b:使用关键字or

语法a:
Select*from score

Where degree in(85,86,88);

用SQL创建学生成绩数据库

语法b:
Select*from score

Where degree =85 or degree =86 or degree =88;

用SQL创建学生成绩数据库

注:这里的or表示并且,不能用and(and表示交集)

13.查询学生表中“95031”班或性别为“女”的记录。

分析:

查询某些同学的记录
where子句筛选符合条件的行
语法a:
select*from student

where sclass=‘95031’ or ssex=‘女’;

用SQL创建学生成绩数据库

语法b:
查询95031这个班级的所有同学的记录
查询性别为女的所有记录
用关键字union连接以上的查询语句。
select*from student where sclass=‘95031’

union

select*from student where ssex=‘女’;
用SQL创建学生成绩数据库

14.查询所有不姓王的同学记录。

SQL语句:
select*from student

where sname not like ‘王%’;
用SQL创建学生成绩数据库

15.以class降序来查询所有学生记录。

分析:

查询所有学生记录
用order by子句对查询结果进行降序排序
SQL语句:
select*from student

Order by sclass desc;

用SQL创建学生成绩数据库