sql个人总结2

前一篇说了sql语句的数据库操作,这一篇就说下表的操作。

1:先说下表的插入,更新,删除这些操作。

a:插入 insert

规正式的插入:

/* insert into tablename(column) values(?) */ 
/* column也可以没有,可以是一张表,没有值的列用null补齐 */
insert into student(sid,sname) values(1,'1')
insert into student values(2,null)
带有查询的插入:
insert into role(rid,rname) select sid,sname from student where sid=2 

b:更新 update

/* update tablename set column=value,column=value,column=value where condition */
update role set rname='2',sid=2
update role set rname='b' where sid in (select sid from student where sid='1')

c:删除 delete

/* delete from tablename where condition */
delete from student where sid=3
delete from role where sid in (select sid from student where sid=2)

2:基本的单表查询

a:格式及注意事项

/* select all/distinct from (tablename or viewname) where group by column having condition order by column asc/desc*/
/* ditinct为区别同一列,作用在整个查询范围,不特指那一列,一个简单查询只能出现一次 */

b:转换大小写

select rid,upper(rname),1-sid,rtest from role    --upper为改变为大写,lower<span style="font-family:SimSun;">为小写</span>

c:查询字符串

select rid,'a' from role                         --查询特殊值字段

d:使用别名改变查询查询标题

select rid RID,upper(rname)RNAME,1-sid RSID,rtest RTEST from role --使用列别名改变查询列标题

e:between的用法

select * from role where rid between 1 and 3     --between的用法,如果不在,在between前加not关键字

f:in的用法

select * from role where rid in(1,2,3)           --in的使用方法

g:模糊查询,以及去掉通配符

select * from role where rtest like '%2'         --%为任意长度的字符串 _为单个长度的任意字符
select * from role where rtest like '2%3' escape '2' --如果%不是通配符,只是普通的符号,我们用escape来排除,这里需要注意的是排除的是你选则的字符的后面的

h:null值得使用

select * from student where sname is null        --这里的null前面的is不能用=代替

i:升序和降序

select * from role order by rid asc,sid desc     --这里是根据rid的升序和sid的降序进行排序

j:聚集函数


/* count(*) 
   count(column) --计数
   sum(column)   --求和
   avg(column)   --求平均数
   max(column)   --最大值
   min(column)   --最小值
   --除count(*),剩下的只处理非空
*/

k:group by分组

select sno,COUNT(*) from sc  where grade>90 group by sno having COUNT(*)>3 --这里仅做实例,查询有三门课程90分以上的学生的学号 

3:连接查询

a:等值连接查询

select rid,r.sid from role r inner join student on r.sid=student.sid  --内链接相当于直接用等号
select rid,r.sid from role r left join student on r.sid=student.sid   --左链接,取第一个表中的所有数据,第二个表中没有的以null补齐,是left outer join的缩写
select rid,r.sid from role r right join student on r.sid=student.sid  --右连接,取第二个表中的所有数据,第一个表中没有的以null补齐,是right outer join的缩写
select * from role r full join student on r.sid=student.sid  --全链接,取二个表的所有数据,第一个表中没有以null补齐,第二个表中没有以null补齐

b:嵌套查询

--子查询不允许出现排序,即order by
--exists先执行前面的查询,再执行exists里面的
select role.* from role where role.sid  in(select student.sid from student)    --in查询,由里向外查询,嵌套查询越多越慢,就不如等值连接的查询速度了,一般可以用等值连接转换
select role.* from role where role.sid < any (select student.sid from student) --any为某一个值,all为所有值,<>为!=,意思是可能大于可能小于
select r.* from role r where exists(select s.sid from student s where s.sid=r.sid)  --in和exists一般能够互换,exists为存在,判断子查询是否为空值

c:集合查询

/* 集合查询 */
--union 并集,必须有相同数量的列,数据类型也必须相同,顺序也必须相同
select * from student where sid=1 union select * from student where sid=2
--intersect 交集
select sid from role intersect select sid from student  
--ecxcept 差集,多的集合放前面
 select sid from student except select sid from role

4:视图

a:创建视图

create view role_view as select * from role with check option  --不能含有 order by 和 distinct,只是把定义存入存入数据字典,并没有执行
 --with check option 防止用户通过视图对不属于视图范围内的数据进行更新
 --group by 视图不能更新,表达式,聚集函数也不能更新

b:删除视图

drop view role_view

基本的操作就这些,里面的细节没有说,因为关于sql基本的大家都知道,但sql的优化是一件很重要和复杂的事,sql的优化最主要的还是优化对索引使用。


猜你喜欢

转载自blog.csdn.net/u014274324/article/details/44040093