数据库系统概论复习总结3 --- 第二章关系数据库标准语言SQL

3.1 SQL 概念

===结构化查询语句

3.1.2 SQL的特点

1、综合统一

2、高度非过程化

3、面向集合的操作方式

4、以同一种语法结构提供多种使用方式

3.1.3 SQL的基本概念

支持SQL的关系数据库管理系统同样支持关系数据库三级模式结构。

其中外模式包括若干视图和部分基本表,数据库模式包括若干基本表

内模式包括若干存储文件

基本表是本身独立存在的表,在关系数据库管理系统中一个关系对应一个基本表,一个或多个基本表对应一个存储文件,一个表可以带若干索引,索引也存放在存储文件中。

存储文件的逻辑结构组成了关系数据库的内模式、对用户是隐藏的

视图是从一个或几个基本表中导出的表,它本身不独立存储在数据库中,即数据库中只存放视图的定义而不存放视图对应的数据。


3.3 数据定义

3.3.1 模式的定义与删除

1、定义模式

create schema <模式名> authorization<用户名>

例:为用户zhang创建一个模式test,并且在其中定义一个基本表 table1

create schema test authorization zhang

create table table1 (

col1 smallint,

col2 int,

col3 char(20),

col4 numeric(10,3),

col5 decimal(5,2)

);

2、删除模式

drop schema <模式名><cascade|restrict>

3.3.2 基本表的定义、删除与修改

1、定义基本表

create table < 表名> (列名 数据类型 列级完整性约束条件)

例: 建立一个:“学生”表student

create table student(

Sno char(9) primary key,

Sname char(20) unique,

Ssex char(2),

Sage smallint,

Sdept char(20)

);

例:建立 一个“课程”表course

create table course (

cno char(4) primary key,

cname char(30) not null,

cpno char(4),

ccedit smallint,

foreign key (cpno) references course(cno)

);

2、数据类型

char(n),character(n)   长度为n的定长字符串

varchar(n), charactervarying(n) 最大长度为n的变长字符串

clob 字符串大对象

blob 二进制大对象 

int , interger 长整数(4字节)

smallint 短整数(2字节)

bigint 大整数 (8字节)

numberi (p,d) 定点数

decimal(p,dP 同numeric

real 取决于机器精度的单精度浮点数

double precision 取决于机器精度的双精度浮点数

float(n) 可选精度的浮点数,至少为n位数字

boolean 逻辑布尔量

date 日期

time 时间

timestamp 时间戳类型

intergval 时间间隔类型

4、修改基本表

alter table <表名> 

add column 新列名 数据类型 完整性约束

drop column 列名 

drop constaint 完整性约束

alter column 列名 数据类型

例:向sudent表增加入学时间列,其数据类型为日期型

alter table student add column _entrance date;

例:将年龄的数据类型由字符型改为整数

alter table student alter column sage int;


5、删除表

drop table < 表名> restrinct cascade可选择

例: 删除student表

DROP TABLE STUDENT CASECADE

3.3.3 索引的建立与删除

1、建立索引

create [unique][cluster] index<索引名> on <表名>

例:为学生-课程数据库中的student-coursr-sc三个表建立索引。其中student表按学号升序建立唯一索引,course按课程号升序建立唯一 索引,sc表按学号升序和课程号降序建立唯一索引

create unique index stusno on student(sno);

create unique index coucno on course(cno);

create unique index scno on sc(sno asc, cno desc);


2、修改索引

alter index <旧索引名>rename to <新索引名>

例;将sc表的scno索引名改为scsno

alter index scno rename to scsno;

3、删除索引

drop index<索引名>

例:删除student 表的stusname 索引

drop index stusname;


3.4 数据查询

select [all | distinct ] <目标列表表达式>[<目标列表达式>]....

from <表名> 

[where <条件表达式>]

[group by <列名1> [having <条件表达式>]]

[order by[列名] [asc| decs]]

3.4.1 单表查询

1、选择表中若干列

例: 查询全班学生的学号与姓名

select sno, sname from student;

2、选择表中的若干元组

(1)消除取值重复的行

例:查询选修了课程的学生学号

select distinct  sno from sc 

( 2) 查询满足条件的元组

查询条件表达式:

比较  = > < =>  =< != !=

确定范围 between and , not between and

确定集合 in , not in

字符匹配 like not like 

空值 is null, is not null

多值条件 and or not

例: 查询计算机科学系全体学生的名单

select  sname from student 

where sdept = 'cs";

例: 查询所有年龄在20岁以下的学生姓名及其年龄

select  sname, sage from student where age < 20;

例: 查询年龄在20--30之间的学生的学号,系别和年龄

select sname,sdept, sage from sage between 20 and 30 ;

例:查询年龄不在20--30之间的学生姓名,系别和年龄

select sname ,sdept , sage from student where not between 20 and 30

例: 查询计算机科学系(cs),数学系(ma),信息系(is)学生的姓名,性别

select sname ,ssex from student where in ('cs',"ma","is");

字符匹配 like not like 

通配符 % >=0长度 _  代表任意长度

例: 查询学号为201215121 的学生的详细信息

select * from student where  sno like '201215121";  ===  select * from student where sn0  = "201215121";

例: 查询姓"欧阳:且全名为三个汉字的学生的姓名

select *  sname from student  sname like ' 欧阳_';

例: 查询所有姓“刘” 的学生的姓名,学号,和性别

select * from student  sname like '刘%';

注意: 如果用户要查询的字符串本身就含有通配符%_,这就要使用escape来进行转义

例: 查询DB_Design课程的课程号和学分

select * from course where cname  like 'DB_Deign' escape '\';


3、order by 子句

用户可以用order by 对查询结果按照一个或多个属性列的升序asc , desc 排列

不好意思,我要吃饭了,学校人多 --- 再叙

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值