当前位置:网站首页>[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
边栏推荐
- Three methods of MySQL backup
- Wechat nucleic acid detection appointment applet system graduation design (2) applet function
- Use dosbox to run the assembly super detailed step "suggestions collection"
- PHP gets the number of days, hours, minutes and seconds between the two timestamps
- RDK仿真实验
- 深入理解ThreadLocal
- MySQL进阶-事务及索引
- Renren potential field method
- Wechat applet video sharing platform system graduation design completion (8) graduation design thesis template
- 【西北工业大学】考研初试复试资料分享
猜你喜欢

Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用

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

使用Zadig从0到1搭建持续交付平台

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"

Redisson high performance redis distributed lock source code analysis

Wechat applet video sharing platform system graduation design completion (6) opening defense ppt

Intelligent hydropower meter energy consumption monitoring cloud platform

Explain kubernetes network model in detail

Wechat applet video sharing platform system graduation design (2) applet function

微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
随机推荐
Remember to use ternary expressions when switching transformations
SteamOS 3.3 Beta 发布,Steam Deck 中文键盘终于来了
From a professional background, I can't get into a small company for interview
Interview, about thread pool
再放宽!这些应届生,可直接落户上海
C # detect whether the picture is rotated and modified to the true rotation
能解决80%故障的排查思路
515. Find the maximum value in each tree row
ESP32-C3入门教程 问题篇⑩——error: implicit declaration of function ‘esp_blufi_close‘;
Vi/vim delete: one line, one character, word, the first character of each line command
MySQL -- basic operation of database
Develop a controller that prohibits deleting namespaces
Detailed explanation of map set
Web chat tool
Graduation summary
In Linux, MySQL sets the job task to start automatically
Nvidia 显卡 Failed to initialize NVML Driver/library version mismatch 错误解决方案
Pychar modify pep8 e501 line too long > 0 characters
Bluetooth technology | new working mode of wearable devices of the Internet of things, and Bluetooth ble helps the new working mode
好玩的免费GM游戏整理汇总