前言:

1.使用的数据库不同,所使用的语法也略有不同

2.SQL对大小写不敏感

3.Oracle中对引号里面的内容大小写敏感

3.表空间名、文件路径……等需要用单引号将其包含

4.一般引号里面的内容需要大写


准备工作:

安装tips:PLSQL、Oracle以及客户端远程连接服务器笔记(仅供参考)-CSDN博客

(1).Win+R打开services.msc

img点击并拖拽以移动

(2)启动一些服务:

(qwq我不知道哪些有用,哪些没用,所以我都把打开了,不知道有没有负面影响,大家参考一下别的博客吧)

img点击并拖拽以移动


登录:

1.打开SQL Plus命令行工具

第一种方式:

img点击并拖拽以移动

第二种方式:

(1)win+R 打开cmd

img点击并拖拽以移动

(2)输入sqlplus

img点击并拖拽以移动

2.以不同用户登录

注意:

1.使用用户口令这种形式登录的时候,是不显示密码的,口令输入的时候是不显示的,直接输就好

2.若是想以显示密码的形式输入,直接在用户名那一块输入:用户名/密码

3.超级管理员(sys)输入时需要注意指定 as sysdba

SYSTEM(普通管理员):

img点击并拖拽以移动

SYS(超级管理员):

不显示密码方式:

用户名:SYS

密码:sys密码 as sysdba

img点击并拖拽以移动

显示密码方式:

用户名:sys/sys密码 as sysdba

img点击并拖拽以移动

SCOTT(普通用户):

若是出现被锁住的情况:img点击并拖拽以移动
解决方法:

(1)登录超级管理员账户,

img点击并拖拽以移动

(2)输入alter user 用户名 account unlock;

img点击并拖拽以移动

(3)重新登录即可:

img点击并拖拽以移动


SQL基本命令

img点击并拖拽以移动


1.数据定义语言(DDL)

数据库操作

查询所有用户:

1
select distinct(OWNER) from all_tables;

点击并拖拽以移动

查看当前用户:

1
show user;

点击并拖拽以移动

img点击并拖拽以移动

创建表空间:

1
create tablespace 表空间名 datafile '存储路径\***.dbf' size 空间大小;

点击并拖拽以移动

img点击并拖拽以移动

创建用户并指定其表空间:

(指定表空间需要先创建表空间,如果不指定表空间,就会按照默认空间存储)

1
create user 用户名 identified by 密码 default tablespace 表空间;

点击并拖拽以移动

img点击并拖拽以移动

给用户授予dba的权限(超级管理员):

1
grant dba to 用户;

点击并拖拽以移动

img点击并拖拽以移动

删除表空间:

1
drop tablespace 表空间名 including contents and datafiles;

点击并拖拽以移动

img点击并拖拽以移动

删除用户(超级管理员):

(1)查看用户是否有活跃对话

1
select sid as session_id, serial# from v$session where username='用户名';

点击并拖拽以移动

img点击并拖拽以移动

(2)如果查询结果显示有活动的会话,结束这些会话

1
kill session 'session_id, serial#' immediate;

点击并拖拽以移动

(3)删除用户

1
drop user 用户名 cascade;

点击并拖拽以移动

img点击并拖拽以移动

切换用户登录:

1
conn 用户名/密码

点击并拖拽以移动

img点击并拖拽以移动

表操作

查询:

查询某个用户下所有表名:

(用户名注意大写)

1
select table_name from dba_tables where owner = '用户名';

点击并拖拽以移动

img点击并拖拽以移动

查询某个用户下表个数:

(用户名注意大写)

1
select count(*) from all_tables where OWNER = '用户名';

点击并拖拽以移动

img点击并拖拽以移动

查询某个用户的表结构:

1
desc 用户名.表名;

点击并拖拽以移动

img点击并拖拽以移动

查询指定表的建表语句:

(表名、用户名注意大写)

1
select dbms_metadata.get_ddl('TABLE', '表名','用户名') from dual;

点击并拖拽以移动

img点击并拖拽以移动

创建:

数据类型:

img点击并拖拽以移动

创建表空间:

1
create tablespace 表空间名 datafile '文件路径\文件名.dbf' size 表空间大小;

点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

