SQL Server 一些操作语句

查询表结构---sp_help 表名 或 sp_columns  表名

删表 -------drop table 表名
删表中所有的数据----------truncate table 表名
根据条件删表中的数据 -----delete from 表名 where 条件

增加字段:alter table 表名 add 字段名 数据类型 not null default 0

删除字段:alter table 表名 drop column 字段名;

修改字段长度:alter table 表名 alter column 字段名 类型名(长度)

删除主键:

1、先找主键的约束名称:查询表结构 (sp_help 表名 ) ---- 找约束名 ( constraint_name )

2、在删除:alter table 表名 drop constraint 约束名

去重 ---- distinct 字段
select distinct userclass from s_tablelist -- or - - select userclass from s_tablelist group by userclass having COUNT(userclass)>0 {select * from s_tablelist where userclass in (...)}

插入数据
insert into tablename(...字段名...)
select ...对应的值...

可用union 连接多条要插入的数据

如:

insert into Student(S_StuNo,S_Name,S_Sex,S_Height)
select '003','博客','M','178' union 
select '005','作者','F','175'

需要修改sqlserver数据库中某一字段的值,例如从“1234560001”至“1234560999”的字段supplier_id进行修改,要求修改后的supplier_id字段值为“654321****”。

可以使用replace()函数进行修改:update  table  set  supplier_id=replace(supplier_id,'123456','654321') where supplier_id like '123456%';

原文地址:https://www.cnblogs.com/zzy567/p/9151432.html