MS SQL common SQL statements (3): create, modify, delete indexes, views, functions, etc. sql

1. Index operation:

--1. View the index of the specified table
--Format: exec sp_helpindex 'tableName';
exec sp_helpindex 'employee';

--2. New index:
--Format:
--Create [Unique][Clustered][NonClustered] index index_name On [Table/View] (Column[Asc/Desc])......
--Unique: Unique index
--Clustered: Clustered index (default)
--NonClustered: non-clustered index
--Table/View: Indexes can be built on tables or views.
create unique index uniqueIndex on employee(empName asc);

--3. Rename the index:
--格式:exec sp_rename 'TableName.indexName','newIndexName','index';
exec sp_rename 'employee.uniqueIndex','uniqueIndex2','index';

--4, delete the index:
--Format: drop index TableName.indexName;
drop index employee.uniqueIndex2;

2. View operation:

View classification: standard view, indexed view, partitioned view

--1. Create a view:
--格式:Create View viewName as select_statement;
create view empView as select  * from employee;

--2. Modify the view:
--Format: alter view viewName as select_statement;
alter view empView as select  * from employee where empAge>30;

--3, delete the view:
---Format: drop view viewName;
drop view empView;

--4, view the view:
SELECT * FROM sysobjects WHERE (xtype = 'V');
--xtype can be replaced with the following parameters:
--C=CHECK constraint
--D = default or DEFAULT constraint
--F = FOREIGN KEY constraint
--L=log --FN=scalar function
--IF = inline table function
--P = stored procedure
--PK = PRIMARY KEY constraint (type is K)
--RF=Replicate Filter Stored Procedure
--S = system table
--TF = table function
--TR = trigger
--U = user table
--UQ=UNIQUE constraint (type is K)
--V = view
--X = extended stored procedure

3. Function operation:

--1, create a function
--grammar structure:
--create Function function name (@parameter variable type [, @parameter variable type])
--Returns variable type
--as
--begin
-- command line and program blocks
--end
create function testFunc(@testA int,@testB int)
returns int
as
begin
	declare @testSum int;
	set @testSum = @testA+@testB;
	return @testSum;
end

--2, view function
select * from sysobjects where xtype='fn'

--3, use function
select dbo.testFunc(100,200);

--4, modify the function
--The modification is consistent with the creation syntax structure, that is, the created keyword can be modified to the alter keyword. As for the business logic, it can be modified according to the actual situation.
alter function testFunc(@testA int,@testB int)
returns int
as
begin
	declare @testSum int;
	set @testSum = @testA+@testB;
	set @testSum = @testSum*2;
	return @testSum;
end

--5, rename the function
--Syntax structure: exec sp_rename function original name, function new name;
exec sp_rename testFunc,testFunc2;
-- may generate a warning: changing any part of the object name may break scripts and stored procedures.

--6, delete function
--Syntax structure: exec procedure function name [, function 2 name, function 3 name];
drop function testFunc;
drop procedure testFunc2,testFunc3;

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326557937&siteId=291194637