oracle常用命令

来源:互联网 发布:4g十网络手机 编辑:程序博客网 时间:2024/05/17 02:08

Oracle数据类型:

  Create table test1(name char(10),sex char(1));

  Insert into test1 values(‘tomcatt北京’,’f’);

  Create table test2(name nchar(10),sex nchar(1));

  Insert into test2 values(‘tomcatt北京’,’男’);

  删除表 drop table 表名;

  Create table test3(name varchar2(10),sex varchar2(2));

  Insert into test3 values(‘tomcatt北京’,’f’);//插入值过大

  Insert into test3 values(‘tomcat北京’,’f’);

  Create table test4(name varchar2(10),age number(3),salary number(8,2));

  Create table test5(name varchar2(10),birth date);

  Insert into test5 values(‘Tom’,’28-2月-08’);

  Insert into test5 values(‘Allen’,sysdate);

  DDL:

  创建表

  create table scott.test6(

  eid number(10),

  name varchar2(20),

  hiredate date default sysdate,

  salary number(8,2) default 0

  )

  插入数据时若没有指定hiredate,salary的话则会取默认值

  数据字典:

  Dba-所有方案包含的对象信息

  All-用户可以访问的对象信息

  User-用户方案的对象信息

  Select * from user_tables;

  Select * from all_tables;


  约束:

  域完整性约束:not null check

  实体完整性约束:unique primary key

  参照完整性约束:foreign key

  视图:

  Create or replace view v1(eid,name,salary) as select empno,ename,sal from emp where deptno = 30;

  序列:sequence

  Create sequence mysequence1 increment by 1 start with 1 nomaxvalue nocycle;

  Insert into test values(mysequence1.nextval,’tom’);

  Create sequence student_sequence start with 1 increment by 1;

  Insert into student values(student_sequence.nextval,’john’);

  表间数据拷贝:

  Insert into dept1(id,name) select deptno,dname from dept;

  实例(创建表 ID字段自增):

  --create table test2(id char(10) primary key not null, name char(10));

  --create sequence test2_sequence increment by 1 start with 1 nomaxvalue nocycle;

  --insert into test2 values(test2_sequence.nextval,'john');

  --insert into test2 values(test2_sequence.nextval,'allen');

  --insert into test2 values(test2_sequence.nextval,'candy');

  --insert into test2 values(test2_sequence.nextval,'aaaa');

  --insert into test2 values(test2_sequence.nextval,'bbbbb');

  --insert into test2 values(test2_sequence.nextval,'cccccc');

  --insert into test2 values(test2_sequence.nextval,'ddddd');

  --insert into test2 values(test2_sequence.nextval,'eeeee');

  --insert into test2 values(test2_sequence.nextval,'ggggg');

  --insert into test2 values(test2_sequence.nextval,'jowwwwhn');

  --insert into test2 values(test2_sequence.nextval,'aaaadd');

  --insert into test2 values(test2_sequence.nextval,'ggghhh');

  --insert into test2 values(test2_sequence.nextval,'eeettt');

  --insert into test2 values(test2_sequence.nextval,'wwwttt');

  select * from test2;


  查看表结构

  EDITDATA 表名;

  修改表字段:

  Alter table 表名 modify(字段名 类型 约束);

  alter table test modify (addd varchar2(10) null);

  alter table 表名 add(字段名 类型 约束);

  alter table test add(age varchar2(5));

  1.登陆系统用户

  sqlplus 然后输入系统用户名和密码

  登陆别的用户

  conn 用户名/密码;

  2.创建表空间

  create tablespace 空间名

  datafile 'c:\空间名' size 15M --表空间的存放路径,初始值为15M

  autoExtend on next 10M --空间的自动增长的值是10M

  permanent online; --永久使用

  3.创建用户

  create user shi --创建用户名为shi

  identified by scj --创建密码为scj

  default tablespace 表空间名 --默认表空间名

  temporary tablespace temp --临时表空间为temp

  profile default --受profile文件的限制

  quota unlimited on 表空间名; --在表空间下面建表不受限制

  4.创建角色

  create role 角色名 identified by 密码;

  5.给角色授权

  grant create session to 角色名;--给角色授予创建会话的权限

  grant 角色名 to 用户名; --把角色授予用户

  6.给用户授予权限

  grant connect,resource to shi;--给shi用户授予所有权限

  Grant dba to shi;-给shi 用户授予DBA权限

  grant create table to shi; --给shi用户授予创建表的权限

  7.察看当前用户下的所有表
