SQLPLUS語句
登陸:sqlpus 用戶名/密碼
以dba權限登陸:sqlplus / as sysdba
切換用戶:conn 用戶名
切換到dba:conn / as sysdba
查看當前用戶:show user
退出登錄:exit
解鎖用戶:(dba權限下)alter user 用戶名(scott) account unlock;
修改密碼:(dba權限下)alter user 用戶名(scott) identified by 新密碼;
查看當前實例:select name from v$database;
切換到其他實例:sqlplus /@orcl as sysdba;
創建視圖權限:(dba權限下)grant create any view to scott
常用表結構
系表:dep(dno char(3) 系代號, dname varchar2(20) 系名, tel number(8) 電話)
學生表:student(sno number(10) 學號, sname varchar2(8) 姓名, sex char(2) 性別, birth date 出生日期,
dno char(3) 系代號)
教師表:teacher(tno char(6) 工作證號, tname varchar2(8) 姓名, title varchar2(10) 職稱, hiredate date 參加工作時間, sal number(8,2) 工資, bonus number(8,2) 獎金, mgr char(6) 上級領導, dno char(3) 系代號)
課程表:course(cno char(3) 課序號, cname varchar2(20) 課程名, credit number(1) 學分, tno char(6) 任課老師工作證號)
成績表:sc(sno number(10) 學號, cno char(3) 課序號, grade number(3) 成績)
職工表:emp(eno char(6) 工作證號, ename varchar2(8) 姓名, title varchar2(10) 職稱, age number(2) 年齡, hiredate date 參加工作時間, sal number(8,2) 工資, bonus number(8,2) 獎金, mgr char(6) 上級領導,
deptno char(3) 部門代號)
部門表:dept(deptno char(3) 部門代號, dname varchar2(20) 部門名稱, tel number(8) 電話)
產品表:it_products(it_no number(5) 產品編號, it_name varchar2(40) 產品名稱, it_origin varchar2(20) 產地, price number(4) 價格, inventory number(4) 庫存)
常用函數
dual:偽表,本身不存在,為了方便操作。例:select查詢需要有特定的對象,select sysdate from dual;
abs(x):返回x的絕對值
sqrt(x):返回x的平方根
power(x,y):返回x的y次冪 --select power(4,2) from dual;(16)
round(n,m):將n四舍五入,保留小數點后m位 --select round(1234.5678,2) from dual;(1234.57)
trun(x[,y]):返回x按精度y截取后的值,如果y不為整數則截取y整數部分, 如果y>0則截取到y位小數, 如果y小於0則截取到小數點向左第y位,小數前其它數據用0表示。 --select trunc(3.1415926,3.2),trunc(2554, -2.6) from dual;(3.141 2500)
floor(x):返回小於等於x的最大整數值 --select floor(3.1),floor(3.1+5.2),floor(0) from dual;(3 8 0)
ceil(x) :返回大於等於x的最小整數值 --select ceil(3.1),ceil(3.1+5.2),ceil(0) from dual;(4 9 0)
upper('字符串'|列名稱):返回字符串或者列名,並將所有的字符或列名大寫 --lower() 與之相反
to_char(x[[,c2],C3]):將日期或者數據轉換成char指定格式 --select to_char(sysdate,'yyyy-mm-dd') from dual;(當前年-月-日) --select to_char(123456789,'999,999,999,999') from dual;(123,456,789)
months_between(d1,d2):返回日期d1到日期d2之間的月數 --select months_between('19-12月-1999','19-3月-1999') mon_between from dual;
extract(c1 from d1):返回日期特定部分 --select extract(hour from sysdate) from dual; C1:(YEAR、MONTH、DAY、HOUR、MINUTE、SECOND)
DDL語句
DDL(Data Definition Language,數據定義語言): 用於定義數據的結構,比如 創建、修改或者刪除數據庫對象(表,視圖等等)。
DDL包括:DDL語句可以用於創建用戶和重建數據庫對象。
下面是DDL命令:
CREATE TABLE:創建表
ALTER TABLE:修改表
DROP TABLE:刪除表
CREATE INDEX
DROP INDEX
*實例-創建/刪除表結構*
1.創建一張系表dep,其架構包括系代號dno(定長字符串3個字節)、系名dname(變長字符串20個字節)和電話tel(12位整數)
create table dep
(dno char(3),
dname varchar2(20),
tel number(12));
2.創建一張課程表course,其架構包括課序號cno(定長字符串3個字節)、課程名cname(變長字符串20個字節)、學分credit(1位整數)、上課時間ctime(日期時間型)和上課人數quota(3位整數)
create table course
(cno char(3),
cname varchar2(20),
credit number(1),
ctime date,
quota number(3));
3.創建一張選課表sc,其架構包括學號sno(10位整數)、課序號cno(定長字符串3個字節)和成績grade(3位整數1位小數)
create table sc
(sno number(10),
cno char(3),
grade number(4,1));
4.刪除課程表course
drop table course;
--快速復制一張表:create table student_tmp as select * from student where 1 = 2;(如果為True[1=1]則同時復制表結構和數據,否則只復制表結構)
*實例-動態修改表結構*
1.給職工表emp添加新列出生日期birth(日期時間型)
alter table emp add birth date;
2.將系表dep中系名dname的數據類型修改為變長字符串16個字節
alter table dep modify dname varchar2(16);
3.修改學生表student中birth的數據類型
alter table student modify birth timestamp;
4.修改課程表course中cname的列名
alter table course rename column cname to c_name;
5.修改學生表student的表名
rename student to s_student;
6.刪除職工表emp中年齡age這一列
alter table emp drop column age;
*實例-創建表結構並定義約束*
約束用於確保數據庫滿足特定的規則,數據庫不僅僅是存儲數據,還必須保證所保存的數據的正確性,通過表中的列定義約束,就可以防止非法數據的操作問題。
1.創建一張教師表teacher,其架構包括工號tno(定長字符串10個字節)、姓名tname(變長字符串8個字節)、職稱title(變長字符串10個字節)、入職時間hiredate(日期時間型)、工資sal(6位整數2位小數)、津貼bonus(6位整數2位小數)、上級領導工號mgr(定長字符串10個字節)、所在部門代號deptno(定長字符串3個字節),其中定義工號主碼完整性約束,姓名和入職時間非空完整性約束,職稱check完整性約束(范圍是教授、副教授、講師和助教),所在部門代號引用完整性約束,引用自系表dep的系代號dno列。
create table teacher
(tno char(10) constraint t1 primary key,
tname varchar2(8) constraint t2 not null,
title varchar2(10) constraint t3 check(title in ('教授','副教授','講師','助教')),
hiredate date constraint t4 not null,
sal number(8,2),
bonus number(8,2),
mgr char(10),
deptno char(3) constraint t5 references dep(dno) on delete cascade);
2.在表最后添加約束,有兩個主碼時,不能同時在sno,cno寫主碼完整性約束
create table sc
(sno number(10),
cno char(3),
grade number(4,1),
constraint sc1 primary key(sno,cno));
3.引用完整性約束放在后面,需要加foreign key()
create table sc
(sno number(10),
cno char(3),
grade number(4,1),
constraint sc1 foreign key(sno) references student(sno));
*實例-修改約束*
1.修改student表,給sname添加非空完整性約束。
alter table student modify sname constraint s1 not null;
或者
alter table student add constraint s1 check(sname is not null);
2.修改dep表,給dno添加主碼完整性約束,給dname添加非空完整性約束。
alter table dep add constraint d1 primary key(dno);
alter table dep modify dname constraint d2 not null;
3.修改emp表,給age添加基於屬性完整性約束,要求age的值在18到60之間。
alter table emp modify age constraint e1 check(age between 18 and 60);
或者
alter table emp add constraint e1 check(age between 18 and 60);
4.給sc表的sno添加引用完整性約束,引用自表student的學號sno列。
alter table sc add constraint sc1 foreign key(sno) references student(sno) on delete set null;
--當刪除學生表的對應的sno學生時,sc的sno將自動設置為null
alter table sc add constraint sc1 foreign key(sno) references student(sno) on delete cascade;
--當刪除學生表的對應的sno學生時,sc的數據將整條刪除
5.修改student表sname的約束名
alter table student rename constraint s1 to s2
6.修改teacher表,刪除約束名為t2的約束。
alter table teacher drop constraint t2;
DML語句
DML(Data Manipulation Language,數據操作語言):用於檢索或者修改數據。
DML包括: SELECT(用於檢索數據);INSERT(用於增加數據到數據庫);UPDATE(用於從數據庫中修改現存的數據 );DELETE(用於從數據庫中刪除數據)。
DML需要提交事務:commit; 回滾:rollback;
*實例-增加數據*
insert into 表名(列名,...) values(值,...)
1.給學生表student插入數據2018122101,李宏,男,01-1月-1980,d01。
insert into student values(2018122101,'李宏','男','01-1月-1980','d01');
2.給成績表sc插入數據2018122101,c01。
insert into sc(sno,cno) values(2018122101,'c01');
insert into sc values(2018122101,'c01',null);
--批量添加數據:insert into student_tmp select * from student;(將舊表所有數據添加到新表)
*實例-修改數據*
update 表名 set 列名 = 值 where 條件表達式
1.給成績表sc中學號為2018122101,課序號為c01這條記錄輸入成績98分。
update sc set grade = 98 where sno = 2018122101 and cno = 'c01';
2.將課程表course中數據庫技術這門課的學分改為4學分。
update coure set credit = 4 where cname = '數據庫技術';
3.修改teacher表,給王瑩教師的職稱添加臨時班主任。
update teacher set title = title || '/臨時班主任' where tname = '王瑩';
3.將教師表teacher中系代號為d02、職稱為副教授的教師的工資增加500元,獎金增加100元。
update teacher set sal = sal+500,bonus = bonus+100 where dno = 'd02' and title = '副教授';
(修改多列用逗號隔開不是and)
*實例-刪除數據*
delete from 表名 where 條件表達式
1.刪除職工表emp中姓名為田中華這位職工的信息。
delete from emp where ename = '田中華';
2.將教師表teacher中系代號為d01的記錄的系代號刪除。
update teacher set dno = null where dno = 'd01';
*實例-簡單查詢數據*
select * from 表名 where 條件表達式
1.查詢出所有學生的學號、姓名,所在系的系號
select sno,sname,dno from student;
2.查詢出所有男學生的學號、姓名,所在系的系號
select sno,sname,dno from student where sex = '男';
3.查詢教授和副教授的年收入,修改輸出的列標題為工號、姓名、職稱和年收入
select tno 工號, tname 姓名, title 職稱, sal*12+nvl(bonus,0) 年收入 from teacher where title = '教授' or title = '副教授';
4.查詢現有教師職稱的種類
select distinct title from teacher;
5.查詢課序號為c01的課程成績為良好(80分到89分,包含80分和89分)的同學的學號
select sno from sc where cno = 'c01' and grade between 80 and 89;
6.查詢有科目不及格的學生的學號
select distinct sno from sc where grade<60;
7.查詢正在學習課序號為c01、c02、c03和c04這幾門課的學生的學號
select distinct sno from sc where cno in('c01','c02','c03','c04') and grade is null;
8.查詢課序號為c01、c02、c03和c04這幾門課之外的課程成績及格的學生的學號
select distinct sno from sc where cno not in('c01','c02','c03','c04') and grade> = 60;
9.查詢姓名中有個田字的同學的信息
select * from student where sname like '%田%';
10.查詢名字中有個田字的同學的信息
select * from student where sname like '_%田%';
11.根據用戶輸入的員工姓名查詢該員工信息
select * from emp where ename = '&員工姓名';
'like':模糊查詢。 like:包含,not like:不包含。
% 表示0或者多個字符,_ 表示一個字符
*實例-排序查詢*
order by (asc) 升序 order by desc 降序
1.從學生表student中查詢學生信息,按年齡從高到低排序輸出(這里的年齡不止要比較出生年份,而且要比較到出生的月份和日子)
select * from student order by birth;
2.從學生表student中查詢男學生信息,按年齡從低到高排序輸出(這里的年齡只比較出生年份,不比較到出生的月份和日子),年齡相同的按學號從高到低排序
select * from student where sex = '男' order by to_char(birth,'yyyy') desc,sno desc;
3.從教師表teacher中查詢所有教授的工齡(工作了多少年),輸出工號、姓名和工齡,按工齡從高到低排序輸出,工齡相同按工號從低到高排序
select tno,tname,to_char(sysdate,'yyyy')-to_char(hiredate,'yyyy') from teacher where title = '教授' order by 3 desc,1;
*實例-多表連接查詢*
1.查詢物理系和化學系教師的信息,輸出工號、姓名、職稱和系名。
select tno,tname,title,dname from teacher,dep where teacher.dno = dep.dno and dname in('物理系','化學系');
2.查詢陳潔有哪些科目不及格,輸出課程名和成績。
select cname,grade from course,sc,student where course.cno = sc.cno and sc.sno = student.sno and sname = '陳潔' and grade<60;
3.查詢正在上大學英語的學生的信息,輸出學號、姓名和系名。
select sc.sno,sname,dname from student,sc,course,dep where student.sno = sc.sno and student.dno = dep.dno and sc.cno = course.cno and cname = '大學英語' and grade is null;
4.查詢物理系大學英語不及格學生的信息,輸出學號、姓名和成績。
select sc.sno,sname,grade from student,sc,dep,course where student.sno = sc.sno and student.dno = dep.dno and sc.cno = course.cno and dname = '物理系' and cname = '大學英語' and grade<60;
5.查詢這學期有上課的物理系老師的工號和姓名。
select distinct teacher.tno,tname from teacher,dep,course,sc
where course.cno = sc.cno and teacher.tno = course.tno and teacher.dno = dep.dno and dname = '物理系' and grade is null;
6.查詢正在給物理系學生上課的教師信息,輸出教師的工號和姓名。
select distinct teacher.tno,tname from teacher,course,sc,student,dep where teacher.tno = course.tno and course.cno = sc.cno and sc.sno = student.sno and student.dno = dep.dno and dname = '物理系' and grade is null;
*實例-層次查詢*
1.查詢教師王瑩直接下屬的信息,輸出工號和姓名。
select t1.tno,t1.tname
from teacher t1,teacher t2
where t1.mgr = t2.tno and t2.tname = '王瑩';
2.查詢教師王瑩及其所有下屬的信息,輸出工號和姓名。
select tno,tname
from teacher
start with tname = '王瑩'
connect by prior tno = mgr;
輸出列層次關系:level
*實例-集合運算查詢*
1.查詢同時選修了英語和日語兩門課的學生,輸出學號和姓名。
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '英語'
intersect --交
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '日語';
2.查詢正在選修英語、但從未選修日語的學生,輸出學號和姓名。
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '英語' and grade is null
minus --差
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '日語';
3.假如外語只開設了英語和日語。查詢當前至少上着一門外語課的學生,輸出學號和姓名。
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '英語' and grade is null
union --並
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and cname = '日語' and grade is null;
*實例-分組查詢*
select column,group_function from table [where condition] [group by group_by_expression] [having group_condition] [order by column];
1.查詢教師年收入總額、平均年收入、最高年收入、最低年收入以及教師人數,將列標題用中文輸出
select sum(sal12+nvl(bonus,0)) 年收入總額,avg(sal12+nvl(bonus,0)) 平均年收入,max(sal12+nvl(bonus,0)) 最高年收入,min(sal12+nvl(bonus,0)) 最低年收入,count(tno) 教師人數
from teacher;
2.查詢這學期上課的學生人數
select count(distinct sno)
from sc
where grade is null;
3.查詢物理系工齡最長的教師的工齡
select max(extract(year from sysdate)-extract(year from hiredate))
from teacher, dep
where teacher.dno = dep.dno and dname = '物理系';
4.查詢每個系的教師人數,輸出系名和人數
select dname,count(tno)
from dep,teacher
where dep.dno = teacher.dno
group by dname;
5.查詢每個系這學期上課的學生人數,輸出系代號、系名和人數
select dep.dno,dname,count(distinct sc.sno)
from dep,sc,student
where dep.dno = student.dno and student.sno = sc.sno and grade is null
group by dep.dno,dname;
6.統計每個系參加工作超過20年的教師的平均月工資,輸出系名和平均月工資
select dname,avg(sal)
from teacher,dep
where teacher.dno = dep.dno and (to_char(sysdate,'yyyy')-to_char(hiredate,'yyyy'))>20
group by dname;
或者
select dname,avg(sal)
from teacher,dep
where teacher.dno = dep.dno and (extract(year from sysdate)-extract(year from hiredate))>20
group by dname;
或者
select dname,avg(sal)
from teacher,dep
where teacher.dno = dep.dno and (months_between(sysdate,hiredate)/12)>20
group by dname;
7.查詢已獲得學分大於15分的學生,輸出學號,姓名和學分
select sc.sno,sname,sum(credit)
from sc,student,course
where sc.sno = student.sno and sc.cno = course.cno and grade> = 60
group by sc.sno,sname
having sum(credit)>15;
8.查詢數據庫技術最高分超過85分的系,男女生分開統計,輸出系名、性別、最高分
select dname,sex,max(grade)
from student,sc,course,dep
where student.sno=sc.sno and sc.cno=course.cno and student.dno=dep.dno and cname='數據庫技術'
group by dname,sex
having max(grade)>85;
或者
select dname,sex,max(grade)
from dep,student,sc,course
where dep.dno=student.dno and student.sno=sc.sno and sc.cno=course.cno and cname='數據庫技術' and grade>85
group by dname,sex;
9.查詢各個系的同學正在選修的課程數,輸出課程數超過3的系名和課程數
select dname,count(distinct cno)
from dep,sc,student
where dep.dno=student.dno and student.sno=sc.sno and grade is null
group by dname
having count(distinct cno)>3;
*實例-子查詢*
1.統計每個系的信息和學生人數
select d.*,(select count(*) from student where dno = d.dno) from dep d;
2.查詢數據庫技術成績大於85分的女學生的名單,輸出學號和姓名(要求七種寫法)
第一種:
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and course.cno = sc.cno and cname = '數據庫技術' and grade>85 and sex = '女';
第二種:
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and sc.cno in
(select cno
from course
where cname = '數據庫技術') and grade>85 and sex = '女';
第三種:
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and sc.cno in
(select cno from course where cname = '數據庫技術') and grade>85 and student.sno in
(select sno from student where sex = '女');
第四種:
select sno,sname
from student
where sex = '女' and sno in
(select sno from sc where grade>85 and cno in
(select cno from course where cname = '數據庫技術'));
第五種:
select sno,sname
from student
where sex = '女'
intersect
select sc.sno,sname
from student,sc,course
where student.sno = sc.sno and course.cno = sc.cno and cname = '數據庫技術' and grade>85;
第六種:
select sno,sname
from student
where sex = '女'
intersect
select sno,sname
from student
where sno in
(select sno
from sc,course
where course.cno = sc.cno and cname = '數據庫技術' and grade>85);
第七種:
select sno,sname
from student
where sex = '女'
intersect
select sno,sname
from student
where sno in
(select sno
from sc
where cno in
(select cno
from course
where cname = '數據庫技術') and grade>85);
3.查詢沒有被物理系學生選修過的課程的課序號和課程名(要求兩種寫法)
第一種:
select cno,cname
from course
where cno not in
(select cno
from sc
where sno in
(select sno
from student
where dno =
(select dno
from dep
where dname = '物理系')));
第二種:
select cno,cname
from course
minus
select sc.cno,cname
from course,student,sc,dep
where student.sno = sc.sno and course.cno = sc.cno and dname = '物理系' and student.dno = dep.dno;
4.查詢各系數據庫技術課程成績大於85分的年紀最小(精確到日)的學生名單,輸出系名,學生姓名和出生日期(要求三種寫法)
第一種:
select dname,sname,birth
from student,dep,course,sc
where student.dno = dep.dno and course.cno = sc.cno and sc.sno = student.sno and cname = '數據庫技術' and grade>85 and(dep.dno,birth) in
(select dno,max(birth)
from student
where sno in
(select sno
from sc
where grade>85 and cno in
(select cno
from course
where cname = '數據庫技術'))
group by dno);
第二種:
select dname,sname,birth
from student,dep,sc
where student.dno = dep.dno and sc.sno = student.sno and cno in
(select cno
from course
where cname = '數據庫技術') and grade>85 and (dep.dno,birth) in
(select dno,max(birth)
from student
where sno in
(select sno
from sc
where grade>85 and cno in
(select cno
from course
where cname = '數據庫技術'))
group by dno);
第三種:
select dname,sname,birth
from student,dep,course,sc
where student.dno = dep.dno and course.cno = sc.cno and sc.sno = student.sno and cname = '數據庫技術' and grade>85 and(dep.dno,birth) in
(select dno,max(birth)
from student,sc,course
where student.sno = sc.sno and sc.cno = course.cno and grade>85 and cname = '數據庫技術'
group by dno);
4.查詢各系年紀最小(只考慮年)且其數據庫技術課程成績大於85分的學生名單,輸出系名、學生姓名和年齡(要求兩種寫法)
第一種:
select dname,sname,extract(year from sysdate)-extract(year from birth)
from dep,student
where dep.dno = student.dno and (dep.dno,extract(year from sysdate)-extract(year from birth)) in
(select dno,min(extract(year from sysdate)-extract(year from birth))
from student
group by dno)
intersect
select dname,sname,extract(year from sysdate)-extract(year from birth)
from dep,student
where dep.dno = student.dno and sno in
(select sno
from sc
where grade>85 and cno in
(select cno
from course
where cname = '數據庫技術'));
第二種:
select dname,sname,extract(year from sysdate)-extract(year from birth)
from student,dep,sc,course
where student.dno = dep.dno and student.sno = sc.sno and sc.cno = course.cno and (extract(year from sysdate)-extract(year from birth),dep.dno) in
(select min(extract(year from sysdate)-extract(year from birth)),dno
from student
group by dno) and grade>85 and cname = '數據庫技術';
5.查詢數據庫技術成績最高的學生的信息,輸出學號、姓名和成績
select sc.sno,sname,grade
from sc,student
where sc.sno=student.sno and grade=(select max(grade) from sc,course where sc.cno=course.cno and cname='數據庫技術') and cno in (select cno from course where cname='數據庫技術');