废话少说用例子来说话。
1.create database sqltest //创建数据库;
2.create table students (sno numeric(6,0)not null,sname varchar(8)not null,age numeric(3,0),
sex char(2),bplace varchar(20),primary key(sno))
//创建表students;
3.create table courses (cno char(4)not null,cname varchar(10)not null,credit int)
//创建表courses;
4.alter table courses add primary key (cno)
//为courses表的cno字段添加主键约束;
5.create table enrools(sno numeric(6,0)not null,cno char(4)not null,grade int,primary key(sno,cno),
foreign key(sno)references students(sno),foreign key(cno) references courses(cno))
//创建表enrools,并添加主外键约束;
6.select sno,sname,age into girl from students where sex='女'
//根据Student表创建gril表,字段为3个;
7.create view faculty as select sno,age,sname from students
//根据Student表创建视图;
8.create view grade_table as select sname,cname,grade from students,enrools,courses where students.sno=enrools.sno and courses.cno=enrools.cno
//从Student、enrools、courses 表中产生一视图,包括sname、cname、grade;
9.drop view grade_table
//删除视图;
10.select cno,cname from courses where credit=3
//在表courses中找出3个学分的cno,cname;
11.select * from students where age >22
//在Student表中找出age大于22的学生状况;
12.select sname,age from students where sex='男' and bplace='北京'
//在Student表中找出北京籍男生的sname,age;
13.select sno,sname,age from students age between 20 and 23 order by age
//找出年龄在20~23之间的学生的sno,sname,age;
14.select sname,sex from students where age<23 and bplace in('北京','上海')
//找出年龄小与23北京上海的学生的sname,sex;
15.select * from students where sname like '张%'
//找出姓张同学的状况;
16.select sname from students where sno=(select sno from enrools where grade=95)
//找出学分为95分的同学姓名,只限查找一条记录(子查询);
17.select sname from students where sno=any(select sno from enrools where grade>90)
或者select sname from students where sno in(select sno from enrools where grade>90)
//找出学分为90分的同学姓名,查找多条记录(子查询);
18.select sname,cno,grade from students,enrools where students.sno=enrools.sno
//查找全部学生的姓名,课程号,成绩;(连接查询);
19.select sname,bplace,grade from student,enrools where bplace in ('北京','上海') and grade>90 and students.sno=enrools.sno
//找出籍贯是北京或上海,成绩在90分以上的学生sname,bpalce,grade;
20.select min(age) from students
//找出年龄最小的学生;
21.select count(*) from students where age<=22
//找出年龄小于等于22的学生人数;
22.select avg(grade),courses=count(*) from enrools group by son
//找出学生的平均成绩和课程门数;
23.select sname from students where age>(select avg(age) from students)
//找出年龄大于平均年龄的学生名字;
24.select con, avg(grade), students=count(*) from enrools group by con ,having count(*)>=3
//找出个课程的平均成绩,按课程号分组,且只选择课程号超过3个的成绩;(GROUP BY子句把一个表按某一指定列(或一些列)上的值相等的原则分组,然后再对每组。GROUP BY 子句总是跟在 Where 子句后面,当 Where 子句缺省时,它跟在 FROM 子句后面。HAVING 子句常用于在计算出聚集之后对行的查询进行控制。)
25.select sname,sno from students where not exists (select * from enrools where students.sno=enrools.sno)
//查询没有任何课程学生的学号和姓名(当一个子查询涉及到一个来自外部查询的列时,称为相关子查询correlated Subquery 。相关子查询要用到存在测试谓词EXISTS 和 NOT EXISTS 及 ALL 、 ANY ( SOME )等。);
26.select * from students where bplace='北京' natural join (select * from enrools where grade>=80)
//查询籍贯是北京,成绩在80分以上的学生信息;
27.inster into students values (009901,'张三',23,'男','北京')
//插入学生信息;
28.inster into teachers (tno,tname) select distinct sno,sname from students,enrools where students.sno=enrools .sno and grade>90
// 把成绩大于90的同学加入到教师的表中,留校。
29.update students set age=age+1
//所有学生年龄加1;
30.update enrolls set grade=0 where con='c1' and '张三'=(select sname from students where students.sno=enrools.sno)
//把张三的成绩改为0;
31.delete from students where age>30
//删除年龄大于30的学生资料;