select table_name from user_tables;   

  8.察看当前用户下的 表空间
select tablespace_name from user_tablespaces; 

  9.察看所有用户名称命令 必须用sys as sysdba登陆
select username from dba_users;


  10.创建表

  create table 表名

  (

  id int not null,

  name varchar2(20) not null

  )tablespace 表空间名 --所属的表空间

  storage

  (

  initial 64K --表的初始值

  minextents 1 --最小扩展值

  maxextents unlimited --最大扩展值

  );

  11.--为usrs表添加主键和索引

  alter table users

  add constraint pk primary key (ID);

  12.为已经创建users表添加外键

  alter table users

  add constraint fk_roleid foreign key (roleid)

  references role(role_id) on delete cascad; --下边写主表的列

  on delete cascad是创建级联

  13.把两个列连接起来

  select concat(name,id) from 表名; --把name和id连接起来

  14.截取字符串

  select column(name,'李') from 表名; --把name中的‘李’去掉

  15.运行事务之前必须写

  set serveroutput on; --打开输入输出(不写的话,打印不出信息)

  16.while的应用

  declare --声明部分

  ccc number:=1; --复职

  a number:=0;

  begin --事务的开始

  while ccc<=100 loop --循环

  if((ccc mod 3)=0) then --条件

  dbms_output.put_line(ccc||',');    --打印显示

  a:=a+ccc;

  end if; --结束if


  end loop; --结束循环

  dbms_output.put_line(a);

  end; --结束事务

  /

  17.select into 的用法 --只能处理一行结果集

  declare

  name varchar(30);

  begin

  select username into name

  from users

  where id=2;

  dbms_output.put_line('姓名为:'||name);

  end;

  /

  18.利用%rowtype属性可以在运行时方便的声明记录变量和其他结构

  Set serveroutput on;

  Declare

  utype users%rowtype;

  Begin

  Select * into utype from users where id=20;

  Dbms_output.put_line('姓名'|| utype.username);

  Dbms_output.put_line('生日'|| utype.brithday);

  end;

  / --%rowtype想当于复制一个表

  19.游标的定义和使用

  Declare

  Cursor ucur is select * from users; --声明游标

  Us users%rowtype;--定义与游标想匹配的变量

  Begin

  Open ucur;--打开游标


  Fetch ucur into us;

  While ucur %found loop --使用循环遍历游标的查询结果

  Dbms_output.put_line('姓名:'||us.username||'生日'||us.brithday);

  Fetch ucur into us;

  End loop;

  Close ucur; --关闭游标

  End;

  =======================================

  %found在前一条的fetch语句至少对应数据库的一行时,%found属性值为true,否则为false;

  % notfound 在前一条fetch语句没有对应的数据库行时,%notfound属性值为true,否则为false;

  %isopen 在游标打开时%isopen属性值为true;否则为false;

  %rowcount显示迄今为止从显示游标中取出的行数

  20.

  删除

  drop tablespace 空间名 including contents; --删除表空间和里面的内容

  drop table 表名 --删除表

  drop user 用户名 --删除用户

  查看有哪些表空间:

  Select tablespace_name from dba_tablespaces;

  查看某个表属于哪个表空间:

  Select tablespace_name from tabs where table_name=表名;

  在指定的表空间创建表:

  create   table(...)   tablespace   users;

  查询前N条数据:

  select * from 表名 where rownum<=N;

  Oracle中创建一个数据库:

  打开Database Configuration Assistant,可以通过视图创建数据库。创建成功后,系统服务里面会多一个OracleServer 数据库名 的服务项。

  查看所有表空间大小:

  select * from dba_free_space;

  问题:user lacks create session privilege logon denied

  grant create session to the_user;

  删除用户:

  Drop user 用户名 cascade;

  导入数据库对象:

  IMP <用户名>/<口令>@<数据库别名> file=<导出数据文件名> fromuser=<导出用户> touser=<导出用户>

  如:IMP jcyy/jcyy@jcyy file=d:\jcyy.dmp fromuser=jcyy touser=jcyy

  导出数据库对象:

  EXP <用户名>/<口令>@<数据库别名> file=<导出文件名> owner=<导出用户>

  如:EXP jcyy/jcyy@jcyy file=d:\lo.dmp owner=jcyy


原创粉丝点击