创建学生情况表:student
create table student(st_class CHAR(8),st_no CHAR(10) NOT NULL,st_name CHAR(8) NOT NULL,st_sex CHAR(2),st_age SMALLINT,PRIMARY KEY(st_no))
创建课程设置表:subject
create table subject(su_no CHAR(4) NOT NULL,su_subject CHAR(20) NOT NULL,su_credit INTEGER,su_period INTEGER,su_preno CHAR(4),PRIMARY KEY(su_no))
创建学生选课表:score
create table score (st_no CHAR(10),st_nom CHAR(4),st_score INTEGERR NULL,FOREIGN KEY(st_no) REFERENCE student)
查看表结构:DESC student
删除表: DROP TABLE student,score
添加字段: alter table student add (st_born date not null)
删除字段: alter table student drop (st_age)
修改字段类型和长度:
alter table student alter column name varchar(23) null
视图
从student表中导出只包括女学生情况的视图:
create view womanview as select st_class,st_no,st_name,st_age from student where
st_sex="女"
从student和score表中创建分数在60分以上的女学生视图:
create view woman_score as select student.st_class student.st_no,student.st_name,
student.st_age,score.sc_score from student score where student.st_sex="女" and
score.sc_score >= 60 and student.st_no=score.st_no
查看 woman_score 视图
select * from woman_score
索引
对基本表student 中 st_no 和 st_age 创建索引,分别为升序和降序排列
create unique index stindex on student (st_no ASC,st_age DESC)
删除索引:
drop index stindex
SELECT查询
JOIN ON 语句 联接
WHERE 语句 指定查询条件
ORDER BY 语句 指定排序字段和排序方式
GROUP BY 和 HAVING 语句 分组
DISTINCT 语句和 UNIQUE 语句 指定是否要重复记录
TOP语句 列在最前
WHERE子句中的操作符和运算函数
算术比较运算符: < <= > >= = <>
逻辑运算符: AND,OR,NOT
集合运算符: UNION(并),INTERSECT(交),EXCEPT(差)
集合成员资格运算符: IN,NOT IN
谓词: EXISTS(存在) , ALL ,SOME, UNIQUE
聚合函数: AVG,MIN,MAX,SUM,COUNT(计数)
条件查询:
select unique student.st_class,student.st_no,student.st_name,student.sex,
student.st.age,score.su_no,score.score
排序查询:
查不及格的课程,并将课程号按从大到小排列
select unique su_no from score where score<60 order by su_no DESC
嵌套查询:
查询课程编号为C03,且课程成绩在80分以上的学生的学号和姓名
select st_no,st_name from student where st_no in (select st_no from score where su_no = 'c03' and score>80)
计算查询:
查询男学生总人数和平均年龄:
select count(*),AVG(st_age) from student where st_sex='男'
统计选修课程人数:
select count(DISTINCT st_no) from score
数据更新
INSERT语句:insert into score(st_no,su_no,score) value ('10002','c02',95)
DELETE语句:delete from student where st_no = '10002'
UPDATE语句:update subject set su_subject = '英语' where su_no = 'c02'
数据控制
安全性控制、完整性控制、事务控制和并发控制
授权语句:grant connect to sse identified by password
grant resource to sse
grant dba to sse
权力回收:
revoke connect from sse
revoke resource from sse
将表student 查询权授予所有用户:
grant select on student to public
将表 subject 的插入及修改权力授予用户SSE并使得他具有将这种权力授予其他人的权力
grant insert,update(su_subject) on subject to sse with grant option