Oracle中创建和管理表详解

以下是对Oracle中的创建和管理表进行了详细的分析介绍,需要的朋友可以过来参考下
 

SQL> /*
SQL> 对于表的操作: 创建表,修改表(添加新的列,改变当前某些列,删除列),删除表
SQL> 创建表: create table(需要create table的权限)
SQL> 修改表: alter table tablename add/modify/drop
SQL> 删除表:drop table tablename
SQL> */
SQL> show user;
USER 为 "SCOTT"
SQL> --访问hr用户下的表
SQL> select * from hr.employees;
select * from hr.employees
                 *
第 1 行出现错误:
ORA-00942: 表或视图不存在
SQL> --测试defaul值
SQL> create table test1
  2  (tid number,
  3   tname varchar(20),
  4   hiredate date default sysdate);
表已创建。
SQL> insert into test1(tid,tname) values(1,'Mary');
已创建 1 行。
SQL> select * from test1;
       TID TNAME                HIREDATE                                                                               
---------- -------------------- --------------                                                                         
         1 Mary                 12-6月 -11                                                                             
SQL> --rowid rownum都是伪列
SQL> select rowid,rownum,empno from emp;
ROWID                  ROWNUM      EMPNO                                                                               
------------------ ---------- ----------                                                                               
AAANA2AAEAAAAAsAAT          1       1122                                                                               
AAANA2AAEAAAAAsAAO          2       1234                                                                               
AAANA2AAEAAAAAsAAP          3       1235                                                                               
AAANA2AAEAAAAAsAAQ          4       2222                                                                               
AAANA2AAEAAAAAsAAR          5       2345                                                                               
AAANA2AAEAAAAAsAAS          6       2346                                                                               
AAANA2AAEAAAAAsAAA          7       7369                                                                               
AAANA2AAEAAAAAsAAB          8       7499                                                                               
AAANA2AAEAAAAAsAAC          9       7521                                                                               
AAANA2AAEAAAAAsAAD         10       7566                                                                               
AAANA2AAEAAAAAsAAE         11       7654                                                                               
ROWID                  ROWNUM      EMPNO                                                                               
------------------ ---------- ----------                                                                               
AAANA2AAEAAAAAsAAF         12       7698                                                                               
AAANA2AAEAAAAAsAAG         13       7782                                                                               
AAANA2AAEAAAAAsAAH         14       7788                                                                               
AAANA2AAEAAAAAsAAI         15       7839                                                                               
AAANA2AAEAAAAAsAAJ         16       7844                                                                               
AAANA2AAEAAAAAsAAK         17       7876                                                                               
AAANA2AAEAAAAAsAAL         18       7900                                                                               
AAANA2AAEAAAAAsAAM         19       7902                                                                               
AAANA2AAEAAAAAsAAN         20       7934                                                                               
已选择20行。
SQL> --rowid:oracle维护一个地址,该地址指向了该行在硬盘上实际存储的位置
SQL> --关于varchar2和char
SQL> create table testchar
  2  ( c char(5),
  3    v varchar(5));
表已创建。
SQL> insert into testchar values('a','b');
已创建 1 行。
SQL> select * from testchar;
C     V                                                                                                                
----- -----                                                                                                            
a     b                                                                                                                
SQL> select concat(c,'#'),concat(v,'#') from testchar;
CONCAT CONCAT                                                                                                          
------ ------                                                                                                          
a    # b#                                                                                                              
SQL> --添加新列
SQL> alter table testchar
  2  add  hiredate date;
表已更改。
SQL> desc testchar;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 C                                                                          CHAR(5)
 V                                                                          VARCHAR2(5)
 HIREDATE                                                                   DATE
SQL> --修改表
SQL> alter table testchar
  2  modify c char(10);
表已更改。
SQL> desc testchar;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 C                                                                          CHAR(10)
 V                                                                          VARCHAR2(5)
 HIREDATE                                                                   DATE
SQL> --删除列
SQL> alter table testchar
  2  drop hiredate;
drop hiredate
     *
第 2 行出现错误:
ORA-00905: 缺失关键字
SQL> ed
已写入 file afiedt.buf
  1  alter table testchar
  2* drop column hiredate
SQL> /
表已更改。
SQL> desc testchar;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 C                                                                          CHAR(10)
 V                                                                          VARCHAR2(5)
