oracle删除索引主键的sql语句,sql 高级 (五)(create index(创建索引) drop)

SQL create index 语句

create  index语句用于在表中创建索引。

在不读取整个表的情况下,索引使数据库应用程序可以更快地查找数据。

用户无法看到索引,它们只能被用来加速搜索/查询。

更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。

理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。

允许使用重复的值:

create index  index_name

ON table_name (column_name)

SQL create unique index 语法

在表上创建一个唯一的索引。唯一的索引意味着两个行不能拥有相同的索引值。

create unique index index_name

ON table_name (column_name)

create index 实例

create index PersonIndex

ON Person (LastName)

以降序索引某个列中的值,添加保留字 DESC:

create index PersonIndex

ON Person (LastName DESC)

为多个列添加索引

create index PersonIndex

ON Person (LastName, FirstName)

SQL 撤销索引、表以及数据库

SQL   drop   index 语句

用于 Microsoft SQLJet (以及 Microsoft Access) 的语法:

drop   index   index_name ON table_name

用于 MS SQL Server 的语法:

drop   index   table_name.index_name

用于 IBM DB2 和 Oracle 语法:

drop   index   index_name

用于 MySQL 的语法:

alter   table   table_name   drop   index   index_name

SQL   truncate  table语句

除去表内的数据,但并不删除表本身

truncate   table  表名称

SQL ALTER TABLE 语句

alter  table语句用于在已有的表中添加、修改或删除列。

在表中添加列

alter table table_name

ADD column_name datatype

删除表中的列

alter table table_name

drop column column_name

注释:某些数据库系统不允许这种在数据库表中删除列的方式 (DROP COLUMN column_name)。

改变表中列的数据类型

alter table table_name

alter column column_name datatype

auto-increment

Auto-increment 会在新记录插入表中时生成一个唯一的数字。

在每次插入新记录时,自动地创建主键字段的值。

MySQL 的语法

CREATE TABLE Persons

(

P_Id int not null auto_increment,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Address varchar(255),

City varchar(255),

primary key(P_Id)

)

MySQL 使用 AUTO_INCREMENT 关键字来执行 auto-increment 任务。

默认地,AUTO_INCREMENT 的开始值是 1,每条新记录递增 1。

要让 auto_increment 序列以其他的值起始

alter table Persons auto_increment=100

SQL Server 的语法

CREATE TABLE Persons

(

P_Id int primary key identity,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Address varchar(255),

City varchar(255)

)

MS SQL 使用 IDENTITY 关键字来执行 auto-increment 任务。

默认地,IDENTITY 的开始值是 1,每条新记录递增 1。

要规定 "P_Id" 列以 20 起始且递增 10,请把 identity 改为identity(20,10)

要在 "Persons" 表中插入新记录,我们不必为 "P_Id" 列规定值(会自动添加一个唯一的值):

INSERT INTO Persons (FirstName,LastName)

VALUES ('Bill','Gates')

Access 的语法

CREATE TABLE Persons

(

P_Id int primary autoincrement,

LastName varchar(255) NOT NULL,

FirstName varchar(255),

Address varchar(255),

City varchar(255)

)

MS Access 使用 AUTOINCREMENT 关键字来执行 auto-increment 任务。

默认地,AUTOINCREMENT 的开始值是 1,每条新记录递增 1。

要规定 "P_Id" 列以 20 起始且递增 10,请把 autoincrement 改为 autoincrement(20,10)

Oracle 的语法

必须通过 sequence 对创建 auto-increment 字段(该对象生成数字序列)。

请使用下面的 create sequence 语法:

create sequence seq_person

minvalue 1

start with 1

increment by 1

cache 10

上面的代码创建名为 seq_person 的序列对象,它以 1 起始且以 1 递增。该对象缓存 10 个值以提高性能。

cache 选项规定了为了提高访问速度要存储多少个序列值。

要在 "Persons" 表中插入新记录,必须使用 nextval 函数(该函数从 seq_person 序列中取回下一个值):

insert into Persons (P_Id,FirstName,LastName)

values (seq_person.nextval,'Lars','Monsen')

上面的 SQL 语句会在 "Persons" 表中插入一条新记录。"P_Id" 的赋值是来自 seq_person 序列的下一个数字。"FirstName" 会被设置为 "Bill","LastName" 列会被设置为 "Gates"。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值