一、新表不存在
1、基本語法
create table 表名稱 ( id varchar2(50) primary key , name char(200) not null, phone number(11) unique,
class carchar(10),
foreign key (name) )
tablespace USERS ----表放在USERS表空間
pctfree 10 ----保留10%空間給更新該塊數據使用
initrans 1 -----初始化事物槽的個數
maxtrans 255 ----最大事務槽的個數
storage ----存儲參數
(
initial 64K ---區段一次擴展64k
next 1M
minextents 1 ---最小區段數
maxextents unlimited --最大區段無限制
);
2、刪除表之前備份數據(創建備份表)
creact table 新表名稱 as select 字段1,字段2 from 舊表名稱
create table 新表名稱 as select * from 舊表名稱 where 1=2; ---復制結構,不要數據
3、添加列 alter table 表名稱 add (name varchar2(100),code varchar(20));
刪除列 alter table 表名稱 drop (name,code)
4、表重命名 rename table 新表名稱 to 舊表名稱;
varcha2 ----0-4000,可變長度
char() ----0-2000,固定長度,用空格在數據的右邊補到固定長度
number(6,2) ---6位整數、2位小數
number(2) --2位整數
clob ---txt文本
blob ---圖片、視頻、聲音等轉換過來的二進制對象
date ---sysdate
1、添加主鍵約束(將stuNo作為主鍵)
alter table stuInfo
add constraint PK_stuNo primary key (stuNo)
2、添加外鍵約束 (主表stuInfo和從表stuMarks建立關系,關聯字段stuNo)
alter table stuInfo
add constraint FK_stuNo foreign key(stuNo) references stuinfo(stuNo)
3、添加唯一約束(身份證號唯一)
alter table stuInfo
add constraint UQ_stuID unique(stuID)
4、添加默認約束(如果地址不填 默認為“地址不詳”)
alter table stuInfo
add constraint DF_stuAddress default (‘地址不詳’) for stuAddress
5、添加檢查約束 (對年齡加以限定 15-40歲之間)
alter table stuInfo
add constraint CK_stuAge check (stuAge between 15 and 40)
6、添加表注釋:學生信息表
comment on table STUINFO
is '學生信息表';
7、添加列名稱:學號
comment on column STUINFO.stuid
is '學號';
comment on column STUINFO.stuname
is '學生姓名';
二、新表存在
insert
into
新表 select
*
from
舊表; ---兩個表存在字段一樣,復制數據
insert
into
新表 (field1,field2,.....)
select
field1,field2,field3
from
舊表; ---新表只有舊表的部分字段,復制部分字段數據
select
*
into
新表 from
舊表; ---全部數據與結構
select
*
into
新表 from
舊表 where
1=2;---結構
以上只復制數據和結構,不能復制約束/索引等信息
如果where條件滿足時,查詢結果有數據,即復制表數據
如果 where 條件不成立時,查詢結果為空,只復制表結構,沒有任務數據
如果新表與舊表字段不一致,要說明取舊表的哪些字段,賦予新表