Oracle常用sql语句(表空间,用户,权限,序列,触发器,表增加,数据增删改,单/多表查询)

必须要开启的Oracle服务

OracleDBConsoleorcl

OracleOraDb11g_home1TNSListener

OracleServiceORCL
标黄,默认已开启

创建表空间

create tablespace j83_db1
datafile ‘D:/xt_java83/Oracle/j83_db1.dbf’
size 100M autoextend
on next 20M maxsize unlimited

创建用户

create user SNKIOD identified by 111;

赋予权限

GRANT

CONNECT, 连接
RESOURCE, 资源
DBA, 数据库管理
unlimited tablespace, 无限表空间
CREATE SESSION, 创建会话
CREATE ANY SEQUENCE, 创建任何序列
CREATE ANY TABLE, 创建任何表
CREATE ANY VIEW , 创建任何视图
CREATE ANY INDEX, 创建任何索引
CREATE ANY PROCEDURE, 创建任何过程
CREATE ANY DIRECTORY, 创建任何目录
ALTER ANY TRIGGER, 修改任何触发器
ALTER SESSION, 修改会话
ALTER ANY SEQUENCE, 修改任何序列
ALTER ANY TABLE, 修改任何表
ALTER ANY VIEW , 修改任何视图
ALTER ANY INDEX, 修改任何索引
ALTER ANY PROCEDURE, 修改任何过程
ALTER ANY DIRECTORY, 修改任何目录
DROP SESSION, 删除会话
DROP ANY SEQUENCE, 删除任何序列
DROP ANY TABLE, 删除任何表
DROP ANY VIEW , 删除任何视图
DROP ANY INDEX, 删除任何索引
DROP ANY PROCEDURE, 删除任何过程
DROP ANY DIRECTORY, 删除任何目录
SELECT ANY TABLE, 查询任何表
SELECT ANY DICTIONARY, 查询任何目录
INSERT ANY TABLE, 增加任何表
UPDATE ANY TABLE, 修改任何表
DELETE ANY TABLE, 删除任何表
DEBUG ANY PROCEDURE, 调试任何过程
DEBUG CONNECT SESSION, 调试连接会话
exp_full_database, 导出
imp_full_database 导入

TO SNKIOD;

创建学生表

create table tb_student
(
id number(10) not null PRIMARY KEY,
stu_name VARCHAR2(50),
stu_gender VARCHAR2(2),
stu_birthday DATE DEFAULT SYSDATE
);
添加数据
INSERT INTO tb_student (id,stu_name,stu_gender) VALUES(1,‘张三’,‘男’);
INSERT INTO tb_student (id,stu_name,stu_gender) VALUES(2,‘李四’,‘男’);
INSERT INTO tb_student (id,stu_name,stu_gender) VALUES(3,‘王五’,‘男’);

创建成绩表

create table tb_exam
(
id number(10) not null PRIMARY KEY,
exam_score number(20),
stu_id number(10) not null,
sub_id number(10) not null
);

添加数据
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(1,90,1,1);
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(2,65,2,1);
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(3,78,1,2);
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(4,83,3,1);
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(5,100,2,2);
INSERT INTO tb_exam (id,exam_score,stu_id,sub_id) VALUES(6,21,3,2);

创建课程表

create table tb_subject
(
id number(10) not null PRIMARY KEY,
sub_name VARCHAR2(50)
);
添加数据
INSERT INTO tb_subject (id,sub_name) VALUES (1,‘Java’);
INSERT INTO tb_subject (id,sub_name) VALUES (2,‘Oracle’);

创建班级表

create table tb_class
(
id number(10) not null PRIMARY KEY,
class_name VARCHAR2(50)
);
添加数据
INSERT INTO tb_class (id,class_name) values (1,‘J83’);
INSERT INTO tb_class (id,class_name) values (2,‘W21’);

ALTER TABLE tb_student ADD ( class_id number(10) );

SELECT * FROM tb_student;

创建student序列:

