【Oracle 期末复习】表空间、表、约束、索引、视图的增删改

创建

1.表空间
create tablespace spaceName
	datafile 'C:\spaceName.dbf' -- 文件存放路径
	size 20M  -- 初始大小 -- 到这步就已经可以创建一个表空间
	autoextend on next 5M  -- 自动增长每次5M
	maxsize 50M; -- 最大50M
-- 为表空间添加新的数据文件
alter tablespace spaceName
    add datafile 'C:\spaceName2.dbf'
    size 10M;
    

2.1
-- 创建表 (并添加了演示约束)
create table tableName(
	id number(4) [primary key], -- 字段 字段数据类型
	name varchar(4) [not null],
    sex char(2) [check (sex in('男','女'))], -- 创建表时就定义check约束
    age number(3) [default '18'], -- 默认值 
    tableId number(4) references tableName2(table2Id) -- 这个外键
)[tablespace spaceName] -- 可以选择指定的表空间
-- 表2为了演示外键
create table tableName2(
    table2Id number(4) [primary key] 
)[tablespace spaceName]

2.1 添加新列(已经创建表字段想加新的字段)
alter table tableName add city varchar(10);-- city 新字段名 varchar(10) 数据类型

3. 添加约束 不给约束起名字oracle会自动起一个名字 建议手动起名字
-- 为已经创建的表字段添加约束
    -- 主键primary key
    alter table tableName
    add [constraint pk_id] primary key(id[,name[,...]]);
    -- check约束 例如 想要限制性别只能填男和女
    alter table tableName
    add [constraint ck_sex] check (sex in('男','女')); -- 约束名可写可不写
    -- 非空约束not null约束
    alter table tableName modify name not null; -- 为表的name字段添加非空约束
    -- unique 约束 唯一约束
    alter table tableName add unique(name);
    -- 外键 foreign key 约束 (需要用到另一个表的主键)
    alter table tableName add [constraint fk_tableId] foreign key(tableId) references tableName2(table2Id); -- 当然不同表中要指定的外键字段是可以一样的 这里为了演示

4.创建索引
-- 4.1.创建B树索引
create  index idx_name on tableName(name) tablespace spaceName; -- 对这个表的name创建一个名为‘idx_name’的B树索引 , B树索引是默认索引
-- 4.2.创建位图索引
create bitmap index idx_sex on tableName(sex) tablespace spaceName;

5.创建视图
-- 创建简单视图 (只想要显示某个表的三个字段name sex age)
create [or replace] view view_name as select name,sex,age from tableName; 
	-- or replace : 如果存在则替换现有视图

修改

1.表空间
-- 修改数据文件的自动扩展
alter database datafile 'C:\spaceName.dbf' autoextend on next 5M maxsize 100M;
-- 重命名表空间
alter tablespace old_tablespaceName rename to new_tablespaceName;
-- 修改表空间数据文件的大小
alter database datafile 'C:\spaceName.dbf' resize 100M;
-- 表空间有4种状态 修改状态
alter tablespace new_tablespaceName offline/ online/ read only/ read write; -- 离线/在线/只读/可读可写 修改为只读必须是online状态
-- 修改数据文件的状态 有三种:online | offline | offline drop
alter database datafile 'C:\spaceName.dbf' online | offline | offline drop;-- 文件可用|文件不可用 用于数据文件在归档模式| 文件不可用 用于数据文件在非归档模式

2.-- 2.1修改列名 old_col_name 旧字段名,new_col_name:新字段名
alter table tableName rename column old_col_name to new_col_name;
-- 2.2修改列对应的数据类型 varchar -> varchar2
alter table tableName modify city varchar2;
-- 2.3重命名表 new_col_name : 新表名
-- 方式1:
alter table tableName rename to new_col_name;
-- 方式2:
rename tableName to new_col_name;
-- 2.4移动表 (指该表移动到新的表空间 如果不指定默认存储到默认表空间) 
-- newSpace 新表空间名字
alter table tableName move tablespace newSpace;
-- 查询是否移动成功 其中tableName指的是要查询的表名 其他默认不变
select table_name,tablespace_name from user_tables where table_name = 'tableName';

3. 索引
-- 重命名索引
alter index idx_name rename to new_idx_name;
-- 合并索引
alter index new_idx_name coalesce [ deallocate unused]; -- 合并索引 [合并索引的同时释放合并后多余的空间]
-- 清楚索引碎片/重建索引
alter index new_idx_name rebuild; -- 重新建立一个新索引,删除原来的索引

4. 更新视图
-- 如果是只在原视图上的加一个字段 可以直接 or replace 创建个新的视图
-- 本身orable 会根据基表DML操作自动判断更新

删除

1.表空间
-- 1.1删除表空间的数据文件
alter tablespace spaceName drop datafile 'C:\spaceName2.dbf';
-- 1.2删除表空间
drop tablespace spaceName |[including contents]|[including contents and datafiles]; -- 删除表空间spaceName|保留数据文件|删除数据文件和全部内容

2.-- 2.1删除表
drop table tableName [cascade constraint][purge] ; -- 删除表[并删除这个表的视图约束索引和触发器等][删除表后立即释放占用的资源空间]
-- 2.2删除新建的列city 
alter table tableName drop column city; 
-- 一次删除多个
alter table tableName drop(city,city1,city2,...)


3.约束
-- 删除约束
drop constraint constraintName; --  删除约束根据约束名
	-- 删除主键约束primary key
	alter table tableName drop constraint pk_id;
	-- 删除检查约束check 
	alter table tableName drop constraint ck_sex;
	-- 删除非空约束not null 前面name字段定义了非空约束
	alter table tableName modify name null;
	-- 删除唯一约束unique 前面name字段定义了唯一约束
	alter table tableName drop unique(name);
	-- 删除外键约束 foreign key
	alter table tableName drop constraint fk_tableId;
drop index indexName ; -- 删除索引

4.索引
-- 删除索引
drop index 索引名;

5.视图
drop view viewName ; -- 删除视图
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值