一、创建student、course、SC、表格 ,注意:sql-sever是不区分大小写的,

创建student学生信息表

create table student

(

    Sno char(9) primary key,

    Sname char(20) unique,

    Ssex char(2),

    Sage smallint,

    Sdept char(20)

);

 

创建course课程表

create table course

(

    Cno char(4) primary key,

    Cname char(40) not null,

    Cpno char(4),

    Ccredit  smallint

);

创建SC成绩表

create table SC

(

    Sno char(9),

    Cno char(4),

    Grade smallint,

    primary key(Sno, Cno),

    foreign key (Sno) references student(Sno),

    foreign key (Cno) references course(Cno)

);

 

数据自己输入进去就行了,SQL下面介绍SQL语句的使用

SQL语句包括select查询、 update修改、insert插入、delete删除操作

 

以上面的三个表格为例子介绍具体操作

 

1.select查询语句是用来查询数据的。

(1)、查询studentf表中的若干列 ,如下,查询的是学号,姓名,可以在显示的时候设置别名

select Sno, Sname

from student;
select Sno 学号, Sname 姓名

from student;

这个是设置中文别名。

(2)、查询全部student信息

select*

from student;

(3)查询经过计算的值,如根据年龄算出出生日期

select Sname, 2017-Sage

from student;

(4)查询了选修课程的学生学号

select Sno

from SC;

(5)查询信息学院的学生名单

select Sname

from student

where Sdept='信息学院';

(6)查询信息学院的学生学号,名字,而且名单按照姓名和学号排序

select Sno, Sname

from student

where Sdept='信息学院'

order by  Sname, Sno;

(7)查询年龄在20~23岁的学生和姓名、学院和年龄

select Sname, Sdept, Sage

from student

where Sage between 20 and 23;

(8)查询student表中所有姓刘的姓名,学号, 性别

select Sname, Sno, Ssex

from student

where Sname like '刘%';

like称为模糊查询,必须使用%或则_

(9)SC成绩表中有许多学生选修了多门科目,

如何查询了选修了课程的学生学号

select   distinct Sno

from SC;

(10)求各个课程号及相应的选课人数

select Cno, Count(Sno)

from SC

group by Cno;

2.insert插入数据

在student表中插入一行数据

insert

into student(Sno, Sname, Ssex, Sdept, Sage)

values('2015151', '张三', '男', 15);

 

3.修改一个元组的值

update student

set Sage=22

where Sno='2015151';

4.删除数据

删除学号为2015151的学生记录

delete

from student

where Sno='2015151';

 

更多推荐