create sequence STU_SEQUENCE
minvalue 1
maxvalue 9999999999999
start with 4
increment by 1
cache 20;
创建student触发器:
CREATE OR REPLACE TRIGGER STU_TRIGGER
before insert on tb_student for each row
begin
select STU_SEQUENCE.nextval into :new.ID from dual;
end;

创建subject序列

create sequence SUB_SEQUENCE
minvalue 1
maxvalue 9999999999999
start with 3
increment by 1
cache 20;
创建subject触发器
CREATE OR REPLACE TRIGGER SUB_TRIGGER
before insert on tb_subject for each row
begin
select SUB_SEQUENCE.nextval into :new.ID from dual;
end;

创建exam序列

create sequence EXAM_SEQUENCE
minvalue 1
maxvalue 9999999999999
start with 7
increment by 1
cache 20;
创建exam触发器
CREATE OR REPLACE TRIGGER EXAM_TRIGGER
before insert on tb_exam for each row
begin
select EXAM_SEQUENCE.nextval into :new.ID from dual;
end;

创建class序列

create sequence CLASS_SEQUENCE
minvalue 1
maxvalue 9999999999999
start with 3
increment by 1
cache 20;
创建class触发器:
CREATE OR REPLACE TRIGGER CLASS_TRIGGER
before insert on tb_class for each row
begin
select CLASS_SEQUENCE.nextval into :new.ID from dual;
end;

再次插入数据,不再指定id

INSERT INTO tb_student (stu_name,stu_gender,class_id) VALUES(‘春丽’,‘女’,2);
INSERT INTO tb_student (stu_name,stu_gender,class_id) VALUES(‘吉米’,‘女’,3);

INSERT INTO tb_exam (exam_score,stu_id,sub_id) VALUES(90,4,3);
INSERT INTO tb_exam (exam_score,stu_id,sub_id) VALUES(72,5,1);

INSERT INTO tb_subject (sub_name) VALUES (‘Web’);

INSERT INTO tb_class (class_name) VALUES (‘U12’);

增加

insert into tb_subject (sub_name) values (‘Ps’);

修改

update tb_subject set sub_name=‘Html’ where id =4;

删除

delete from tb_subject where id=4;

常量列与别名

select s.stu_name As 学生姓名,‘J83’ As 所属班级 from tb_student s;常量列就是列名为所属班级,值均为J83

单表查询

SELECT * from tb_student
SELECT * from tb_student WHERE id=4;

SELECT * from tb_student s where s.id=2;

新增列(年龄)

ALTER TABLE tb_student ADD ( stu_age number(30) );
UPDATE tb_student s set stu_age=19 WHERE s.id=1;
UPDATE tb_student s set stu_age=21 WHERE s.id=2;
UPDATE tb_student s set stu_age=25 WHERE s.id=3;
UPDATE tb_student s set stu_age=18 WHERE s.id=4;
UPDATE tb_student s set stu_age=17 WHERE s.id=5;

SELECT * from tb_student s where s.stu_age>20;查找出年龄大于20岁的
SELECT * from tb_student s where s.stu_age<>25;查找出年龄不等于25岁的

SELECT * from tb_student s where s.stu_age>20 and s.stu_age<23;查找出年龄大于20岁且年龄小于23岁的

新增列(住址)

ALTER TABLE tb_student ADD ( stu_address VARCHAR2(50));
UPDATE tb_student s set stu_address=‘合肥市’ WHERE s.id=1;
UPDATE tb_student s set stu_address=‘南京市’ WHERE s.id=4;

SELECT * from tb_student s where stu_address is null and stu_age>20;查找出地址是空的且年龄大于20岁的

SELECT * from tb_student s where stu_age BETWEEN 20 and 25;查找出年龄在20~25岁之间的

SELECT * from tb_student s where (stu_age BETWEEN 20 and 25) and (stu_address is null);查找出年龄在20~25岁之间,且地址是空的

SELECT * from tb_student s where stu_name like ‘%张%’;模糊查询

SELECT * from tb_student s WHERE stu_address in (‘北京市’,‘合肥市’);查找出地址在北京市或合肥市的

