1.表的命名
- 必须以字母开头
- 字符长度在1-30之间
- 只能包含A-Z,a-z,0-9,_,$和#
- 被同一个用户拥有的对象不能有重复的名字
2.表的创建
SQL> create table t01(id number(4),name varchar2(15));
Table created.
SQL> desc t01
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
ID NUMBER(4)
NAME VARCHAR2(15)
SQL> select * from t01;
no rows selected
SQL>
3. 子查询建表并copy 表
SQL> create table e01 as select * from emp;
Table created.
SQL> select * from e01;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL>
4. 子查询建表只拷贝表结构
SQL> create table e02 as select * from emp where 1=0;
Table created.
SQL> select * from e02;
no rows selected
SQL>
5.用户表 和数据字典
用户表
- 由用户创建和维护的表的集合
- 包含用户信息
数据字典
- 由Oracle服务器创建和维护的表的集合
- 包含数据库的信息
- USER_ : (有关用户对象的信息)
- ALL_ : 所有用户可以方法的表的信息(对象表和相关表)
- DBA_ : 受限视图,只能够被分配给DBA角色的人访问
- V$ : 动态性能视图,数据库服务器性能,内存和锁
查询数据字典
- 查看用户拥有的表的名称
SQL>
SQL> select table_name from user_tables;
TABLE_NAME
--------------------------------------------------------------------------------------------------------------------------------
DEPT
EMP
BONUS
SALGRADE
T01
E01
E02
7 rows selected.
SQL>
- 查看用户拥有的不同对象类型
SQL>
SQL> select distinct object_type from user_objects;
OBJECT_TYPE
-----------------------
INDEX
TABLE
SQL>
- 查看用户拥有的表,视图,同义字和序列
SQL>
SQL> select * from user_catalog;
TABLE_NAME TABLE_TYPE
-------------------------------------------------------------------------------------------------------------------------------- -----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
7 rows selected.
SQL>
数据类型:
Alter table 语句
SQL>
SQL> select * from user_catalog;
TABLE_NAME TABLE_TYPE
-------------------------------------------------------------------------------------------------------------------------------- -----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
7 rows selected.
SQL> desc e01
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
SQL> alter table e01 add (location varchar2(10));
Table altered.
SQL> desc e01
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
LOCATION VARCHAR2(10)
SQL> select * from e01;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO LOCATION
---------- ---------- --------- ---------- --------- ---------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> alter table e01 modify (location varchar2(13));
Table altered.
SQL> desc dept;
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
DEPTNO NOT NULL NUMBER(2)
DNAME VARCHAR2(14)
LOC VARCHAR2(13)
SQL> desc e01;
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
LOCATION VARCHAR2(13)
SQL> alter table e01 modify (HIREDATE date default sysdate);
Table altered.
SQL> desc user_tab_columns
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
TABLE_NAME NOT NULL VARCHAR2(128)
COLUMN_NAME NOT NULL VARCHAR2(128)
DATA_TYPE VARCHAR2(128)
DATA_TYPE_MOD VARCHAR2(3)
DATA_TYPE_OWNER VARCHAR2(128)
DATA_LENGTH NOT NULL NUMBER
DATA_PRECISION NUMBER
DATA_SCALE NUMBER
NULLABLE VARCHAR2(1)
COLUMN_ID NUMBER
DEFAULT_LENGTH NUMBER
DATA_DEFAULT LONG
NUM_DISTINCT NUMBER
LOW_VALUE RAW(2000)
HIGH_VALUE RAW(2000)
DENSITY NUMBER
NUM_NULLS NUMBER
NUM_BUCKETS NUMBER
LAST_ANALYZED DATE
SAMPLE_SIZE NUMBER
CHARACTER_SET_NAME VARCHAR2(44)
CHAR_COL_DECL_LENGTH NUMBER
GLOBAL_STATS VARCHAR2(3)
USER_STATS VARCHAR2(3)
AVG_COL_LEN NUMBER
CHAR_LENGTH NUMBER
CHAR_USED VARCHAR2(1)
V80_FMT_IMAGE VARCHAR2(3)
DATA_UPGRADED VARCHAR2(3)
HISTOGRAM VARCHAR2(15)
DEFAULT_ON_NULL VARCHAR2(3)
IDENTITY_COLUMN VARCHAR2(3)
EVALUATION_EDITION VARCHAR2(128)
UNUSABLE_BEFORE VARCHAR2(128)
UNUSABLE_BEGINNING VARCHAR2(128)
COLLATION VARCHAR2(100)
SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
COLUMN_NAME
--------------------------------------------------------------------------------------------------------------------------------
DATA_DEFAULT
--------------------------------------------------------------------------------
LOCATION
EMPNO
ENAME
JOB
MGR
HIREDATE
sysdate
SAL
COMM
DEPTNO
9 rows selected.
SQL> col COLUMN_NAME for a20
SQL> col DATA_DEFAULT for a30
SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
COLUMN_NAME DATA_DEFAULT
-------------------- ------------------------------
LOCATION
EMPNO
ENAME
JOB
MGR
HIREDATE sysdate
SAL
COMM
DEPTNO
9 rows selected.
SQL>
对于大表列的删除采用的方法如下:(先置为unused,之后再删除,效率最高,最快)
SQL>
SQL>
SQL> alter table e01 drop (HIREDATE);
Table altered.
SQL> desc e01;
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
LOCATION VARCHAR2(13)
SQL> rollback;
Rollback complete.
SQL> desc e01;
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
LOCATION VARCHAR2(13)
SQL> select COLUMN_NAME,DATA_DEFAULT from user_tab_columns where table_name='E01';
COLUMN_NAME DATA_DEFAULT
-------------------- ------------------------------
LOCATION
EMPNO
ENAME
JOB
MGR
SAL
COMM
DEPTNO
8 rows selected.
SQL>
SQL> alter table e01 set unused column mgr;
Table altered.
SQL> desc e01;
Name Null? Type
----------------------------------------------------------------------------------------------------------------- -------- ----------------------------------------------------------------------------
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
LOCATION VARCHAR2(13)
SQL> alter table e01 drop unused columns;
Table altered.
SQL>
闪回表恢复drop的表,drop的table仍在回收站中,只需要flashback 操作便可以将其捡回来。
SQL> insert into e02 select * from emp;
14 rows created.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
7 rows selected.
SQL> drop table e02;
Table dropped.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
BIN$7ZYRI7OIbgzgU4oIqMCjqg==$0 TABLE
7 rows selected.
SQL> select * from "BIN$7ZYRI7OIbgzgU4oIqMCjqg==$0";
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL> flashback table e02 to before drop;
Flashback complete.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
7 rows selected.
SQL> select * from e02;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 24-JAN-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 02-APR-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
14 rows selected.
SQL>
以下语句为purged回收站语句。
SQL> purge recyclebin;
Recyclebin purged.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
7 rows selected.
SQL>
重命名
SQL> create table "123" as select * from dept;
Table created.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
123 TABLE
8 rows selected.
SQL> rename "123" to detail_dept;
Table renamed.
SQL> select * from tab;
TNAME TABTYPE CLUSTERID
-------------------------------------------------------------------------------------------------------------------------------- ------------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
T01 TABLE
E01 TABLE
E02 TABLE
DETAIL_DEPT TABLE
8 rows selected.
SQL>
Truncate table 语句
- 删除表中所有的行
- 释放被表占用的存储空间
当使用TRUNCATE 时不能回滚行删除。