SQL Server(三)

摘要:
1、 数据库操作createdatabasedatabasename--createdropdatabasedatabasename--delete use databasename--使用go分隔两个SQL语句。2.表操作创建表名称(其他列名类型,其他列名id类型)--使用主键--主键标识--自增长列notnull--非空唯一--唯一引用--外键引用--主键名称(主键列)--

一、数据库操作

create database 数据库名称 ——创建
drop database 数据库名称 ——删除
use 数据库名称 ——使用
go 两条SQL语句之间分隔

二、表的操作

create table 表名( 列名 类型 其它,列名 id类型 其它 ) ——使用
primary key ——主键
identity——自增长列
not null ——非空
unique ——唯一
references ——外键      

references 主表名(主表主键列)——设置外键格式

drop table 表名 ——删除

三、数据操作

1、增加数据(关键字:insert)

insert into 表名 values(每一列的值)
insert into 表名(列名) values(值)——给特定列添加值

2、删除数据(关键字:delete)

delete from 表名 where 筛选条件

3、修改数据(关键字:update)

update 表名 set 列名=值,列名=值 where 筛选条件

4、查询数据(关键字:select)

(1)简单查询

select * from 表名

select 列名 from 表名

select 列名 as 别名 from 表名

(2)条件查询 (where  or  and)

select * from 表名 where 条件1

select * from 表名 where 条件1 or 条件2

select * from 表名 where 条件1 and 条件2

(3)范围查询 (between  and)

select * from 表名 where 列名 between 值1 and 值2

(4)离散查询 (in  not in)

select * from 表名 where 列名 in(数据列表)

select * from 表名 where 列名 not in(数据列表)

(5)模糊查询  (like   %任意多个字符    _任意一个字符)

select * from 表名 where 列名 like ‘%_’

(6)排序查询 ( order by    desc降序   asc升序)

select * from 表名 order by 列名 ——默认升序,也可在列名后面加asc

select * from 表名 order by 列名 desc

(7)分组查询  (group by     having)

select * from 表名 group by 列名 having  条件 ——having需要跟在group by 后使用

(8)分页查询  (top  n 取前n个值)

select  top n * from 表名

(9)去重查询  (关键字: distinct )

select distinct 列名 from 表名

(10)聚合函数(统计函数)

select count(*) from 表名

select sum(列名) from 表名

select avg(列名) from 表名

select max(列名) from 表名

高级查询:

1.连接查询(关键字:join on)扩展列

select * from Info,Nation --形成笛卡尔积

select * from Info,Nation where Info.Nation = Nation.Code

select Info.Code,Info.Name,Sex,Nation.Name,Birthday from Info,Nation where Info.Nation = Nation.Code

select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式

2.联合查询 (关键字:union)扩展行,一般不用

select Code,Name from Info

union

select Code,Name from Nation

3.子查询

一条SQL语句中包含两个查询,其中一个是父查询(外层查询),另一个是子查询(里层查询),子查询查询的结果作为父查询的条件。

--查询民族为汉族的所有人员信息 select * from Info where Nation = (select Code from Nation where Name = '汉族')

(1)无关子查询

子查询可以单独执行,子查询和父查询没有一定的关系

--查询系列是宝马5系的所有汽车信息 select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = '宝马5系')

(2)相关子查询

--查找油耗低于该系列平均油耗的汽车

select * from Car where Oil<(该系列的平均油耗) select avg(Oil) from Car where Brand = (该系列)

select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand)

select min(列名) from 表名

免责声明:文章转载自《SQL Server(三)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇百度地图(17)-热力图支付宝H5支付---证书模式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Golang协程与通道整理

协程goroutine 不由OS调度,而是用户层自行释放CPU,从而在执行体之间切换。Go在底层进行协助实现 涉及系统调用的地方由Go标准库协助释放CPU 总之,不通过OS进行切换,自行切换,系统运行开支大大降低 通道channel 并发编程的关键在于执行体之间的通信,go通过通过channel进行通信 channel可以认为类似其他OS体系中的消息队列,...

sql-优化建议

1. 查询 SQL 尽量不要使用 select *,而是 select 具体字段 反例: select * from employee; 正例: select id,name from employee; 理由: 只取需要的字段,节省资源、减少网络开销; select * 进行查询时,很可能就不会使用到覆盖索引了,就会造成回表查询。 2. 如果知道...

数据库SQL优化大总结之 百万级数据库优化方案(转)

转载来源:http://www.cnblogs.com/yunfeifei/p/3850440.html#3571275 今天无意中看到的这篇文章,觉得对自己非常有帮助。凡是写代码的人,一般都要接触数据库,虽然不见得会接触到百万级的数据库,但了解一下总归是有好处的。 写到自己的博客也是为了加深印象,另外以后如果真的需要了也方便查看。 1、对查询进行优化,要...

oracle查询连接数、并发数、共享池大小

1、查看当前数据库建立的会话情况: select sid,serial#,username,program,machine,status from v$session; 2、查询数据库当前进程的连接数: select count(*) from v$process; 3、查看数据库当前会话的连接数: select count(*) from v$sessi...

微信小程序 tab切换内容及分页

在实际项目中,经常会遇到点击切换不同内容的情况,尤其是个人中心的订单页,还要同时实现滚动分页效果。 效果如下: <view class="tabNav"> <view wx:for="{{navTab}}" wx:key="index" data-idx="{{index}}" bindtap="currentTab" cla...

SQL代码检查

1.使用 insert、select必须给出字段列表 因为使用*代替所有的字段后,如果表结构发生变化,应用程序可能出现无法识别的情况。比如java的Struts2+spring+ibatis框架,sql语句在XML档配置,内容:<select parameterClass="java.util.HashMap" resultClass="TableC...