最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Oracle基本語法使用及說明(SQLPlus)

 更新時間:2026年04月21日 09:40:26   作者:kkkwang0o0  
本文概述了SQL數(shù)據(jù)庫的基本操作,包括數(shù)據(jù)庫的準備工作、用戶登錄、SQL基本命令(DDL、DML、DQL、DCL)、函數(shù)使用,以及約束、多表查詢和視圖的基本概念和操作,通過實際示例,詳細介紹了如何創(chuàng)建、修改和刪除表、用戶權(quán)限管理、查詢和排序數(shù)據(jù)等內(nèi)容

前言

1.使用的數(shù)據(jù)庫不同,所使用的語法也略有不同

2.SQL對大小寫不敏感

3.Oracle中對引號里面的內(nèi)容大小寫敏感

3.表空間名、文件路徑......等需要用單引號將其包含

4.一般引號里面的內(nèi)容需要大寫

準備工作

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

(2)啟動一些服務(wù):

(qwq我不知道哪些有用,哪些沒用,所以我都把打開了,不知道有沒有負面影響,大家參考一下別的博客吧)

登錄

1.打開SQL Plus命令行工具

第一種方式

第二種方式

(1)win+R 打開cmd

(2)輸入sqlplus

2.以不同用戶登錄

注意:

1.使用用戶口令這種形式登錄的時候,是不顯示密碼的,口令輸入的時候是不顯示的,直接輸就好

2.若是想以顯示密碼的形式輸入,直接在用戶名那一塊輸入:用戶名/密碼

3.超級管理員(sys)輸入時需要注意指定 as sysdba

SYSTEM(普通管理員)

SYS(超級管理員)

不顯示密碼方式:

用戶名:SYS

密碼:sys密碼 as sysdba

顯示密碼方式:

用戶名:sys/sys密碼 as sysdba

SCOTT(普通用戶)

若是出現(xiàn)被鎖住的情況:

解決方法:

(1)登錄超級管理員賬戶,

(2)輸入alter user 用戶名 account unlock;

(3)重新登錄即可:

SQL基本命令

1.數(shù)據(jù)定義語言(DDL)

數(shù)據(jù)庫操作

查詢所有用戶

select distinct(OWNER) from all_tables;

查看當(dāng)前用戶

show user;

創(chuàng)建表空間

create tablespace 表空間名 datafile '存儲路徑\***.dbf' size 空間大小;

創(chuàng)建用戶并指定其表空間

(指定表空間需要先創(chuàng)建表空間,如果不指定表空間,就會按照默認空間存儲)

create user 用戶名 identified by 密碼 default tablespace 表空間;

給用戶授予dba的權(quán)限(超級管理員)

 grant dba to 用戶;

刪除表空間

drop tablespace 表空間名 including contents and datafiles;

刪除用戶(超級管理員)

(1)查看用戶是否有活躍對話

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

(2)如果查詢結(jié)果顯示有活動的會話,結(jié)束這些會話

kill session 'session_id, serial#' immediate;

(3)刪除用戶

drop user 用戶名 cascade;

切換用戶登錄

conn 用戶名/密碼

表操作

查詢

查詢某個用戶下所有表名:

(用戶名注意大寫)

select table_name from dba_tables where owner = '用戶名';

查詢某個用戶下表個數(shù):

(用戶名注意大寫)

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

查詢某個用戶的表結(jié)構(gòu):

desc 用戶名.表名;

查詢指定表的建表語句:

(表名、用戶名注意大寫)

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

創(chuàng)建

數(shù)據(jù)類型:

創(chuàng)建表空間:

 create tablespace 表空間名 datafile '文件路徑\文件名.dbf' size 表空間大小;

創(chuàng)建表:

#創(chuàng)建表
create table 表名(

        字段1 字段1類型,

        字段2 字段2類型,

        字段3 字段3類型,

        .......

        字段n 字段n類型

) ;       

給表添加注釋:

 comment on table 表名 is '注釋';

給字段添加注釋:

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

表備份:

create table 用戶名.備份表名 as select * from 用戶名.需要備份的表名;

給表添加一列:

alter table 表名 add (字段名 字段類型 約束條件);

修改

重命名表:

alter table 用戶名.舊表名 rename to 新表名;

添加字段:

alter table 用戶名.表名 add 新字段名 新字段類型 default '默認值';

修改字段名:

 alter table 用戶名.表名 rename column 舊字段名 to 新字段名;

修改數(shù)據(jù)類型:

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

刪除

刪除表字段:

alter table 用戶名.表名 drop column 字段名;

刪除表:

drop table 表名;

刪除表空間:

(1)查看是否有其它用戶在使用該表空間:

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

(2)若有,則刪除這些用戶或者將這些用戶遷移到別的表空間

