黑马程序员技术交流社区

标题: 自己总结了下SQL 的常用语句,有什么错误或者不足,请指出 [打印本页]

作者: 穷傻呆丑萌    时间: 2013-7-30 10:42
标题: 自己总结了下SQL 的常用语句,有什么错误或者不足,请指出
create database  class2--  创建 数据库  数据库名
use class2 --  使用  数据库名
go
create table class22
(
sno char(10) not null constraint  pk_zj primary key,--设置的主键
sname char(10)  not null,
ssex char(2)  not null,
sage int not null,
)
主键:主键就是数据库唯一标示,不会重复的列可以做主键,一个表可以没有主键
insert into class22 values('20110235','晓晓','男',21)--向表中插入数据
insert into 表名 values('参数一','参数二','参数三')
需要注意的是,values括号内的参数的个数类型必须和你表相符合

delete class22  --这是删除表中数据命令
update class22 set sname='张俊峰' where sno='20110233'  --更新数据--意思就是把“张俊峰”名字改成“蔡晓晓”更改数据
updata  表名  set  字段名称='所要更改的' where 更具什么条件更改
select * from class22  --   * 号表示所有列
select sno from class22  --   表示查询sno那一列数据
select sname as '姓名',sage as '年龄' from class22 where age>100 --查询class22这个表中所有的年龄大于100的数据,显示结果列名姓名代替sname 年龄代替asge
select count(*) from class22  --查询有多少数据
select max(age) from  class22  --聚合函数,查找最高年龄的
select * from class22 order by age  desc--按照年龄排序    排序有两个  一个是asc(升序),一个事desc(降序)
select * from class22 where age>20 order by id asc--查询class22这个表中所有的age大于20并且按照id排序 升序
select * from class22 where name like '_俊峰'--模糊查询
select * from class22 where name like '%俊%'
_代表一个字符,%代表0到无限个
null  (我自己习惯叫做空) 视频里面老是说可以称作’不知道‘   null不是没有
selct * from class22 where name is null
selct * from class22 where name is not null
--查询表里面名为为null和不为null的数据

select * from class22 where age=23 ,or age=25,or age=28
select * from class22 where age in(23,25,28)
select * from class22 where age>20 and age<30
selcet *from class22 where age berween 20 and 30
--我自己给这个起码叫并列查询
select age,count(*) from class22 group by sage-- 将查询数据分组
selcet top 3 * from class22 order by age desc-- 将查询的数据排序(降序)并且取前三个

select top 3 * from class22 where number not in
(secect top 5 number form class22 order by age desc)
order by age dese--查询第5个开始的前3个数据
select age name form class22  union select age name from class11
union:将两个表合到一起,其中列的数量必须一样,且类型相容
union 去掉重复的
union all  不去掉重复的
在写程序中按照要去来运用
abs() --求绝对值
ceiling() --舍入最大整数
floor() --舍入最小整数
round()  --四舍五入
len() --求长度
rtrm() --去掉右面空格
lrtm() --去掉左面空格
substring(0,0) --截取字符串 从哪里开始截取,截取几个
getdate() --获取日期
datadiff(0,1,2) --计算两个时间的差个 单位,起始时间,结束时间

select cast('123' as int), --转换类型 将字符转换成整形
cast('2008-08-08' as datetime) -- 讲字符转换成时间类型
convert(datetime,'2009-09-09')  --将字符转换成时间
convert(varchar(50),123) --将整形转换成字符
select isnull (name,'佚名') as '姓名'from class22 --如果是null则显示佚名
select name,
(
   case id
when 1 then '第一'
when 1 then '第二'
when 1 then '第三'
) as '名次' from class22 --case 用法


作者: 赵宗荣    时间: 2013-7-30 11:40
建议你总结起来发表成博客
作者: 穷傻呆丑萌    时间: 2013-7-30 11:44
赵宗荣 发表于 2013-7-30 11:40
建议你总结起来发表成博客

这是我从开始学习数据库到现在 总结的一点知识点,经过精简  删改后得出的这篇总结   不过您说的博客 确实是个好主意呀   哈哈 谢谢
作者: 马晓凤    时间: 2013-7-30 12:22
挺好的,不错,不过  
create table class22
(
sno char(10) not null constraint  pk_zj primary key,--设置的主键
sname char(10)  not null,
ssex char(2)  not null,
sage int not null,
)
亲 多了个逗号, 建议写博客
作者: 小天    时间: 2013-7-30 13:04
数据表中的主键一般为int类型,且是自动增长




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2