SQL> host cls
SQL> --删除表
SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID                                                                      
------------------------------ ------- ----------                                                                      
DEPT                           TABLE                                                                                   
EMP                            TABLE                                                                                   
BONUS                          TABLE                                                                                   
SALGRADE                       TABLE                                                                                   
EMP10                          TABLE                                                                                   
EMP101                         TABLE                                                                                   
TEST1                          TABLE                                                                                   
BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 TABLE                                                                                   
TESTDELETE                     TABLE                                                                                   
TESTCHAR                       TABLE                                                                                   
已选择10行。
SQL> drop table testdelete;
表已删除。
SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID                                                                      
------------------------------ ------- ----------                                                                      
DEPT                           TABLE                                                                                   
EMP                            TABLE                                                                                   
BONUS                          TABLE                                                                                   
SALGRADE                       TABLE                                                                                   
EMP10                          TABLE                                                                                   
EMP101                         TABLE                                                                                   
TEST1                          TABLE                                                                                   
BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 TABLE                                                                                   
TESTCHAR                       TABLE                                                                                   
BIN$aJrS9iffT4O1GcD0H3fepg==$0 TABLE                                                                                   
已选择10行。
SQL> --使用purge参数彻底删除表
SQL> drop table test1 purge;
表已删除。
SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID                                                                      
------------------------------ ------- ----------                                                                      
DEPT                           TABLE                                                                                   
EMP                            TABLE                                                                                   
BONUS                          TABLE                                                                                   
SALGRADE                       TABLE                                                                                   
EMP10                          TABLE                                                                                   
EMP101                         TABLE                                                                                   
BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 TABLE                                                                                   
TESTCHAR                       TABLE                                                                                   
BIN$aJrS9iffT4O1GcD0H3fepg==$0 TABLE                                                                                   
已选择9行。
SQL> --oracle的回收站
SQL> --查看回收站
SQL> show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME                                                 
---------------- ------------------------------ ------------ -------------------                                       
TESTDELETE       BIN$aJrS9iffT4O1GcD0H3fepg==$0 TABLE        2011-06-12:15:43:34                                       
TESTDELETE       BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 TABLE        2011-06-12:14:51:43                                       
SQL> --清空回收站
SQL> purge recyclebin;
回收站已清空。
SQL> show recyclebin;
SQL> --关于约束:
SQL> --创建一个表,包含所有约束
SQL> create table myuser
  2  ( userID number constraint pk primary key,
  3    username varchar2(20) constraint c_name not null,
  4    gender   varchar2(2)  constraint c_gender check (gender in ('男','女')),
  5    email    varchar2(20) constraint c_email1 not null
  6                          constraint c_email2 unique
  7    deptno   number constraint fk refereneces dept(deptno)
  8  );
  deptno   number constraint fk refereneces dept(deptno)
  *
第 7 行出现错误:
ORA-00907: 缺失右括号
SQL>   create table myuser
  2  ( userID number constraint pk primary key,
  3    username varchar2(20) constraint c_name not null,
  4    gender   varchar2(2)  constraint c_gender check (gender in ('男','女')),
  5    email    varchar2(20) constraint c_email1 not null
  6                          constraint c_email2 unique,
  7    deptno   number constraint fk refereneces dept(deptno)
  8  );
  deptno   number constraint fk refereneces dept(deptno)
                  *
第 7 行出现错误:
ORA-02253: 此处不允许约束条件说明
SQL> ed
已写入 file afiedt.buf
  1    create table myuser
  2  ( userID number constraint pk primary key,
  3    username varchar2(20) constraint c_name not null,
  4    gender   varchar2(2)  constraint c_gender check (gender in ('男','女')),
  5    email    varchar2(20) constraint c_email1 not null
  6                          constraint c_email2 unique,
  7    deptno   number constraint fk references dept(deptno)
  8* )
SQL> /
表已创建。
SQL> desc myuser;
 名称                                                              是否为空? 类型
 ----------------------------------------------------------------- -------- --------------------------------------------
 USERID                                                            NOT NULL NUMBER
 USERNAME                                                          NOT NULL VARCHAR2(20)
 GENDER                                                                     VARCHAR2(2)
 EMAIL                                                             NOT NULL VARCHAR2(20)
 DEPTNO                                                                     NUMBER
SQL> insert into myuser values(1,'Tom','男','ddd@126.com',10);
已创建 1 行。
SQL> insert into myuser values(1,'Tom','男','ddd@126.com',10);
insert into myuser values(1,'Tom','男','ddd@126.com',10)
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (SCOTT.PK)
SQL> insert into myuser values(2,'Tom','啊','ddd@126.coddm',10);
insert into myuser values(2,'Tom','啊','ddd@126.coddm',10)
*
第 1 行出现错误:
ORA-02290: 违反检查约束条件 (SCOTT.C_GENDER)
SQL> --触发器也可以检查数据的正确与否
SQL> spool off

