首页  >  文章  >  数据库  >  Oracle中创建和管理表详解

Oracle中创建和管理表详解

WBOY
WBOY原创
2016-06-07 14:53:251033浏览

以下是对Oracle中的创建和管理表进行了详细的分析介绍,需要的朋友可以过来参考下 SQL /* SQL 对于表的操作: 创建表,修改表(添加新的列,改变当前某些列,删除列),删除表 SQL 创建表: create table(需要create table的权限) SQL 修改表: alter table tabl

以下是对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                                                                               
AAANA2AAAEAAAAAAsAAO 2 1234
AAANA2AAAEAAAAAAsAAP 3 1235
AAANA2AAAEAAAAAAsAAQ 4 2222
AAANA2AAAEAAAAAAsAAR 5 2345
AAANA2AAAEAAAAAAsAAS 6 2346
AAANA2AAAEAAAAAAsAAA 7 7369
AAANA2AAAEAAAAAAsAAB 8 7499
AAANA2AAAEAAAAAAsAAC 9 7521
AAANA2AAAEAAAAAAsAAD 10 7566
AAANA2AAAEAAAAAAsAAE 11 7654
ROWID ROWNUM EMPNO
-------------------------------------------------- -------------------------------------------------- ----------------------------------- AAANA2AAAEAAAAAAsAAF 12 7698
AAANA2AAAEAAAAAAsAAG 13 7782
AAANA2AAAEAAAAAAsAAH 14 7788
AAANA2AAAEAAAAAAsAAI 15 7839
AAANA2AAAEAAAAAAsAAJ 16 7844
AAANA2AAAEAAAAAAsAAK 17 7876
AAANA2AAAEAAAAAAsAAL 18 7900
AAANA2AAAEAAAAAAsAAM 19 7902
AAANA2AAAEAAAAAAsAAN 20 7934
已电影20行。
SQL> --rowid:oracle维护单一地址,该地址指向硬盘实际存储位置的位置 SQL> -- 可以varchar2和char
SQL>创建表 testchar
  2 (c char(5),
  3    v varchar(5));

SQL>插入 testchar 值('a','b');
已剧情1行。
SQL>从测试字符中选择*; 电压V ----- -----                                                            a                                                                                                            
SQL> select concat(c,'#'),concat(v,'#') from testchar;
CONCAT CONCAT                                                                                                                  
------ ------                                                                                                                  
A#B#
SQL> -- 添加新列
SQL>更改表测试字符
  2 添加雇佣日期;
表已更改。
SQL> desc 测试字符;
 名称                                                              是否为空? 类型
 -------------------------------------------------- --------------- -------- --------------------------- -----------------
 C                                                                         CHAR(5)
 V                                                                         VARCHAR2(5)
 聘用日期                                                                  日期
SQL> --修改表
SQL>更改表测试字符
  2  修改 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                                                                      
------------------------------ ------- ----------                                                                      
部门                           表                                                                                   
电磁脉冲                            表                                                                                    
奖金                          表                                                                                    
萨尔格德                       表                                                                                            
EMP10                          表                                                                                    
EMP101                         表                                                                                    
测试1                          表                                                                                    
BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 TABLE                                                                                   
TESTDELETE                     TABLE                                                                                   
TESTCHAR                       TABLE                                                                                   
已选择10行。
SQL> drop table testdelete;
表已删除。
SQL> select * from tab;
TNAME                          TABTYPE  CLUSTERID                                                                      
------------------------------ ------- ----------                                                                      
部门                           表                                                                                   
电磁脉冲                            表                                                                                    
奖金                          表                                                                                    
萨尔格德                       表                                                                                            
EMP10                          表                                                                                    
EMP101                         表                                                                                    
测试1                          表                                                                                    
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                          表                                                                                    
EMP101                         表                                                                                    
BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 表                                                                                    
测试表                        表                                                                                           
BIN$aJrS9iffT4O1GcD0H3fepg==$0 表                                                                                  
选择9行。
SQL> --oracle的恢复站
SQL> --查看回收站
SQL>显示回收站;
原名称    回收站名称                对象类型  掉落时间                                                 
---------------- ------------------------------------------ ---- -------- -------------------                                       
测试删除       BIN$aJrS9iffT4O1GcD0H3fepg==$0 表        2011-06-12:15:43:34                                       
测试删除       BIN$gNM24ey8RKW0vjhtZ7ZFsA==$0 表        2011-06-12:14:51:43                                       
SQL> --清空回收站
SQL>清除回收站;
回收站已清空。
SQL>显示回收站;
SQL> --关于约束:
SQL> --创建一个表,包含所有约束
SQL>创建表 myuser
  2 (用户ID号约束pk主键,
  3    用户名 varchar2(20) 约束 c_name 不为空,
  4    性别   varchar2(2)  约束 c_gender 检查(性别 in ('男','女')),
  5    电子邮件    varchar2(20) 约束 c_email1 不为空
  6                          约束 c_email2 唯一
  7    deptno   数字约束 fk 引用 dept(deptno)
  8  );
  deptno   数字约束 fk 引用 dept(deptno)
  *
第 7 行出现错误:
ORA-00907: 短路右
SQL>   创建表 myuser
  2 (用户ID号约束pk主键,
  3    用户名 varchar2(20) 约束 c_name 不为空,
  4    性别   varchar2(2)  约束 c_gender 检查(性别 in ('男','女')),
  5    电子邮件    varchar2(20) 约束 c_email1 不为空
  6                          约束 c_email2 唯一,
  7    deptno   数字约束 fk 引用 dept(deptno)
  8  );
  deptno   数字约束 fk 引用 dept(deptno)
                  *
第 7 行出现错误:
ORA-02253: 此处不允许约束条件说明
SQL>编辑
已写入文件 afiedt.buf
  1    创建表 myuser
  2 (用户ID号约束pk主键,
  3    用户名 varchar2(20) 约束 c_name 不为空,
  4    性别   varchar2(2)  约束 c_gender 检查(性别 in ('男','女')),
  5    电子邮件    varchar2(20) 约束 c_email1 不为空
  6                          约束 c_email2 唯一,
  7    deptno   数字约束 fk 引用 dept(deptno)
  8* )
SQL> /
表正在创建。 SQL> desc myuser;
 名称                                                              是否为空? 类型
 -------------------------------------------------- --------------- -------- --------------------------- -----------------
 用户ID                                                           非空数字
 用户名                                                         NOT NULL VARCHAR2(20)
 性别                                                                    VARCHAR2(2)
 电子邮件                                                            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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn