廢話少說用例子來說話。
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的學生資料;