创建表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#创建表
create table 表名(

字段1 字段1类型,

字段2 字段2类型,

字段3 字段3类型,

.......

字段n 字段n类型

) ;

点击并拖拽以移动

img点击并拖拽以移动

给表添加注释:

1
comment on table 表名 is '注释';

点击并拖拽以移动

img点击并拖拽以移动

给字段添加注释:

1
comment on column 表名.字段名 is '注释';

点击并拖拽以移动

img点击并拖拽以移动

表备份:

1
create table 用户名.备份表名 as select * from 用户名.需要备份的表名;

点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

给表添加一列:

1
alter table 表名 add (字段名 字段类型 约束条件);

点击并拖拽以移动

img点击并拖拽以移动

修改:

重命名表:

1
alter table 用户名.旧表名 rename to 新表名;

点击并拖拽以移动

img点击并拖拽以移动

添加字段:

1
alter table 用户名.表名 add 新字段名 新字段类型 default '默认值';

点击并拖拽以移动

img点击并拖拽以移动

修改字段名:

1
alter table 用户名.表名 rename column 旧字段名 to 新字段名;

点击并拖拽以移动

img点击并拖拽以移动

修改数据类型:

1
alter table T1.emp1 modify temp varchar(30);

点击并拖拽以移动

img点击并拖拽以移动

删除:

删除表字段:

1
alter table 用户名.表名 drop column 字段名;

点击并拖拽以移动

img点击并拖拽以移动

删除表:

1
drop table 表名;

点击并拖拽以移动

img点击并拖拽以移动

删除表空间:

(1)查看是否有其它用户在使用该表空间:

1
select * from dba_users where default_tablespace='表空间名';

点击并拖拽以移动

img点击并拖拽以移动 (2)若有,则删除这些用户或者将这些用户迁移到别的表空间

(3)删除表空间

1
drop tablespace 表空间名 including contents and datafiles;

点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

删除指定表并重新创建该表:

1
truncate table 表名;

点击并拖拽以移动

img点击并拖拽以移动


2.数据操作语言(DML)

添加数据(insert)

注意:

1.插入数据注意顺序

2.插入的数据大小要合法

给指定字段添加数据:

1
insert into 表名 (字段1,字段2......) values(值1, 值2......);

点击并拖拽以移动

img点击并拖拽以移动

给表中批量添加数据:

1
2
3
insert all into 用户名.表名(字段1,字段2,字段3......) values(值1,值2,值3.......)
into 用户名.表名(字段1,字段2,字段3......) values(值1,值2,值3.......)
select * from dual;

点击并拖拽以移动

img点击并拖拽以移动

修改数据(update)

修改数据:

注意:如果没有条件,则会修改整张表

1
update 表名 set 字段1=1,字段2=2......[where 条件];

点击并拖拽以移动

img点击并拖拽以移动

删除数据(delete)

删除数据:

注意:如果没有条件,则会删除整张表

1
delete from 表名;

点击并拖拽以移动

img点击并拖拽以移动


3.数据查询语言(DQL)

编写顺序:

1
2
3
4
5
6
7
8
9
10
11
12
select [distinct|all] 字段列表

from 表名

where 查询条件

group by 分组字段列表

having 分组后条件列表

order by 排序字段列表:asc或者desc
;

点击并拖拽以移动

作示范的表结构:

img点击并拖拽以移动

表名:T_STUDENT

img点击并拖拽以移动

执行顺序:

1
2
3
4
5
6
7
8
9
10
11
12
from 表名 : 从哪张表查询

where 条件 :查询条件

group by 分组条件 :分组

having 分组后查询条件 :分组后查询条件

select 字段列表 :选择字段

order by 排序方式 :对查询结果进行排序
;

点击并拖拽以移动

基本查询

查询多个字段:

1
2
3
select 字段1,字段2,字段3...from 表名;

select * from 表名;

点击并拖拽以移动

设置别名:

1
select 字段1[as 别名1],字段2 [as 别名2]......from 表名;

点击并拖拽以移动

img点击并拖拽以移动

去除重复记录:

1
select distinct 字段列表 from 表名;

点击并拖拽以移动

条件查询

语法:

1
select 字段列表 from 表名 where 条件列表;

点击并拖拽以移动

img点击并拖拽以移动

条件:

