oracle数据库-创建表空间

创建表空间

create tablespace tb_space
DATAFILE 'D:\xxx.DBF'
size 100M
AUTOEXTEND ON
PERMANENT

创建用户

create user testUser
IDENTIFIED BY 123456
default tablespace tb_space

授予用户权限

grant connect,resource to testUser;

登陆testUser

sqlplus testUser/123456

创建表

create table testTable(
    id number(15) not null,
    accountName varchar2(30) null,
    tname varchar2(50) null,
    money number(8,1) null,
    cztime Date default sysdate null,
    czr varchar2(20) not null,
    fjm char(3) null,
    --增加主键
    CONSTRAINT pk_id primary key(id)
);

在该表中,如果id需要设置为自增列,由于oracle没有自增列的设置,只有通过序列和触发器实现,代码如下:

--创建序列
create SEQUENCE seq_test_table increment by 1 start with 1 minvalue 1 maxvalue 9999999999 nocache order;
--创建触发器
create or replace trigger trigger_test_table
before insert
on testTable
for each row
begin
    select seq_test_table .nextval into:new.id from sys.dual;
end;

创建表还可以用查询一个表来创建新表,此方法也可以用于备份某个表

--将会创建new_table并复制testTable中的所有数据
create table new_table as select * from testTable;
--如果不复制数据,仅仅拷贝表结构
create table new_table as select * from testTable where 1=2;
--可以使用NOLOGGING选项来禁用重做日志,这样在插入一条新数据时候不会重做日志信息
create table new_table as select * from testTable NOLOGGING;
--使用以上方法创建的表不会复制testTable中约束条件以及默认值

表创建完成,可以对表进行修改,例如删除列,增加列,修改列属性等操作

--新增列
alter table testTable add newColum varchar2(20) not null;
--修改列
alter table testTable modify newColum varchar2(30) null;
/*删除列,有两种方法
1.直接删除
2.使用unused标记,在数据库空闲时使用
alter table table_name drop unused columns;
*/
alter table testTable drop newColum;
--如果存在相关约束则使用CASCADE CONSTRAINTS删除约束才能成功删除列
alter table testTable drop newColum CASCADE CONSTRAINTS;

重命名表可以使用RENAME或ALTER TABLE

--使用rename
rename testTable to new_testTable;
--使用alter table
alter table testTable rename to new_testTable;

为列添加约束可以在建表时候添加,也可以在表创建成功后添加

--建表时候
create table tb(
id number(9,0) not null,
sex char(1) not null,
CONSTRAINT pk_id primayr key(id) validate,
CONSTRAINT check_sex CHECK(sex in('1','0')) validate
);
--在建表后添加约束
alter table tb add CONSTRAINT pk_id primayr key(id);
--删除约束
alter table tb drop CONSTRAINT check_sex;
--修改约束名称
alter table tb rename CONSTRAINT check_sex to new_check_sex;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值