暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

opengauss学习的第9天

原创 hehe 2021-12-09
301

#opengauss的第九天打卡
创建表products, 分别为表创建一个unique索引1,指定b-tree索引2和表达式索引3
omm=# create table products(id int unique,num int);
NOTICE: CREATE TABLE / UNIQUE will create implicit index “products_id_key” for table “products”
CREATE TABLE
omm=# omm=#

omm=# alter table products add column name char(40);
ALTER TABLE
omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
id | integer |
num | integer |
name | character(40) |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default

omm=# create index idx_name on products(upper(name));
CREATE INDEX

omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
id | integer |
num | integer |
name | character(40) |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default
“idx_name” btree (upper(name::text)) TABLESPACE pg_default
“idx_num” btree (num) TABLESPACE pg_default

设置索引1不可用,修改索引2的表空间,重命名索引3
omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
id | integer |
num | integer |
name | character(40) |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default
“idx_name” btree (upper(name::text)) TABLESPACE pg_default
“idx_num” btree (num) TABLESPACE pg_default

omm=# alter index products_id_key UNUSABLE;
ALTER INDEX

omm=# create tablespace tbs1 RELATIVE LOCATION ‘tbs1’;
CREATE TABLESPACE

omm=# alter index idx_name SET TABLESPACE tbs1;
ALTER INDEX

omm=# alter index idx_num rename to idx_numrename;
ALTER INDEX

omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
omm=# id | integer |
num | integer |
name | character(40) |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default
“idx_name” btree (upper(name::text)) TABLESPACE tbs1, tablespace “tbs1”
“idx_numrename” btree (num) TABLESPACE pg_default

重建索引2和products的所有索引
omm=# alter index products_id_key rebuild;
REINDEX

omm=# alter index idx_name rebuild;
REINDEX

omm=# alter index idx_numrename rebuild;
REINDEX

omm=# drop index idx_name;
DROP INDEX

omm=# create index idx_name on products(name);
CREATE INDEX

使用\d+和系统视图pg_indexes查看索引信息
omm=# \d+ products
Table “public.products”
Column | Type | Modifiers | Storage | Stats target | Description
--------±--------------±----------±---------±-------------±------------
id | integer | | plain | |
num | integer | | plain | |
name | character(40) | | extended | |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default
“idx_name” btree (name) TABLESPACE pg_default
“idx_numrename” btree (num) TABLESPACE pg_default
Has OIDs: no
Options: orientation=row, compression=no

omm=# select * from pg_indexes where tablename like ‘products’;
schemaname | tablename | indexname | tablespace | indexdef

------------±----------±----------------±-----------±----------------------------------------------------------

public | products | products_id_key | | CREATE UNIQUE INDEX products_id_key ON products USING btre
e (id) TABLESPACE pg_default
public | products | idx_numrename | | CREATE INDEX idx_numrename ON products USING btree (num) TABLESPACE pg_default
public | products | idx_name | | CREATE INDEX idx_name ON products USING btree (name) TABLESPACE pg_default

删除索引、表和表空间
omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
id | integer |
num | integer |
name | character(40) |
Indexes:
“products_id_key” UNIQUE CONSTRAINT, btree (id) TABLESPACE pg_default
“idx_name” btree (name) TABLESPACE pg_default
“idx_numrename” btree (num) TABLESPACE pg_default

omm=# alter table products drop constraint products_id_key;
ALTER TABLE
omm=# drop index idx_name;
DROP INDEX
omm=# drop index idx_numrename;
DROP INDEX
omm=# drop tablespace tbs1;
DROP TABLESPACE
omm=# \d products
Table “public.products”
Column | Type | Modifiers
--------±--------------±----------
id | integer |
num | integer |
name | character(40) |

omm=# \db
List of tablespaces
Name | Owner | Location
------------±------±---------
pg_default | omm |
pg_global | omm |

最后修改时间:2021-12-09 19:51:53
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论