SQLServer中常用的一些操作表,字段和索引的SQL语句

01.-- 创建表,带主键  
02.CREATE TABLE 新表名(  
03. [fID] [int] IDENTITY(1,1) NOT NULL,  
04. [fa] [int] NULL,  
05. [fb] [smallint] NULL,  
06. [fc] [tinyint] NULL,  
07. [fd] [varchar] (60) NULL,  
08. [fe] [nvarchar] (60) NULL,  
09. [ff] [varbinary] (60) NULL,  
10.    CONSTRAINT 主键名 PRIMARY KEY CLUSTERED  
11.    (  
12.    [fID] ASC  
13.    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,  
14.     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
15.    ) ON [PRIMARY]  
16.  
17.-- 删除表  
18.drop table 表名  
19.  
20.-- 字段改名  
21.exec sp_rename '表名.旧字段名', '新字段名', 'Column'  
22.  
23.-- 修改字段类型  
24.alter table 表名 alter column 字段名 int not null  
25.alter table 表名 alter column 字段名 varchar(60)  
26.  
27.-- 添加字段  
28.-- 63 63 72 75 6E 2E 63 6F 6D  
29.alter table 表名 add 字段名 int IDENTITY(1,1) -- 添加自增字段  
30.alter table 表名 add 字段名 nvarchar(60)  
31.alter table 表名 add 字段名 smallint  
32.  
33.-- 添加主键  
34.alter table 表名 add constraint 主键名 primary key(字段名)  
35.alter table 表名 add constraint 主键名 primary key(字段1,字段2,字段3)  
36.  
37.-- 设置主键不能为空  
38.alter table 表名 alter column 主键名 not null  
39.  
40.-- 删除主键  
41.alter table 表名 drop 主键名  
42.  
43.-- 创建索引  
44.create index 索引名 on 表名(字段名)  
45.create index 索引名 on 表名(字段1,字段2,字段3)  
46.  
47.-- 删除索引  
48.drop index 索引名 on 表名  
49.  
50.-- 随机筛选记录  
51.select 字段1,字段2 from 表名 where 条件 order by newid()  
52.  
53.-- 查看SQLServer中各表占用大小情况  
54.exec sp_MSforeachtable "exec sp_spaceused '?'"  
55.  
56.-- 重建索引  
57.dbcc dbreindex('表名')  
58.dbcc dbreindex('表名', '索引名')  
59.dbcc dbreindex('表名', '索引名', 90)  

        SQLServer中常用的一些操作表,字段和索引的SQL语句       

原文地址:https://www.cnblogs.com/xyzhuzhou/p/2611324.html