比较运算符 功能
>
>=
<
<=
=
<> 或 != 不等于
between…and… 在某个范围之内
in(…) 在in之后的括号中,多选一
like 占位符 模糊匹配(_匹配单个字符,%匹配任意个字符)
is null
and 或 && 并且
or 或 ||
not 或 !

聚合函数

1
select 聚合函数(字段列表) from 表名;

点击并拖拽以移动

img点击并拖拽以移动

注意:

​ 对一列进行计算 所有null值不参与聚合函数的计算

函数 功能
count 统计数量
max 最大值
min 最小值
avg 平均值
sum 求和

分组查询

1
select 字段列表 from 表名 [where 条件] group by 分组字段名 [having 分组后过滤条件];

点击并拖拽以移动

where **分组之前执行,不满足where条件的不参与分组,where不能对聚合函数进行判断**
having **分组之后对结果进行过滤,having可以对聚合函数进行判断**

eg:

img点击并拖拽以移动

排序查询

1
select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2;

点击并拖拽以移动

asc 升序(默认)
desc 降序

img点击并拖拽以移动

分页查询

(oracle查询没有limit关键字,引入rownum进行分页查询)

1
2
3
4
5
6
select * from
(
select rownum rn, t.* from
(select 字段 from t_student) t where rownum <= 终止行
)
where rn >= 起始行;

点击并拖拽以移动

img点击并拖拽以移动


4.数据控制语言(DCL)

管理用户

查看当前用户:

1
show user;

点击并拖拽以移动

切换用户:

1
connect 用户名/密码;

点击并拖拽以移动

img点击并拖拽以移动

注意连接到数据库超级管理员的时候:

可能会出现以下错误:

img点击并拖拽以移动

解决方法:

1
connect sys/密码 as sysdba

点击并拖拽以移动

img点击并拖拽以移动

权限控制

查询用户权限:

1
select * from dba_sys_privs where grantee='用户名';

点击并拖拽以移动

授予权限:

1
grant 权限 to 用户;

点击并拖拽以移动

权限 说明
create session 登录权限
create table 创建表的权限
drop any table 删除任意表
insert any table 向任意表中插入行
update any table 修改任意表中行的权限
select on 表名 查看指定表的权限

eg:

img点击并拖拽以移动

回收权限:

1
2
--回收用户权限
revoke 权限 from 用户;

点击并拖拽以移动

1
revoke select on 用户2.2 from 用户1; #回收用户1查看表2的权限

点击并拖拽以移动

img点击并拖拽以移动


函数

使用

一般形式:

1
select 函数 from 表名 where 条件;

点击并拖拽以移动

如果只是想看函数的返回结果可以使用以下形式:

1
select 函数 from sys.dual;

点击并拖拽以移动

sys.dual

dual是一个虚拟表,用来构成select的语法规则,oracle保证dual里面永远只有一条记录

字符串函数

eg:

img点击并拖拽以移动

img点击并拖拽以移动 img点击并拖拽以移动

数值函数

eg:

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

日期函数

日期表示:

日期-月份-年份

eg:21-9月-2024

函数概述:

eg:

sysdate:

img点击并拖拽以移动

next_day():

img点击并拖拽以移动 last_day():img点击并拖拽以移动

round() :

img点击并拖拽以移动 add_months():img点击并拖拽以移动

months_between():

img点击并拖拽以移动

extract():

img点击并拖拽以移动


约束

概念

​ 约束是作用于表中字段上的规则,用于限制存储在表中的数据

​ 约束可以在创建表/修改表的时候添加

分类

约束 描述 关键字
非空约束 限制该字段的数据不能为null not null
唯一约束 保证该字段的所有数据都是唯一的、不重复的 unique
主键约束 主键是一行数据的唯一标识,要求非空且唯一 primary key
默认约束 保存数据时,如果未指定该字段的值,则采用默认值 default
检查约束 保证字段值满足某一条件 check
外键约束 用来让两张表的数据之间建立连接,保证数据的一致性和完整性 foreign key

建表时添加约束:

img点击并拖拽以移动

img点击并拖拽以移动

sql语句添加:

1
alter table 表名 add constraints 约束名称 primary key(列名);

点击并拖拽以移动

img点击并拖拽以移动

外键约束

子表(从表):具有外键的表

父表(主表):外键所关联的表

