SqlServer总结

Unicode:两个字节表示一个字符(包括字母和汉字) 非Unicode:一个字节表示一个字母,两个字节表示一个汉字(utf-8,ios-8859-1,gbk,gb2312,big5) 【字符类】 char 非Unicode类型,最大8000个字符,定长。 nchar Unicode类型,最大4000个字符,定长。 varchar 非Unicode类型,最大8000个字符,变长。 nvarchar Unicode类型,最大4000个字符,变长。 text 可变长度非Unicode数据,最大长度2^31-1。 ntext 可变长度Unicode数据,最大长度2^30-1。 【注释】 --:单行注释sql语句 /*..*/块注释 【数值型】 在使用float时应指定位数不然会产生误差 create table test( Id int primary key identity(1,1),/表示该字段自增,从1,每次加1/  test1 float(3) ) decimal和numeric需指定总位数p与小数位数s即: decimal[(p[,s])和numeric[(p[,s])] create table test( test2 numeric(10,2) ) 【日期类型】 datetime(表示日期) timestamp(时间戳) create table test( birthday datetime ) insert into test birthday values (getdate()); 【增删改查】 与MysqL相同 若以null为定位依据,用is代替= 例如:delete from test where name is null; 如果where有多个条件用and连接 【忽略重行查询】 select distinct 字段 from 表名 where 条件 【算数式查询】 例子:查询员工年工资 select name,salary*12 from emp 加入别名 select name,salary*12 as 年工资 from emp  /*别名可以单/双/无引号,也可以无as*/ select name,salary*12+[奖金字段]*13 年工资 from emp //假设其中奖金为null,则会出现错误,null与任何数运算都为null; 故采用函数 select name,salary*12+isnull([奖金字段],0)*13 年工资 from emp  【where查询】 时间比较: 例:查询1982.1.1月入职的员工 select * from emp where hireDate >'1982-1-1';  /数据库自动将其转化为datetime进行比较/ 区域比较: 例:查询工资在2000和2500之间的员工 select * from emp where salary>=2000 and salary<=2500; select * from emp where salary between 2000 and 2500;/包括2000和2500,效率高/ 【模糊查询】 %:表示0到多个字符 _:表示一个字符 例1:查询首字母为s的雇员 select * from emp where name like 's%'; 例2:查询第三个字符是O的所有员工 select * from emp where name like '__O%'; 例3:查询编号为123,456,678号的雇员 select * from emp where id=123 or id=456 or id=678;/效率低下,一般采用批量查询代替/ 即: select * from emp where id in(123,678); 【order by】 例:将员工按工资从低到高排序 select * from emp order by salary [asc];/认/ 从高到低 select * from emp order by salary desc;/如果是字符,则按照字典序/ 例:按照部门升序,而雇员工资降序排列 select *  from emp order by depId asc  ,salary desc; 例:统计每个人年薪,并按照从低到高排序; select name,salary*12  '年薪'  from emp order by '年薪' ;  【函数查询】 例:显示最低工资和雇员的名字 select name,salary from emp where salary = (select min(salary) from emp);/包含子查询,因为sql检查是从右往左/  例:显示员工中工资最高和工资最低的 select max(salary),min(salary) from emp;  例:显示平均工资和工资总和 select avg(salary) 平均工资,sum(salary) 总工资 from emp; 例:显示工资大于平局工资的雇员; select * from emp where salary>(select avg(salary) from emp); 【group by】 例:显示每个部门的平均工资  select avg(salary),depId from emp group by depId 例:显示每个部门每种岗位的平均工资和最低工资 select avg(salary),min(salary),depId,job from emp group by depId,job order by; 【having】/通常和group by 结合使用,用于查询结果的筛选/ 例:显示平均工资低于2000的部门和他的平均工资 select avg(salary),depId from emp group by depId having avg(salary)<2000 【总结】 选择语句中如果同时出现 group by, having, order by 则顺序为 group by, having, order by  如果选择列中有列,表达式,分组函数,那么这些列或表达式必须有一个出现在group by 子句中,否则就会出错 如:select avg(salary),depId from emp group by depId having avg(salary)<2000 depId必须出现在group by子句中 【多表查询】 前提: 雇员表:empno,ename,job,mgr(上级),hiredate,salary,comm(奖金),depno 部门表:depno,dname,loc; 笛卡尔集:select * from emp,dept;  显示雇员名,雇员工资,以及部门的地址:select ename,salary,loc from emp,dept where emp.deptno=dept.deptno 别名:select ename,salary,loc e.depno from emp e,dept d where e.deptno=d.deptno 【单表内复杂查询】 例:显示某个员工上级领导姓名,比如ford的上级 select ename from emp where empno=(select mgr from emp where ename='ford'); 例:显示每个员工的名字和上级的名字 分析:将这一个表看做是两个表 emp worker和emp boss select worker.empno,boss.ename from emp worker,emp boss where worker.mgr=boss.empno 【单行子查询显示与smith同一部门所有员工 select * from emp where deptno=(select deptno from emp where ename='smith');/多行子查询=变为in/ 【多行子查询】 如何显示高于各个部门平均工资的员工的姓名 1:各个部门平均工资:select avg(salary),deptno from emp group by depno; 2:把上面的表当做一个临时表对待 select ename from emp, (select avg(salary) myavg,deptno from emp group by depno) temp  where emp.deptno=temp.deptno and emp.salary>myavg; 【分页查询】 例:显示第5到第10个入职的雇员,按入职时间顺序 1:找出最先四个入职雇员:select top 4 * from emp order by hiredate; 2:确定第5到第10:select top 5 * from emp empno not in (select top 4 empno from emp order by hiredate) order by hiredate; 【表内快速复制】 insert into 表名 字段 select 字段from 表名; 【用查询结果创建新表】 select 字段 into 另一个表名 from 表 例:删除表cat中重复的数据 select distinct * into #temp from cat; delete from cat; insert into cat select * from #temp; drop table #temp; 【复合主键】 create table test1( id int,name varchar(10),nickname varchar(10),address varchar(10)  primary key (id,address) ) 【行级定义与表级定义】 行级定义:创建完列定义以后直接定义其约束; 表级定义:表创建完成之后在定义其约束; 【check】 create table test1( id int,address) salary int check(salary>=2000 and salary<=3000) ) 【default】 create table test1( id int,address varchar(10),primary key (id,address),hiredate datetime default getdate()/如果不指定值,认当前时间/ ) 【修改表】 1:添加一个字段 alter table distributors add column 字段; 2:修改字段类型或是名字(其中不能有数据) alter table distributors alter column 字段 type 类型, alter table distributors rename column 字段名 to 字段名 【数据库备份和恢复--图形界面操作】 【分离与附加】 1:分离,将数据库脱离管理,即sqlServer不再关联此数据库,分离过后在sqlServer安装目录下找到 数据库名.mdf 和 数据库名.ldf 两个文件即分离的数据库,分离后的数据库不能再使用 2:附加,sqlServer重新关联次数据库。 操作:鼠标右键分离,鼠标右键附加; 【备份和恢复】 将数据复制一份到demo.bak,只有一个文件 当原数据库损坏时,右键还原(从设备) 【数据库备份和恢复--命令行操作】例数据库aaa 备份:backup database aaa to disk='d:/demo.bak' 恢复:restore database aaa from disk='d:/demo.bak' 【数据库太大时备份表】与数据库备份相似   【java操作数据库--jdbc-odbc桥连接方式】(odbc微软推出的对数据库操作的api) 【数据源配置】 1:打开控制面板--管理工具--数据源--用户dsn添加--链接sqlServer--选择服务器(写local或一个.)--... 2:在程序中链接数据源 (1)加载驱动: class.forname("sun.jdbc.odbc.JdbcOdbcDriver"); (2)得到链接 Drivermanger.getConnection("jdbc:odbc:数据源名","用户名","密码")/如果配置数据源时是windows验证,则不需用户名 密码/ package demo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class Demo {       public static void main(String[] args) {       Connection con=null; Statement stmt=null; ResultSet rs=null;         try {              Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");              System.out.println("成功加载sql驱动程序");          }          catch (Exception e) {              System.out.println("找不到sql驱动程序");          }           try {         con = DriverManager.getConnection("jdbc:odbc:LibrarySeat","","");              stmt = con.createStatement();  String sql = "select * from seatnumber"; rs=stmt.executeQuery(sql); while(rs.next()){ System.out.println(rs.getobject(1)+"\t"+rs.getobject(2)); }            System.out.println("数据库连接成功");           }           catch (Exception e) {              System.out.println("数据库连接失败");           }       }   }    

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...