时间: 2024-11-15 19:52:23

Oracle中创建和管理表详解的相关文章

Oracle中创建和管理表详解_oracle

SQL> /*SQL> 对于表的操作: 创建表,修改表(添加新的列,改变当前某些列,删除列),删除表SQL> 创建表: create table(需要create table的权限)SQL> 修改表: alter table tablename add/modify/dropSQL> 删除表:drop table tablenameSQL> */SQL> show user;USER 为 "SCOTT"SQL> --访问hr用户下的表SQ

oracle中exp,imp的使用详解_oracle

基本语法和实例:      1.EXP:       有三种主要的方式(完全.用户.表)       1.完全:           EXP SYSTEM/MANAGER BUFFER=64000 FILE=C:\FULL.DMP FULL=Y           如果要执行完全导出,必须具有特殊的权限       2.用户模式:           EXP SONIC/SONIC    BUFFER=64000 FILE=C:\SONIC.DMP OWNER=SONIC           这

Spring中的事务管理实例详解_java

本文实例讲述了Spring中的事务管理.分享给大家供大家参考.具体分析如下: 事务简介: 事务管理是企业级应用程序开发中必不可少的技术,用来确保数据的完整性和一致性 事务就是一系列的动作,它们被当作一个单独的工作单元.这些动作要么全部完成,要么全部不起作用 事务的四个关键属性(ACID) ① 原子性(atomicity):事务室一个原子操作,有一系列动作组成.事务的原子性确保动作要么全部完成,要么完全不起作用 ② 一致性(consistency):一旦所有事务动作完成,事务就被提交.数据和资源就

Oracle中TO_DATE函数使用方法详解

一.在使用Oracle的to_date函数来做日期转换时,很多Java程序员也许会直接的采用"yyyy-MM-dd HH:mm:ss"的格式作为格式进行转换,但是在Oracle中会引起错误:"ORA 01810 格式代码出现两次".如: select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') from dual;  原因是SQL中不区分大小写,MM和mm被认为是相同的格式代码,所以Oracle的SQ

oracle中lpad函数的用法详解_oracle

oracle中lpad的用法 pad翻译:填充 lpad函数,在字符串的左侧添加指定字符串,用法: www.jb51.net lpad(String ,截取长度,添加的字符串). 说是添加字符串也不准确,比较准确的说法是对String进行截取字符串, 如果截取长度大于String的长度,则在 String的左侧添加字符串进行填补,如果第三个参数未指定,则用空格进行填补. 例如: select lpad('test',10) from dual; 将返回" test" select lp

如何在Oracle中使用Java存储过程(详解)

其实,这篇短文,我早就应该写了.因为,Java存储过程今后在各大数据库厂商中越来越流行,功能也越来越强大.这里以Oracle为例,介绍一下java存储过程的具体用法. 一.如何创建java存储过程? 通常有三种方法来创建java存储过程. 1.使用oracle的sql语句来创建: e.g. 使用create or replace and compile java source named "<name>" as 后边跟上java源程序.要求类的方法必须是public sta

Oracle中SPOOL命令使用方法详解

举例:test.sh  代码如下 复制代码 #! /bin/sh sqlplus -s scott/tiger<<EOF  set head off  set linesize 20000  set echo off  set feedback off  set pagesize 0  set termout off  set trimout on  set trimspool on   spool /home/test.txt    select empno || '|' || ename

oracle中复杂sql查询语句详解 (1/4)

1.查询语句的使用 使用 select语句和子查询(subquery)可以从一个或多个表,视图,实体试图中返回数据.   1.1相关子查询 可以将子查询(as subquery)或in或exists当成where的一个条件的一部分,这样的查询称为子查询   .where中可以包含一个select语句的子查询   .where中可以包含in,exists语句   .最多可以嵌套16层   .层次过多会影响性能   [例]简单子查询实例   查询是否有的专家既以研究所的名义来申请基金项目,又以大学系

Oracle临时表的用法及意义详解

Oracle临时表可以说是提高数据库处理性能的好方法,在没有必要存储时,只存储在Oracle临时表空间中.希望本文能对大家有所帮助. 1 .前言 目前所有使用 Oracle 作为数据库支撑平台的应用,大部分数据量比较庞大的系统,即表的数据量一般情况下都是在百万级以上的数据量. 当然在 Oracle 中创建分区是一种不错的选择,但是当你发现你的应用有多张表关联的时候,并且这些表大部分都是比较庞大,而你关联的时候发现其中的某一张或者某几张表关联之后得到的结果集非常小并且查询得到这个结果集的速度非常快