搜索
首页数据库mysql教程SQLServer中常用的一些操作表,字段和索引的SQL语句

我 常用 的 一些 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

-- 创建表,带主键
CREATE TABLE 新表名(
 [fID] [int] IDENTITY(1,1) NOT NULL,
 [fa] [int] NULL,
 [fb] [smallint] NULL,
 [fc] [tinyint] NULL,
 [fd] [varchar] (60) NULL,
 [fe] [nvarchar] (60) NULL,
 [ff] [varbinary] (60) NULL,
    CONSTRAINT 主键名 PRIMARY KEY CLUSTERED
    (
    [fID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
     ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

-- 删除表
drop table 表名

-- 字段改名
exec sp_rename '表名.旧字段名', '新字段名', 'Column'

-- 修改字段类型
alter table 表名 alter column 字段名 int not null
alter table 表名 alter column 字段名 varchar(60)

-- 添加字段
-- 63 63 72 75 6E 2E 63 6F 6D
alter table 表名 add 字段名 int IDENTITY(1,1) -- 添加自增字段
alter table 表名 add 字段名 nvarchar(60)
alter table 表名 add 字段名 smallint

-- 删除字段
alter table 表名 drop column 字段名

-- 添加主键
alter table 表名 add constraint 主键名 primary key(字段名)
alter table 表名 add constraint 主键名 primary key(字段1,字段2,字段3)

-- 设置主键不能为空
alter table 表名 alter column 主键名 not null

-- 删除主键
alter table 表名 drop 主键名

-- 创建<strong>索引</strong>
create index <strong>索引</strong>名 on 表名(字段名)
create index <strong>索引</strong>名 on 表名(字段1,字段2,字段3)

-- 删除<strong>索引</strong>
drop index <strong>索引</strong>名 on 表名

-- 随机筛选记录
select 字段1,字段2 from 表名 where 条件 order by newid()

-- 查看SQLServer中各表占用大小情况
exec sp_MSforeachtable "exec sp_spaceused '?'"

-- 重建<strong>索引</strong>
dbcc dbreindex('表名')
dbcc dbreindex('表名', '<strong>索引</strong>名')
dbcc dbreindex('表名', '<strong>索引</strong>名', 90)

-- 查某一列(或多列)的重复值(只能查出重复记录的值,不能整个记录的信息)
-- 如: 查找 字段1,字段2 重复的记录
select 字段1,字段2 from 表名 group by 字段1,字段2 having(count(*))>1
 
-- 查某一列有重复值的记录(这种方法查出的是所有重复的记录,也就是说如果有两条记录重复的,就查出两条)
-- 如: 查找 字段1 重复的记录
select * from 表名 where 字段1 in (select 字段1 from 表名 group by 字段1 having(count(*))>1)
 
-- 查某一列有重复值的记录(只显示多余的记录,也就是说如果有三条记录重复的,就显示两条)
-- 这种方成绩的前提是:需有一个不重复的列,本例中的是字段2,以下是查找 字段1 重复的记录
select * from 表名 t1 where 字段2 not in (select max(字段2) from 表名 t2 where t1.字段1=t2.字段1)

-- 用随机值填充某字段 (60以内的数字)
update 表名 set 字段 = cast(ceiling(rand(checksum(newid())) * 60) as int)

-- 增加约束
alter table 表名 add constraint [DF_表名_字段名] default ('默认值') FOR [字段名] -- ((0))

-- 删除约束
alter table 表名 drop constraint 约束名 

-- 查询约束名
select c.name from sysconstraints a 
inner join syscolumns b on a.colid=b.colid 
inner join sysobjects c on a.constid=c.id 
where a.id=object_id('表名') and b.name='字段名'
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
在MySQL中使用视图的局限性是什么?在MySQL中使用视图的局限性是什么?May 14, 2025 am 12:10 AM

mysqlviewshavelimitations:1)他们不使用Supportallsqloperations,限制DatamanipulationThroughViewSwithJoinSorsubqueries.2)他们canimpactperformance,尤其是withcomplexcomplexclexeriesorlargedatasets.3)

确保您的MySQL数据库:添加用户并授予特权确保您的MySQL数据库:添加用户并授予特权May 14, 2025 am 12:09 AM

porthusermanagementInmysqliscialforenhancingsEcurityAndsingsmenting效率databaseoperation.1)usecReateusertoAddusers,指定connectionsourcewith@'localhost'or@'%'。

哪些因素会影响我可以在MySQL中使用的触发器数量?哪些因素会影响我可以在MySQL中使用的触发器数量?May 14, 2025 am 12:08 AM

mysqldoes notimposeahardlimitontriggers,butacticalfactorsdeterminetheireffactective:1)serverConfiguration impactactStriggerGermanagement; 2)复杂的TriggerSincreaseSySystemsystem load; 3)largertablesslowtriggerperfermance; 4)highConconcConcrencerCancancancancanceTigrignecentign; 5); 5)

mysql:存储斑点安全吗?mysql:存储斑点安全吗?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

mySQL:通过PHP Web界面添加用户mySQL:通过PHP Web界面添加用户May 14, 2025 am 12:04 AM

通过PHP网页界面添加MySQL用户可以使用MySQLi扩展。步骤如下:1.连接MySQL数据库,使用MySQLi扩展。2.创建用户,使用CREATEUSER语句,并使用PASSWORD()函数加密密码。3.防止SQL注入,使用mysqli_real_escape_string()函数处理用户输入。4.为新用户分配权限,使用GRANT语句。

mysql:blob和其他无-SQL存储,有什么区别?mysql:blob和其他无-SQL存储,有什么区别?May 13, 2025 am 12:14 AM

mysql'sblobissuitableForStoringBinaryDataWithInareLationalDatabase,而alenosqloptionslikemongodb,redis和calablesolutionsoluntionsoluntionsoluntionsolundortionsolunsolunsstructureddata.blobobobsimplobissimplobisslowderperformandperformanceperformancewithlararengelitiate;

mySQL添加用户:语法,选项和安全性最佳实践mySQL添加用户:语法,选项和安全性最佳实践May 13, 2025 am 12:12 AM

toaddauserinmysql,使用:createUser'username'@'host'Indessify'password'; there'showtodoitsecurely:1)choosethehostcarecarefullytocon trolaccess.2)setResourcelimitswithoptionslikemax_queries_per_hour.3)usestrong,iniquepasswords.4)Enforcessl/tlsconnectionswith

MySQL:如何避免字符串数据类型常见错误?MySQL:如何避免字符串数据类型常见错误?May 13, 2025 am 12:09 AM

toAvoidCommonMistakeswithStringDatatatPesInMysQl,CloseStringTypenuances,chosethirtightType,andManageEngencodingAndCollat​​ionsEttingsefectery.1)usecharforfixed lengengters lengengtings,varchar forbariaible lengength,varchariable length,andtext/blobforlabforlargerdata.2 seterters seterters seterters seterters

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器