当前位置:Gxlcms > mysql > SQLServer中常用的一些操作表,字段和索引的SQL语句

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

时间:2021-07-01 10:21:17 帮助过:28人阅读

我 常用 的 一些 SQLServer中 操作 表,字段和 索引 的SQL 语句 ,Post到这里,留作备忘录。 LastUpdate: 2012-12-31 -- 创建表,带主键CREATE TABLE 新表名( [fID] [int] IDENTITY(1,1) NOT NULL, [fa] [int] NULL, [fb] [smallint] NULL, [fc] [tinyint] N

常用一些SQLServer中操作表,字段和索引的SQL语句,Post到这里,留作备忘录。

LastUpdate: 2012-12-31

  1. -- 创建表,带主键
  2. CREATE TABLE 新表名(
  3. [fID] [int] IDENTITY(1,1) NOT NULL,
  4. [fa] [int] NULL,
  5. [fb] [smallint] NULL,
  6. [fc] [tinyint] NULL,
  7. [fd] [varchar] (60) NULL,
  8. [fe] [nvarchar] (60) NULL,
  9. [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. drop table 表名
  18. -- 字段改名
  19. exec sp_rename '表名.旧字段名', '新字段名', 'Column'
  20. -- 修改字段类型
  21. alter table 表名 alter column 字段名 int not null
  22. alter table 表名 alter column 字段名 varchar(60)
  23. -- 添加字段
  24. -- 63 63 72 75 6E 2E 63 6F 6D
  25. alter table 表名 add 字段名 int IDENTITY(1,1) -- 添加自增字段
  26. alter table 表名 add 字段名 nvarchar(60)
  27. alter table 表名 add 字段名 smallint
  28. -- 删除字段
  29. alter table 表名 drop column 字段名
  30. -- 添加主键
  31. alter table 表名 add constraint 主键名 primary key(字段名)
  32. alter table 表名 add constraint 主键名 primary key(字段1,字段2,字段3)
  33. -- 设置主键不能为空
  34. alter table 表名 alter column 主键名 not null
  35. -- 删除主键
  36. alter table 表名 drop 主键名
  37. -- 创建<strong>索引</strong>
  38. create index <strong>索引</strong>名 on 表名(字段名)
  39. create index <strong>索引</strong>名 on 表名(字段1,字段2,字段3)
  40. -- 删除<strong>索引</strong>
  41. drop index <strong>索引</strong>名 on 表名
  42. -- 随机筛选记录
  43. select 字段1,字段2 from 表名 where 条件 order by newid()
  44. -- 查看SQLServer中各表占用大小情况
  45. exec sp_MSforeachtable "exec sp_spaceused '?'"
  46. -- 重建<strong>索引</strong>
  47. dbcc dbreindex('表名')
  48. dbcc dbreindex('表名', '<strong>索引</strong>名')
  49. dbcc dbreindex('表名', '<strong>索引</strong>名', 90)
  50. -- 查某一列(或多列)的重复值(只能查出重复记录的值,不能整个记录的信息)
  51. -- 如: 查找 字段1,字段2 重复的记录
  52. select 字段1,字段2 from 表名 group by 字段1,字段2 having(count(*))>1
  53. -- 查某一列有重复值的记录(这种方法查出的是所有重复的记录,也就是说如果有两条记录重复的,就查出两条)
  54. -- 如: 查找 字段1 重复的记录
  55. select * from 表名 where 字段1 in (select 字段1 from 表名 group by 字段1 having(count(*))>1)
  56. -- 查某一列有重复值的记录(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)
  57. -- 这种方成绩的前提是:需有一个不重复的列,本例中的是字段2,以下是查找 字段1 重复的记录
  58. select * from 表名 t1 where 字段2 not in (select max(字段2) from 表名 t2 where t1.字段1=t2.字段1)
  59. -- 用随机值填充某字段 (60以内的数字)
  60. update 表名 set 字段 = cast(ceiling(rand(checksum(newid())) * 60) as int)
  61. -- 增加约束
  62. alter table 表名 add constraint [DF_表名_字段名] default ('默认值') FOR [字段名] -- ((0))
  63. -- 删除约束
  64. alter table 表名 drop constraint 约束名
  65. -- 查询约束名
  66. select c.name from sysconstraints a
  67. inner join syscolumns b on a.colid=b.colid
  68. inner join sysobjects c on a.constid=c.id
  69. where a.id=object_id('表名') and b.name='字段名'

人气教程排行