ORACLE的基础用法

ORACLE的基础用法

转 自:http://blog.163.com/kelly_19831017/blog/static/6196787020083133348873/

sqlplus sys/oracle as sysdba  //以SYS用户名进入数据库
startup  //启动数据库
shutdown immediate  //正常关闭数据库(迫使每个用户执行完SQL语句时断开连接)
shutdown abort //强行关闭数据库(不建议使用)
help index //查询sqlplus所有功能
set sqlblanklines on //支持空格行
select * from dept where deptno=&tt //tt为变量,要给tt副值
list //查看在缓冲区查看数据(l是简写)
l2 4 //查看在缓冲区查看数据(第二行到第4行)
change //修改
c/n/m //把n改成m
/ //执行缓冲区的命令
del 4 //删除缓冲区第行
a from dept //追加缓冲区的信息
save c:\text.txt //保存缓冲区的信息到C盘下text.txt
@c:\text.txt  //对记事本的信息保存缓冲区里头
get c:\text.txt //获得脚本内容
edit //用记事本打开缓冲区的内容
col deptno heading "编号" //把deptno改成编号
desc dept //查看数据库对象dept表字段类型
col dname format a10 //取10个字符格式华输出
connect //改变连接用户
break on dept //对重复的数据只显示一条
comp count label "别名" of 字段 on 字段  //对重复的字段进行记数


-------
create table abc  //创建表
alter table abc add c number  //向表abc追加一个字段
alter table abc drop column c  //向表abc删除一个字段
grant select on dept to tt    //授权dept这张表给TT用户
revoke select on dept from tt //收回使用这张表的权限
select to_char(sysdate,'yyyy-mm-dd') from dual //把日期转化为yyyy-mm-dd这种格式
select user from dual //查询当前用户
decode  //做统计用
---
create sequence identity
increment by 1
start with 1000
nomaxvalue        //创建自动增长列
---
insert into 表 select 字段 from 表  //从一张表插入多条数据到另外一张表
create table 表 as (select 字段 from 表)  //创建一张新表并且把另外一张表的数据插入进去
exp scott/oracle file=路径  //把scott这个空间的表备份到这个路径
imp scott/oracle ignore=true file=路径 //把这个路径下的文件导入到scott这个空间里面

---
declare
x number;  //定义一个变量
begin
x:=0;   //给变量赋值
loop    //循环
x:=x+1;  //记数
if x>=3 then exit; //当变量>=3时,跳出当前循环
end if;
end loop;
dbms_output.put_line('x='||x); //打印X的值
end;

---
while x<3 loop
x:=x+1;
end loop;

---
begin
for i in 1..5 loop  //循环i从1到5
dbms_output.put_line('x='||x);
end loop;
end;

---
begin
for i in reverse 1..5 loop  //循环i从1到5,倒序输出
dbms_output.put_line('x='||x);
end loop;
end;

---
declare num number;
begin
select ClassID into num from Class where id=1000;   //把查询出来的ClassID这个值放入num这个变量中
exception
when no_date_found then dbms_output.put_line('没有找到数据');
end;

---法器创建格式
create or replace trigger 法器名
after delete on 表名
for each row   //对每一行进行
begin
 ~~~~~~~~~~~~~~~``
end

----
raise_application_error(-2000,"错误内容")  //强行不准删除


---
alter user scott identified by aaa  //把scott用户的表空间密码改成aaa
alter user scott account lock  //把Scott表空间进行锁定
alter user scott account unlock  //把Scott表空间进行解除锁定
create user qqq identified by qqq123 default tablespace user //创建一个qqq,密码为的用qqq123户表空间, 默认表空间为qqq123
grant connect to qqq  //为qqq创建连接
with grant option //下放权限
grant all on scott.dept to tt //把所有权限给TT用户
grant execute on tt.proc01 to text //把使用存贮过程的权限给Test用户
grant create user to text //把创建用户的权限给text用户
grant drop user to text //把删除用户的权限给text用户
grant connect,resource to userBook  //把一般的权限受给这个用户
---
creare tablespace tabs 
datafile 'E:\Oracle\product\10.1.0\oradata\oracle\tabs.dbf' size 10M autoextend on;//创建一个tabs的表空间,默认的文件路径,大小为10M
grant unlimited tablespace ,dba to test  //给表空间授权

---
select constraint_name,constraint_type from user_constraints
where table_name='emp';


日期函数:
select * from XiangMuBiao where kaishishijian<=last_day(sysdate) and kaishishijian>=add_months(last_day(sysdate)+1,-1)  //在本月中的所有字段
Select last_day(sysdate) from dual;//一个月的最后一天
select add_months(last_day(sysdate)+1,-1) month from dual;//本月的第一天
select   to_date('20040401 122525','yyyymmdd   hh24miss') from dual;//转换为日期格式

posted @ 2010-10-20 14:21  ~笑  阅读(260)  评论(0编辑  收藏  举报