(3)刪除表空間

drop tablespace 表空間名 including contents and datafiles;

刪除指定表并重新創(chuàng)建該表:

truncate table 表名;

2.數(shù)據(jù)操作語言(DML)

添加數(shù)據(jù)(insert)

注意:

1.插入數(shù)據(jù)注意順序

2.插入的數(shù)據(jù)大小要合法

給指定字段添加數(shù)據(jù)

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

給表中批量添加數(shù)據(jù)

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

修改數(shù)據(jù)(update)

修改數(shù)據(jù)

注意:如果沒有條件,則會修改整張表

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

刪除數(shù)據(jù)(delete)

刪除數(shù)據(jù)

注意:如果沒有條件,則會刪除整張表

delete from 表名;

3.數(shù)據(jù)查詢語言(DQL)

編寫順序

select [distinct|all] 字段列表

from 表名

where 查詢條件

group by 分組字段列表

having 分組后條件列表

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

作示范的表結(jié)構(gòu):

表名:T_STUDENT

執(zhí)行順序

from 表名 : 從哪張表查詢

where 條件 :查詢條件

group by 分組條件 :分組

having 分組后查詢條件 :分組后查詢條件

select 字段列表 :選擇字段

order by 排序方式 :對查詢結(jié)果進行排序
;

基本查詢

查詢多個字段

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

設(shè)置別名

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

去除重復(fù)記錄

select distinct 字段列表 from 表名;

條件查詢

語法

select 字段列表 from 表名 where 條件列表;

條件

比較運算符功能
>
>=
<
<=
=
<> 或 !=不等于
between...and...在某個范圍之內(nèi)
in(...)在in之后的括號中,多選一
like 占位符模糊匹配(_匹配單個字符,%匹配任意個字符)
is null
and 或 &&并且
or 或 ||
not 或 !

聚合函數(shù)

select 聚合函數(shù)(字段列表) from 表名;

注意:

對一列進行計算 所有null值不參與聚合函數(shù)的計算

函數(shù)功能
count統(tǒng)計數(shù)量
max最大值
min最小值
avg平均值
sum求和

分組查詢

select 字段列表 from 表名 [where 條件] group by 分組字段名 [having 分組后過濾條件];
where
分組之前執(zhí)行,不滿足where條件的不參與分組,where不能對聚合函數(shù)進行判斷
having
分組之后對結(jié)果進行過濾,having可以對聚合函數(shù)進行判斷

eg:

排序查詢

select 字段列表 from 表名 order by 字段1 排序方式1, 字段2 排序方式2;
asc升序(默認)
desc降序

分頁查詢

(oracle查詢沒有l(wèi)imit關(guān)鍵字,引入rownum進行分頁查詢)

select * from
(
    select rownum rn, t.* from
    (select 字段 from t_student) t where rownum <= 終止行
)
 where rn >= 起始行;

4.數(shù)據(jù)控制語言(DCL)

管理用戶

查看當(dāng)前用戶

show user;

切換用戶:

connect 用戶名/密碼;

注意連接到數(shù)據(jù)庫超級管理員的時候:

可能會出現(xiàn)以下錯誤:

解決方法:

connect sys/密碼 as sysdba

權(quán)限控制

查詢用戶權(quán)限

select * from dba_sys_privs where grantee='用戶名';

授予權(quán)限

 grant 權(quán)限 to 用戶;
權(quán)限說明
create session登錄權(quán)限
create table創(chuàng)建表的權(quán)限
drop any table刪除任意表
insert any table向任意表中插入行
update any table修改任意表中行的權(quán)限
select on 表名查看指定表的權(quán)限

eg:

回收權(quán)限

--回收用戶權(quán)限
revoke 權(quán)限 from 用戶;
revoke select on 用戶2.表2 from 用戶1; #回收用戶1查看表2的權(quán)限

函數(shù)

使用

一般形式:

select 函數(shù) from 表名 where 條件;

如果只是想看函數(shù)的返回結(jié)果可以使用以下形式:

select 函數(shù) from sys.dual;

sys.dual

dual是一個虛擬表,用來構(gòu)成select的語法規(guī)則,oracle保證dual里面永遠只有一條記錄

字符串函數(shù)

eg:

數(shù)值函數(shù)

eg:

日期函數(shù)

日期表示

日期-月份-年份

eg:21-9月-2024

函數(shù)概述

eg:

sysdate:

  • next_day():
  • last_day():
  • round() :
  • add_months():
  • months_between():

  • extract():

約束

概念

約束是作用于表中字段上的規(guī)則,用于限制存儲在表中的數(shù)據(jù)

約束可以在創(chuàng)建表/修改表的時候添加

分類

