程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Oracle數據庫 >> 關於Oracle數據庫 >> Oracle中創建和管理表詳解

Oracle中創建和管理表詳解

編輯:關於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','男','[email protected]',10);
    已創建 1 行。
    SQL> insert into myuser values(1,'Tom','男','[email protected]',10);
    insert into myuser values(1,'Tom','男','[email protected]',10)
    *
    第 1 行出現錯誤:
    ORA-00001: 違反唯一約束條件 (SCOTT.PK)
    SQL> insert into myuser values(2,'Tom','啊','[email protected]',10);
    insert into myuser values(2,'Tom','啊','[email protected]',10)
    *
    第 1 行出現錯誤:
    ORA-02290: 違反檢查約束條件 (SCOTT.C_GENDER)
    SQL> --觸發器也可以檢查數據的正確與否
    SQL> spool off

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved