当前位置:网站首页>【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 ; -- 删除视图
边栏推荐
- Web聊天工具
- 如何开启IDEA的Run Dashboard功能
- Architecture design - ID generator "suggestions collection"
- Yingguang pmc131 SOP16 16pin eight bit MCU
- Songhan sn8p2511 sop8 single chip microcomputer can be used for burning, providing single chip microcomputer scheme development and single chip microcomputer decryption
- Please, stop painting star! This has nothing to do with patriotism!
- Win10 uninstall CUDA
- 铁塔安全监测系统 无人值守倾角振动监测系统
- Yingguang MCU development case
- 微信小程序视频分享平台系统毕业设计毕设(1)开发概要
猜你喜欢

SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了

MySQL --- 数据库的基本概念

Enter a valid user name and password in the Microsoft LDAP configuration page, and enter a valid user name in the Microsoft LDAP configuration page

Pit encountered during installation of laravel frame

Hbuilderx runs to the mobile phone or simulator and prompts that the device is not found

Wechat applet video sharing platform system graduation design completion (4) opening report

Aloam code reading and summary

pycharm 修改 pep8 E501 line too long > 0 characters

NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution

Microsoft LDAP 配置页中输入有效的用户名及密码,microsoft ldap 配置页中输入有效的用户名
随机推荐
Export Excel files using npoi
Tower safety monitoring system unattended inclination vibration monitoring system
Microsoft LDAP 配置页中输入有效的用户名及密码,microsoft ldap 配置页中输入有效的用户名
Finally detailed explanation
A good programmer is worth five ordinary programmers!
Yingguang pmc131 SOP16 16pin eight bit MCU
巴比特 | 元宇宙每日必读:一千块就能买一个虚拟主播?这是小企业的直播福音还是在“割韭菜”?...
Typescript
Taiwan Feiling fm8pb513b MCU provides MCU program development product design
MySQL进阶-事务及索引
微信核酸检测预约小程序系统毕业设计毕设(5)任务书
How can you omit a large number of switch statements
From a professional background, I can't get into a small company for interview
In Linux, MySQL sets the job task to start automatically
MySQL advanced - transaction and index
【Zuul】com. netflix. zuul. exception. ZuulException: Hystrix Readed time out
Embedded development board ~ description
D constructor problem
Deep understanding of ThreadLocal
iframe嵌套详解