当前位置:网站首页>[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
边栏推荐
- 实施阴影介绍
- From a professional background, I can't get into a small company for interview
- Viewing technological changes through Huawei Corps (VI): smart highway
- 微信核酸检测预约小程序系统毕业设计毕设(5)任务书
- 好评率计算
- MySQL -- basic concept of database
- Simple understanding of cardinality sorting
- Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
- [games101] operation 4 B é zier curve
- 如何下载微信支付证书(API证书)
猜你喜欢
再放宽!这些应届生,可直接落户上海
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
Wechat applet video sharing platform system graduation design completion (1) development outline
Easyai notes - machine learning
Embedded ~ introduction
Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline
详解Kubernetes网络模型
Wechat applet video sharing platform system graduation design (2) applet function
NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution
Mysql - opérations de base de la base de données
随机推荐
Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
Explain kubernetes network model in detail
Modbus protocol communication exception
深入理解ThreadLocal
【Golang | gRPC】使用openssl生成证书
一个优秀程序员可抵五个普通程序员!
2020互联网行业术语
微信小程序视频分享平台系统毕业设计毕设(1)开发概要
em120.gige. h
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
Yingguang MCU development case
vimium映射鍵
Embedded ~ introduction
515. Find the maximum value in each tree row
Web chat tool
架构设计——ID生成器「建议收藏」
Use dosbox to run the assembly super detailed step "suggestions collection"
From a professional background, I can't get into a small company for interview
微信小程序视频分享平台系统毕业设计毕设(8)毕业设计论文模板
ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null