sql SQL备忘录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql SQL备忘录相关的知识,希望对你有一定的参考价值。

--如何高效率统计表的行数
SELECT Schema_name(schema_id)   AS [SchemaName],
       [Tables].name            AS [TableName],
       Sum([Partitions].[rows]) AS [TotalRowCount]
FROM   sys.tables AS [Tables]
       JOIN sys.partitions AS [Partitions]
         ON [Tables].[object_id] = [Partitions].[object_id]
            AND [Partitions].index_id IN ( 0, 1 )
-- WHERE [Tables].name = N'name of the table'
GROUP  BY Schema_name(schema_id),
          [Tables].name; 

--查字段是不是在视图里
--1.
SELECT *
FROM   INFORMATION_SCHEMA.VIEW_COLUMN_USAGE AS UsedColumns
WHERE  UsedColumns.VIEW_NAME = 'NameofView' 
--2.
SELECT v.name           AS ViewName,
       c.name           AS ColumnName,
       columnTypes.name AS DataType,
       aliases.name     AS Alias
FROM   sys.views v
       INNER JOIN sys.sql_dependencies d
               ON d.object_id = v.object_id
       INNER JOIN .sys.objects t
               ON t.object_id = d.referenced_major_id
       INNER JOIN sys.columns c
               ON c.object_id = d.referenced_major_id
       INNER JOIN sys.types AS columnTypes
               ON c.user_type_id = columnTypes.user_type_id
                  AND c.column_id = d.referenced_minor_id
       INNER JOIN sys.columns AS aliases
               ON c.column_id = aliases.column_id
                  AND aliases.object_id = Object_id('[SchemaName].[ViewName]')
WHERE  v.name = 'ViewName'; 

--查字段是否在某个表里
--1.
IF Col_length('Person.Address', 'AddressID') IS NOT NULL
  PRINT 'Column Exists'
ELSE
  PRINT 'Column doesn''t Exists' 
--2.
IF EXISTS(SELECT 1
          FROM   sys.columns
          WHERE  Name = N'AddressID'
                 AND Object_ID = Object_id(N'Person.Address'))
  PRINT 'Column Exists'
ELSE
  PRINT 'Column doesn''t Exists' 

--3.
IF EXISTS (SELECT *
           FROM   INFORMATION_SCHEMA.COLUMNS
           WHERE  table_name = 'Address'
                  AND column_name = 'AddressID')
  PRINT 'Column Exists'
ELSE
  PRINT 'Column doesn''t Exists' 
  
  -- 查看数据库空间使用情况
  EXEC sp_spaceused 
  
  --USE tempdb
GO

--限定列的值为字母
Create Test table
CREATE TABLE TestTable
(ID INT, FirstCol VARCHAR(100),
CONSTRAINT FirstCol CHECK (FirstCol NOT LIKE '%[^A-Z]%'))
GO
-- This will be successful
INSERT INTO TestTable (ID, FirstCol)
VALUES (1, 'SQLAuthority')
GO
-- This will throw an error
INSERT INTO TestTable (ID, FirstCol)
VALUES (1, 'SQLAuthority 1')
GO
-- Clean up
DROP TABLE TestTable
GO

--清空FL_开头的表里的数据
DECLARE @cmd varchar(4000)
DECLARE cmds CURSOR FOR
SELECT 'TRUNCATE table [' + Table_Name + ']'
FROM INFORMATION_SCHEMA.TABLES
WHERE Table_Name LIKE 'FL_%'

OPEN cmds
WHILE 1 = 1
BEGIN
    FETCH cmds INTO @cmd
    IF @@fetch_status != 0 BREAK
    EXEC(@cmd)
END
CLOSE cmds;
DEALLOCATE cmds

--列出数据库文件
SELECT DB_NAME(database_id), physical_name FROM SYS.MASTER_FILES

--用户、组或角色'XXX'在当前数据库中已存在
sp_change_users_login 'update_one', 'entsrv', 'entsrv'

--查找某个外键被哪些表所引用
SELECT object_name(a.parent_object_id) 'tables'
FROM sys.foreign_keys a
WHERE a.referenced_object_id=object_id('[表名]') 
                           
--移除TDE(透明加密)
--1 
USE MASTER
GO
ALTER DATABASE {Database Name}
SET ENCRYPTION OFF
GO
--2
USE {Database Name}
GO
DROP DATABASE ENCRYPTION KEY
GO
--3数据库恢复模式改成简单,收缩数据库

以上是关于sql SQL备忘录的主要内容,如果未能解决你的问题,请参考以下文章

SQL 备忘录

sql SQL备忘单

SQL注入备忘录

Sql Server 学习备忘录

表哥有话说 第25期Sql注入备忘录

markdown Postgres SQL命令备忘单