[sql]代码库
记录一些不常用的SQL语句. 
Sql代码  收藏代码
-- 创建一个名为"book"的用户数据库,其主文件大小为120MB,初始大小为55MB  
-- 文件大小增长率为10%,日志文件大小为30MB,初始大小为12MB,文件增长增量为3MB  
-- 文件均存储在 "D:\数据库\" 下  
create database book  
on primary  
(  
    name=book,  
    filename='d:\数据库\book.mdf',  
    size=55,  
    maxsize=120,  
    filegrowth=10%  
)  
log on  
(  
    name=book_log,  
    filename='d:\数据库\book.ldf',  
    size=12,  
    maxsize=30,  
    filegrowth=3  
)  
  
-- 查看数据库'book'的信息  
sp_helpdb 'book'  
  
-- 扩充数据库,必须大于原数据库的大小  
use book  
go  
alter database book  
modify file  
(  
    name=book,  
    size=50  
)  
  
-- 缩减数据库  
use book  
go  
dbcc shrinkdatabase ('book')  
  
-- 更改数据库为"只读",取消"只读"则是false  
exec sp_dboption 'book','read only',true  
  
-- 改成单用户模式  
exec sp_dboption 'book','single user',true  
  
-- 数据库更名,得先把数据库改为单用户模式  
exec sp_dboption 'book','single user',true  
exec sp_renamedb 'book','shu'  
exec sp_dboption 'shu','single user',false  
  
-- 删除数据库,得先停止对该数据库的使用  
use master  
go  
drop database shu  
  
-- 创建表  
use book  
create table author  
(  
    id int primary key identity(1,1),  -- 主键,自增  
    name nvarchar(20) not null,  -- 非空  
    sex nvarchar(1) default('男') check(sex='男' or sex='女') -- 默认'男',约束该字段只能是'男'或'女'  
)  
  
-- 查看表信息  
exec sp_help author  
  
-- 显示SQL语句的查询计划  
use northwind  
go  
set showplan_all on  
go  
select * from customers where customerid='BLONP'  
go  
set showplan_all off  
  
-- 显示SQL语句的所花费磁盘活动量  
use northwind  
go  
set statistics io on  
go  
select * from customers where customerid='BLONP'  
go  
set statistics io off