sql 高级 (五)(create index(建立索引) drop)

SQL create index 语句数据库

        create  index语句用于在表中建立索引。缓存

        在不读取整个表的状况下,索引使数据库应用程序能够更快地查找数据。ide

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

        更新一个包含索引的表须要比更新一个没有索引的表更多的时间,这是因为索引自己也须要更新。性能

         理想的作法是仅仅在经常被搜索的列(以及表)上面建立索引。spa

容许使用重复的值:
orm

        create index  index_name对象

        ON table_name (column_name)索引

SQL create unique index 语法rem

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

        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"。

相关文章
相关标签/搜索