Oracle语法使用说明

数据库常见的SQL语句类型

1、DQL(Data Query Language):用于查询数据的语言,主要包括SELECT语句。DQL语句用于从数据库中检索数据。

2、DML(Data Manipulation Language):用于操作数据的语言,主要包括INSERT、UPDATE和DELETE语句。DML语句用于对数据库中的数据进行增、删、改操作。

3、DDL(Data Definition Language):用于定义数据库结构的语言,主要包括CREATE、ALTER和DROP语句。DDL语句用于创建、修改和删除数据库、表、索引等对象。

4、DCL(Data Control Language):用于控制数据库访问权限和安全性的语言,主要包括GRANT和REVOKE语句。DCL语句用于授权和撤销用户对数据库对象的权限。

5、TCL(Transaction Control Language):用于控制事务处理的语言,主要包括COMMIT和ROLLBACK语句。TCL语句用于管理事务的提交和回滚操作。

DQL语句示例

select(查询)

select 列名 from 表名 as [别名] where[条件表达式] like [模糊查询]
Order by(排序)
selec 列名 from 表名order by asc(升序) desc(降序)
多表联查
集合,并集:
select * from emp where sal>1000
union all/union(去重)
select * from emp where sal<3000
交集:intersect
差集:minus
联合查询:
语法1:
select 列名 from 表名join 表名2 on 条件表达(1表和2表的字段相等)
语法2:
select 列名 from 表名,表名2 where 条件表达(1表和2表的字段相等)
左外连接:
select 列名 from 表名left join 表名2 on 条件表达(1表和2表的字段相等)
select 列名 from 表名,表名2 where 表1.name=表2.name(+)
右外连接:
select 列名 from 表名right join 表名2 on 条件表达(1表和2表的字段相等)
select 列名 from 表名,表名2 where 表1.name(+)=表2.name
交叉连接:
select 列名 from 表A cross join 表B
子查询:
where 后:select * from emp where sal>(子查询)
exists/not exists:如果子查询查出数据则真,查不出则条件不成立
select * from emp where eists(子查询)
having 后:select * from emp gruop by empno having (子查询)
select 和from 之间:select (子查询) from emp
分页SQL:
select 列名rownum from 表名
select 列名 row_number()over(order by 列名 desc) r from 表名
selecr 列名 dense_rank()over(order by 列名 desc) r from 表名
selecr 列名 rank()over(order by 列名 desc) r from 表名
selecr 列名 rank()over(partition by 列名order by 列名 desc) r from 表名
select 列名 case when from 表名

DDL语句示例

tablespace(表空间)

创建表空间
create tablespace 表空间名
datafile 路径
size 1G
autoextend 空间大小自动扩展 on nex 100M表空间满了以后扩展的大小
maxsize 表空间最大存储值 unlimited 不限制表空间最大值
 创建临时表空间
create temporary tablespace 表空间名
tempfile 路径
size 1G
autoextend on next 100M
maxsize unlimited
修改表空间
alter database
datadile 路径
autoextend on next 200M
maxsize 2G
(datafile)修改原有数据文件大小
alter database datafile 路径 resize 100M
为表空间增加新的数据文件
alter tablespace 表空间名 add datafile 数据文件size 大小

user(用户)

创建用户
create user 用户名
default tablespace用户存在的表空间
temporary tablespace 临时表空间名
identified by 指定用户密码password 用户密码
修改用户
alter user 用户名
default tablespace用户存在的表空间
temporary tablespace 临时表空间名
identified by 指定用户密码password 用户密码

table(表)

创建表
create table 表名(字段名 数据类型 约束 默认值(可省略)
字段名 数据类型 约束 默认值,
字段名 数据类型 约束 默认值
表级约束--可选
) tablespace 表空间名
(constraint)约束
not null 非空约束
unique 唯一约束
primary key主键约束
foreign key 外键约束
check 检查约束
删除表
drop table 表名
备份表:
create table 表名 as select * from 表名
修改表
给表添加列
alter table 表名 add 列名 数据类型 [约束] [默认值]
删除表中的一个列
alter table 表名 drop column 列名
修改一个列
alter table 表名 modify 被修改列名 数据类型 [约束][默认值]
--注:请看《Oracle——数据类型、建表、约束、修改表和约束、表的注释》
增加一个约束:
alter table 表名 add 表级约束法
alter table 表名 add constraint 约束名 unique(列名)
alter table 表名 add constraint 约束名 primary key(列名)
alter table 表名 add constraint 约束名 foreign key(外键列) references 主表名(主表主键列)
alter table 表名 add constant 约束名 check(约束条件)
删除一个约束:
alter table 表名 drop constraint 约束名
给表加注释:
comment on table 表名 is ‘注释’;
comment on table user_info is ‘用户基础信息表’;
comment on column 表名.列名 is ‘注释’;
comment on column user_info.id is ‘主键’;

index(索引)

创建索引
create index 索引名 on 表名(索引列名1[,索引列名2])
注:用索引为条件查询是,不要再索引上进行数学运算
删除索引
drop index 索引名

view(视图)

创建视图
create [or replace如果视图存在则替换][force基表不存在也可创建视图] view
视图名
[with check option] 子句保证能在视图条件内对视图进行DML
[with read only] 表示是一个只读视图
删除视图
drop view 视图名

SEQUENCE(序列)

创建序列
create sequence 序列名
start with A指定序列的初始值,A为自然数
increment by B指定序列每次增加多少,B为自然数
minvalue C指定序列的最小值,C为自然数
maxvalue D指定序列最大值,D为自然数
cache/no cache 指序列缓存,默认20个
cycle/no cycle 指定序列是否循环生成
修改序列
alter sequence 序列名 maxvalue 2000 cycle
删除序列
drop sequence 序列名

SYNONYM(同义词)

创建同义词
create [public公共的] [or replace] synonym 同义词名 for 同义词对象(表名、视图名等)
删除同义词
drop synonym 表名
drop public synonym 表名

DCL语法示例

grant(授权)

系统权限
grant create session to est1允许用户连接到数据库上
grant create table to test1允许用户创建表
grant unlimited tablespace to test1允许test1用户任意使用表空间
对象权限
grant select on 用户名.表名 to test1 允许用户查询表的记录

grant update on 用户名.表名 to test1 允许用户更新表的记录

grant all on 用户名.表名 to test1 允许用户插入、删除、更新、查询表的记录

DML语法示例

insert(插入)数据

insert into 表名(列1,列2,列3) values(字段值1,字段值2,字段值3 )
--从已有数据库表中某一条数据插入到另一个表
insert into 表名(列名1,列名2,列名3)select子查询(可用集合并集等)

drop(修改)数据

update 表名 set 列名=值,列名=值 where条件

delete(删除)数据

delete from 表where 条件——删除表数据,保留表结构,根据条件删除
truncate table 表名——删除表中所有数据保留结构,不记录日志
drop table 表名——删除表结构和数据

TCL语句

COMMIT提交事务

ROLLBACK回滚事务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值