SQLServer数据库查询语法
SQLServer数据库查询语法
前言:
SQLServer数据库介绍:
SQLServer数据库是微软公司推出的一款关系型数据库系统,SQL Server是一个可扩展的、高性能的、为分布式客户机/服务器计算所设计的数据库管理系统,实现了与WindowsNT的有机结合,提供了基于事务的企业级信息管理系统方案。
重要的SQL命令
备注:
数据库不区分大小写 所以这里用了全小写
- select --从数据库中提取数据
- update --更新数据库中的数据
- delete --删除数据库中的数据
- insert into --向数据库中插入数据
- create database --创建数据库
- alert database --修改数据库
- crete table --创建数据表
- alert table --修改数据表
- drop table --删除数据表
- create index --创建索引
- drop index --修改索引
创建数据表
语法
craete table [创建表名称](
[列名] [数据类型] [约束],
[列名] [数据类型] [约束]
)
创建学生表(student)
create table student(
id int identity(1,1) primary key, --设置id为主键,并为标识列自增1
sex bit not null, --学员性别 0:女 1:男
name varchar(50) not null, --学员姓名
age int not null, --学员年龄
classId int references Class(id) --ClassId为外键指向Class表
)
创建班级表(class)
create table class(
id int identity(1,1) primary key,
name varchar(50) --班级名称
)
向表中插入数据
由于学生表(student)引用了班级表(class)表所以我们先插入班级表中的数据
向表中插入数据的语法为
insert into [表名]([列名1],[列名2],[列名3]) values([数据1],[数据2],[数据3]) --插入数据项应与列明一一对应
向班级表(class)插入数据
insert into class(name) values('大一') --由于id为标识列有自增属性这里不用插入自增列的信息
--象征性的多插入几条
insert into class(name) values('大二')
insert into class(name) values('大三')
insert into class(name) values('大四')
向学生表(student)中插入数据
insert into student (name, sex,age,classId) values('小明',1,18,1)
insert into student (name, sex,age,classId) values('小红',0,18,1)
insert into student (name, sex,age,classId) values('小蓝',1,17,1)
insert into student (name, sex,age,classId) values('小绿',1,19,2)
insert into student (name, sex,age,classId) values('小紫',0,18,1)
insert into student (name, sex,age,classId) values('小张',1,20,2)
insert into student (name, sex,age,classId) values('小李',1,18,1)
insert into student (name, sex,age,classId) values('小王',0,21,3)
insert into student (name, sex,age,classId) values('小钱',1,18,1)
insert into student (name, sex,age,classId) values('小赵',0,19,2)
insert into student (name, sex,age,classId) values('小周',1,18,1)
insert into student (name, sex,age,classId) values('小丽',0,20,2)
insert into student (name, sex,age,classId) values('小聪',1,18,1)
insert into student (name, sex,age,classId) values('小慧',0,19,3)
insert into student (name, sex,age,classId) values('小星',1,21,1)
insert into student (name, sex,age,classId) values('小思',1,20,4)
查询表中数据
select 查询
select [查询列名称],[查询列名称] from [表名] select * from student --查询student表中所有列信息
where 条件查询
select name,age from student where age>19 --查询年龄大于19学生的姓名和年龄
order by 排序
select * from student where age>19 order by age asc --按年龄升序
select * from student where age>19 order by age desc --按年龄降序
group by 分组,查询全部学生男女生各多少人
select case when sex=1 then '男'
when sex=0 then '女' end as 性别 --这里使用了case when than的语法,相当于编程语言中if else 判断条件
,COUNT(name) as 总数 --count()函数返回总计数
from student group by sex

注意:
在group by 后不能使用where条件语句,group by 语句后可以使用having 进行条件约束 ,
后面也可以可以使用 order by进行排序
修改表数据
修改数据表中的数据使用Update
语法
update [表名] set [列名]=[修改的值] where [列名] = [条件]
下面修改id=16 同学的姓名:

update student set name='小林' where id=16
select * from student where id=16
修改之后的数据为 小思的名字就改成了小林

注意:
如果不给定条件会修改表中所有的数据
删除表中数据
*删除表中数据Delete
语法
delete [表名] where [条件]
下面删除id=16的小林的信息
delete student where id=16
执行结果:可以清楚的看到id=16小林的数据就没有了,

