芯が強い人になるESTJ-A

# DB2数据库表索引删除命令详解

IT開発 Tags: 无标签 阅读: 176

DB2数据库是一种关系型数据库,它的表索引是实现数据存储和检索的重要组成部分。因为一个表中可能包含数万行数据,查询这些数据时需要用到索引来快速定位。但有时候,我们需要删除表中的某个索引,这时需要掌握相应的命令方法。

本文将详细介绍在DB2数据库中删除表索引的命令,包括删除单个索引和批量删除索引。

db2 => !date
2021年  7月  2日 金曜日 16:32:11 JST

実行は、以下のシンタックスで行う。
db2 -tvf ファイル名
-t:終端文字の指定。デフォルトがセミコロン。
-v:入力コマンドを標準出力にエコーする。
-f: 入力ファイル

一、删除单个索引

在DB2数据库中,删除单个索引的命令格式如下:

“`

DROP INDEX ON

“`

其中,为需要删除的索引名称,为索引所在的表名称。

例如,我们要删除名为“index_name”的索引,它在表“table_name”中,可以使用以下命令:

“`

DROP INDEX index_name ON table_name

“`

执行该命令后,即可将指定的索引从表中删除。需要注意的是,删除索引并不会删除表中的数据,只是删除了索引这个数据结构。

二、批量删除索引

如果需要删除表中多个索引,一个个手动执行命令显然很费时费力。在这种情况下,可以使用批量删除索引的命令,语法如下:

“`

DROP INDEX ,,… ON

“`

其中,至为需要删除的多个索引名称,用逗号分隔,为这些索引所在的表名称。

例如,要删除表“table_name”中的三个索引,它们分别命名为“index_name1”、“index_name2”和“index_name3”,可以使用以下命令:

“`

DROP INDEX index_name1, index_name2, index_name3 ON table_name

“`

执行该命令后,即可将指定的多个索引从表中批量删除。

常用命令:
连接到数据库:
db2 connect to DATABASE_NAME USER USERNAME USING PASSWORD
1
断开数据库连接:
db2 connect reset
1
显示当前连接的所有表:
db2 list tables
1
显示表结构:
db2 describe table TABLE_NAME
1
运行一个SQL文件:
db2 -tvf filename.sql
1
显示当前Schema中所有的表:
db2 list tables for schema SCHEMA_NAME
1
显示当前Schema中所有的视图:
db2 list tables for all for schema SCHEMA_NAME

数据库备份
完全备份(Full Backup):

db2 backup database DATABASE_NAME to <backup_directory>

数据库还原
完全还原是指将完全备份的数据恢复到数据库中,并覆盖现有的数据和对象。

db2 restore database DATABASE_NAME from <backup_directory> replace existing
1

表空间还原(Tablespace Restore):
表空间还原是指只将指定的表空间(包含一组相关表和索引)恢复到数据库中。它可以用于修复或恢复特定的表空间级别的数据。

db2 restore database DATABASE_NAME tablespace (TABLESPACE_NAME) from <backup_directory> replace existing

--重构表
reorg table studentinfo;

2.刷新表(REORG)

对数据库的表结构进行修改之后,必须进行reorg操作,不然会出现表不活跃甚至锁表的情况,直观的结果便是无法增删改数据,甚至查询也会报错。这种情况的db2错误码一般情况下都是:57016。

刷新表当然是reorg命令,但是在不同的地方(操作的目标对象上)命令也会有所差异。

在服务器上使用命令行刷新表:


--创建数据库
create database Etp;


--连接数据库
connect to Etp;


--断开连接
disconnect Etp;


--查看当前数据库下有哪些表
list tables;


--建表
create table studentInfo(
 stuno char(5) not null,
 stuname varchar(8),
 stubirth date
);


--查看表结构
describe table studentinfo;


--新增表字段
alter table studentinfo add stutel int;
alter table studentinfo add abc int;


--修改字段类型
alter table studentinfo alter column stutel set data type char(11);


--删除字段
alter table studentinfo drop column abc;


--增加一个非空约束
alter table studentinfo alter column stuname set not null;

 

--重构表
reorg table studentinfo;

 

--增加一个唯一约束
alter table studentinfo alter column stutel set not null;
alter table studentinfo add constraint un_stutel unique(stutel);

 

