1. The statement to create the index
create index index name on table (field 1, field 2,,,,,);
Such as
create index index_AttachFile on AttachFile(CBillGuid,CGuid,DCreateTime);
Indexes created in this default way are nonclustered indexes.
To create a non-clustered index, you can use the nonclustered keyword.
Such as create nonclustered index index name on table (field 1, field 2,,,,,);
To create a clustered index, use the following sql:
CREATE CLUSTERED INDEX CLUSTER_id ON TABLE_name(ID)
If data is added frequently to the table, a clustered index can be defined, so that the newly added data is arranged according to the clustered index, and the access is relatively fast.
2. What indexes does the query table have?
select b.* from sys.sysobjects a, sys.sysindexes b where a.id = b.id and a.name = 表名 and b.rows <>0;
3. Common system tables
4, custom sorting
When there is no suitable sorting field and you need to customize the sorting order, you can use ORDER BY CHARINDEX (<'substring_expression'>, <expression>) to perform custom sorting.
Where substring _expression is the character expression to find, expression can be a string or a column name expression. A value of 0 is returned if no substring is found.
This function cannot be used with TEXT and IMAGE data types.Such as:
- select charindex('cd','abcdefg') --3
- select charindex('ac','abcdefg') --0
Therefore, when custom sorting is required, the following methods can be used:
SELECT * from TaskBudgetBody ORDER BY charindex(State, '1,2,3,4,5,6,7,8,9,0') ASC。
stuff()
- select stuff('abcde',2,3,'mmmm')
- -- ammme
-- Create and return a character by deleting three characters from the first string (abcde) starting at the second position (character b),
-- then inserting the second string at the beginning of the deletion string.
5. Query the maximum value in duplicate data
In a table, query data according to field A, and you will find multiple pieces of data with the same value of field A, and there may be multiple pieces of data with the same value of field A, but these multiple pieces of data, only keep the data with the largest Id A piece of data, the simple sql writing method is:
select max(id) from dic_dqxx
where parentid in (select parentid from dic_dqxx group by parentid having count(parentid) > 0)
group by parentid;
The second way of writing:
select *
FROM TaskAppreCord a where a.dateTime in (select max(b.dateTime) from TaskAppreCord b where b.cTaskGuid=a.cTaskGuid and b.userId = a.userId and b.signTag = a.signTag and b.resType=a.resType)
and a.cTaskGuid='1111111111’ ORDER BY a.signTag ASC
6. Get the fields of the table
select a.name columnname,c.name as typename,
case when a.is_nullable =0 then 'Not Null' else 'Null' end as nullable,
a.*
from sys.columns a , sys.objects b, sys.types c
where a.object_id= b.object_id and b.name='taskhome' and a.system_type_id=c.system_type_id order by a.column_id;
Among them: select * from sys.objects; are all objects in the database, and type_desc determines whether the object is a system table, user table, view or other.
select * from sys.types: field type
select * from sys.columns : The fields of the table are associated with sys.onjects through object_id.
7. Add primary key constraint index
alter table TaskAppreCord
add constraint PK_TaskAppr_1CE36B853EC74557 Primary key (cGuid)