oracle alter table詳解
- //建測試表
- create table dept(
- deptno number(3) primary key,
- dname varchar2(10),
- loc varchar2(13)
- );
- create table employee_info(
- empno number(3),
- deptno number(3),
- ename varchar2(10),
- sex char(1),
- phone number(11),
- address varchar2(50),
- introduce varchar2(100)
- );
- --
- //0.重新命名
- //0.1 表:rename dept to dt;
- rename dt to dept;
- //0.2 列:alter table dept rename column loc to location;
- alter table dept rename column location to loc;
- //1.新增約束
- //1.1 primary key
- alter table employee_info add constraint pk_emp_info primary key(empno);
- //1.2 foreign key
- alter table employee_info add constraint fk_emp_info foreign key(deptno)
- references dept(deptno);
- //1.3 check
- alter table employee_info add constraint ck_emp_info check
- (sex in ('F','M'));
- //1.4 not null
- alter table employee_info modify phone constraint not_null_emp_info not null;
- //1.5 unique
- alter table employee_info add constraint uq_emp_info unique(phone);
- //1.6 default
- alter table employee_info modify sex char(2) default 'M';
- //2.新增列
- alter table employee_info add id varchar2(18);
- alter table employee_info add hiredate date default sysdate not null;
- //3.刪除列
- alter table employee_info drop column introduce;
- //3.修改列
- //3.1 修改列的長度
- alter table dept modify loc varchar2(50);
- //3.2 修改列的精度
- alter table employee_info modify empno number(2);
- //3.3 修改列的資料型別
- alter table employee_info modify sex char(2);
- //3.4 修改預設值
- alter table employee_info modify hiredate default sysdate+1;
- //4.禁用約束
- alter table employee_info disable constraint uq_emp_info;
- //5.啟用約束
- alter table employee_info enable constraint uq_emp_info;
- //6.延遲約束
- alter table employee_info drop constraint fk_emp_info;
- alter table employee_info add constraint fk_emp_info foreign key(deptno)
- references dept(deptno)
- deferrable initially deferred;
- //7.向表中新增註釋
- comment on table employee_info is 'information of employees';
- //8.向列新增註釋
- comment on column employee_info.ename is 'the name of employees';
- comment on column dept.dname is 'the name of department';
- //9.清除表中所有資料
- truncate table employee_info;
- //10.刪除表
- drop table employee_info;
- --
- //下面來看看剛剛才我們對錶dept和表employee_info所做的更改
- //user_constraints檢視裡麵包含了剛剛才我們建立的所有約束,以及其他資訊,
- //你可以用desc user_constraints命令檢視其詳細說明
- select constraint_name,constraint_type,status,deferrable,deferred
- from user_constraints
- where table_name='EMPLOYEE_INFO';
- --
- CONSTRAINT_NAME CONSTRAINT_TYPE STATUS DEFERRABLE DEFERRED
- ------------------------------ --------------- -------- -------------- ---------
- PK_EMP_INFO P ENABLED NOT DEFERRABLE IMMEDIATE
- FK_EMP_INFO R ENABLED DEFERRABLE DEFERRED
- NOT_NULL_EMP_INFO C ENABLED NOT DEFERRABLE IMMEDIATE
- SYS_C005373 C ENABLED NOT DEFERRABLE IMMEDIATE
- UQ_EMP_INFO U ENABLED NOT DEFERRABLE IMMEDIATE
- CK_EMP_INFO C ENABLED NOT DEFERRABLE IMMEDIATE
- //我們可以透過user_cons_columns檢視檢視有關列的約束資訊;
- select owner,constraint_name,table_name,column_name
- from user_cons_columns
- where table_name='EMPLOYEE_INFO';
- --
- OWNER CONSTRAINT_NAME TABLE_NAME COLUMN_NAME
- ------------------------------ ------------------------------ ------------------------------ ---------------
- YEEXUN PK_EMP_INFO EMPLOYEE_INFO EMPNO
- YEEXUN CK_EMP_INFO EMPLOYEE_INFO SEX
- YEEXUN NOT_NULL_EMP_INFO EMPLOYEE_INFO PHONE
- YEEXUN SYS_C005373 EMPLOYEE_INFO HIREDATE
- YEEXUN UQ_EMP_INFO EMPLOYEE_INFO PHONE
- YEEXUN FK_EMP_INFO EMPLOYEE_INFO DEPTNO
- //我們將user_constraints檢視與user_cons_columns檢視連線起來
- //檢視約束都指向哪些列
- column column_name format a15;
- select ucc.column_name,ucc.constraint_name,uc.constraint_type,uc.status
- from user_constraints uc,user_cons_columns ucc
- where uc.table_name=ucc.table_name and
- uc.constraint_name=ucc.constraint_name and
- ucc.table_name='EMPLOYEE_INFO';
- --
- COLUMN_NAME CONSTRAINT_NAME CONSTRAINT_TYPE STATUS
- --------------- ------------------------------ --------------- --------
- EMPNO PK_EMP_INFO P ENABLED
- DEPTNO FK_EMP_INFO R ENABLED
- PHONE NOT_NULL_EMP_INFO C ENABLED
- HIREDATE SYS_C005373 C ENABLED
- PHONE UQ_EMP_INFO U ENABLED
- SEX CK_EMP_INFO C ENABLED
- --
- //這裡有個constraint_type,他具體指下面幾種型別:
- //C:check,not null
- //P:primary key
- //R:foreign key
- //U:unique
- //V:check option
- //O:read only
- --
- //我們可以透過user_tab_comments檢視獲得對錶的註釋
- select * from user_tab_comments
- where table_name='EMPLOYEE_INFO';
- TABLE_NAME TABLE_TYPE COMMENTS
- ------------------------------ ----------- --------------------------
- EMPLOYEE_INFO TABLE information of employees
- --
- //我們還可以透過user_col_comments檢視獲得對錶列的註釋:
- select * from user_col_comments
- where table_name='EMPLOYEE_INFO';
- --
- TABLE_NAME COLUMN_NAME COMMENTS
- ------------------------------ ------------------------------ ---------------------------
- EMPLOYEE_INFO EMPNO
- EMPLOYEE_INFO DEPTNO
- EMPLOYEE_INFO ENAME the name of employees
- EMPLOYEE_INFO SEX
- EMPLOYEE_INFO PHONE
- EMPLOYEE_INFO ADDRESS
- EMPLOYEE_INFO ID
- EMPLOYEE_INFO HIREDATE
- --
- select * from user_col_comments
- where table_name='EMPLOYEE_INFO' and
- comments is not null;
- --
- TABLE_NAME COLUMN_NAME COMMENTS
- ------------------------------ ------------------------------ ------------------------
- EMPLOYEE_INFO ENAME the name of employees
- --
- //最後我們來檢視一下修改後的表:
- desc employee_info;
- Name Type Nullable Default Comments
- -------- ------------ -------- --------- ---------------------
- EMPNO NUMBER(2)
- DEPTNO NUMBER(3) Y
- ENAME VARCHAR2(10) Y the name of employees
- SEX CHAR(2) Y 'M'
- PHONE NUMBER(11)
- ADDRESS VARCHAR2(50) Y
- ID VARCHAR2(18) Y
- HIREDATE DATE sysdate+1
- --
- desc dept;
- Name Type Nullable Default Comments
- ------ ------------ -------- ------- ----------------------
- DEPTNO NUMBER(3)
- DNAME VARCHAR2(10) Y the name of department
- LOC VARCHAR2(50) Y
- --
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/90618/viewspace-1097091/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Alter table for ORACLEOracle
- alter table move 和 alter table shrink space的區別
- Oracle 10g Shrink Table 詳解Oracle 10g
- Oracle 11g alter table move與shrink spaceOracle
- oracle 中 ALTER TABLE ADD default 的明確Oracle
- oracle 10g__alter table shrink space compactOracle 10g
- oracle10g_alter table_測試3Oracle
- mysql的ALTER TABLE命令MySql
- alter table using indexIndex
- ORACLE中RECORD、VARRAY、TABLE的使用詳解Oracle
- v$lock之alter table drop column與alter table set unused column區別系列五
- How to adjust the high watermark in ORACLE 10g – ALTER TABLE SHRINKOracle 10g
- alter table語法增補(一)
- ALTER TABLE MOVE | SHRINK SPACE區別
- ALTER TABLE MOVE和SHRINK SPACE區別
- table細線表格詳解
- alter database in OracleDatabaseOracle
- Alter system in OracleOracle
- alter table move 與shrink space的區別
- alter table move跟shrink space的區別
- alter table engine=memory ERROR 1114Error
- pt-table-checksum原理詳解
- alter system backup controlfile to trace內容詳解
- alter table nologging /*+APPEND PARALLEL(n)*/APPParallel
- alter table move跟shrink space的區別(轉)
- ALTER TABLE causes auto_increment resulting key 'PRIMARY'REM
- 表、索引遷移表空間alter table move索引
- alter table列管理的一些區別
- DBMS_STATS.GATHER_TABLE_STATS詳解
- alter table table_name move ; 在自身表空間move是如何操作的?
- Oracle 12.2之後ALTER TABLE .. MODIFY轉換非分割槽表為分割槽表Oracle
- MySQL oak-online-alter-table工具使用初探MySql
- alter table modify constraint_disable_enable_novalidateAI
- 資料庫審計(create/alter/drop table、user、tablespace)資料庫
- Flutter 佈局(九)- Flow、Table、Wrap詳解Flutter
- oracle10g_alter table shrink space_compact_cascade回收空間測試(一)Oracle
- oracle11g alter table set unused column指定表某列不可用之系列一Oracle
- MySQL-ALTER TABLE命令學習[20180503]MySql