約束描述關(guān)鍵字
非空約束限制該字段的數(shù)據(jù)不能為nullnot null
唯一約束保證該字段的所有數(shù)據(jù)都是唯一的、不重復(fù)的unique
主鍵約束主鍵是一行數(shù)據(jù)的唯一標(biāo)識,要求非空且唯一

primary key

默認約束保存數(shù)據(jù)時,如果未指定該字段的值,則采用默認值default
檢查約束保證字段值滿足某一條件check
外鍵約束用來讓兩張表的數(shù)據(jù)之間建立連接,保證數(shù)據(jù)的一致性和完整性

foreign key

建表時添加約束:

sql語句添加:

alter table 表名 add constraints 約束名稱 primary key(列名);

外鍵約束

子表(從表):具有外鍵的表

父表(主表):外鍵所關(guān)聯(lián)的表

FOREIGN KEY約束可以與另外一個表的PRIMARY KEY及UNIQUE關(guān)聯(lián)

添加外鍵

在創(chuàng)建表的時候添加
 constraint 外鍵名稱 foreign key(外鍵) references 表名(外鍵關(guān)聯(lián)的鍵)

foreign key(列名) references 外鍵關(guān)聯(lián)的表名

sql語句添加(alter)
alter table 表名 add constraint 外鍵名稱 foreign key(列名) references 表名(外鍵關(guān)聯(lián)的鍵);

查詢外鍵

根據(jù)外鍵名稱查看外鍵所在位置
select * from user_cons_columns cl where cl.constraint_name= upper('外鍵名稱');

查找表的外鍵

(包括名稱,引用表的表名和對應(yīng)的列名)

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

刪除外鍵

alter table 表名 drop constraint 外鍵名稱;

多表查詢

基本概念

自然連接(內(nèi)連接):左表與右表中每一個元組進行條件匹配,滿足條件的才會查詢出來

自連接:同一個表內(nèi)的查詢

外連接:

  • 左外連接:左表中的元組不會丟失,即每一個左表中的元素都可查出,若右表沒有匹配的,就置為空
  • 右外連接:右表中的元組不會丟失,即每一個右表中的元素都可查出,若左表沒有匹配的,就置為空
  • 全連接:左右表的懸浮元組都會加入到最后的查詢結(jié)果中

基本結(jié)構(gòu)

內(nèi)連接+外連接

 select 查詢的字段名 from 左表 [inner|left|right|full] join 右表 on 連接條件;

自連接

select 別名.查詢的字段名(列名)from 表1 別名1, 表1 別名2 where 連接條件;

關(guān)系運算

大學(xué)模式

首先以黑書里面的大學(xué)模式為例:

創(chuàng)建基本表結(jié)構(gòu)

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)
	);

插入數(shù)據(jù)

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');

內(nèi)連接 (自然連接)

select 查詢的字段(列名)from 左表 inner join 右邊 on 連接條件;

外連接

左外連接

select 查詢的字段名(列名)from 左表 left join 右表 on 連接條件;

右外連接

全連接

select 查詢出的字段名(列名) from 左表 full join 右表 on 連接條件;

自連接

select 別名.查詢的字段名(列名)from 表1 別名1, 表1 別名2 where 連接條件;

視圖

基本概念

  1. 視圖是一個虛擬表,視圖不在數(shù)據(jù)庫中存儲數(shù)據(jù),視圖的行和列來自于子查詢所返回的結(jié)果
  2. 視圖名不允許與基本表重名
  3. 可以通過視圖對數(shù)據(jù)進行增刪改查:insert、delete、select
  4. 基本表中的數(shù)據(jù)改變時,視圖中的數(shù)據(jù)也會動態(tài)更新

基本操作

查看視圖

desc Vcourse;

創(chuàng)建視圖

create [or replace] [{force|noforce}] view 視圖名(別名列表) as 子查詢;
--or replace:如果視圖已存在,則用新視圖替換舊視圖
--force:基本表不存在,也可以創(chuàng)建視圖,但是創(chuàng)建的視圖不能正常使用
--noforce:基本表不存在,不能創(chuàng)建視圖

修改視圖

create or replace view 視圖名 as 子查詢;

刪除視圖

drop view [if exists] 視圖名;

子查詢

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

大兴区| 纳雍县| 澄迈县| 万宁市| 永宁县| 漠河县| 威海市| 卫辉市| 会同县| 沈阳市| 宾川县| 舟曲县| 诸暨市| 永年县| 西吉县| 安顺市| 读书| 古交市| 鞍山市| 天祝| 隆安县| 澄江县| 崇阳县| 平南县| 水富县| 济南市| 宁河县| 陆良县| 木兰县| 大荔县| 澄迈县| 洪泽县| 洛隆县| 怀宁县| 南皮县| 阿勒泰市| 靖远县| 呈贡县| 固安县| 西华县| 巴马|