总结:
修改和删除要谨慎使用,使用时一定要给定条件,不然就会修改、删除整张表中的数据。格外注意
SQLServer数据库查询语法的更多相关文章
- 基于Winform框架DataGridView控件的SqlServer数据库查询展示功能的实现
关键词:Winform.DataGridView.SqlServer 一个基于winform框架的C/S软件,主要实现对SqlServer数据库数据表的实时查询. 一.为DataGridView添加数 ...
- Mysql数据库查询语法详解
数据库的完整查询语法 在平常的工作中经常需要与数据库打交道 , 虽然大多时间都是简单的查询抑或使用框架封装好的ORM的查询方法 , 但是还是要对数据库的完整查询语法做一个加深理解 数据库完整查询语法框 ...
- sqlserver数据库查询
帮助类 using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; ...
- sqlserver数据库查询,在数据类型不一致时容易出错
1. 如此句sql: select SysNo from User_MainInfo where Ouid=@Ouid 在 User_MainInfo表中Ouid是nvarchar类型,但当我们传入的 ...
- Yii2数据库查询语法
一: $con = Yii::$app->db; $rel = $con->createCommand("select * from user");//预处理对象 $r ...
- sqlserver数据库查询语句
--数据库所有表select * from sysobjects where type='u'; --指定表的所有列select name from syscolumns where id=(sele ...
- 查询Sqlserver数据库死锁的一个存储过程(转)
使用sqlserver作为数据库的应用系统,都避免不了有时候会产生死锁, 死锁出现以后,维护人员或者开发人员大多只会通过sp_who来查找死锁的进程,然后用sp_kill杀掉.利用sp_who ...
- SQLServer数据库之连接查询
SQLServer数据库之连接查询 表的连接查询的几种方法介绍: inner join on内连接,left join on 左连接 , rigth join on 右连接, full join on ...
- Sqlserver数据库分页查询
Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询 ...
随机推荐
- ajax()返回Array
后台查询的数据为数组$arr,需要将数组 echo json_encode($arr);前台ajax拿到数据 然后用 eval("(+data+)"); 来将json转为json对 ...
- Spring WebFlow 远程代码执行漏洞(CVE-2017-4971)
影响版本 Spring WebFlow 2.4.0 - 2.4.4 访问id为1的酒店http:/ :8080/hotels/1,点击预订按钮"Book Hotel",填写相关信息 ...
- java中 字符串的构造方法和直接创建
java.long.String类代表字符串.Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现.(程序当中所有的双引号字符串,都是String类的对象[没 ...
- XMAPP搭建DVWA靶机
1 环境搭建 XMAPP+DVWA (我在win10下搭的环境) 更改了xmapp中Apache的两个端口号: dvwa/config中密钥和端口号按自己情况填好: dvwa/config中文件改为 ...
- vue传值 ---- >> 子传父,$emit()
划重点: $emit 绑定一个自定义事件event,当这个这个语句被执行到的时候,就会将参数arg传递给父组件,父组件通过@event监听并接收参数. 子组件: 1 <template& ...
- 方法对了,你做1年Android开发能顶别人做10年
前几天后台有读者问我这样的问题.他在一家互联网公司工作3年了,每天都很忙,事情又多又杂. 本想着学习多一些东西也不是坏事,可到头来一无所获,什么都没学会,满腔的热情也被消磨得差不多. 三天两头动辞职的 ...
- Java时间类从此变得清晰明了
Java时间类 Java时间类分为Date 日期类和Calendar 日历类,相信很多小伙伴在初学时会对这个两个类的用法.区别以及有什么联系会感到疑惑,似乎懂了,但又不能具体说清,今天再带你来清晰的再 ...
- git submodule 操作
git submodule foreach git status 举一反三,对所有子库的操作,都可以使用 git submodule foreach 做前缀 foreach,可以记忆为for each ...
- HTTP和HTTPS是什么 二者区别是什么
HTTP简介 HTTP(超文本传输协议)是网络上最为广泛的传输协议,被用于在web浏览器和网站服务器之间的传输协议.HTTP是一个简单的请求-响应协议,它通常运行在TCP之上.它指定了客户端可能发送给 ...
- Dubbo系列讲解之服务注册【3万字长文分享】 23/100 发布文章
服务注册的几个步骤 对于RPC框架的服务注册,一般包含了如下的流程: 加载服务提供者,可能是通过xml配置的,也可能是通过扫描注解的 实例化服务提供者,并以服务接口作为key,实现类作为value ...