---
title: "SqlServer基础"
author: "yunchow"
date: "2009-09-20T23:34:29Z"
tags: ["JDBC", "SQL", "SQL Server", "Web", "XML"]
slug: "sqlserver"
draft: false
lastmod: "2009-09-21T00:21:40Z"
id: 472807
url: "https://yunchow.iteye.com/blog/472807"
---

--------- SQL ----------  
T-SQL是SQL的加强版  
dml:select, insert, update, delete  
dcl:grant, revoke  
ddl:create table, drop table  
&lt;&gt; : 不等于  
\=: 等于  
\_: 代表一个字符  
%: 代表任意长度的字符  
\[]: 指定范围内的字符  
\[^]: 取反,不是指定的字符  
通配符常常与LIKE关键字一起使用  
and, or, not逻辑表达式  
\-----------------------  
insert \[into] &lt;表名&gt; \[列名] values&lt;值列表&gt;  
注意:  
1,要满足完整性的要求  
2,不能为标识列指定值  
3,不允许为空必须要插入数据  
4,具有缺省值的可以用default来代替  
insert into &lt;表名&gt; \[列名]  
select &lt;列名&gt;  
from &lt;源表名&gt;

--先会创建表,再插入数据  
select &lt;列名&gt;  
into &lt;表名&gt;  
from &lt;源表名&gt;

--生成新的标识  
select identity (int ,1,1) as userId ...

update 表名 set 列名=更新值  
where 更新条件

delete from 表名 where 删除条件

--删除表里所有的数据  
truncate table 表名  
\-- 会将所有相关信息都清除,包括标识外键信息  
\-- 执行后,不能回滚

 

事务  
conn.setAutoCommit(false);  
conn.commit();  
conn.rollback();

批处理  
stmt.addBatch(sql);  
stmt.executeBach();

连接池  
import javax.naming.Context;  
import javax.naming.InitialContext;  
import javax.naming.NamingException;

JNDI(java naming directory interface)  
Context ctxt = new initialContext();  
DataSource ds = (DataSource)ic.lookup("java:comp/env/jdbc/books");  
comp/env : j2ee默认路径名

JNDI 配置  
&lt;Context&gt;  
  &lt;Recource  
 auth="Container"  
 type="javax.sql.DataSource"  
 maxActive=   
 name="jdbc/books"  
   username=  
  password=  
 driverClassName=  
 url=  
  /&gt;  
&lt;/Context&gt;  
在web.xml中配置  
&lt;resource-ref&gt;  
&lt;res-ref-name/&gt;  
&lt;res-type/&gt;  
&lt;/resource-ref&gt;

\----------------  
查询  
sql server中的函数  
字符函数:  
charindex,len,lower,upper,ltrim,rtrim,right,replace,stuff  
日期函数:  
getdate,dateadd,datediff,datename,datepart  
系统函数:  
convert(用得比较多),current\_user,datalength  
host\_name,system\_user,user\_name  
聚合函数  
sum,avg,max,min,count,  
\-----------  
分组查询对比  
where, group by, having  
多表连接查询  
内连接:inner join on 为默认的无先后顺序  
左外连接:left join on以左边的表为基准  
右外连接:right join on  
\---------------------------  
类与数据库设计---继承  
1,如何在关系数据库中实现继承  
每个类均映射为数据库表  
\* 对报表的支持较差,除非定义视图