主頁(yè) > 知識(shí)庫(kù) > Oracle中創(chuàng)建和管理表詳解

Oracle中創(chuàng)建和管理表詳解

熱門(mén)標(biāo)簽:人工智能地圖標(biāo)注自己能做嗎 阿爾巴尼亞地圖標(biāo)注app 美圖秀秀地圖標(biāo)注 word地圖標(biāo)注方向 百度地圖標(biāo)注素材 征服者火車(chē)站地圖標(biāo)注 征服眼公司地圖標(biāo)注 開(kāi)封智能外呼系統(tǒng)廠家 外呼線路外顯本地號(hào)碼
SQL> /*
SQL> 對(duì)于表的操作: 創(chuàng)建表,修改表(添加新的列,改變當(dāng)前某些列,刪除列),刪除表
SQL> 創(chuàng)建表: create table(需要create table的權(quán)限)
SQL> 修改表: alter table tablename add/modify/drop
SQL> 刪除表:drop table tablename
SQL> */
SQL> show user;
USER 為 "SCOTT"
SQL> --訪問(wèn)hr用戶下的表
SQL> select * from hr.employees;
select * from hr.employees
                 *
第 1 行出現(xiàn)錯(cuò)誤:
ORA-00942: 表或視圖不存在
SQL> --測(cè)試defaul值
SQL> create table test1
  2  (tid number,
  3   tname varchar(20),
  4   hiredate date default sysdate);
表已創(chuàng)建。
SQL> insert into test1(tid,tname) values(1,'Mary');
已創(chuàng)建 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維護(hù)一個(gè)地址,該地址指向了該行在硬盤(pán)上實(shí)際存儲(chǔ)的位置
SQL> --關(guān)于varchar2和char
SQL> create table testchar
  2  ( c char(5),
  3    v varchar(5));
表已創(chuàng)建。
SQL> insert into testchar values('a','b');
已創(chuàng)建 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;
 名稱(chēng)                                                              是否為空? 類(lèi)型
 ----------------------------------------------------------------- -------- --------------------------------------------
 C                                                                          CHAR(5)
 V                                                                          VARCHAR2(5)
 HIREDATE                                                                   DATE
SQL> --修改表
SQL> alter table testchar
  2  modify c char(10);
表已更改。
SQL> desc testchar;
 名稱(chēng)                                                              是否為空? 類(lèi)型
 ----------------------------------------------------------------- -------- --------------------------------------------
 C                                                                          CHAR(10)
 V                                                                          VARCHAR2(5)
 HIREDATE                                                                   DATE
SQL> --刪除列
SQL> alter table testchar
  2  drop hiredate;
drop hiredate
     *
第 2 行出現(xiàn)錯(cuò)誤:
ORA-00905: 缺失關(guān)鍵字
SQL> ed
已寫(xiě)入 file afiedt.buf
  1  alter table testchar
  2* drop column hiredate
SQL> /
表已更改。
SQL> desc testchar;
 名稱(chēng)                                                              是否為空? 類(lèi)型
 ----------------------------------------------------------------- -------- --------------------------------------------
 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參數(shù)徹底刪除表
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> --關(guān)于約束:
SQL> --創(chuàng)建一個(gè)表,包含所有約束
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 行出現(xiàn)錯(cuò)誤:
ORA-00907: 缺失右括號(hào)
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 行出現(xiàn)錯(cuò)誤:
ORA-02253: 此處不允許約束條件說(shuō)明
SQL> ed
已寫(xiě)入 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> /
表已創(chuàng)建。
SQL> desc myuser;
 名稱(chēng)                                                              是否為空? 類(lèi)型
 ----------------------------------------------------------------- -------- --------------------------------------------
 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);
已創(chuàng)建 1 行。
SQL> insert into myuser values(1,'Tom','男','ddd@126.com',10);
insert into myuser values(1,'Tom','男','ddd@126.com',10)
*
第 1 行出現(xiàn)錯(cuò)誤:
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 行出現(xiàn)錯(cuò)誤:
ORA-02290: 違反檢查約束條件 (SCOTT.C_GENDER)
SQL> --觸發(fā)器也可以檢查數(shù)據(jù)的正確與否
SQL> spool off
您可能感興趣的文章:
  • oracle創(chuàng)建表的方法和一些常用命令
  • shell腳本操作oracle刪除表空間、創(chuàng)建表空間、刪除用戶
  • oracle存儲(chǔ)過(guò)程創(chuàng)建表分區(qū)實(shí)例
  • 六分鐘學(xué)會(huì)創(chuàng)建Oracle表空間的實(shí)現(xiàn)步驟
  • oracle 創(chuàng)建表空間步驟代碼
  • Oracle創(chuàng)建主鍵自增表(sql語(yǔ)句實(shí)現(xiàn))及觸發(fā)器應(yīng)用
  • oracle 創(chuàng)建表空間詳細(xì)介紹
  • Oracle 創(chuàng)建用戶及數(shù)據(jù)表的方法
  • Oracle 查看表空間的大小及使用情況sql語(yǔ)句
  • Oracle數(shù)據(jù)庫(kù)如何創(chuàng)建第一張表

標(biāo)簽:淮南 孝感 宜春 酒泉 葫蘆島 六安 海北 泰安

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Oracle中創(chuàng)建和管理表詳解》,本文關(guān)鍵詞  Oracle,中創(chuàng),建和,管理,表,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Oracle中創(chuàng)建和管理表詳解》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Oracle中創(chuàng)建和管理表詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章