--添加检查约束
alter table studentinfo add column stuAge int;
alter table studentinfo add constraint ch_stuAge check(stuAge > 0 and stuAge <150);

 

--添加主键约束
alter table studentinfo add constraint pk_stuno primary key(stuno);

 

--删除表
drop table studentinfo;

 

--创建表的同时添加约束方式1
create table studentinfo(
 stuNo int not null,
 stuName varchar(8) not null,
 stuAge int,
 stuTel char(8),
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150)
);

 

--创建表的同时添加约束方式2
create table studentinfo(
 stuNo int not null primary key,
 stuName varchar(8) not null unique,
 stuAge int check(stuAge >=0 and stuAge <150),
 stuTel char(8)
);

 

--添加主外键
--新增班级表
create table classInfo(
 classId int not null primary key,
 className varchar(20)
);

 


--建表的同时添加外键
create table studentinfo(
 stuNo int not null,
 stuName varchar(8) not null,
 stuBirth date not null,
 stuAge int,
 stuTel char(8),
 fclassId int,
 stuBirth date not null,
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150),
 constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);


-- 自增
create table studentinfo(
 stuNo int not null generated always as identity(start with 1 ,increment by 1),
 stuName varchar(8) not null,
 stuAge int,
 stuTel char(8),
 fclassId int,
 stuBirth date not null,
 constraint pk_stuNo primary key(stuNo),
 constraint un_stuName unique(stuName),
 constraint ch_stuAge check(stuAge >=0 and stuAge <150),
 constraint fk_fcalssId foreign key(fclassid) references classInfo(classId)
);


--先建表再添加外键
alter table studentinfo add constraint fk_classId foreign key(fclassid) references classInfo(classId);


--从系统表中查询约束名
select constname, tabname, refkeyname, reftabname, colcount, deleterule, updaterule from syscat.references;


--插入
insert into classinfo values(1,'ETP-1');
insert into studentInfo values(1,'xy',20,'12345',1,'1990-02-28');

--不是全部插入则需要写列名
insert into studentinfo(stuNo,stuName,stuTel) values(2,'wj','111');

                  
-- 有自增长的列要写清楚列名
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth) values('xy',20,'12345',1,'1990-02-28');
insert into studentinfo(stuName,stuAge,stuTel,fclassid,stuBirth) values('tom',22,'12345',2,'1990-02-28');


--更新
update studentinfo set stuBirth = '1990-02-21' where stuName='xy';
update studentinfo set stuBirth = '1990-02-21',stuAge = 21 where stuName='xy';


--删除
deleted from studentinfo where stuName='xy';


--查询
select * from studentinfo where stuName='xy';
select stuName,stuAge from studentinfo;


--别名查询
select stuName as 姓名,stuAge as 年龄 from studentinfo;
select s.stuName as 姓名,s.stuAge as 年龄 from studentinfo s;


--运算查询
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s;


--串联运算查询
select stuName||stuAge from studentinfo;


--and 和 or
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuName='xy' and s.stuAge=20;
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuName='xy' or s.stuAge=20;


--null
select * from studentinfo where stuAge is null; 
select * from studentinfo where stuAge is not null; 

 

--between and 包括边界 相当于>=和<=s
select s.stuName as 姓名,s.stuAge+5 as 年龄 from studentinfo s where s.stuAge between 10 and 20


--in
select * from studentinfo  where stuName in ('xy','wj');
select * from studentinfo  where stuName not in ('xy','wj');


--模糊查询 like%,%表示多个字符
select * from studentinfo where stuName like 'x%'

 

--模糊查询 like_ , _表示单个字段
select * from studentinfo where stuName like 'x_';

 

--排序 order by
select * from studnetinfo order by fclassid desc;
select * from studnetinfo order by fclassid asc;


--distinct去掉重复
select distinct stuAge as 年龄 from studentinfo;


--group by,使用的时候,select 后面只能加2种字段: 1.group by 后面出现的,2.聚合函数

select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo group by fclassid;


-having 在分组的基础上过滤,出现顺序where-group by-having

select fclassId as 班级号,count(stuName) as 学生个数 from studentinfo group by fclassid having count(StuName)>=2

7.服务器上执行sql文件
db2 -tvf 文件名;