SELECT * from tb_student s ORDER BY s.stu_age asc;将年龄按照升序排列(默认升序)
SELECT * from tb_student s ORDER BY s.stu_age DESC;将年龄按照降序排列
SELECT * from tb_student s ORDER BY s.stu_age DESC,s.id asc;先按照年龄降序,再按id升序

分组查询:按照地址分组,统计同一地址的学生人数小于2的城市

SELECT stu_address ,COUNT()
FROM tb_student GROUP BY stu_address
HAVING COUNT(
)<2;

分组查询:按照班级id分组,统计同一班级的学生人数大于1的班级

SELECT class_id,COUNT()
FROM tb_student GROUP BY class_id
HAVING COUNT(
)>1;

SELECT DISTINCT stu_address FROM tb_student;去重

分页查询:查询学生表的前2条数据(1、2),从1开始计算,不包括0

select * from ( select s., rownum RN from tb_student s ) where RN > 0 and RN < 3;
select * from ( select s.
, rownum RN from tb_student s ) where RN > 3 and RN <= 5;

设置外键

ALTER TABLE tb_student ADD CONSTRAINT FK_student_class FOREIGN KEY(class_id ) REFERENCES tb_class(id);

ALTER TABLE tb_exam ADD CONSTRAINT FK_exam_student FOREIGN KEY(stu_id )
REFERENCES tb_student(id);

ALTER TABLE tb_exam ADD CONSTRAINT FK_exam_subject FOREIGN KEY(sub_id )
REFERENCES tb_subject(id);

多表查询:

三表交集查询(内连接):
SELECT s.stu_name,sub.sub_name ,e.exam_score
FROM tb_student s join tb_exam e ON (s.id=e.stu_id)
JOIN tb_subject sub ON (e.sub_id=sub.id);

左外连接:
SELECT s.stu_name,e.exam_score
FROM tb_student s LEFT OUTER JOIN tb_exam e on (s.id=e.stu_id);

SELECT s.stu_name,e.exam_score
FROM tb_student s,tb_exam e where (s.id(+)=e.stu_id);

右外连接:
SELECT s.stu_name,e.exam_score
FROM tb_student s RIGHT OUTER JOIN tb_exam e on (s.id=e.stu_id);

SELECT s.stu_name,e.exam_score
FROM tb_student s ,tb_exam e where (s.id=e.stu_id(+));

内连接:
SELECT c.class_name,s.stu_name
FROM tb_class c JOIN tb_student s on (c.id=s.class_id);

SELECT s.stu_name,e.exam_score
FROM tb_student s ,tb_exam e where (s.id=e.stu_id);

子查询(嵌套查询)

查询课程名为Java成绩大于等于75分的学生
select s.id , s.stu_name , temp.sub_id , temp.exam_score
from tb_student s join (
select e.stu_id , e.sub_id , e.exam_score from tb_exam e
where e.sub_id in (
select sub.id from tb_subject sub where sub.sub_name=‘Java’
) And e.exam_score>=75
) temp on (s.id = temp.stu_id);

联合查询

UNION— : 并集查询,删除重复
—UNION ALL— : 并集查询,不删除重复,所以快于union
select s.stu_name from tb_student s where id=4;
UNION all
select s.stu_name from tb_student s where id=3;

创建视图:视图可以主动增加原表数据,但是受限(多表连接)

GRANT CREATE ANY VIEW TO SNKIOD;权限

create or replace view vw_subjects As select * from tb_subject;
select * from vw_subjects;

INSERT INTO vw_subjects (id,sub_name) VALUES (4,‘Ps’);

create or replace view vw_stuSubExam As
select s.stu_name,sub.sub_name,e.exam_score from tb_student s join tb_exam e on (s.id=e.stu_id)
join tb_subject sub on (e.sub_id=sub.id);

select * from vw_stuSubExam ;

附:以上代码操作的数据库源文件(SNKIOD.sql)

发布了15 篇原创文章 · 获赞 11 · 访问量 953

猜你喜欢

转载自blog.csdn.net/qq_41414186/article/details/103024127