当前位置:网站首页>【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
2022-07-02 16:43:00 【学编程_叫Alun】
创建
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 ; -- 删除视图
边栏推荐
- Detailed explanation of map set
- Web聊天工具
- 515. Find the maximum value in each tree row
- Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"
- Aloam code reading and summary
- matplotlib的安装教程以及简单调用
- Vi/vim delete: one line, one character, word, the first character of each line command
- Wasserstein slim gain with clipping penalty (wsgain-cp) introduction and code implementation -- missing data filling based on generating countermeasure network
- 读写 XML/JSON/INI 和 UBJSON 等格式的数据文件的统一接口
- Clé de cartographie vimium
猜你喜欢
Picking up the camera is the best artistic healing
Microsoft LDAP 配置页中输入有效的用户名及密码,microsoft ldap 配置页中输入有效的用户名
使用Zadig从0到1搭建持续交付平台
Viewing technological changes through Huawei Corps (VI): smart highway
WPS inserts a picture and displays it completely
微信小程序视频分享平台系统毕业设计毕设(5)任务书
微信核酸检测预约小程序系统毕业设计毕设(4)开题报告
如何下载微信支付证书(API证书)
Qt Official examples: Qt Quick Controls - Gallery
From a professional background, I can't get into a small company for interview
随机推荐
Unified interface for reading and writing data files in xml/json/ini and ubjson formats
Clé de cartographie vimium
如何下载微信支付证书(API证书)
Freemaker+poi realizes dynamic generation and parsing of Excel files
D constructor problem
MySQL installation and configuration
Pit encountered during installation of laravel frame
Customize a loading instruction
Ora-19838 -- restore control files to the standby database
Calculation of favorable comment rate
MySQL advanced - transaction and index
微信小程序视频分享平台系统毕业设计毕设(5)任务书
A good programmer is worth five ordinary programmers!
深入理解ThreadLocal
MySQL --- 数据库的基本概念
Web chat tool
From a professional background, I can't get into a small company for interview
Aptos教程-参与官方激励测试网(AIT2 激励测试网)
MySQL安装与配置
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用