oracle 創建表、刪除表、添加字段、刪除字段、表備注、字段備注、修改表屬性


1、創建表

 create table 表名(
        classid number(2) primary key,
             表字段     數據類型    是否允許為空(not null:不為空/null:允許空)    默認值(default 'XXX')
       );
-- Create table
create table STUDENT.stuinfo
(
  stuid      varchar2(11) not null,--學號:'S'+班號(7位數)+學生序號(3位數)(1)
  stuname    varchar2(50) not null,--學生姓名
  sex        char(1) not null,--性別
  age        number(2) not null,--年齡
  classno    varchar2(7) not null,--班號:'C'+年級(4位數)+班級序號(2位數)
  stuaddress varchar2(100) default '地址未錄入',--地址 (2)
  grade      char(4) not null,--年級
  enroldate  date,--入學時間
  idnumber   varchar2(18) default '身份證未采集' not null--身份證
)-- Add comments to the table 
comment on table STUDENT.stuinfo --(4)
  is '學生信息表';
-- Add comments to the columns 
comment on column STUDENT.stuinfo.stuid -- (5)
  is '學號';
comment on column STUDENT.stuinfo.stuname
  is '學生姓名';
comment on column STUDENT.stuinfo.sex
  is '學生性別';
comment on column STUDENT.stuinfo.age
  is '學生年齡';
comment on column STUDENT.stuinfo.classno
  is '學生班級號';
comment on column STUDENT.stuinfo.stuaddress
  is '學生住址';
comment on column STUDENT.stuinfo.grade
  is '年級';
comment on column STUDENT.stuinfo.enroldate
  is '入學時間';
comment on column STUDENT.stuinfo.idnumber
  is '身份證號';

代碼解析:

(1)處: not null 表示學號字段(stuid)不能為空。

(2)處:default 表示字段stuaddress不填時候會默認填入‘地址未錄入’值。

(3)處:表示表stuinfo存儲的表空間是users,storage表示存儲參數:區段(extent)一次擴展64k,最小區段數為1,最大的區段數不限制。

(4)處:comment on table 是給表名進行注釋。

(5)處:comment on column 是給表字段進行注釋。

往表中添加一個字段,默認為一個表中的某一個值

alter table PROJ add suggest AS (UPPER("PROJ_ID"))

comment on column PROJ.Suggest is '模糊搜索列格式:xxx|yyy|zzz;默認:PROJ_ID';

2、刪除表(慎用)

 drop  table 表名

3、給表加表備注

 comment on table 表名 is 'XXXXXX'

4、給表字段加備注

 comment on column 表名.字段名 is 'XXXXX'

5、修改表字段屬性

 alter table 表名 modify (字段名 字段類型 默認值 是否為空);

6、刪除表字段

 alter table 表名 drop column 字段名

7、添加表字段 

 格式:alter table 表名 add (字段名 字段類型 默認值 是否為空);
 
 例:alter table sf_users add (userName varchar2(30) default '' not null);

8、修改表字段名稱

 alter table 表名 rename column 舊列名 to 新列名;

9、修改表名

 alter table 舊表名 rename to 新表名


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM