oracle创建表

转自:https://www.cnblogs.com/diandixingyun/p/11608535.html

一、新表不存在
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 条件不成立时,查询结果为空,只复制表结构,没有任务数据

如果新表与旧表字段不一致,要说明取旧表的哪些字段,赋予新表

posted @ 2021-02-24 16:13  DaXianZ  阅读(422)  评论(0编辑  收藏  举报