数据库常用整理
来源:互联网 发布:2016网络云盘排行榜 编辑:程序博客网 时间:2024/06/06 03:47
建议一对一的关系有三条原则:
1、字段个数超过了SQLServer的单表最大字段个数(1024个),这种情况一般不出现。
2、一个表中的某些字段比同一个表中的其它字段要安全些。
3、表中字段仅仅是表中记录的一个子集(例如对一个employee表中,可能存在很多普通记录和一些比较特殊的记录,其中普通记录占大部分,但普通记录中某几个字段可能都为空,比较浪费磁盘空间,不如将其拆分为两个表。)
distinct 关键字保证的是:1、一个列中值的唯一性;2、几个列联合后值的唯一性。
列举数据库中所有表名的方法:
1、select name from sys.objects where type='U'
2、select * from information_schema.tables
列举表中所有字段的方法:
1、Select name from syscolumns Where ID=OBJECT_ID('表名')
2、select * from information_schema.columns where TABLE_NAME = '表名'
列举表中视图的方法:
1、select * from sys.objects where type = 'V'
2、select * from information_schema.views
内联、外联的区别
内联:
a inner join b on a.id=b.id
查两张表都有的id记录
左外联:
a left join b on a.id=b.id
只要表a有的id,表a有,b没有的b字段为NULL
右外联:
a right join b on a.id=b.id
只要表b有记录,与左相反
GROUP BY子句中必须有聚集函数!
HAVING与WHERE的区别:SQL Server applies it after it summarizes the data rather than beforehand. In other words, the WHERE clause is used to determine which rows are grouped.
注意ISNULL、NULLIF、COALESCE的函数用法!
NULLIF:返回类型与第一个 expression 相同。如果两个表达式不相等,则 NULLIF 返回第一个 expression 的值。如果表达式相等,则 NULLIF 返回第一个 expression 类型的空值。
视图可以SELECT,但是UPDATE有限制。
使用OUTER JOIN时候,如果要查询的一个列值为空时:比如 Sales.SalesOrderHeader.CustomerID为空,在where子句中不能使用:Sales.SalesOrderHeader.CustomerID = NULL
应该使用:Sales.SalesOrderHeader.CustomerID IS NULL
数据库 删除冗余
delete from article_chapter
where aid not in
(
select min(aid)
from article_chapter
group by articleid,chaptername
)
常用查询语言:
select * from Tab_PhotoInfo where PhotoNo = '20090617202741075596'
insert into Tab_WebPagePhoto (PhotoNo,BusinessNo,PhotoPath) values('20090617202741075596','2009052010120001','Z:/200905/wangzp/2009052010120001/dsc_0051.jpg')
delete from Tab_WebPagePhoto where WebPagePhotoId = 3
update Tab_WebPagePhoto set PageNo=1,ColumnNo=2,OrdinalNo=2,PublishStatus=1
select * from Tab_WebPagePhot
复制插入:insert into Tab_WebPagePhoto (PageNo,ColumnNo,OrdinalNo,PhotoNo,BusinessNo,PhotoPath,Photodesc,ViewTimes,PublishStatus) select
PageNo,5,OrdinalNo,PhotoNo,BusinessNo,PhotoPath,Photodesc,ViewTimes,PublishStatus from Tab_WebPagePhoto where WebPagePhotoId=1
按某字段排序:select * from Tab_WebPagePhoto order by ColumnNo desc,WebPagePhotoId asc
select top 10 ColumnNo from Tab_WebPagePhoto order by ColumnNo desc,WebPagePhotoId asc
求WebPagePhotoId字段和:SELECT COUNT(DISTINCT WebPagePhotoId) FROM Tab_WebPagePhoto
求WebPagePhotoId字段和并付给PageNo字段:update Tab_WebPagePhoto set PageNo=(SELECT COUNT(DISTINCT WebPagePhotoId) FROM Tab_WebPagePhoto) where WebPagePhotoId = 1
另有: update Tab_WebPagePhoto set OrdinalNo=(SELECT COUNT(DISTINCT OrdinalNo)+1 FROM Tab_WebPagePhoto) where WebPagePhotoId = 8
最近常用:update Tab_WebPagePhoto set OrdinalNo=(SELECT COUNT(DISTINCT OrdinalNo) FROM Tab_WebPagePhoto) where WebPagePhotoId = 8
select * from Tab_WebPagePhoto
declare @ono int
set @ono = (select count(WebPagePhotoId) from Tab_WebPagePhoto where PageNo = 0 and ColumnNo = 1)
update Tab_WebPagePhoto set OrdinalNo=@ono+1 where WebPagePhotoId = 1
选择某个重复记录的字段单一值:select authno, max(authName) as authName from Tab_Authority group by authno
初始化语句:
use PSDB
GO
if exists (select * from sys.objects where type='T' and name='Tab_DD1')
drop Table Tab_DD1
GO
create Table Tab_DD1
- 数据库常用整理
- 数据库常用sql整理
- 数据库常用对象查询 整理
- 数据库常用对象查询 整理
- 数据库常用系统表整理
- 常用数据库语句的整理
- 数据库SQL常用函数整理
- 常用数据库JDBC连接写法(整理)
- 常用数据库JDBC连接写法(整理)
- 常用T-SQL数据库函数整理
- 常用T-SQL数据库函数整理
- 常用T-SQL数据库函数整理
- 数据库之【常用数据字典整理】
- 数据库最常用语句整理1
- 数据库最常用语句整理2
- 不常用的数据库操作整理
- mysql数据库常用sql的分类整理
- 数据库基本概念整理及常用SQL语句
- 遍历Cache实现RemoveAll方法
- Windows下PHP开启mcrypt扩展和SSL(OpenSSL)扩展支持
- Qt,Qt Embedded,Qtopia Core, Qtopia的区别
- C#多线程之Thread
- Session中remove()、removeAll()、clear()、Abandon()区别
- 数据库常用整理
- 清华申请退学博士作品:完全用Linux工作
- fedora linux下安装vmware tools
- Zero-Copy&sendfile浅析
- Linux 之路
- boost_1_45_0在vs2010上的安装使用例子
- gcc g++多文件编译问题!
- TabHost两种实现方式
- MySQL数据库基础:数据类型及列类型