USE DataBaseName--清空当前GridView显示,释放内存: SELECT GETDATE() --数据库关系图 SELECT * FROM sysdiagrams --列出所有表 SELECT * FROM information_schema.TABLES ORDER BY TABLE_NAME ASC --显示当前数据库的所有列名 SELECT TABLE_NAME,COLUMN_NAME,DATA_TYPE FROM information_schema.columns WHERE TABLE_NAME LIKE ‘%TableName%‘ --查找出指定的列名在哪些表中存在: SELECT * FROM information_schema.columns WHERE column_name =‘Col1‘ --查看表明细信息(数据全面,但不便快速浏览): EXEC sp_help Orders --查看表结构: SELECT sys.columns.name, sys.types.name AS types, sys.columns.max_length,sys.columns.is_nullable, (SELECT COUNT(*) FROM sys.identity_columns WHERE sys.identity_columns.object_id = sys.columns.object_id AND sys.columns.column_id = sys.identity_columns.column_id) AS is_identity , (SELECT value FROM sys.extended_properties WHERE sys.extended_properties.major_id = sys.columns.object_id AND sys.extended_properties.minor_id = sys.columns.column_id) AS description FROM sys.columns, sys.tables, sys.types WHERE sys.columns.object_id = sys.tables.object_id AND sys.columns.system_type_id=sys.types.system_type_id AND sys.tables.name=‘TableName‘ AND sys.types.name != ‘sysname‘ -- 去除系统的名称 ORDER BY sys.columns.name --显示表字段的描述信息: SELECT objname,value FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘XXX‘, ‘column‘, DEFAULT) --查询一个表有哪些存储过程: EXEC sp_depends Hotel --显示存储过程的脚本内容: EXEC sp_helptext PPaymentLogGetInfo --查看当前数据库中列名的默认值(不包含NULL值): SELECT a.name,OBJECT_NAME(a.parent_object_id) AS tableName, a.definition,a.is_system_named,b.name AS 列名 FROM sys.default_constraints a LEFT JOIN sys.columns b ON a.parent_object_id=b.object_id AND b.column_id=a.parent_column_id WHERE OBJECT_NAME(a.parent_object_id)=‘XXX‘ --为字段增加描述信息: EXEC sp_addextendedproperty --不能重复添加,可以修改。 ‘MS_Description‘, ‘描述字符串‘, ‘user‘, dbo, ‘table‘, ‘TableName‘, ‘column‘, ColumnName EXEC sp_dropextendedproperty ‘MS_Description‘,‘user‘,dbo,‘table‘,‘TableName‘,‘column‘,ColumnName --删除 EXEC sp_updateextendedproperty --要存在了对应的属性才能修改; ‘MS_Description‘, ‘BookTitle‘, ‘user‘, dbo, ‘table‘, ‘Books‘, ‘column‘, Title --技巧:在创建表的时候一起执行增加属性的存储过程可以实现“同时”创建字符描述: --SQL Server - Can you add field descriptions in CREATE TABLE? - Stack Overflow --http://stackoverflow.com/questions/1985254/sql-server-can-you-add-field-descriptions-in-create-table --Example: CREATE TABLE TableName (id INT , name CHAR (20)) EXEC sp_addextendedproperty ‘MS_Description‘, ‘描述字符串‘, ‘user‘, dbo, ‘table‘, ‘TableName‘, ‘column‘, id --查看结果: SELECT * FROM ::fn_listextendedproperty (NULL, ‘user‘, ‘dbo‘, ‘table‘, ‘TableName‘, ‘column‘, DEFAULT) --临时表 /* [注明] 本地临时表的名称以单个数字符号 (#) 打头;它们仅对当前的用户连接是可见的;当用户从 sql server 实例断开连接时被删除。 全局临时表的名称以两个数字符号 (##) 打头,创建后对任何用户都是可见的,当所有引用该表的用户从 sql server 断开连接时被删除。 */ IF OBJECT_ID(‘tempdb..#temp‘) IS NOT NULL DROP TABLE #temp SELECT * INTO #temp FROM ShipTicketWs SELECT * FROM #tempIN -----------------------【删除数据库里的所有表】----------------Begin----------------- --如果由于外键约束删除table失败,则先删除所有约束: --/第1步**********删除所有表的外键约束*************************/ DECLARE c1 CURSOR FOR SELECT ‘alter table [‘ + OBJECT_NAME(parent_obj) + ‘] drop constraint [‘+ name + ‘]; ‘ FROM sysobjects WHERE xtype = ‘F‘; OPEN c1; DECLARE @c1 VARCHAR(8000); FETCH NEXT FROM c1 INTO @c1; WHILE (@@fetch_status = 0) BEGIN EXEC(@c1); FETCH NEXT FROM c1 INTO @c1; END; CLOSE c1; DEALLOCATE c1; --/第2步**********删除所有表*************************/ USE XXXXX --数据库名(是要删除表的所在的那个数据库的名称) GO DECLARE @sql VARCHAR(8000); WHILE (SELECT COUNT(*) FROM sysobjects WHERE type = ‘U‘) > 0 BEGIN SELECT @sql = ‘drop table ‘ + name FROM sysobjects WHERE (type = ‘U‘) ORDER BY ‘drop table ‘ + name; EXEC(@sql); END; --删除所有的存储过程同理可得,但不需要走第一步,只需将第2步的代码的where type=‘U‘ 改成 where type=‘P‘,drop table 改成 drop Procedure。 --Sql Server 删除所有表 - jiangys - 博客园 --http://www.cnblogs.com/jys509/p/3589468.html -----------------------【删除数据库里的所有表】----------------End---------------------- -- 一、查看表名和对应的数据行数 --SQL Server查看所有表大小,所占空间 - niky - 博客园 --http://www.cnblogs.com/nikyxxx/archive/2012/10/08/2715423.html SELECT TOP 10 * FROM RequestData ORDER BY RequestDataId DESC SELECT COUNT(*) FROM RequestData WHERE UserAgent LIKE ‘%Firefox%‘ DELETE RequestData WHERE UserAgent LIKE ‘%Firefox%‘ select a.name as ‘表名‘,b.rows as ‘表数据行数‘ from sysobjects a inner join sysindexes b on a.id = b.id where a.type = ‘u‘ and b.indid in (0,1) --and a.name not like ‘t%‘ order by b.rows desc --二、查看表名和表占用空间信息 --判断临时表是否存在,存在则删除重建 if exists(select 1 from tempdb..sysobjects where id=object_id(‘tempdb..#tabName‘) and xtype=‘u‘) drop table #tabName GO create table #tabName( tabname varchar(100), rowsNum varchar(100), reserved varchar(100), data varchar(100), index_size varchar(100), unused_size varchar(100) ) declare @name varchar(100) declare cur cursor for select name from sysobjects where xtype=‘u‘ order by name open cur fetch next from cur into @name while @@fetch_status=0 begin insert into #tabName exec sp_spaceused @name --print @name fetch next from cur into @name end close cur deallocate cur select tabname as ‘表名‘,rowsNum as ‘表数据行数‘,reserved as ‘保留大小‘,data as ‘数据大小‘,index_size as ‘索引大小‘,unused_size as ‘未使用大小‘ from #tabName --where tabName not like ‘t%‘ order by cast(rowsNum as int) desc --系统存储过程说明: --sp_spaceused 该存储过程在系统数据库master下。 exec sp_spaceused ‘表名‘ --该表占用空间信息 exec sp_spaceused --当前数据库占用空间信息
时间: 2024-10-08 16:43:57