一、創建表
創建表Student:
create table Student1
(
Sno char(9) primary key,
Sname char(9) ,
Ssex char(2),
Sdept char(8),
Sage smallint);
創建表Course:
create table Course1
(
Cno char(4)primary key,
Cname char(10),
Cpno char(4),
Ccredit smallint,
foreign key Cpno references Course1(Cno)
);
創建表SC:
create table SC1(
Cno char(2),
Sno char(10),
Grade int,
primary key(Sno,Cno),
foreign key(Sno)references Student1(Sno),
foreign key(Cno)references Course1(Cno)
);
二、修改表基本表:
alter table 表名
1.增加列:
add 新列名 數據類型 完整性約束;
例如:
alter table Student add ID char(20);
2.刪除列(屬性):
drop column 屬性名(列);
例如:
alter table Student drop column ID;
3.修改數據類型:
alter column 列名 數據類型;
例如:
alter table Student alter column Sage char(8);
三、刪除基本表:
drop table 表名 [restrict|cascade];
resrrict:刪除表有限制條件(不能有視圖,不能有觸發器)
cascade:刪除表沒有限制條件(刪除表時,相關視圖將一起刪除)
例如:
drop table Teacher cascade;//有問題
四、建立索引:
create [unique][cluster]index<索引名>
on <表名>(<表名>[<次序>[,<列名>[<次序>]]…);
unique:表明此索引只對應唯一的數據記錄
cluster:表示要建立的是聚族索引
例如:
create cluster index StuSname on Student(Sname);
/*將會在Student表的Sname列上建立聚族索引,且Student表中的記錄將按照Sname值的升序存放*/
例如:建立唯一索引
create unique index StuSno on Student(Sno);
create unique index CouCno on Course(Cno);
create unique index SCno on SC(Sno asc,Cno desc);
注:asc升序,desc降序,默認升序
五、刪除索引:
drop index 索引名;
例如:
drop index StuSname;
六、數據查詢:
**select [all|distinct] 目標列,目標列表達式,……
from 表名或視圖
where 條件
group by 列名1 having 條件表達式
order by 列名2 [asc|desc]; //升|降,默認升
**
**
補充:
lower(列名):變小寫
upper(列名):變大寫
**
(I)
A、單表查詢:
1.查詢全體學生的學號,姓名
select Sno,Sname
from Student ;
B、查詢全部列:
1.查詢全體學生的詳細記錄
select *
from Student;
C、查詢經計算的值:
1.查詢全體學生的姓名及出生年份
select Sname,2014-Sage
from Student;
2.查詢全體學生的姓名,出生年份,和所在系,要用小寫字母表示系名
select Sname,'Birth',2014-Sage,lower(Sdept)
from Student;
D、取別名:
在查詢列名后加上別名
例如:
select Sname Name,'Year-Birth',2014-Sage Birthday,upper(Sdept)Department
from Student;
E、去掉結果中的重復項加distinct
例如:
select distinct Sno
from SC;
(II)
**查詢滿足條件的元組
比較:=,<,>,<=,>=,!=,<>,!>,!<,NOT+上述比較符
確定范圍:between and,not between and
確定集合:in,not in
字符匹配:like,not like
注:查詢字符本身包括%,_時,加反斜杠
空值:is null,not is null
注:is不能用=代替
多重條件:and,or,not
**
1.查詢計算系的學生
select Sname
from Student
where Sdept='CS';
2.查詢成績在90分以上的學生學號,姓名
select distinct Student.Sno,Sname
from Student,SC
where Grade>90;
3.查詢年齡不再20到30之間的學生
select Sno,Sage
from Student
where Sage not between 20 and 30;
//或者:where Sage<20 or Sage>30;
4.查詢計算機系,數學系,信息系學生的學號,姓名
select Sno,Sname
from Student
where Sdept in('CS','IS','MA');
//或者:where Sdept='CS'or Sdept='IS'or Sdept='MA';
5.找出姓李,并且成績大于90學生的學號,姓名,性別,成績
Select Student.Sno,Sname,Ssex,Grade
From Student,SC
where Sname LIKE '李%'and Grade >90;
6.查詢姓李名字為三個字的學生信息
select *
from Student
where Sname like '李____';
7.查詢第二個字不為‘芳’的學生
select *
from Student
where Sname not like '__芳%' ;
8.查詢有成績學生的學號
select distinct SC.Sno
from Student ,SC
where Grade is not null;
(III)
order by 列名 [asc|desc];
1.查詢選修了課程號為3的學生的學號,成績安降序排列
select Sno
from SC
where Cno='3'
order by Grade desc;
(IV)聚集函數
count([distinct|all] * ) 統計元組個數
count([distinct|all] 列名 ) 統計一列值的個數
sum([distinct|all] 列名) 計算一列值的總和
avg([distinct|all] 列名) 計算一列值的平均值
**max([distinct|all] 列名) 求一列值的最大值 **
**min([sistinct|all] 列名) 求一列值的最小值
**
1.查詢學生總人數
select count(Sno)
from Student;
2.計算1號課程的平均成績
select avg(Grade)
from SC
where Cno='1';
3.查詢200215012選修課程的總學分
select sum(Ccredit)
from SC,Course
where SC.Cno=Course.Cno and Sno='200215012';
(V)group by
**查詢結果按某一列或多列的值分組,值相等的為一組
目的:為了細化聚集函數的作用對象。如果未對查詢結果分組,聚集函數將作用于整個查詢結果。分組后聚集函數將作用于每個分組,即每個函數都有一個函數值。
**
1.求各個課程號及相對應的選課人數
select Cno,count(Sno)
from SC
group by Cno;
2.查詢選修了1門課以上的學生學號
select Sno
from SC
group by Sno
having count (*)>1;
(VI)連接查詢:
等值與非等值連接
1.查詢每個學生及其選修課程情況
select Student.*,SC.*
from Student,SC
where Student.Sno=SC.Sno;
自身連接
1.查詢每一門的間接先修課。
select first.Cno,second.Cpno
from Course first,Course second
where first.Cpno=second.Cno;
外連接
1.查詢每個學生及其選修課程情況
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
from Student left outer join SC on(Student.Sno=Sc.Sno);
/*from Student left outer join SC using (Sno);*/有問題
復合連接
1.查詢選修了2號課程且成績在90分以上的所有學生
select Student.Sno,Sname,Grade
from SC,Student
where SC.Sno=Student.Sno and Cno='2' and Grade>90;
2.查詢每個學生的學號,姓名,選修課程名及成績
select Student.Sno,Sname,Cname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno ;
嵌套查詢
select Sname ,Sno,Sdept
from Student
where Sno in(select Sno
from Student
where Sdept='女');
查詢與劉晨同一系的學生
select *
from Student
where Sdept in(select Sdept
from Student
where Sname='劉晨');
集合查詢
- 查詢數學系和信息系的學生的信息
select *
from student
where sdept=’MA’
union
select *
from student
where sdept='IS';
- 查詢選修了1號課程或2號課程的學生的學號
select sno from sc where cno='1'
Union
select sno from sc where cno='2';
3.查詢同時選修了“英語”和“數學”這兩門課的同學的學號,姓名及成績。
select SC.Sno,Sname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname in('數學','英語')and SC.Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='數學'
intersect
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='英語') ;
4.查詢計算機系學生年齡不大于23的學生的差集
select*
from student
where sdept='cs'
except
select *
from student
where sage<23;
查詢測試:
1、查詢不姓劉也不姓王的同學學號、姓名、出生年份。
select Sno,Sname,2014-Sage Birthday
from Student
where Sname not like '王%'and Sname not like '劉%';
2、查詢“信息”系同學中最大年齡,最小年齡的同學的信息。
方法一
select *
from Student
where Sdept='IS'and( Sage =(select max(Sage)
from Student
where Sdept ='IS'
)or Sage in(select min(Sage)
from Student
where Sdept='IS'));
方法二
select *
from Student
where Sdept='IS'and Sage in(
select max(Sage)
from Student
where Sdept ='IS'
union
select min(Sage)
from Student
where Sdept='IS'
);
3、查詢選修了沒有先行課程的同學的課程號及成績。
select Sno,SC.Cno,Grade
from Course,SC
where SC.Cno=Course.Cno and Cpno is null;
4、查詢和王敏同一個系的同學的總人數。
select count(Sno)
from Student
where Sdept in(
select Sdept
from Student
where Sname ='王敏');
5、查詢成績在85分以上的同學信息,按男女分組顯示,系別以大寫字母顯示。
select Student.Sno,Sname,Sage,Ssex,upper(Sdept)
from Student,SC
where Student.Sno=SC.Sno and Grade>85
order by Ssex;
6、查詢“數據庫”這門課程的總成績及平均分。
select sum(Grade),avg(Grade)
from SC,Course
where SC.Cno=Course.Cno and Cname='數據庫';
7、查詢數學系和信息系同學的信息,按照系別降序,年齡升序排列。
select *
from Student
where Sdept in('IS','MA')
order by Sage asc,Sdept desc;
8、查詢姓張的兩個字同學的選課的學號,課程號,課程名及成績。
select SC.Sno,SC.Cno,Cname,Ccredit
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Sname like '張__';
9、查詢有選課記錄的同學的人數。
select count( distinct Sno )
from SC;
10、查詢選修課程信息中女同學的選修課程號及相應選課人數。
select SC.Cno,count(SC.Cno)
from SC
where Cno in(select SC.Cno
from Student,SC
where Student.Sno=SC.Sno and Ssex='女')
group by SC.Cno;
11、查詢選修“1”號課程且成績在80分以上的同學的選課信息。
select*
from SC
where Cno='1' and Grade>80;
12、查詢選修了“信息系統”和“數學”兩門課的同學的學號,姓名及成績。
select SC.Sno,Sname,Grade
from SC,Course,Student
where SC.Sno=Student.Sno and SC.Cno=Course.Cno and Cname in('數學','信息系統')and SC.Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname ='數學'and Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='信息系統'));
1、查詢選修了“信息系統”課程的人數。
select count(Sno)
from Course,SC
where Course.Cno=SC.Cno and Cname='信息系統';
2、查詢姓劉的同學的選課信息及出生年份。
select Sname,SC.*,2014-Sage Birth
from Student,SC
where Student.Sno=SC.Sno and Sname like'劉%';
3、查詢選修了“1”號課程的同學的總成績及平均分。
select sum(Grade) sum,avg(Grade) avg
from SC,Course
where SC.Cno=Course.Cno and SC.Cno='1';
4、查詢信息系和計算機系同學的學號,姓名和年齡,并按系別升序,年齡降序排列。
select Sno,Sname,Sage,Sdept
from Student
where Sdept in('IS','CS')
order by Sage desc,Sdept asc;
5、查詢選修了“數學”且在90分以上的同學的學號,姓名,成績。
select SC.Sno,Sname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname='數學' and Grade>90;
6、統計和李勇同一個系的學生人數。
select Count(Sno)
from Student
where Sdept=(select Sdept
from Student
where Sname like '李勇');
7、查詢“男同學中最大年齡,最小年齡的同學的信息。
方法一
select*
from Student
where Sage in(
select max(Sage)
from Student
where Ssex like '男')or
Sage in(
select min(Sage)
from Student
where Ssex like '男');
方法二
select*
from Student
where Sage in(
select max(Sage)
from Student
where Ssex like '男'
union
select min(Sage)
from Student
where Ssex like '男');
8、查詢姓王的兩個字的同學的選課的課程名稱及學分。
select Cname,Ccredit
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Sname like '王__';
9、查詢數據庫課程成績在85分以上的同學的人數。
select count(Sno)
from SC,Course
where SC.Cno=Course.Cno and Cname like '數據庫'and Grade>85;
10、查詢不及格的同學的學生信息,按男女進行分組顯示,系別以大寫字母顯示。
select Student.Sno,Sname,Sage,Ssex,upper(Sdept)
from Student,SC
where Student.Sno=SC.Sno and Grade<60
order by Ssex;
11、查詢沒有先行課的課程號及課程名。
select Cno,Cname
from Course
where Cpno is null;
12、查詢選修了“數據庫”或“信息系統”兩門課程的同學的學號、姓名及成績。
select SC.Sno,Sname,Grade,Cname
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname in('數據庫','信息系統');
1、查詢“信息”系同學中最大年齡和平均年齡。
select sum(Sage),avg(Sage)
from Student
where Sdept='IS';
2、查詢不是信息系也不是數學系的同學學號、姓名、出生年份。
select Sno,Sname,2014-Sage Birthday
from Student
where Sdept!='IS' and Sdept!='MA';
3、查詢選修了沒有先行課程的同學的課程號及成績。
select SC.Cno,Grade
from Course,SC
where SC.Cno=Course.Cno and Cpno is null;
4、查詢和王敏同性別的的同學的學號、姓名。
select Sno,Sname
from Student
where Ssex in (
select Ssex
from Student
where Sname ='王敏');
5、查詢不及格的同學信息,按男女分組顯示,系別以小寫字母顯示。
select Student.Sno,Sname,Sage,Ssex,lower(Sdept)
from Student,SC
where Student.Sno=SC.Sno and Grade<60
order by Ssex;
6、查詢選修了“數據庫”這門課程的總人數。
select count(Sno)
from SC,Course
where SC.Cno=Course.Cno and Cname='數據庫';
7、查詢計算機系和數學系同學的信息,按照系別降序,年齡升序排列。
select *
from Student
where Sdept in ('CS','MA')
order by Sdept desc,Sage asc;
8、查詢姓張和姓王的同學的選課的學號,課程號,課程名及成績。
select SC.Sno,SC.Cno,Cname,Grade
from Student,SC,Course
where STudent.Sno=SC.Sno and SC.Cno=Course.Cno and (Sname like'王%' or Sname like '張%');
9、查詢各個課程號及相應的選課人數。
select Cno ,count(Cno)
from SC
group by Cno;
10、查詢選修了3門課程的同學的學生學號。
select Sno
from SC
group by Sno
having count(Cno)=3;
11、查詢選修“1”號課程且成績在80分以上的同學的學號、姓名、課程名及成績。
select SC.Sno,Sname,Cname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and SC.Cno='1' and Grade>80;
12、查詢同時選修了“英語”和“數學”這兩門課的同學的學號,姓名及成績。
方法一
select SC.Sno,Sname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname in('數學','英語')and SC.Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='數學'
intersect
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='英語') ;
方法二
select SC.Sno,Sname,Grade
from Student,SC,Course
where Student.Sno=SC.Sno and SC.Cno=Course.Cno and Cname in('數學','英語')and SC.Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='數學'and Sno in(
select Sno
from SC,Course
where SC.Cno=Course.Cno and Cname='英語')) ;
插入
1.插入一條學生紀錄
insert
into student(sno,sname,ssex,sdept,sage)
values ('201212019','程東','男','IS',18);
insert
into Student(sno,sname,ssex,sage,sdept)
values('95005', '劉云', '女',18, 'CS ');
2,插入一條選課記錄
insert
into SC
values ('6','201212016',56);
或者
insert
into SC
values ('9','201212013',null);
3.對每一個系,求學生的平均年齡,并把結果存入數據庫
create table Dept_age
(Sdept char(15),
ave_age smallint);
insert
into Dept_age(sdept,avg_age)
select Sdept,avg(Sage)
from student
group by Sdept;
修改
1.將學號201212016的年齡改為25
update Student
set Sage=25
where Sno='201212016';
2.將所有的學生年齡加1
update student
set sage=sage+1;
3.將張三成績置0
update SC
set grade=0
where'張三'=(select Sname
from Student
where Student.Sno=SC.sno);
4.輸出數據庫成績前三名信息
方法一:
select top 3 SC.Sno,Sname,sdept,Grade
from sc,course,student
where Student.sno=SC.sno and course.cno=sc.cno and cname='數據庫'
order by grade desc;
方法二:
create view 數據庫成績
as
select SC.Sno,Sname,sdept,Grade
from sc,course,student
where Student.sno=SC.sno and course.cno=sc.cno and cname='數據庫';
select top 3*
from 數據庫成績
order by grade desc;
刪除
1.刪除學號是201212018的同學信息
delete
from Student
where Sno='201212018';
2.刪除程東同學的選修信息
delete
from SC
where '程東'=(select Sname
from student
where SC.sno=student.sno);
視圖
創建視圖
1.創建一個反映學生出生年份的視圖:
create view BT_S(sno, sname, 出生年份)
as
select sno, sname, 2014- sage
from student;
2.將所有女生的記錄定義為一個視圖:
create view F_student
as
select *
from student
where ssex='女';
3.建立數學系學生視圖
create view MA_student
as
select *
from student
where Sdept='ma';
4.建立信息系學生視圖,要求進行修改和插入操作時仍需保證該視圖只有信息系學生
create view IS_student
as
select sno,sname,sage
from student
where Sdept='is'
with check option;
刪除視圖
1.刪除視圖SC-s
drop view SC_s;
或
drop view SC_s cascade;//刪除了視圖和它導出的所有視圖
查詢視圖
1.查找出信息系視圖中年齡小于20的所有學生
select *
from IS_student
where Sage<20;
更新視圖與更新表的操作一樣
完整性約束命令子句建表
1.創建學生表要求,姓名唯一,年齡在18-25,性別只能是男,女,學號是主碼
create table Stu(
Sname char(9) unique,
Sno char(10),
Sage smallint constraint c1 check (Sage between 18 and 45),
Ssex char(2) constraint c2 check (Ssex in('男','女')),
Sdept char(20),
constraint Stukey primary key(Sno));
修改完整性約束
alter table Stu2 add constraint check ( Sname not like '劉%'and Ssex='女');
alter table Stu2 add constraint t9 check (Sno between 900and 999);
4.刪除原來的完整性約束,增加新的
alter table student drop constraint C1;
alter table student add constraint C1 check(Sno between 900 and 999);
更新視圖測試:
1、將計算機系和數學系姓劉的同學年齡加1歲。
update Student
set sage=sage+1
where Sdept in('MA','CS');
2、將同學們選修的“數據庫”課程的成績設為85分。
update SC
set Grade=85
where '數據庫'=(
select Cname
from Course
where Course.cno=SC.Cno );
3、在數據表的全屬性中將新同學記錄(200215178,張鵬,女,18歲);
(200215179,劉明,男, 19歲)插入到學生表中。
insert
into Student(Sno,Sname,Ssex,Sage)
values ('200215179','劉明','男' ,'19');
4、求選修了數據庫同學的平均成績,并將結果存入數據庫中。
create table DB
( Sno char(13),
Avgs smallint);
insert
into DB
select cname,avg(Grade)
from sc,course
where sc.cno=course.cno and cname='數據庫'
group by cname;
5、刪除“7”號課程的課程信息。
delete
from sc
where cno='7';
6、將選修課各科的課程號、課程名及平均成績的定義一個視圖。
create view SC_Course
as
select SC.Cno,Cname,avg(grade) 平均成績
from sc,course
where sc.cno=course.cno
group by sc.cno,cname;
7、向信息系學生視圖IS_S中插入一條新的學生記錄(200215199,趙庚,20歲)。
create view IS_S
as
select *
from student
where Sdept='Is';
insert
into IS_S(sno,sname,sage)
values('200215199','趙庚','20');
insert
into IS_S(sno,sname,sage,sdept)
values('200215199','趙庚','20','is');
8、查詢計算機系及信息系選修1號課程視圖中成績在80-95之間的學生的學號,姓名,課程名及成績。
create view SC_s(sno,sname,cno,cname,sdept,grade)
as
select sc.sno,sname,sc.cno,cname,sdept,grade
from sc,student,course
where student.sno=sc.sno and sc.cno=course.cno and sc.cno='1'and sdept in ('cs','is');
select sno,sname,cname,grade
from SC_s
where grade between 80 and 95;
9、刪除信息系同學視圖IS_S中學號為200215199的學生記錄。
delete
from IS_S
where sno='200215199';
10、將信息系學生視圖IS_S中學號為200215125的同學姓名改為楊悅。
update IS_S
set sname='楊悅'
where sno='201212019';
11、用命令創建一個職工表,其中有職工號,姓名,性別,年齡,
其中職工號為四位整數,姓名不允許取空值,性別只能取男或女。
create table Workers
( Wno char(10)constraint W1 check (Wno between 1000 and 9999),
Wname char(20)constraint W2 not null,
Wsex char(3) constraint W3 check (Wsex in('男','女')),
Wage int);
12、用命令創建一個倉庫管理表,其中有職工號,倉庫號,庫存量,其中庫存量在0到1000之間。
create table Chouse
( Wno char(10),
Cno char(10),
Cquanlity float constraint C1 check (Cquanlity between 0 and 1000));
授權與回收
1.將查詢student表的權限授予用戶U1
grant select
on student
to U1;
考試時寫
grant select
on table student
to U1;
2.把student,course表的全部權限授予U2和U1
grant all privileges
on student,course
to U1,U2;
3.把對表SC的查詢權限授予所有用戶
grant select
on SC
to public;
4.把查詢student和修改學生學號的權限授予用戶2
grant update(sno),select
on student
to U2;
5.把對表SC的插入權限授予用戶2,并允許將此權限在授予其他用戶
grant insert
on SC
to U2
with grant option;
回收權限
6.把用戶2修改學生學號的權限回收
revoke update(sno)
on student
from u2;
7.回收所有用戶對表的查詢
revoke select
on SC
from public;
8.把用戶u2對SC的插入權限回收
revoke insert
on SC
from U2 cascade;
創建角色
1.創建一個角色R1;
create role R1;
2.用grant對角色R1授予student的select,update,insert權限
grant select,update,insert
on student
to R1;
3.將角色授予U1
grant R1
to U1;
4.回收u1的三個權限
revoke R1
from U1;