Oracle数据库语句大全一.入门部分1.创建表空间create tablespace schooltbs datafile ‘D:\oracle\datasource\schooltbs.dbf’ size 10M autoextend on;2.删除表空间drop tablespace schooltbs[including contents and datafiles];3.查询表空间基本信息select *||tablespace_name from DBA_TABLESPACES;4.创建用户create user lihuaidentified by lihuadefault tablespace schooltbstemporary tablespace temp;5.更改用户alter user lihuaidentified by 123default tablespace users;6.锁定用户alter user lihua account lock|unlock;7.删除用户drop user lihua cascade;--删除用户模式8.oracle数据库中的角色connect,dba,select_catalog_role,delete_catalog_role,execute_catalo g_role,exp_full_database,imp_full_database,resource9.授予连接服务器的角色grant connect to lihua;10.授予使用表空间的角色grant resource to lihua with grant option;--该用户也有授权的权限11.授予操作表的权限grant select,insert on user_tbl to scott;--当前用户grant delete,update on er_tbl to scott;--系统管理员二.SQL查询和SQL函数1.SQl支持的命令:数据定义语言(DDL):create,alter,drop数据操纵语言(DML):insert,delete,update,select数据控制语言(DCL):grant,revoke事务控制语言(TCL):commit,savepoint,rollback2.Oracle数据类型字符,数值,日期,RAW,LOB字符型char:1-2000字节的定长字符varchar2:1-4000字节的变长字符long:2GB的变长字符注意:一个表中最多可有一列为long型Long列不能定义唯一约束或主键约束long列上不能创建索引过程或存储过程不能接受long类型的参数。
数值型number:最高精度38位日期时间型date:精确到sstimestamp:秒值精确到小数点后6位函数sysdate,systimestamp返回系统当前日期,时间和时区。
更改时间的显示alter session set nls_date_language=’american’;alter session set nls_date_format=’yyyy-mm-dd’;Oracle中的伪列像一个表列,但没有存储在表中伪列可以查询,但不能插入、更新和修改它们的值常用的伪列:rowid和rownumrowid:表中行的存储地址,可唯一标示数据库中的某一行,可以使用该列快速定位表中的行。
rownum:查询返回结果集中的行的序号,可以使用它来限制查询返回的行数。
3.数据定义语言用于操作表的命令create tablealter tabletruncate tabledrop table修改表的命令alter table stu_table rename to stu_tbl;--修改表名alter table stu_tbl rename column stu_sex to sex;--修改列名alter table stu_tbl add (stu_age number);--添加新列alter table stu_tbl drop(sex);--删除列alter table stu_tbl modify(stu_sex varchar2(2));--更改列的数据类型alter table stu_tbl add constraint pk_stu_tbl primary key(id);--添加约束4.数据操纵语言select,update,delete,insert利用现有的表创建表create table stu_tbl_log as select id,stu_name,stu_age from stu_tbl;-- 选择无重复的行select distinct stu_name from stu_tbl;--插入来自其他表中的记录insert into stu_tbl_log select id,stu_name,stu_age from stu_tbl;5.数据控制语言grant,revoke6.事务控制语言commit,savepoint,rollback7.SQL操作符算术操作符:☹+-*/比较操作符:☹=,!=,<>,>,<,>=,<=,between-and,in,like,is null等逻辑操作符:☹and,or,not集合操作符:☹union,union all,intersect,minus连接操作符:☹||示例中stu_tbl_log中的数据如下:ID STU_NAME STU_AGE---------- -------------------- ----------1000 李华 201001 accp 201003 nimda 3stu_tbl中的数据如下:ID STU_NAME ST STU_AGE---------- -------------------- -- ----------1000 李华男 201001 accp 男 201002 admin 男 30示例:select (3+2)/2 from dual;--算术操作符,结果:2.5select * from stu_tbl where stu_age>=20;--比较操作符select * from stu_tbl where stu_name like '%a%';--比较操作符:likeselect * from stu_tbl where stu_name like 'a___';--比较操作符:like select * from stu_tbl where stu_age in(20,30);--比较操作符:inselect * from stu_tbl where stu_age between 20 and 30;--比较操作符:between select stu_name from stu_tbl union allselect stu_name from stu_tbl_log;--集合操作符:union all,测试结果具体如下:STU_NAME-----------李华accpadmin李华accpnimda已选择6行。
select stu_name from stu_tbl unionselect stu_name from stu_tbl_log;--集合操作符:union,测试结果具体如下:STU_NAME---------accpadminnimda李华select stu_name from stu_tbl intersectselect stu_name from stu_tbl_log;--集合操作符:intersect,测试结具体如下:STU_NAME----------accp李华select stu_name from stu_tbl minusselect stu_name from stu_tbl_log;--集合操作符:minus,测试结果如下:STU_NAME----------Admin从中可以看出:minus是获取第一张表独有的数据intersect是获取两张表中都有的数据union是整合两张表的数据,都有的只显示一次union all是纯粹的两张表数据整合select id,stu_name||' '||stu_sex as name_sex,stu_agefrom stu_tbl;--连接操作符||,测试结果具体如下:ID NAME_SEX STU_AGE---------- ----------------------- ----------1000 李华男 201001 accp 男 201002 admin 男 308.SQL函数单行函数:从表中查询的每一行只返回一个值,可出现在select子句,where子句中日期函数数字函数字符函数转换函数:ToChar(),ToDate(),ToNumber()其他函数:Nvl(exp1,exp2):表达式一为null时,返回表达式二Nvl2(exp1,exp2,exp3):表达式一为null时返回表达式三,否则返回表达式二Nullif(exp1,exp2):两表达式相等时,返回null,否则返回表达式一分组函数:基于一组行来返回Avg,Min,Max,Sum,CountGroup by,having分析函数Row_number,rank,dense_rank示例:select er_name,sum(oi.order_num*oi.order_price) as total,row_number() over (order by sum(oi.order_num*oi.order_price) desc) as sort from order_item_tbloi,user_tbl u,order_tbl o where oi.order_id = o.id and er_id = u.id group by er_name;三.锁和数据库对象1.锁:数据库用来控制共享资源并发访问的机制。
锁的类型:行级锁,表级锁行级锁:对正在被修改的行进行锁定。
行级锁也被称之为排他锁。
在使用下列语句时,Oracle会自动应用行级锁:insert,update,delete,select…… for updateselect……for update允许用户一次锁定多条记录进行更新。
使用commit or rollback释放锁。