查询
select 列名 from 表名 as[别名]where[条件表达式]like[模糊查询]
排序
selec 列名 from 表名orderbyasc(升序)desc(降序)
集合
并集:
select*from emp where sal>1000unionall/union(去重)select*from emp where sal<3000
交集:intersect
差集:minus
联合查询:
语法1:select 列名 from 表名join 表名2on 条件表达(1表和2表的字段相等)
语法2:select 列名 from 表名,表名2where 条件表达(1表和2表的字段相等)
左外连接:select 列名 from 表名leftjoin 表名2on 条件表达(1表和2表的字段相等)select 列名 from 表名,表名2where 表1.name=表2.name(+)
右外连接:select 列名 from 表名rightjoin 表名2on 条件表达(1表和2表的字段相等)select 列名 from 表名,表名2where 表1.name(+)=表2.name
交叉连接:select 列名 from 表A crossjoin 表B
子查询:
where 后:select*from emp where sal>(子查询)
exists/notexists:如果子查询查出数据则真,查不出则条件不成立
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(orderby 列名 desc) r from 表名
selecr 列名 dense_rank()over(orderby 列名 desc) r from 表名
selecr 列名 rank()over(orderby 列名 desc) r from 表名
selecr 列名 rank()over(partitionby 列名orderby 列名 desc) r from 表名
select 列名 casewhenfrom 表名
创建表空间:
createtablespace 表空间名
datafile 路径
size 1G
autoextend 空间大小自动扩展 on nex 100M表空间满了以后扩展的大小
maxsize 表空间最大存储值 unlimited 不限制表空间最大值
修改表空间:
alterdatabase
datadile 路径
autoextend onnext200M
maxsize 2G
修改原有数据文件大小:
alterdatabase datafile 路径 resize 100M
为表空间增加新的数据文件
altertablespace 表空间名 add datafile 数据文件size 大小
临时表空间
createtemporarytablespace 表空间名
tempfile 路径
size 1G
autoextend onnext100M
maxsize unlimited
创建用户:
createuser 用户名
defaulttablespace用户存在的表空间
temporarytablespace 临时表空间名
identified by 指定用户密码password 用户密码
修改用户:
alteruser 用户名
defaulttablespace用户存在的表空间
temporarytablespace 临时表空间名
identified by 指定用户密码password 用户密码
系统权限:
grantcreatesessionto est1允许用户连接到数据库上
grantcreatetableto test1允许用户创建表
grant unlimited tablespaceto test1允许test1用户任意使用表空间
对象权限:
grantselecton 用户名.表名 to test1 允许用户查询表的记录
grantupdateon 用户名.表名 to test1 允许用户更新表的记录
grantallon 用户名.表名 to test1 允许用户插入、删除、更新、查询表的记录
删除用户:
dropuser 用户名
删除表空间:
drop tablespase 表空间名
删除表空间的同时删除数据文件:
drop tablespase 表空间的名字 including contents and datafiles
创建表:
createtable 表名(字段名 数据类型 约束 默认值(可省略)
字段名 数据类型 约束 默认值,
字段名 数据类型 约束 默认值
表级约束--可选)tablespace 表空间名
删除表:
droptable 表名
备份表:
createtable 表名 asselect*from 表名
约束:
notnull 非空约束
unique 唯一约束
primarykey主键约束
foreignkey 外键约束
check 检查约束
给表添加列
altertable 表名 add 列名 数据类型 [约束][默认值]
删除表中的一个列
altertable 表名 dropcolumn 列名
修改一个列
altertable 表名 modify 被修改列名 数据类型 [约束][默认值]--注:请看《Oracle——数据类型、建表、约束、修改表和约束、表的注释》
增加一个约束:
altertable 表名 add 表级约束法
altertable 表名 addconstraint 约束名 unique(列名)altertable 表名 addconstraint 约束名 primarykey(列名)altertable 表名 addconstraint 约束名 foreignkey(外键列)references 主表名(主表主键列)altertable 表名 add constant 约束名 check(约束条件)
删除一个约束:
altertable 表名 dropconstraint 约束名
给表加注释:
commentontable 表名 is ‘注释’;commentontable user_info is ‘用户基础信息表’;commentoncolumn 表名.列名 is ‘注释’;commentoncolumn user_info.id is ‘主键’;
插入语句:
insertinto 表名(列1,列2,列3)values(字段值1,字段值2,字段值3)--从已有数据库表中某一条数据插入到另一个表insertinto 表名(列名1,列名2,列名3)select子查询(可用集合并集等)
修改语句:
update 表名 set 列名=值,列名=值 where条件
删除语句:
deletefrom 表where 条件——删除表数据,保留表结构,根据条件删除
truncatetable 表名——删除表中所有数据保留结构,不记录日志
droptable 表名——删除表结构和数据
创建索引:
createindex 索引名 on 表名(索引列名1[,索引列名2])
注:用索引为条件查询是,不要再索引上进行数学运算
删除索引:
dropindex 索引名
创建视图:
create[orreplace如果视图存在则替换][force基表不存在也可创建视图]view
视图名
[withcheckoption] 子句保证能在视图条件内对视图进行DML
[withread only] 表示是一个只读视图
删除视图
dropview 视图名
创建序列:
create sequence 序列名
startwith A指定序列的初始值,A为自然数
increment by B指定序列每次增加多少,B为自然数
minvalue C指定序列的最小值,C为自然数
maxvalue D指定序列最大值,D为自然数
cache/no cache 指序列缓存,默认20个
cycle/nocycle 指定序列是否循环生成
修改序列:
alter sequence 序列名 maxvalue 2000cycle
删除序列:
drop sequence 序列名
创建同义词:
create[public公共的][orreplace] synonym 同义词名 for 同义词对象(表名、视图名等)
删除同义词
drop synonym 表名
droppublic synonym 表名
版权归原作者 琪实不难 所有, 如有侵权,请联系我们删除。