虽然现在Microsoft的产品SQLServer好像不太受欢迎(从我身边了解到的),这里就简单说说一些SQLServer的SQL,其中很多是标准的SQL语句,数据库通用的。 连接步骤 //前期准备 String driver =com.microsoft.sqlserver.jdbc.SQLServerDriver;URL: String url =jd
虽然现在Microsoft的产品SQLServer好像不太受欢迎(从我身边了解到的),这里就简单说说一些SQLServer的SQL,其中很多是标准的SQL语句,数据库通用的。
连接步骤
//前期准备
String driver =com.microsoft.sqlserver.jdbc.SQLServerDriver; URL: String url =jdbc:sqlserver://localhost:1433;databadeName=db_Blog; USERNAME: String username = sa; PASSWORD: String password = ysjian //按照自己的设定
//利用发射机制创建类的加载和连接
Class.forName(driver); Connection conn =DriverManager.getConnection(url,username,passWord);
//执行预编译
String sql ; String[] param; PreparedStatementpstm = conn.prepareStatement(sql); If(param!=null&?m.length>0){ for(inti=0;i<param.length i pstm.setstring><br> <br> <p>执行查询:ResultSetrs = pstm.executeQuery();</p> <p>执行更新:int result = pstm.executeUpdate();</p> <p> </p> <p><strong>主键(primarykey)</strong>:数据的唯一标识,不会重复的列做主键</p> <p>1. 业务主键:使用有业务意义的字段做主键,如用户名,身份证号,银行账号等(不推荐)</p> <p>2. <span>逻辑主键:</span>使用无任何意义的字段做主键,因为很难保证业务主键不会重复,所以<span>推荐使用逻辑主键</span></p> <p><strong>外键(foreignkey):</strong>在表与表之间建立联系的枢纽,标间关联</p> <p> </p> <p><strong>列的数据类型:</strong></p> <p>bit(0或1):相当于boolean类型的数据; </p> <p>char(n):不可变的字符串,不足部分用空格填充</p> <p>varchar(n):最大长度为8000</p> <p>nvarchar(MAX):类似无限大,2^31-1</p> <p>datetime(时间类型):date</p> <p>timestamp:时间戳,时间格式较全的格式</p> <p>uniqueidentifier:唯一标示符(推荐做主键)</p> <p><strong>主键的选择:</strong></p> <p>1. int(bigint)+标识列(自增字段)</p> <p>2. uniqueidentifier(GUID):<strong>业界主流</strong></p> <p><strong>int自增做主键的优缺点:</strong></p> <p> 优点:占用空间小,无需开发人员干预</p> <p> 缺点:效率低,数据导入导出时不便</p> <p><strong>GUID做主键的优缺点<br> </strong> 优点:效率高,数据的导入导出方便</p> <p> 缺点: 占用空间大,不易读</p> <p><strong>SQL语句</strong></p> <p><strong> </strong></p> <p><strong><u>◎插入语句</u></strong></p> <p><strong>int自增做主键:</strong></p> <p></p> <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">insert into users values(‘ysjian’,22)--自增主键可以不给值 insert into users(name,age) values(‘ysjian’,22)--推荐带上列名
uniqueidentifier做主键:
insert into users values(‘ysjian’,22)--自增主键可以不给值 insert into users(name,age) values(‘ysjian’,22)--推荐带上列名
--表示不等于20
update users set name=N’袁’ where age20 update users set name=N’袁’ where age!=20 update users set name=N’袁’ where age>20 and age <p><strong><u>◎删除语句</u></strong></p> <p></p> <pre class="brush:php;toolbar:false"> delete from users--清空表(注意delete后面不能加*) delete from users where age=20
◎查询语句(重点)
select* from users select name as ‘姓名’,ageas 年龄,id as ‘编号’from users select ‘姓名’ =name , 年龄= age,id as ‘编号’from users select age+3 as 年龄 from users
//聚合函数
Select count(*) from users Select max(age) from users Select min(age) from users Select avg(age) from users Select from users
//排序
Select * from users order by age desc--按年龄降序 Select * from users where age>20 order by age asc--按年龄升序
//模糊查询(通配符’_’和’%’)
Select * from users where name like‘袁_’--查询以”袁”开头后面有一个字符 Select * from users where name like‘%袁%’--查询名字有”袁”字的数据
//null(不知道)
Select * from users where name is null Select null+1--结果为null Select null+’123’--结果为null
//分组查询
Select age ,count(*) from users group by age --查询的列名必须与分组一致,聚合函数不能出现在where子句中 (错)Select count(*) from users where count(*)>5 group by age(错)--having子句是对分组后的信息过滤,能用的列是查询的列 (错)Select count(*) from users group by age having id>5(错) Selectage, count(*) from users group by age having age>20 and count(*)>5
//限行查询
Select top 5 * from users order by age desc Select top 5 percent * from users
//经典例子:按工资从高到低的排序检索从第六名开始一共五人信息
Select top 5 salary from employee where id not in(select top 5 id from users order by salary desc) order by salary desc
//保持数据的唯一
Select distinct eName fromemployee--保持整行数据的唯一性
//联合查询,上下字段的个数必须一致,且数据类型相容
Select name,age from users Union all--默认会将完全重复的数据合并,all可以阻止合并 Select name,5 from users2
//(联合查询的运用)报表的制作
Select ‘正式工最大年龄’,max(fAge) from T_employee Union all Select ‘正式工最小年龄’,min(fAge) from T_employee Union all Select ‘临时工最小年龄’,min(fAge) from T_tempEmployee Union all Select ‘临时工最大年龄’,max(fAge) from T_tempEmployee Select FNumber,FSalary from T_Employee Union all Select ‘工资合计’,sum(Fsalary) from T_Employee
//数据库函数
Select ABS(-5)--绝对值5 Select ceiling(5.2) --大于5.2的最小整数 Select floor(-3.5)--小于-3.5的最大整数 Select round(3.1415926,3)--四舍五入,指定取舍位3,结果为3.1420000 Select len(‘abc’)--3 Select lower(‘ABC’)--abc Select upper(‘abc’)--ABC Select ltrim(‘ china ’)--china Select rtrim(‘ china ’)-- china Select substring(‘yuanshenjian’,3,5)--开始位置为3,长度为5
//日期函数
Select getdate();--取得当前日期 Select daeAdd(day,5,getdate())--当前时间天数加3 Select dateDiff(day,’1990-08-02’,getdate())--1990-08-02距离当前时间的天数 Select datePart(year,getDate())--返回一个日期的特定部分
//经典语句
Select dateDiff(year,FinDate,getDate()), count(*) from T_Employee Group by dateDiff(year,FinDate,getDate()) Having count(*)>2
//类型转换
Select cast (‘123’asint),cast(‘2012-11-23’as datetime) Select convert(datetime,’2012-11-23’),convert(varchar(50),123)
//流控函数,如果FName为null,赋值为“佚名”
Select isnull(FName,’佚名’) as 姓名 from T_Employee
//单值判断
Select FName, ( case Flevel when 1 then‘普通客户’ when 2 then‘会员’ when 3 then‘VIP’ else ‘未知客户类型’ end--一定要加end )as 客户类型 from T_Customer
//l练习:表中有A,B,C三列,但A大于B时选A,否则选B,但B大于C时
选B,否则选C
Select ( case when A>B then A else B end ), ( case when B>C then B else C end ) From player
//练习二
Select Name as 队名, sum( case scores when ‘胜’ then 1 else 0 end )as 胜, sum( case scores when ‘负’ then 1 else 0 end )as 负 from Team group by Name
//数据库的创建
if exsits(select * from sys.database when [name]=’market’) drop database market create database market on ( name=’market.mdf’, filename=’E:\Microsoft\market.mdf’, size=5, maxsize=555, filegrowth=55 ) log on ( name=’market’, filename=’market.ldf’, size=5, maxsize=55, filegrowth=55% )
//表的创建
if exists(select * from sys.objects where[name]=’employee’) drop table employee create table employee ( eId varchar(5) not null primary key, eSex bit not null default(1), uidint not null identity pid varchar not null foreign key references employees(pId) )