FOREIGN KEY约束可以与另外一个表的PRIMARY KEY及UNIQUE关联

添加外键:

在创建表的时候添加:

1
constraint 外键名称 foreign key(外键) references 表名(外键关联的键)

点击并拖拽以移动

img点击并拖拽以移动

1
foreign key(列名) references 外键关联的表名

点击并拖拽以移动

img点击并拖拽以移动

sql语句添加(alter):

1
alter table 表名 add constraint 外键名称 foreign key(列名) references 表名(外键关联的键);

点击并拖拽以移动

img点击并拖拽以移动

查询外键

根据外键名称查看外键所在位置:

1
select * from user_cons_columns cl where cl.constraint_name= upper('外键名称');

点击并拖拽以移动

img点击并拖拽以移动

查找表的外键:

(包括名称,引用表的表名和对应的列名)

1
select * from user_constraints c where c.constraint_type='R' and c.table_name=upper('表名');

点击并拖拽以移动

img点击并拖拽以移动

删除外键

1
alter table 表名 drop constraint 外键名称;

点击并拖拽以移动

img点击并拖拽以移动


多表查询

基本概念

自然连接(内连接):左表与右表中每一个元组进行条件匹配,满足条件的才会查询出来

自连接:同一个表内的查询

外连接:

  • 左外连接:左表中的元组不会丢失,即每一个左表中的元素都可查出,若右表没有匹配的,就置为空
  • 右外连接:右表中的元组不会丢失,即每一个右表中的元素都可查出,若左表没有匹配的,就置为空
  • 全连接:左右表的悬浮元组都会加入到最后的查询结果中

基本结构

内连接+外连接:

1
select 查询的字段名 from 左表 [inner|left|right|full] join 右表 on 连接条件;

点击并拖拽以移动

自连接:

1
select 别名.查询的字段名(列名)from1 别名1, 表1 别名2 where 连接条件;

点击并拖拽以移动

关系运算

img点击并拖拽以移动

大学模式:

img点击并拖拽以移动 img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

首先以黑书里面的大学模式为例:

创建基本表结构:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
create table classroom
(building varchar(15),
room_number varchar(7),
capacity numeric(4,0),
primary key (building, room_number)
);

create table department
(dept_name varchar(20),
building varchar(15),
budget numeric(12,2) check (budget > 0),
primary key (dept_name)
);

create table course
(course_id varchar(8),
title varchar(50),
dept_name varchar(20),
credits numeric(2,0) check (credits > 0),
primary key (course_id),
foreign key (dept_name) references department (dept_name)
on delete set null
);

create table instructor
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2) check (salary > 29000),
primary key (ID),
foreign key (dept_name) references department (dept_name)
on delete set null
);

create table section
(course_id varchar(8),
sec_id varchar(8),
semester varchar(6)
check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0) check (year > 1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course (course_id)
on delete cascade,
foreign key (building, room_number) references classroom (building, room_number)
on delete set null
);

create table teaches
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
on delete cascade,
foreign key (ID) references instructor (ID)
on delete cascade
);

create table student
(ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) check (tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department (dept_name)
on delete set null
);

create table takes
(ID varchar(5),
course_id varchar(8),
sec_id varchar(8),
semester varchar(6),
year numeric(4,0),
grade varchar(2),
primary key (ID, course_id, sec_id, semester, year),
foreign key (course_id, sec_id, semester, year) references section (course_id, sec_id, semester, year)
on delete cascade,
foreign key (ID) references student (ID)
on delete cascade
);

create table advisor
(s_ID varchar(5),
i_ID varchar(5),
primary key (s_ID),
foreign key (i_ID) references instructor (ID)
on delete set null,
foreign key (s_ID) references student (ID)
on delete cascade
);

create table time_slot
(time_slot_id varchar(4),
day varchar(1),
start_hr numeric(2) check (start_hr >= 0 and start_hr < 24),
start_min numeric(2) check (start_min >= 0 and start_min < 60),
end_hr numeric(2) check (end_hr >= 0 and end_hr < 24),
end_min numeric(2) check (end_min >= 0 and end_min < 60),
primary key (time_slot_id, day, start_hr, start_min)
);

create table prereq
(course_id varchar(8),
prereq_id varchar(8),
primary key (course_id, prereq_id),
foreign key (course_id) references course (course_id)
on delete cascade,
foreign key (prereq_id) references course (course_id)
);

