码迷,mamicode.com
首页 > 数据库 > 详细

SQL基础1

时间:2014-12-30 23:16:01      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:

insert into TblStudent(tSName,tSGender,tSClassId)
select ‘name‘,‘男‘,1 union all
select ‘name‘,‘男‘,1
多条插入

什么时候需要在汉字N‘男‘
当数据类型为varchar并且不是中文排序规则

删除表中所有数据
delete from 表名
truncate table 表名 比较快 最小方式记录日志,自动编号重置,不触发delete触发器
drop table 表名 删除表


percent 百分比 select top 10 percent * from tblStudent where tsAge is not null order by tsAge asc


任何聚合函数都不计算null值,count(age) 如果age列有null那么不算。


select * from TblStudent where tSAge>=20 or tSAge<=30 快
select * from TblStudent where tSAge between 20 and 30 会转换
select * from TblStudent where tSAge in(20,21,22,23,24,25,26,27,28,29,30) 这种不会转换成>= <= 也有not in


类型转换
select ‘张三‘+cast(18 as char(2))
select ‘张三‘+CONVERT(char(2),18) convert的第三个参数为样式号


联合union 就是竖向合并对应的列要兼容
select tSName,tSGender from TblStudent union
select tTName,tTGender from TblTeacher
union会把重复数据去掉 union all 实际多少就是多少


一次插入多条数据
insert into Score(studentId,English,Math)
select 1500 union
select 1500 union
select 1200 union all
select 13
这里同样会去除重复

如果表不存在
select * into NewStudent from TblStudent
将TblStudent中的数据导入NewStudent中,表不存在会创建,存在会报错 不会创建约束

select * into NewStudent from tblStudent where 1<>1 会创建相同的表,没有数据 不满足条件,分数据库 有可能逐条比较
select top 0 * into NewStudent from tblStudent 也可以

向表追加别的表数据
insert into NewStudent select tsName from tblStudent

 

SQL基础1

标签:

原文地址:http://www.cnblogs.com/woge/p/4194627.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!