New, delete, and modify indexes in SQL server

First create a basic student table for the following test data
-student information table

CREATE TABLE Student(
   Sno char(9) primary key,
	 Sname char(20) unique,
	 Ssex char(2),
	 Sage smallint,
	 Sdep char(20)
)

insert into Student values ('201215121','李勇','男',20,'CS');
insert into Student values ('201215122','刘晨','女',19,'CS');
insert into Student values ('201215123','王敏','女',18,'MA');
insert into Student values ('201215125','张立','男',19,'IS');

select * from Student

1. Create
statement: create <unique | cluster> index <index name> on <table name> <column name, order>

Create index Stusno for table Student

create unique index Stusno on Student (Sno asc,sAGE desc)

Where unique indicates that each index value of this index only corresponds to a unique data record.
Cluster indicates that the index to be built is a clustered index.

2. Delete
statement: drop index index name on table name
delete index Stusno

DROP INDEX Stusno ON Student

3. Modifying the index name
alter cannot change the index name. If you change the index name, you need to call a stored procedure.

The error code is as follows:
Statement: alter index <old index name> rename to <new index name>
alter index Stusno rename to Stu

The code for calling the stored procedure is as follows:

1、EXEC sp_rename @objname = 'student.Stu', @newname = 'Stusno', @objtype = 'index'

2、EXEC sp_rename 'Student.Stusno', 'Stu', 'index'

Note: Changing any part of the object name may break scripts and stored procedures.

At this time the command has been successfully executed.

Guess you like

Origin blog.csdn.net/Y_6155/article/details/106277628