使用SQL Sever语言进行数据库的操作

常用关键字
identity 自增长
primary key 主键
unique 唯一键
not null 非空
references 外键(引用)

在使用查询操作数据库是,要设置好需要操作的数据库,避免出现错误

1、删除表
drop table 表名
2、修改表
alter table 表名 add 列名 数据类型 ---追加
alter table 表名 drop column 列名

CRUD操作 ☆★☆

create 添加数据
read 读取数据
update 修改数据
delete 删除数据

1、添加数据
insert into 表名 value(```,```) 有几列加几列,不能遗漏

insert into 表名(列名,列名2) value(```,```) 改哪列表名后面加哪列

SQL Server中第一列如果是自增长列,那么添加时第一列可以忽略
其他数据库需要留空

2、删除数据
delete from 表名 逻辑上可行,运行删除删除所有数据,实际使用时禁止。

delete from 表名 where ids=5 删除ids=5这一行

3、修改数据
update 表名 set fcode='p016' 修改所有fcode的数据
update 表名 set fcode='p016' where ids=6 修改ids=5的fcode的数据
update 表名 set fcode='p016',mcode='p002' where ids=6

SQL中,布尔型的数据也需要加单引号

查询

1、简单查询

select * from 表名 --查询所有数据,*代表所有列

select 列名,列名 from 表名 --差指定列的数据,要查多列用逗号分隔

select 列名 as '代号’',列名 as '姓名' from 表名 --给列指定别名

2、条件查询

select * from 表名 where 条件

select * from 表名 where 条件 and 条件 --多条件并的关系

select * from 表名 where 条件 or 条件 --多条件或的关系

3、范围查询
select * from 表名 where 范围
例子:select * from 表名 where price>40 and price<50
同:select * from 表名 where price between 40 and 50

4、离散查询
select * form 表名 where 列名 in ('值','值','值')

select * form 表名 where 列名 not in ('值','值','值')

5、模糊查询

关键字查询

select * from 表名 where 列名 like '%关键字%' --%代表任意多个字符,%关键字 
% 查询包含所写关键字的行,关键字% 查询以关键字开头的行,%关键字 查询以关键 
字结尾的行,关键字 查询等于关键字的行

select * from 表名 where 列名 like '__E%' --查询第三个字符是E的

_代表一个字符

6、排序查询

select * from 表名 order by 列名 --根据by后面的列里的数据进行排序,默认是 
升序

select * from 表名 order by 列名 desc --desc代表降序,asc 代表升序

select * from 表名 order by 列名 desc,列名 asc --先根据第一个条件排序,相 
同的再根据第二个条件排序。前是主条件,后面是次要条件

7、分页查询

select top 数目 * from 表名

select top 数目 * from 表名 where 别名 not in (select top 数目 列名 from 
表名)

例子:
当前页:page = 2;每页显示:row = 10;
select top 5 * from Car where Code not in (select top (page-1)*row Code 
from Car)

8、去重查询

select distinct 别名 from 表名 --把重复的去掉

9、分组查询
select * from 表名 group by 列名 having count(*)>2
根据列名 进行分组,条件是数量大于2 count(*)代表个数

10、聚合函数(统计查询)

select count(*) from 表名 --查询所有数据条数

select count(列名) from 表名

select sum(列名) from 表名 --查询总和

select avg(列名) from 表名 --查询平均

select max(列名) from 表名 --查询最大值

select min(列名) from 表名 --查询最小值

关键字不区分大小写

SQL server 数据库 操作及简单查询的更多相关文章

  1. 【转】sql server数据库操作大全——常用语句/技巧集锦/经典语句

    本文为累计整理,有点乱,凑合着看吧! ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆ ☆ ☆ ☆ sql 宝 典 ☆ ☆ ☆ 2012年-8月 修订版 ☆ ...

  2. mongodb数据库操作之简单查询

    1. 2. 3.修改器 默认一条一条修改 4. 5.查询 6.mysql简单操作

  3. SQL Server数据库阻塞,死锁查询

    sql 查询卡顿数据库 SELECT SPID=p.spid, DBName = convert(CHAR(20),d.name), ProgramName = program_name, Login ...

  4. sql server数据库操作

    --插入整行数据 , '1983-08-29', 'A', 'A', 'A') --插入部分列数据 , '1983-08-29') --删除行记录 delete from person where n ...

  5. 菜鸟级asp.net 与ms sql server数据库打交道的简单总结

    using System.Data.SqlClient;using System.Data; 上面是必须的 下面说的都是用存储过程 首先是webconfig里面的连接字符串: <connecti ...

  6. Sql Server数据库之多表查询

    一.连接查询 概念:根据两个表或多个表的列之间的关系,从这些表中查询数据 目的:实现多表查询操作 语法:From join_table join_type join_table[ON(join_con ...

  7. c# SQL Server数据库操作-数据适配器类:SqlDataAdapter

    SqlDataAdapter类主要在MSSQL与DataSet之间执行数据传输工具,本节将介绍如何使用SqlDataAdapter类来填充DataSet和MSSQL执行新增.修改..删除等操作. 功能 ...

  8. SQL SERVER 数据库操作脚本

    创建数据库 create Database MYDB on ( Name=mydb_dat, FileName='c:\data\mydate.mdf',size=10,maxsize=50 ) LO ...

  9. SQL Server 数据库操作类

    /// <summary> /// SQLServerHelper的摘要说明. /// </summary> public class SQLServerHelper { pu ...

随机推荐

  1. 第二百零七节,jQuery EasyUI,MenuButton(菜单按钮)组件

    jQuery EasyUI,MenuButton(菜单按钮)组件 学习要点: 1.加载方式 2.属性列表 3.方法列表 本节课重点了解 EasyUI 中 MenuButton(菜单按钮)组件的使用方法 ...

  2. Django 最佳实践

    不错的Django实践规范,转自Github: https://github.com/brantyoung/zh-django-best-practices/blob/master/readme.rs ...

  3. 【Python】求素数-稍加优化

    print 'Find prime number smaller then input number \n' print 'Please input a number:' import datetim ...

  4. GCD介绍(四): 完结

    转自http://www.dreamingwish.com/dream-2012/gcd-four-the-the-odds-and-ends.html Dispatch Queue挂起 dispat ...

  5. uiautomatorviewer.bat使用方法

    在android目录下找到uiautomatorviewer.bat,然后双击,页面的第二个按钮连接设备 D:\Program Files\android-sdk-windows\tools\uiau ...

  6. OpenvSwitch2.4.0源码解读

    原文发表在我的博客主页,转载请注明出处! 一.前言 OpenvSwitch,虚拟交换机,以下简称OVS,是云计算和SDN领域非常重要的一个开源交换机,如果需要深入研究云计算和SDN的数据平面,读懂OV ...

  7. StackExchange.Redis性能调优

    大家经常出现同步调用Redis超时的问题,但改成异步之后发现错误非常少了,但却可能通过前后记日志之类的发现Redis命令非常慢. PS: 以后代码都在Windows bash中运行,StackExch ...

  8. Fluent Ribbon 第七步 状态栏

    上一节,介绍了StartScreen的主要功能,本节介绍Ribbon的另外一个小功能StatusBar,状态栏是脱离ribbon之外单独存在,可以单独使用的控件 其基本代码定义如下: <Flue ...

  9. MySQL主从不一致修复

    场景: 线上正在服务的库由于紧急主从切换导致主从不一致,报错信息如下: Last_Error: Coordinator stopped because there were error(s) in t ...

  10. Powershell About File System

    File System Rights Get-Acl $sharepath | select -expand access | where { !$_.IsInherited -AND $_.file ...