• Oracle 表创建和表管理


    1.表的命名

    • 必须以字母开头
    • 字符长度在1-30之间
    • 只能包含A-Z,a-z,0-9,_,$和#
    • 被同一个用户拥有的对象不能有重复的名字

    2.表的创建

    1. SQL> create table t01(id number(4),name varchar2(15));
    2. Table created.
    3. SQL> desc t01
    4. Name Null? Type
    5. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    6. ID NUMBER(4)
    7. NAME VARCHAR2(15)
    8. SQL> select * from t01;
    9. no rows selected
    10. SQL>

    3. 子查询建表并copy 表

    1. SQL> create table e01 as select * from emp;
    2. Table created.
    3. SQL> select * from e01;
    4. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    5. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
    6. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    7. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
    8. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    9. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    10. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    11. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    12. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    13. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    14. 7839 KING PRESIDENT 17-NOV-81 5000 10
    15. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    16. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    17. 7900 JAMES CLERK 7698 03-DEC-81 950 30
    18. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    19. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    20. 14 rows selected.
    21. SQL>

    4. 子查询建表只拷贝表结构

    1. SQL> create table e02 as select * from emp where 1=0;
    2. Table created.
    3. SQL> select * from e02;
    4. no rows selected
    5. SQL>

    5.用户表 和数据字典

    用户表

    • 由用户创建和维护的表的集合
    • 包含用户信息

    数据字典

    • 由Oracle服务器创建和维护的表的集合
    • 包含数据库的信息
    • USER_ : (有关用户对象的信息)
    • ALL_  : 所有用户可以方法的表的信息(对象表和相关表)
    • DBA_ : 受限视图,只能够被分配给DBA角色的人访问
    • V$  :  动态性能视图,数据库服务器性能,内存和锁

    查询数据字典

    • 查看用户拥有的表的名称
    1. SQL>
    2. SQL> select table_name from user_tables;
    3. TABLE_NAME
    4. --------------------------------------------------------------------------------------------------------------------------------
    5. DEPT
    6. EMP
    7. BONUS
    8. SALGRADE
    9. T01
    10. E01
    11. E02
    12. 7 rows selected.
    13. SQL>
    • 查看用户拥有的不同对象类型
    1. SQL>
    2. SQL> select distinct object_type from user_objects;
    3. OBJECT_TYPE
    4. -----------------------
    5. INDEX
    6. TABLE
    7. SQL>
    • 查看用户拥有的表,视图,同义字和序列
    1. SQL>
    2. SQL> select * from user_catalog;
    3. TABLE_NAME TABLE_TYPE
    4. -------------------------------------------------------------------------------------------------------------------------------- -----------
    5. DEPT TABLE
    6. EMP TABLE
    7. BONUS TABLE
    8. SALGRADE TABLE
    9. T01 TABLE
    10. E01 TABLE
    11. E02 TABLE
    12. 7 rows selected.
    13. SQL>

    数据类型:

    Alter table 语句

    1. SQL>
    2. SQL> select * from user_catalog;
    3. TABLE_NAME TABLE_TYPE
    4. -------------------------------------------------------------------------------------------------------------------------------- -----------
    5. DEPT TABLE
    6. EMP TABLE
    7. BONUS TABLE
    8. SALGRADE TABLE
    9. T01 TABLE
    10. E01 TABLE
    11. E02 TABLE
    12. 7 rows selected.
    13. SQL> desc e01
    14. Name Null? Type
    15. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    16. EMPNO NUMBER(4)
    17. ENAME VARCHAR2(10)
    18. JOB VARCHAR2(9)
    19. MGR NUMBER(4)
    20. HIREDATE DATE
    21. SAL NUMBER(7,2)
    22. COMM NUMBER(7,2)
    23. DEPTNO NUMBER(2)
    24. SQL> alter table e01 add (location varchar2(10));
    25. Table altered.
    26. SQL> desc e01
    27. Name Null? Type
    28. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    29. EMPNO NUMBER(4)
    30. ENAME VARCHAR2(10)
    31. JOB VARCHAR2(9)
    32. MGR NUMBER(4)
    33. HIREDATE DATE
    34. SAL NUMBER(7,2)
    35. COMM NUMBER(7,2)
    36. DEPTNO NUMBER(2)
    37. LOCATION VARCHAR2(10)
    38. SQL> select * from e01;
    39. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOCATION
    40. ---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
    41. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    42. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
    43. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    44. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    45. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    46. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    47. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    48. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    49. 7839 KING PRESIDENT 17-NOV-81 5000 10
    50. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    51. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    52. 7900 JAMES CLERK 7698 03-DEC-81 950 30
    53. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    54. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    55. 14 rows selected.
    56. SQL> alter table e01 modify (location varchar2(13));
    57. Table altered.
    58. SQL> desc dept;
    59. Name Null? Type
    60. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    61. DEPTNO NOT NULL NUMBER(2)
    62. DNAME VARCHAR2(14)
    63. LOC VARCHAR2(13)
    64. SQL> desc e01;
    65. Name Null? Type
    66. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    67. EMPNO NUMBER(4)
    68. ENAME VARCHAR2(10)
    69. JOB VARCHAR2(9)
    70. MGR NUMBER(4)
    71. HIREDATE DATE
    72. SAL NUMBER(7,2)
    73. COMM NUMBER(7,2)
    74. DEPTNO NUMBER(2)
    75. LOCATION VARCHAR2(13)
    76. SQL> alter table e01 modify (HIREDATE date default sysdate);
    77. Table altered.
    78. SQL> desc user_tab_columns
    79. Name Null? Type
    80. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    81. TABLE_NAME NOT NULL VARCHAR2(128)
    82. COLUMN_NAME NOT NULL VARCHAR2(128)
    83. DATA_TYPE VARCHAR2(128)
    84. DATA_TYPE_MOD VARCHAR2(3)
    85. DATA_TYPE_OWNER VARCHAR2(128)
    86. DATA_LENGTH NOT NULL NUMBER
    87. DATA_PRECISION NUMBER
    88. DATA_SCALE NUMBER
    89. NULLABLE VARCHAR2(1)
    90. COLUMN_ID NUMBER
    91. DEFAULT_LENGTH NUMBER
    92. DATA_DEFAULT LONG
    93. NUM_DISTINCT NUMBER
    94. LOW_VALUE RAW(2000)
    95. HIGH_VALUE RAW(2000)
    96. DENSITY NUMBER
    97. NUM_NULLS NUMBER
    98. NUM_BUCKETS NUMBER
    99. LAST_ANALYZED DATE
    100. SAMPLE_SIZE NUMBER
    101. CHARACTER_SET_NAME VARCHAR2(44)
    102. CHAR_COL_DECL_LENGTH NUMBER
    103. GLOBAL_STATS VARCHAR2(3)
    104. USER_STATS VARCHAR2(3)
    105. AVG_COL_LEN NUMBER
    106. CHAR_LENGTH NUMBER
    107. CHAR_USED VARCHAR2(1)
    108. V80_FMT_IMAGE VARCHAR2(3)
    109. DATA_UPGRADED VARCHAR2(3)
    110. HISTOGRAM VARCHAR2(15)
    111. DEFAULT_ON_NULL VARCHAR2(3)
    112. IDENTITY_COLUMN VARCHAR2(3)
    113. EVALUATION_EDITION VARCHAR2(128)
    114. UNUSABLE_BEFORE VARCHAR2(128)
    115. UNUSABLE_BEGINNING VARCHAR2(128)
    116. COLLATION VARCHAR2(100)
    117. SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
    118. COLUMN_NAME
    119. --------------------------------------------------------------------------------------------------------------------------------
    120. DATA_DEFAULT
    121. --------------------------------------------------------------------------------
    122. LOCATION
    123. EMPNO
    124. ENAME
    125. JOB
    126. MGR
    127. HIREDATE
    128. sysdate
    129. SAL
    130. COMM
    131. DEPTNO
    132. 9 rows selected.
    133. SQL> col COLUMN_NAME for a20
    134. SQL> col DATA_DEFAULT for a30
    135. SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
    136. COLUMN_NAME DATA_DEFAULT
    137. -------------------- ------------------------------
    138. LOCATION
    139. EMPNO
    140. ENAME
    141. JOB
    142. MGR
    143. HIREDATE sysdate
    144. SAL
    145. COMM
    146. DEPTNO
    147. 9 rows selected.
    148. SQL>

    对于大表列的删除采用的方法如下:(先置为unused,之后再删除,效率最高,最快)

    1. SQL>
    2. SQL>
    3. SQL> alter table e01 drop (HIREDATE);
    4. Table altered.
    5. SQL> desc e01;
    6. Name Null? Type
    7. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    8. EMPNO NUMBER(4)
    9. ENAME VARCHAR2(10)
    10. JOB VARCHAR2(9)
    11. MGR NUMBER(4)
    12. SAL NUMBER(7,2)
    13. COMM NUMBER(7,2)
    14. DEPTNO NUMBER(2)
    15. LOCATION VARCHAR2(13)
    16. SQL> rollback;
    17. Rollback complete.
    18. SQL> desc e01;
    19. Name Null? Type
    20. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    21. EMPNO NUMBER(4)
    22. ENAME VARCHAR2(10)
    23. JOB VARCHAR2(9)
    24. MGR NUMBER(4)
    25. SAL NUMBER(7,2)
    26. COMM NUMBER(7,2)
    27. DEPTNO NUMBER(2)
    28. LOCATION VARCHAR2(13)
    29. SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
    30. COLUMN_NAME DATA_DEFAULT
    31. -------------------- ------------------------------
    32. LOCATION
    33. EMPNO
    34. ENAME
    35. JOB
    36. MGR
    37. SAL
    38. COMM
    39. DEPTNO
    40. 8 rows selected.
    41. SQL>
    42. SQL> alter table e01 set unused column mgr;
    43. Table altered.
    44. SQL> desc e01;
    45. Name Null? Type
    46. ----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
    47. EMPNO NUMBER(4)
    48. ENAME VARCHAR2(10)
    49. JOB VARCHAR2(9)
    50. SAL NUMBER(7,2)
    51. COMM NUMBER(7,2)
    52. DEPTNO NUMBER(2)
    53. LOCATION VARCHAR2(13)
    54. SQL> alter table e01 drop unused columns;
    55. Table altered.
    56. SQL>

    回表恢复drop的表,drop的table仍在回收站中,只需要flashback 操作便可以将其捡回来。

    1. SQL> insert into e02 select * from emp;
    2. 14 rows created.
    3. SQL> select * from tab;
    4. TNAME TABTYPE CLUSTERID
    5. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    6. DEPT TABLE
    7. EMP TABLE
    8. BONUS TABLE
    9. SALGRADE TABLE
    10. T01 TABLE
    11. E01 TABLE
    12. E02 TABLE
    13. 7 rows selected.
    14. SQL> drop table e02;
    15. Table dropped.
    16. SQL> select * from tab;
    17. TNAME TABTYPE CLUSTERID
    18. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    19. DEPT TABLE
    20. EMP TABLE
    21. BONUS TABLE
    22. SALGRADE TABLE
    23. T01 TABLE
    24. E01 TABLE
    25. BIN$7ZYRI7OIbgzgU4oIqMCjqg==$0 TABLE
    26. 7 rows selected.
    27. SQL> select * from "BIN$7ZYRI7OIbgzgU4oIqMCjqg==$0";
    28. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    29. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
    30. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    31. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
    32. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    33. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    34. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    35. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    36. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    37. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    38. 7839 KING PRESIDENT 17-NOV-81 5000 10
    39. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    40. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    41. 7900 JAMES CLERK 7698 03-DEC-81 950 30
    42. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    43. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    44. 14 rows selected.
    45. SQL> flashback table e02 to before drop;
    46. Flashback complete.
    47. SQL> select * from tab;
    48. TNAME TABTYPE CLUSTERID
    49. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    50. DEPT TABLE
    51. EMP TABLE
    52. BONUS TABLE
    53. SALGRADE TABLE
    54. T01 TABLE
    55. E01 TABLE
    56. E02 TABLE
    57. 7 rows selected.
    58. SQL> select * from e02;
    59. EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    60. ---------- ---------- --------- ---------- --------- ---------- ---------- ----------
    61. 7369 SMITH CLERK 7902 17-DEC-80 800 20
    62. 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
    63. 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
    64. 7566 JONES MANAGER 7839 02-APR-81 2975 20
    65. 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
    66. 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
    67. 7782 CLARK MANAGER 7839 09-JUN-81 2450 10
    68. 7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
    69. 7839 KING PRESIDENT 17-NOV-81 5000 10
    70. 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
    71. 7876 ADAMS CLERK 7788 02-APR-87 1100 20
    72. 7900 JAMES CLERK 7698 03-DEC-81 950 30
    73. 7902 FORD ANALYST 7566 03-DEC-81 3000 20
    74. 7934 MILLER CLERK 7782 23-JAN-82 1300 10
    75. 14 rows selected.
    76. SQL>

    以下语句为purged回收站语句。

    1. SQL> purge recyclebin;
    2. Recyclebin purged.
    3. SQL> select * from tab;
    4. TNAME TABTYPE CLUSTERID
    5. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    6. DEPT TABLE
    7. EMP TABLE
    8. BONUS TABLE
    9. SALGRADE TABLE
    10. T01 TABLE
    11. E01 TABLE
    12. E02 TABLE
    13. 7 rows selected.
    14. SQL>

    重命名

    1. SQL> create table "123" as select * from dept;
    2. Table created.
    3. SQL> select * from tab;
    4. TNAME TABTYPE CLUSTERID
    5. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    6. DEPT TABLE
    7. EMP TABLE
    8. BONUS TABLE
    9. SALGRADE TABLE
    10. T01 TABLE
    11. E01 TABLE
    12. E02 TABLE
    13. 123 TABLE
    14. 8 rows selected.
    15. SQL> rename "123" to detail_dept;
    16. Table renamed.
    17. SQL> select * from tab;
    18. TNAME TABTYPE CLUSTERID
    19. -------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
    20. DEPT TABLE
    21. EMP TABLE
    22. BONUS TABLE
    23. SALGRADE TABLE
    24. T01 TABLE
    25. E01 TABLE
    26. E02 TABLE
    27. DETAIL_DEPT TABLE
    28. 8 rows selected.
    29. SQL>

    Truncate table 语句

    • 删除表中所有的行
    • 释放被表占用的存储空间

    当使用TRUNCATE 时不能回滚行删除。

  • 相关阅读:
    FTP文件传输服务
    实操演练 | 不使用联接查询多个表
    UI Toolkit 计时器
    负载均衡原理及应用
    java创建本地文件并读取文件
    Centos7-----Mysql8密码忘记并重置
    NumPy学习挑战第四关-NumPy数组属性
    使用VScode编译betaflight固件--基于windows平台
    子矩形计数(冬季每日一题 17)
    海格里斯HEGERLS仓储货架厂家标准解析|智能自动化立体仓库AS/RS存储系统
  • 原文地址:https://blog.csdn.net/u011868279/article/details/127891063