点击并拖拽以移动

插入数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
delete from prereq;
delete from time_slot;
delete from advisor;
delete from takes;
delete from student;
delete from teaches;
delete from section;
delete from instructor;
delete from course;
delete from department;
delete from classroom;
insert into classroom values ('Packard', '101', '500');
insert into classroom values ('Painter', '514', '10');
insert into classroom values ('Taylor', '3128', '70');
insert into classroom values ('Watson', '100', '30');
insert into classroom values ('Watson', '120', '50');
insert into department values ('Biology', 'Watson', '90000');
insert into department values ('Comp. Sci.', 'Taylor', '100000');
insert into department values ('Elec. Eng.', 'Taylor', '85000');
insert into department values ('Finance', 'Painter', '120000');
insert into department values ('History', 'Painter', '50000');
insert into department values ('Music', 'Packard', '80000');
insert into department values ('Physics', 'Watson', '70000');
insert into course values ('BIO-101', 'Intro. to Biology', 'Biology', '4');
insert into course values ('BIO-301', 'Genetics', 'Biology', '4');
insert into course values ('BIO-399', 'Computational Biology', 'Biology', '3');
insert into course values ('CS-101', 'Intro. to Computer Science', 'Comp. Sci.', '4');
insert into course values ('CS-190', 'Game Design', 'Comp. Sci.', '4');
insert into course values ('CS-315', 'Robotics', 'Comp. Sci.', '3');
insert into course values ('CS-319', 'Image Processing', 'Comp. Sci.', '3');
insert into course values ('CS-347', 'Database System Concepts', 'Comp. Sci.', '3');
insert into course values ('EE-181', 'Intro. to Digital Systems', 'Elec. Eng.', '3');
insert into course values ('FIN-201', 'Investment Banking', 'Finance', '3');
insert into course values ('HIS-351', 'World History', 'History', '3');
insert into course values ('MU-199', 'Music Video Production', 'Music', '3');
insert into course values ('PHY-101', 'Physical Principles', 'Physics', '4');
insert into instructor values ('10101', 'Srinivasan', 'Comp. Sci.', '65000');
insert into instructor values ('12121', 'Wu', 'Finance', '90000');
insert into instructor values ('15151', 'Mozart', 'Music', '40000');
insert into instructor values ('22222', 'Einstein', 'Physics', '95000');
insert into instructor values ('32343', 'El Said', 'History', '60000');
insert into instructor values ('33456', 'Gold', 'Physics', '87000');
insert into instructor values ('45565', 'Katz', 'Comp. Sci.', '75000');
insert into instructor values ('58583', 'Califieri', 'History', '62000');
insert into instructor values ('76543', 'Singh', 'Finance', '80000');
insert into instructor values ('76766', 'Crick', 'Biology', '72000');
insert into instructor values ('83821', 'Brandt', 'Comp. Sci.', '92000');
insert into instructor values ('98345', 'Kim', 'Elec. Eng.', '80000');
insert into section values ('BIO-101', '1', 'Summer', '2017', 'Painter', '514', 'B');
insert into section values ('BIO-301', '1', 'Summer', '2018', 'Painter', '514', 'A');
insert into section values ('CS-101', '1', 'Fall', '2017', 'Packard', '101', 'H');
insert into section values ('CS-101', '1', 'Spring', '2018', 'Packard', '101', 'F');
insert into section values ('CS-190', '1', 'Spring', '2017', 'Taylor', '3128', 'E');
insert into section values ('CS-190', '2', 'Spring', '2017', 'Taylor', '3128', 'A');
insert into section values ('CS-315', '1', 'Spring', '2018', 'Watson', '120', 'D');
insert into section values ('CS-319', '1', 'Spring', '2018', 'Watson', '100', 'B');
insert into section values ('CS-319', '2', 'Spring', '2018', 'Taylor', '3128', 'C');
insert into section values ('CS-347', '1', 'Fall', '2017', 'Taylor', '3128', 'A');
insert into section values ('EE-181', '1', 'Spring', '2017', 'Taylor', '3128', 'C');
insert into section values ('FIN-201', '1', 'Spring', '2018', 'Packard', '101', 'B');
insert into section values ('HIS-351', '1', 'Spring', '2018', 'Painter', '514', 'C');
insert into section values ('MU-199', '1', 'Spring', '2018', 'Packard', '101', 'D');
insert into section values ('PHY-101', '1', 'Fall', '2017', 'Watson', '100', 'A');
insert into teaches values ('10101', 'CS-101', '1', 'Fall', '2017');
insert into teaches values ('10101', 'CS-315', '1', 'Spring', '2018');
insert into teaches values ('10101', 'CS-347', '1', 'Fall', '2017');
insert into teaches values ('12121', 'FIN-201', '1', 'Spring', '2018');
insert into teaches values ('15151', 'MU-199', '1', 'Spring', '2018');
insert into teaches values ('22222', 'PHY-101', '1', 'Fall', '2017');
insert into teaches values ('32343', 'HIS-351', '1', 'Spring', '2018');
insert into teaches values ('45565', 'CS-101', '1', 'Spring', '2018');
insert into teaches values ('45565', 'CS-319', '1', 'Spring', '2018');
insert into teaches values ('76766', 'BIO-101', '1', 'Summer', '2017');
insert into teaches values ('76766', 'BIO-301', '1', 'Summer', '2018');
insert into teaches values ('83821', 'CS-190', '1', 'Spring', '2017');
insert into teaches values ('83821', 'CS-190', '2', 'Spring', '2017');
insert into teaches values ('83821', 'CS-319', '2', 'Spring', '2018');
insert into teaches values ('98345', 'EE-181', '1', 'Spring', '2017');
insert into student values ('00128', 'Zhang', 'Comp. Sci.', '102');
insert into student values ('12345', 'Shankar', 'Comp. Sci.', '32');
insert into student values ('19991', 'Brandt', 'History', '80');
insert into student values ('23121', 'Chavez', 'Finance', '110');
insert into student values ('44553', 'Peltier', 'Physics', '56');
insert into student values ('45678', 'Levy', 'Physics', '46');
insert into student values ('54321', 'Williams', 'Comp. Sci.', '54');
insert into student values ('55739', 'Sanchez', 'Music', '38');
insert into student values ('70557', 'Snow', 'Physics', '0');
insert into student values ('76543', 'Brown', 'Comp. Sci.', '58');
insert into student values ('76653', 'Aoi', 'Elec. Eng.', '60');
insert into student values ('98765', 'Bourikas', 'Elec. Eng.', '98');
insert into student values ('98988', 'Tanaka', 'Biology', '120');
insert into takes values ('00128', 'CS-101', '1', 'Fall', '2017', 'A');
insert into takes values ('00128', 'CS-347', '1', 'Fall', '2017', 'A-');
insert into takes values ('12345', 'CS-101', '1', 'Fall', '2017', 'C');
insert into takes values ('12345', 'CS-190', '2', 'Spring', '2017', 'A');
insert into takes values ('12345', 'CS-315', '1', 'Spring', '2018', 'A');
insert into takes values ('12345', 'CS-347', '1', 'Fall', '2017', 'A');
insert into takes values ('19991', 'HIS-351', '1', 'Spring', '2018', 'B');
insert into takes values ('23121', 'FIN-201', '1', 'Spring', '2018', 'C+');
insert into takes values ('44553', 'PHY-101', '1', 'Fall', '2017', 'B-');
insert into takes values ('45678', 'CS-101', '1', 'Fall', '2017', 'F');
insert into takes values ('45678', 'CS-101', '1', 'Spring', '2018', 'B+');
insert into takes values ('45678', 'CS-319', '1', 'Spring', '2018', 'B');
insert into takes values ('54321', 'CS-101', '1', 'Fall', '2017', 'A-');
insert into takes values ('54321', 'CS-190', '2', 'Spring', '2017', 'B+');
insert into takes values ('55739', 'MU-199', '1', 'Spring', '2018', 'A-');
insert into takes values ('76543', 'CS-101', '1', 'Fall', '2017', 'A');
insert into takes values ('76543', 'CS-319', '2', 'Spring', '2018', 'A');
insert into takes values ('76653', 'EE-181', '1', 'Spring', '2017', 'C');
insert into takes values ('98765', 'CS-101', '1', 'Fall', '2017', 'C-');
insert into takes values ('98765', 'CS-315', '1', 'Spring', '2018', 'B');
insert into takes values ('98988', 'BIO-101', '1', 'Summer', '2017', 'A');
insert into takes values ('98988', 'BIO-301', '1', 'Summer', '2018', null);
insert into advisor values ('00128', '45565');
insert into advisor values ('12345', '10101');
insert into advisor values ('23121', '76543');
insert into advisor values ('44553', '22222');
insert into advisor values ('45678', '22222');
insert into advisor values ('76543', '45565');
insert into advisor values ('76653', '98345');
insert into advisor values ('98765', '98345');
insert into advisor values ('98988', '76766');
insert into time_slot values ('A', 'M', '8', '0', '8', '50');
insert into time_slot values ('A', 'W', '8', '0', '8', '50');
insert into time_slot values ('A', 'F', '8', '0', '8', '50');
insert into time_slot values ('B', 'M', '9', '0', '9', '50');
insert into time_slot values ('B', 'W', '9', '0', '9', '50');
insert into time_slot values ('B', 'F', '9', '0', '9', '50');
insert into time_slot values ('C', 'M', '11', '0', '11', '50');
insert into time_slot values ('C', 'W', '11', '0', '11', '50');
insert into time_slot values ('C', 'F', '11', '0', '11', '50');
insert into time_slot values ('D', 'M', '13', '0', '13', '50');
insert into time_slot values ('D', 'W', '13', '0', '13', '50');
insert into time_slot values ('D', 'F', '13', '0', '13', '50');
insert into time_slot values ('E', 'T', '10', '30', '11', '45 ');
insert into time_slot values ('E', 'R', '10', '30', '11', '45 ');
insert into time_slot values ('F', 'T', '14', '30', '15', '45 ');
insert into time_slot values ('F', 'R', '14', '30', '15', '45 ');
insert into time_slot values ('G', 'M', '16', '0', '16', '50');
insert into time_slot values ('G', 'W', '16', '0', '16', '50');
insert into time_slot values ('G', 'F', '16', '0', '16', '50');
insert into time_slot values ('H', 'W', '10', '0', '12', '30');
insert into prereq values ('BIO-301', 'BIO-101');
insert into prereq values ('BIO-399', 'BIO-101');
insert into prereq values ('CS-190', 'CS-101');
insert into prereq values ('CS-315', 'CS-101');
insert into prereq values ('CS-319', 'CS-101');
insert into prereq values ('CS-347', 'CS-101');
insert into prereq values ('EE-181', 'PHY-101');

点击并拖拽以移动

内连接 (自然连接)

1
select 查询的字段(列名)from 左表 inner join 右边 on 连接条件;

点击并拖拽以移动

img点击并拖拽以移动

外连接

左外连接

1
select 查询的字段名(列名)from 左表 left join 右表 on 连接条件;

点击并拖拽以移动

img点击并拖拽以移动

右外连接

img点击并拖拽以移动

全连接

1
select 查询出的字段名(列名) from 左表 full join 右表 on 连接条件;

点击并拖拽以移动

img点击并拖拽以移动

自连接

1
select 别名.查询的字段名(列名)from1 别名1, 表1 别名2 where 连接条件;

点击并拖拽以移动

img点击并拖拽以移动


视图

基本概念

  1. 视图是一个虚拟表,视图不在数据库中存储数据,视图的行和列来自于子查询所返回的结果
  2. 视图名不允许与基本表重名
  3. 可以通过视图对数据进行增删改查:insert、delete、select
  4. 基本表中的数据改变时,视图中的数据也会动态更新

基本操作

查看视图

1
desc Vcourse;

点击并拖拽以移动

img点击并拖拽以移动

创建视图

1
2
3
4
create [or replace] [{force|noforce}] view 视图名(别名列表) as 子查询;
--or replace:如果视图已存在,则用新视图替换旧视图
--force:基本表不存在,也可以创建视图,但是创建的视图不能正常使用
--noforce:基本表不存在,不能创建视图

点击并拖拽以移动

img点击并拖拽以移动

img点击并拖拽以移动

修改视图

1
create or replace view 视图名 as 子查询;

点击并拖拽以移动

img点击并拖拽以移动

删除视图

1
drop view [if exists] 视图名;

点击并拖拽以移动

img点击并拖拽以移动


子查询


如有错误,欢迎指正!!!