基本sql语句
oracle数据库常用sql
基本sql
--查询表
select * from table
--查看表空间
select * from dba_tablespaces
--删除表
drop table 表名
--条件查询
select * from table where 字段 = xx
--删除
delete from table where 字段 = xx
--删除指定行之外的所有行
delete from table where 字段<>(select max(id) from OilInfo)
delete from table where 字段<>值
--添加
insert into table (字段,字段,..) values ( 内容,内容,.. )
--修改/更新
update table set 字段 where = xx
--模糊查询
select * from table where 字段 like %111%
--倒序排序
select * from table order by 字段 desc
--顺序排序
select * from table order by 字段 asc
--分组
select 字段 from table group by 字段
--总数
select count(*) from table where 字段
--求和
select sum(字段) as sumvalue from table
--平均值
select avg(字段) as avgvalue from table
--最大值
select max(字段) as maxvalue from table
--最小值
select min(字段) as minvalue from table
--主键:primary key
--自动增长列:identity
--外键:references
--默认:default
视图、索引
--视图
创建:create view viewname
删除:drop view viewname
--索引
创建:create index idxname
删除:drop index idxname
连表查询:例
select * from 表a inner join 表b on 表a.Id=表b.Id --方式1
select * from 表a left join 表b on 表a.Id=表b.Id --方式2
select * from 表a right join 表b on 表a.Id=表b.Id --方式3
//
select * from 表a inner join 表b on 表a.Id=表b.Id inner join 表c inner join 表b on 表b.Id=表c.Id --多表联查
Substr(截取字符串)
SELECT SUBSTR('Hello SQL!', 1) FROM dual --截取所有字符串,返回'Hello SQL!'
SELECT SUBSTR('Hello SQL!', 2) FROM dual --从第2个字符开始,截取到末尾。返回'ello SQL!'
SELECT SUBSTR('Hello SQL!', -4) FROM dual --从倒数第4个字符开始,截取到末尾。返回'SQL!'
SELECT SUBSTR('Hello SQL!', 3, 6) FROM dual --从第3个字符开始,截取6个字符。返回'llo SQ'
SELECT SUBSTR('Hello SQL!', -4, 3) FROM dual --从倒数第4个字符开始,截取3个字符。返回'SQL'
异库创建链接
create database link LINK_TEST --数据库别名
connect to 用户名 identified by 密码 --分别对应用户名和密码
using '127.0.0.1:123/orcl'; --域名:端口号/数据库实例
--异库表名@连接的B数据库的别名
select * from BASE_TEST@LINK_TEST


浙公网安备 33010602011771号