当前位置:网站首页>[Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
[Oracle final review] addition, deletion and modification of tablespaces, tables, constraints, indexes and views
2022-07-02 18:21:00 【Learn programming_ It's Alun】
establish
1. Table space
create tablespace spaceName
datafile 'C:\spaceName.dbf' -- File storage path
size 20M -- Initial size -- At this point, you can create a table space
autoextend on next 5M -- Automatic growth every time 5M
maxsize 50M; -- Maximum 50M
-- Add a new data file for the tablespace
alter tablespace spaceName
add datafile 'C:\spaceName2.dbf'
size 10M;
2. surface 1
-- Create table ( And added demonstration constraints )
create table tableName(
id number(4) [primary key], -- Field Field data type
name varchar(4) [not null],
sex char(2) [check (sex in(' male ',' Woman '))], -- Define when you create a table check constraint
age number(3) [default '18'], -- The default value is
tableId number(4) references tableName2(table2Id) -- This foreign key
)[tablespace spaceName] -- You can select the specified tablespace
-- surface 2 To demonstrate foreign keys
create table tableName2(
table2Id number(4) [primary key]
)[tablespace spaceName]
2.1 Add new column ( The table field has been created and you want to add a new field )
alter table tableName add city varchar(10);-- city new field name varchar(10) data type
3. Adding constraints Don't name constraints oracle Will automatically give a name It is recommended to name manually
-- Add constraints to the table fields that have been created
-- Primary key primary key
alter table tableName
add [constraint pk_id] primary key(id[,name[,...]]);
-- check constraint for example If you want to limit gender, you can only fill in male and female
alter table tableName
add [constraint ck_sex] check (sex in(' male ',' Woman ')); -- The constraint name can be written or not
-- Non empty constraint not null constraint
alter table tableName modify name not null; -- For the table name Add a non NULL constraint to the field
-- unique constraint Unique constraint
alter table tableName add unique(name);
-- Foreign keys foreign key constraint ( You need to use the primary key of another table )
alter table tableName add [constraint fk_tableId] foreign key(tableId) references tableName2(table2Id); -- Of course, the foreign key fields to be specified in different tables can be the same Here's a demonstration
4. Create index
-- 4.1. establish B Tree index
create index idx_name on tableName(name) tablespace spaceName; -- For this table name Create a file called ‘idx_name’ Of B Tree index , B Tree index is the default index
-- 4.2. Create bitmap index
create bitmap index idx_sex on tableName(sex) tablespace spaceName;
5. Create view
-- Create a simple view ( You only want to display three fields of a table name sex age)
create [or replace] view view_name as select name,sex,age from tableName;
-- or replace : Replace the existing view if it exists
modify
1. Table space
-- Modify the automatic extension of data files
alter database datafile 'C:\spaceName.dbf' autoextend on next 5M maxsize 100M;
-- Rename a table space
alter tablespace old_tablespaceName rename to new_tablespaceName;
-- Modify the size of the tablespace data file
alter database datafile 'C:\spaceName.dbf' resize 100M;
-- The table space has 4 States modify state
alter tablespace new_tablespaceName offline/ online/ read only/ read write; -- offline / On-line / read-only / Can read but write Change to read-only must be online state
-- Modify the status of the data file There are three kinds of :online | offline | offline drop
alter database datafile 'C:\spaceName.dbf' online | offline | offline drop;-- Documents are available | File not available Used for data files in archive mode | File not available Used for data files in non archive mode
2. surface
-- 2.1 Change column names old_col_name Old field name ,new_col_name: new field name
alter table tableName rename column old_col_name to new_col_name;
-- 2.2 Modify the data type corresponding to the column varchar -> varchar2
alter table tableName modify city varchar2;
-- 2.3 rename table new_col_name : The new name of the table
-- The way 1:
alter table tableName rename to new_col_name;
-- The way 2:
rename tableName to new_col_name;
-- 2.4 Move table ( Move the table to a new table space If you do not specify the default storage to the default tablespace )
-- newSpace New tablespace name
alter table tableName move tablespace newSpace;
-- Query whether the move is successful among tableName It refers to the table name to be queried Other defaults remain unchanged
select table_name,tablespace_name from user_tables where table_name = 'tableName';
3. Indexes
-- Rename index
alter index idx_name rename to new_idx_name;
-- Merge index
alter index new_idx_name coalesce [ deallocate unused]; -- Merge index [ Free up extra space after merging while merging indexes ]
-- Clear index fragments / Rebuild index
alter index new_idx_name rebuild; -- Rebuild a new index , Delete the original index
4. Update the view
-- If you only add a field to the original view Can directly or replace Create a new view
-- In itself orable According to the base table DML Operation automatically determines the update
Delete
1. Table space
-- 1.1 Delete the data file of the tablespace
alter tablespace spaceName drop datafile 'C:\spaceName2.dbf';
-- 1.2 Delete tablespace
drop tablespace spaceName |[including contents]|[including contents and datafiles]; -- Delete tablespace spaceName| Keep data files | Delete data files and all contents
2. surface
-- 2.1 Delete table
drop table tableName [cascade constraint][purge] ; -- Delete table [ And delete the view constraint index and trigger of this table ][ Free the occupied resource space immediately after deleting the table ]
-- 2.2 Delete the new column city
alter table tableName drop column city;
-- Delete more than one at a time
alter table tableName drop(city,city1,city2,...)
3. constraint
-- Delete constraints
drop constraint constraintName; -- Delete constraint according to constraint name
-- Delete primary key constraint primary key
alter table tableName drop constraint pk_id;
-- Delete check constraint check
alter table tableName drop constraint ck_sex;
-- Delete non empty constraints not null front name Field defines a non empty constraint
alter table tableName modify name null;
-- Delete unique constraint unique front name Fields define unique constraints
alter table tableName drop unique(name);
-- Delete foreign key constraint foreign key
alter table tableName drop constraint fk_tableId;
drop index indexName ; -- Delete index
4. Indexes
-- Delete index
drop index Index name ;
5. View
drop view viewName ; -- Delete view
边栏推荐
- 微信小程序视频分享平台系统毕业设计毕设(8)毕业设计论文模板
- matplotlib的安装教程以及简单调用
- 如何下载微信支付证书(API证书)
- Yingguang MCU development case
- Viewing technological changes through Huawei Corps (VI): smart highway
- [golang | grpc] use grpc to realize simple remote call
- Typescript
- Tower safety monitoring system unattended inclination vibration monitoring system
- Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
- MySQL -- basic operation of database
猜你喜欢

Remember to use ternary expressions when switching transformations

Wechat applet video sharing platform system graduation design completion (7) Interim inspection report

Does pytorch support 32 bits?

微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT

MySQL -- basic operation of database

wait_ for_ Gap -- restore archive from primary archive to secondary Archive

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

Ora-19838 -- restore control files to the standby database

微信核酸检测预约小程序系统毕业设计毕设(3)后台功能

Pit encountered during installation of laravel frame
随机推荐
人人工势场法
Vimium mapping key
Tower safety monitoring system unattended inclination vibration monitoring system
C # detect whether the picture is rotated and modified to the true rotation
使用NPOI导出Excel文件
Mysql 备份的三种方式
Wechat nucleic acid detection appointment applet system graduation design completion (5) task statement
微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
Ora-19838 -- restore control files to the standby database
paddlepaddle 28 搭建基于卷积的自动编码机
架构设计——ID生成器「建议收藏」
【Golang | gRPC】使用gRPC实现简单远程调用
Develop a controller that prohibits deleting namespaces
Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
Nvidia 显卡 Failed to initialize NVML Driver/library version mismatch 错误解决方案
Outsourcing for five years, abandoned
能解决80%故障的排查思路
Android cycle timer implementation, to achieve fixed Android cache cleaning
567. Arrangement in string
Does pytorch support 32 bits?