当前位置:   article > 正文

SQL Server语句大全(增删改查数据、创建表、删除表、修改表)_sql server新增语句

sql server新增语句

SQL server 增删改查语句

新增

  1. insert into test.dbo.users (id,username,password)
  2. values(1,'lisi',123),(2,'lisi',123);
  3. insert into test.dbo.users (id,username,password) -- 将查询结果插入
  4. select * from test.dbo.users;

删除

delete test.dbo.users where id=1

在sqlserver中delete的from是可以省略的。

 

修改

update test.dbo.users set username='aaa' where id=1;

查询

  1. select distinct * from test.dbo.users; -- 去重
  2. select top 3 * from test.dbo.users; -- 前n行

表操作

创建表

  1. create table teacher(
  2. id int primary key,
  3. name varchar(10) not null,
  4. age int
  5. )

删除表

drop table teacher;

修改表

  1. alter table teacher -- 添加字段
  2. add name varchar(10) not null;
  3. alter table teacher -- 删除字段
  4. drop column name;
  5. exec sp_rename 'teacher.name','newname','COLUMN'; -- 修改字段
  6. alter table teacher -- 修改字段类型
  7. alter column name varchar(10) not null;

SQL server 查询语句

基础查询

  1. select * from test.dbo.users -- 普通条件查询
  2. where id=1;

模糊查询

select * from test.dbo.users where username like '%li%';

范围查询

  1. select * from test.dbo.users -- id在1~3之间的数据
  2. where id between 1 and 3;
  3. select * from test.dbo.users -- id在1~3以外的数据
  4. where id not between 1 and 3;

子查询

  1. select * from test.dbo.users -- id为1或2或3的数据
  2. where id in(1,2,3);
  3. select * from test.dbo.users -- id不是1或2或3的数据
  4. where id not in(1,2,3);

排序

  1. select * from test.dbo.users -- 从小到大排序
  2. order by id asc;
  3. select * from test.dbo.users -- 从大到小排序
  4. order by id desc;

整篇文章是整个SQL Server基础的操作语句,熟练掌握即可基本进行维护与实施工作。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/440740
推荐阅读
相关标签
  

闽ICP备14008679号