-- SQL创建联合唯一索引的语法
-- Create a nonclustered index on a table or view
CREATE INDEX i1 ON t1 (col1);
-- Create a clustered index on a table and use a 3-part name for the table
CREATE CLUSTERED INDEX i1 ON d1.s1.t1 (col1);
-- Syntax for SQL Server and Azure SQL Database
-- Create a nonclustered index with a unique constraint
-- on 3 columns and specify the sort order for each column
CREATE UNIQUE INDEX i1 ON t1 (col1 DESC, col2 ASC, col3 DESC);
-- 快速查看表结构
1.sp_help table_name;
如: [sql] sp_help Student;
2.sp_columns table_name;
如: [sql] sp_columns Student;
--merge的用法(待openJson with的语法)
DECLARE @json VARCHAR(max) = '{
"employeeId": 1,
"createTime": "2020-04-09 16:35:12"
}';
MERGE dbo.tEmployeeEmergencyContact AS t -- 需要操作的主体表
USING
(SELECT * FROM OPENJSON(@json) WITH (employeeId BIGINT) ) AS s (employeeId) -- USING里面是数据源,as以后,必须按照顺序指定字段名
ON t.employeeId = s.employeeId -- 这里是对比的条件
WHEN MATCHED THEN
-- 如果有匹配就更新,只用写要更新的内容即可
UPDATE SET t.emergencyContact = '张三',
t.emergencyPhone = '18621704425',
t.modifyTime = GETDATE(),
t.modifierName = 'system'
WHEN NOT MATCHED THEN
-- 没有匹配就新增,注意这里的INSERT不能再跟表名了
INSERT
(
employeeId,
emergencyContact,
emergencyPhone,
creatorName,
createTime,
modifierName,
modifyTime,
isDel
)
VALUES
(1, '张三', '18621704425', 'system', GETDATE(), 'system', GETDATE(), 0);
-- merge必须分号结尾,不然报错
--开启/关闭数据库的执行时间(可以看到执行的时间精确到毫秒)
SET STATISTICS TIME ON
SET STATISTICS TIME OFF
-- 查看表结构(可以直接粘贴到Excel)
SELECT
表名 = Case When A.colorder=1 Then D.name Else '' End,
表说明 = Case When A.colorder=1 Then isnull(F.value,'') Else '' End,
字段序号 = A.colorder,
字段名 = A.name,
字段说明 = isnull(G.[value],''),
标识 = Case When COLUMNPROPERTY( A.id,A.name,'IsIdentity')=1 Then '√'Else '' End,
主键 = Case When exists(SELECT 1 FROM sysobjects Where xtype='PK' and parent_obj=A.id and name in (
SELECT name FROM sysindexes WHERE indid in( SELECT indid FROM sysindexkeys WHERE id = A.id AND colid=A.colid))) then '√' else '' end,
类型 = B.name,
占用字节数 = A.Length,
长度 = COLUMNPROPERTY(A.id,A.name,'PRECISION'),
小数位数 = isnull(COLUMNPROPERTY(A.id,A.name,'Scale'),0),
允许空 = Case When A.isnullable=1 Then '√'Else '' End,
默认值 = isnull(E.Text,'')
FROM
syscolumns A
Left Join
systypes B
On
A.xusertype=B.xusertype
Inner Join
sysobjects D
On
A.id=D.id and D.xtype='U' and D.name<>'dtproperties'
Left Join
syscomments E
on
A.cdefault=E.id
Left Join
sys.extended_properties G
on
A.id=G.major_id and A.colid=G.minor_id
Left Join
sys.extended_properties F
On
D.id=F.major_id and F.minor_id=0
where d.name IN(
'表名'
) --如果只查询指定表,加上此条件
Order By
A.id,A.colorder;