1. Create student, course, SC, table, note: sql-sever is case-insensitive,
Create student information sheet
create table student
(
Sno char(9) primary key,
Sname char(20) unique,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
Create a course curriculum
create table course
(
Cno char (4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint
);
Create SC Score Sheet
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)
);
Just enter the data yourself. SQL The use of SQL statements is described below.
SQL statements include select query, update modification, insert insert, delete delete operations
Take the above three tables as examples to introduce specific operations
1. The select query statement is used to query data.
(1), query several columns in the studentf table, as follows, the query is the student number, name, you can set the alias when displayed
select Sno, Sname
from student;
select Sno Student ID, Sname Name
from student;
This is to set the Chinese alias.
(2), query all student information
select*
from student;
(3) Query the calculated value, such as calculating the date of birth based on age
select Sname, 2017-Sage
from student;
(4) Check the student number of the elective course
select Sno
from SC;
(5) Query the student list of the School of Information
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';