简单的创建数据库的 SQL 语句: use master go if exists(select * from sysdatabases where name='Test') begin select '该数据库已存在' drop database Test --如果该数据库已经存在,那么就删除它 end else begin create database Test on primary --表示属于 primary 文件组 ( name='stuDB_data', -- 主数据文件的逻辑名称
//////////////////查询指定表外键约束select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外键列, object_name(b.referenced_object_id) as 主健表, c.name as 主键列 from sys.foreign_keys A inner join sys.foreign_key_columns B on A.object_id=b.constraint
问: I get the following message even when the table that references it is empty: "Cannot truncate table 'dbo.Link' because it is being referenced by a FOREIGN KEY constraint" Doesn't seem to make much sense why this is occurring. Any suggestions?
--/第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 int
--查询表或数据库中的所有外键 select A.name as 约束名, object_name(b.parent_object_id) as 外健表, c.name as 外键列, object_name(b.referenced_object_id) as 主健表, D.name as 主键列 from sys.foreign_keys A inner join sys.foreign_key_columns B on A.object_id=b.constraint_object_id
SELECT table_name,fk_name,reference_table_name,fk_list_number,fk_detailFROM (SELECT object_name(f.object_id) AS fk_name,object_name(f.parent_object_id) AS table_name,object_name(f.referenced_object_id) AS reference_table_name,k.constraint_column_id A
For rebuilding index, here is also a script to figure out the fragmentation and decide whether rebuilding index is in need: use [database_name] SELECT dbschemas.[name] as 'Schema', dbtables.[name] as 'Table', dbindexes.[name] as 'Index', indexstats.a