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

(0)

相关推荐

  • 六分钟学会创建Oracle表空间的实现步骤

    经过长时间学习创建Oracle表空间,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西. 1.先查询空闲空间 复制代码 代码如下: select tablespace_name,file_id,block_id,bytes,blocks from dba_free_space; 2.增加Oracle表空间 先查询数据文件名称.大小和路径的信息,语句如下: 复制代码 代码如下: select tablespace_name,file_id,bytes,file_name fr

  • Oracle创建主键自增表(sql语句实现)及触发器应用

    1.创建表 复制代码 代码如下: createtableTest_Increase( useridnumber(10)NOTNULLprimarykey,/*主键,自动增加*/ usernamevarchar2(20) ); 2.创建自动增长序列 复制代码 代码如下: CREATESEQUENCETestIncrease_Sequence INCREMENTBY1--每次加几个 STARTWITH1--从1开始计数 NOMAXVALUE--不设置最大值,设置最大值:maxvalue9999 NO

  • oracle 创建表空间详细介绍

    注意点: 1.如果在PL/SQL 等工具里打开的话,直接修改下面的代码中[斜体加粗部分]执行 2.确保路径存在,比如[D:\oracle\oradata\Oracle9i\]也就是你要保存文件的路径存在 /*分为四步 */ /*第1步:创建临时表空间 */ 复制代码 代码如下: create temporary tablespace user_temp tempfile 'D:\oracle\oradata\Oracle9i\user_temp.dbf' size 50m autoextend

  • Oracle数据库如何创建第一张表

    对于Oracle数据库的使用,创建一张符合自己需求的表是至关重要的,下面就让我们进行创建第一张属于自己的表 创建表时有两种种方式: (1)在cmd里边更具需要进行创建 (2)在sql developer中进行创建,而对于在可视化界面sqldeveloper中创建时,也有两种方式,即一种是使用命令直接进行创建,另外一种是使用可视化界面按钮进行点击创建 下面就是创建的具体过程: (1)首先我们进行数据的连接,在SQLdeveloper中进行操作,在使用sqldeveloper时需要下载sqldeve

  • oracle创建表的方法和一些常用命令

    1.主键和外键主键:关系型数据库中的一条记录中有若干个属性,若其中的某一个属性组(注意是组,可以是一个,也可以是多个)能唯一标识一条记录,那么该属性组就是主键外键:关系型数据库表中的一列或者某几列的组合,它的值与另外一张表的某一列或者某几列相匹配,且为另一张表的主键(即这张表的某一列或某几列是另外一张表的主键,称这一列或几列为另外一张表的外键) 注1:一张表主键只能有一个,可以有多个外键以及唯一索引 注2:Oracle数据库共有5个约束:主键.外键.非空.唯一.条件非空:这个列的值不能为空(NO

  • oracle 创建表空间步骤代码

    /*第1步:创建临时表空间  */create temporary tablespace user_temp  tempfile 'D:\oracle\oradata\Oracle9i\user_temp.dbf' size 50m  autoextend on  next 50m maxsize 20480m  extent management local; /*第2步:创建数据表空间  */create tablespace user_data  logging  datafile 'D:

  • oracle存储过程创建表分区实例

    用存储过程创建数据表:创建时注意必须添加authid current_user,如果创建的表已存在,存储过程继续执行,但如不不加此关键语句,存储过程将出现异常,这个语句相当于赋权限.例1创建语句如下: 复制代码 代码如下: create or replaceprocedure sp_create_mnl(i_id varchar2) authid current_user  as   /********************************* 名称:sp_create_mnl 功能描述

  • Oracle 创建用户及数据表的方法

    一.概念 1. 数据库 (Database) 什么是数据库? 数据库是依照某种数据模型组织起来并存放二级存储器中的数据集合.这种数据集合具有如下特点:尽可能不重复,以最优方式为某个特定组织的多种应用服务,其数据结构独立于使用它的应用程序,对数据的增.删.改和检索由统一软件进行管理和控制.从发展的历史看,数据库是数据管理的高级阶段,它是由文件管理系统发展起来的. 什么是数据库系统? 数据库系统是一个实际可运行的存储.维护和应用系统提供数据的软件系统,是存储介质.处理对象和管理系统的集合体.它通常由

  • Oracle 查看表空间的大小及使用情况sql语句

    SQL1: 复制代码 代码如下: --1.查看表空间的名称及大小 SELECT t.tablespace_name, round(SUM(bytes / (1024 * 1024)), 0) ts_size FROM dba_tablespaces t, dba_data_files d WHERE t.tablespace_name = d.tablespace_name GROUP BY t.tablespace_name; --2.查看表空间物理文件的名称及大小 SELECT tables

  • shell脚本操作oracle删除表空间、创建表空间、删除用户

    oracle下表空间的导出,用户的删除,表空间删除,用户新建,表空间新建,数据导入的shell使用非oracle用户执行该脚本参数说名$1:base表空间的用户名$2:同步表空间的用户名使用场景测试用,base表空间用于升级建立一些固化数据.同步表空间用于测试用,每次去和base表空间拉平数据 复制代码 代码如下: #!/bin/shoraclehome=$ORACLE_HOMEecho $oraclehomelocaldir="/oracle/data"echo $localdir#

随机推荐