zoukankan      html  css  js  c++  java
  • oracle修改表结构

    1. //建测试表  
    2. create table dept(  
    3.        deptno number(3) primary key,  
    4.        dname varchar2(10),  
    5.        loc varchar2(13)   
    6.        );  
    7. create table employee_info(  
    8.        empno number(3),  
    9.        deptno number(3),  
    10.        ename varchar2(10),  
    11.        sex char(1),  
    12.        phone number(11),  
    13.        address varchar2(50),  
    14.        introduce varchar2(100)  
    15.        );  
    16. --  
    17. //0.重命名  
    18.   //0.1 表:rename dept to dt;  
    19.            rename dt to dept;  
    20.   //0.2 列:alter table dept rename column loc to location;  
    21.            alter table dept rename column location to loc;  
    22. //1.添加约束  
    23.   //1.1 primary key  
    24.       alter table employee_info add constraint pk_emp_info primary key(empno);  
    25.   //1.2 foreign key  
    26.       alter table employee_info add constraint fk_emp_info foreign key(deptno)  
    27.       references dept(deptno);  
    28.   //1.3 check  
    29.       alter table employee_info add constraint ck_emp_info check  
    30.       (sex in ('F','M'));  
    31.   //1.4 not null  
    32.       alter table employee_info modify phone constraint not_null_emp_info not null;  
    33.   //1.5 unique  
    34.       alter table employee_info add constraint uq_emp_info unique(phone);  
    35.   //1.6 default  
    36.       alter table employee_info modify sex char(2) default 'M';  
    37. //2.添加列  
    38.    alter table employee_info add id varchar2(18);  
    39.    alter table employee_info add hiredate date default sysdate not null;  
    40. //3.删除列  
    41.    alter table employee_info drop column introduce;  
    42. //3.修改列  
    43.   //3.1 修改列的长度  
    44.       alter table dept modify loc varchar2(50);  
    45.   //3.2 修改列的精度  
    46.       alter table employee_info modify empno number(2);  
    47.   //3.3 修改列的数据类型  
    48.       alter table employee_info modify sex char(2);  
    49.   //3.4 修改默认值  
    50.       alter table employee_info modify hiredate default sysdate+1;  
    51. //4.禁用约束  
    52.   alter table employee_info disable constraint uq_emp_info;  
    53. //5.启用约束  
    54.   alter table employee_info enable constraint uq_emp_info;  
    55. //6.延迟约束  
    56.   alter table employee_info drop constraint fk_emp_info;  
    57.   alter table employee_info add constraint fk_emp_info foreign key(deptno)  
    58.         references dept(deptno)  
    59.   deferrable initially deferred;  
    60. //7.向表中添加注释  
    61.   comment on table employee_info is 'information of employees';  
    62. //8.向列添加注释  
    63.   comment on column employee_info.ename is 'the name of employees';  
    64.   comment on column dept.dname is 'the name of department';  
    65. //9.清除表中所有数据  
    66.   truncate table employee_info;  
    67. //10.删除表  
    68.   drop table employee_info;  
    69. --  
    70. //下面来看看刚刚才我们对表dept和表employee_info所做的更改  
    71. //user_constraints视图里面包含了刚刚才我们创建的所有约束,以及其他信息,  
    72. //你可以用desc user_constraints命令查看其详细说明  
    73. select constraint_name,constraint_type,status,deferrable,deferred  
    74. from user_constraints  
    75. where table_name='EMPLOYEE_INFO';  
    76. --  
    77. CONSTRAINT_NAME                CONSTRAINT_TYPE STATUS   DEFERRABLE     DEFERRED  
    78. ------------------------------ --------------- -------- -------------- ---------  
    79. PK_EMP_INFO                    P               ENABLED  NOT DEFERRABLE IMMEDIATE  
    80. FK_EMP_INFO                    R               ENABLED  DEFERRABLE     DEFERRED  
    81. NOT_NULL_EMP_INFO              C               ENABLED  NOT DEFERRABLE IMMEDIATE  
    82. SYS_C005373                    C               ENABLED  NOT DEFERRABLE IMMEDIATE  
    83. UQ_EMP_INFO                    U               ENABLED  NOT DEFERRABLE IMMEDIATE  
    84. CK_EMP_INFO                    C               ENABLED  NOT DEFERRABLE IMMEDIATE  
    85. //我们可以通过user_cons_columns视图查看有关列的约束信息;  
    86. select owner,constraint_name,table_name,column_name  
    87. from user_cons_columns  
    88. where table_name='EMPLOYEE_INFO';  
    89. --  
    90. OWNER                          CONSTRAINT_NAME                TABLE_NAME                     COLUMN_NAME  
    91. ------------------------------ ------------------------------ ------------------------------ ---------------  
    92. YEEXUN                         PK_EMP_INFO                    EMPLOYEE_INFO                  EMPNO  
    93. YEEXUN                         CK_EMP_INFO                    EMPLOYEE_INFO                  SEX  
    94. YEEXUN                         NOT_NULL_EMP_INFO              EMPLOYEE_INFO                  PHONE  
    95. YEEXUN                         SYS_C005373                    EMPLOYEE_INFO                  HIREDATE  
    96. YEEXUN                         UQ_EMP_INFO                    EMPLOYEE_INFO                  PHONE  
    97. YEEXUN                         FK_EMP_INFO                    EMPLOYEE_INFO                  DEPTNO  
    98. //我们将user_constraints视图与user_cons_columns视图连接起来  
    99. //查看约束都指向哪些列  
    100. column column_name format a15;  
    101. select ucc.column_name,ucc.constraint_name,uc.constraint_type,uc.status  
    102. from user_constraints uc,user_cons_columns ucc  
    103. where uc.table_name=ucc.table_name and  
    104.       uc.constraint_name=ucc.constraint_name and  
    105.       ucc.table_name='EMPLOYEE_INFO';  
    106. --  
    107. COLUMN_NAME     CONSTRAINT_NAME                CONSTRAINT_TYPE STATUS  
    108. --------------- ------------------------------ --------------- --------  
    109. EMPNO           PK_EMP_INFO                    P               ENABLED  
    110. DEPTNO          FK_EMP_INFO                    R               ENABLED  
    111. PHONE           NOT_NULL_EMP_INFO              C               ENABLED  
    112. HIREDATE        SYS_C005373                    C               ENABLED  
    113. PHONE           UQ_EMP_INFO                    U               ENABLED  
    114. SEX             CK_EMP_INFO                    C               ENABLED  
    115. --  
    116. //这里有个constraint_type,他具体指下面几种类型:  
    117. //C:check,not null  
    118. //P:primary key  
    119. //R:foreign key  
    120. //U:unique  
    121. //V:check option  
    122. //O:read only  
    123. --  
    124. //我们可以通过user_tab_comments视图获得对表的注释  
    125. select * from user_tab_comments  
    126. where table_name='EMPLOYEE_INFO';  
    127. TABLE_NAME                     TABLE_TYPE  COMMENTS  
    128. ------------------------------ ----------- --------------------------  
    129. EMPLOYEE_INFO                  TABLE       information of employees  
    130. --  
    131. //我们还可以通过user_col_comments视图获得对表列的注释:  
    132. select * from  user_col_comments  
    133. where table_name='EMPLOYEE_INFO';  
    134. --  
    135. TABLE_NAME                     COLUMN_NAME                    COMMENTS  
    136. ------------------------------ ------------------------------ ---------------------------  
    137. EMPLOYEE_INFO                  EMPNO                            
    138. EMPLOYEE_INFO                  DEPTNO                           
    139. EMPLOYEE_INFO                  ENAME                          the name of employees  
    140. EMPLOYEE_INFO                  SEX                              
    141. EMPLOYEE_INFO                  PHONE                            
    142. EMPLOYEE_INFO                  ADDRESS                          
    143. EMPLOYEE_INFO                  ID                               
    144. EMPLOYEE_INFO                  HIREDATE   
    145. --  
    146. select * from user_col_comments  
    147. where table_name='EMPLOYEE_INFO' and  
    148.       comments is not null;  
    149. --  
    150. TABLE_NAME                     COLUMN_NAME                    COMMENTS  
    151. ------------------------------ ------------------------------ ------------------------  
    152. EMPLOYEE_INFO                  ENAME                          the name of employees  
    153. --  
    154. //最后我们来查看一下修改后的表:  
    155. desc employee_info;  
    156. Name     Type         Nullable Default   Comments                
    157. -------- ------------ -------- --------- ---------------------   
    158. EMPNO    NUMBER(2)                                               
    159. DEPTNO   NUMBER(3)    Y                                          
    160. ENAME    VARCHAR2(10) Y                  the name of employees   
    161. SEX      CHAR(2)      Y        'M'                               
    162. PHONE    NUMBER(11)                                              
    163. ADDRESS  VARCHAR2(50) Y                                          
    164. ID       VARCHAR2(18) Y                                          
    165. HIREDATE DATE                  sysdate+1  
    166. --  
    167. desc dept;  
    168. Name   Type         Nullable Default Comments                 
    169. ------ ------------ -------- ------- ----------------------   
    170. DEPTNO NUMBER(3)                                              
    171. DNAME  VARCHAR2(10) Y                the name of department   
    172. LOC    VARCHAR2(50) Y  
    173. -- 
  • 相关阅读:
    Python第三方库之openpyxl(3)
    python的openpyxl的使用笔记
    openpyxl模块(excel操作)
    openpyxl
    OpenPyXl的使用
    python操作Excel模块openpyxl
    re,xpath,BeautifulSoup三种方法爬取古诗词网上诗歌
    python爬虫demo01
    ffmpeg录制报错
    解决ffmpeg执行报错“ffmpeg: error while loading shared libraries: libavdevice.so.58: cannot open shared object file: No such file or directory”的问题
  • 原文地址:https://www.cnblogs.com/guifang/p/2720112.html
Copyright © 2011-2022 走看看