Sql语句

1.删除表中全部数据两种方式:

delete from t_user;

truncate t_user;--推荐,效率更高

2.创建索引:

create index emp_last_name_idx on employees(last_name);

同时对多列建立索引

create index emp_last_name_idx2 on employees(first_name,last_name);

3.Mysql在创建视图时使用 with check option,代表该视图不允许被更改。Oracle 采用的是with only read

4.case when两种用法:

(1)

case java_teacher

when 1 then 'Java 老师'

when 2 then "Ruby 老师'

else '其他老师'

end

(2)

case

when student_id<=3 then '初级班'

when student_id<=6 then ‘中级班'

else ’高级班'

end

第二种更灵活

5.函数:

select DATE_ADD('1988-01-02',Interval 2 MONTH);

select ADDDATE('1988-01-02',3);

select CURDATE();

select CURTIME();

select MD5('123456');//MD5加密

ifnull(espr1,expr2) 如果为空则返回expr2,否则返回expr1

nullif(expr1,expr2)如果相等则返回空,否则返回expr1

if(expr1,expr2,expr3)三目运算

isnull(expr1)expr1为空,返回true,否则false

6.连接查询:

自然链接:natural join 不用关联条件,会自动使用同名列作为关联条件,超过一列以上同名会将所有的同名列都作为关联条件

using 子句连接:可以显示指定同名列作为查询条件,join t_teacher using(teacher_id)

全外连接:会把连个表中的不满足连接条件的记录全部列出。

7.in >any  >all 

in 满足子查询中的条件

>any 大于列表中的最小值

<all 小于列表中的最小值

=any:子查询中可以返回多行,多列

select * from t_student where (student_id,student_name)= any(select teacher_id,teacher_name from t_teacher)

8.集合运算:

union:select 语句 union select 语句  注:要求两个结果集的数据列的数量和类型必须一致

minus:是从A结果集-B结果集中与A相同的记录

intersect:找出A结果集和B结果集相同的记录,但是Mysql不支持,可以用下面的方法来代替

select student_id,student_name from t_student where student_id

join 

t_teacher

on (student_id=teacher_id and student_name=teacher _name)

where (student_id<4 and teacher_name like '冯%')

 9.说明:复制表( 只复制结构, 源表名:a新表名:b) 

create table b   

As

Select * from a where 1=2

(where1=1,拷贝表结构和数据内容)

10.说明:拷贝表( 拷贝数据, 源表名:a目标表名:b) 

insert into b(a, b, c) select d,e,f from a; 

11.有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value

这道题的SQL 语句怎么写?

update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);

12.

NULL是什么意思?

NULL这个值表示UNKNOWN(未知):它不表示“”(空字符串)。您不能把任何值与一个 UNKNOWN值进行比较,并在逻辑上希望获得一个答案。您必须使用IS NULL操作符。

13.维护数据库的完整性和一致性,你喜欢用触发器还是自写业务逻辑?为什么?

答:我是这样做的,尽可能使用约束,如check, 主键,外键,非空字段等来约束,这样做效率最高,也最方便。其次是使用触发器,这种方法可以保证,无论什么业务系统访问数据库都可以保证数据的完整新和一致性。最后考虑的是自写业务逻辑,但这样做麻烦,编程复杂,效率低下。

14.什么是存储过程?用什么来调用?

答:存储过程是一个预编译的SQL 语句,优点是允许模块化的设计,就是说只需创建一次,以后在该程序中就可以调用多次。如果某次操作需要执行多次SQL ,使用存储过程比单纯SQL 语句执行要快。可以用一个命令对象来调用存储过程。

15.mysql分页查询:

第一个参数指定第一个返回记录行的偏移量第二个参数指定返回记录行的最大数目

SELECT * FROM table LIMIT [offset,] rows
select * from table limit 5,10; --返回6-15行

如何优化limit

当一个查询语句偏移量offset很大的时候,如select * from table limit 10000,10 , 最好不要直接使用limit,而是先获取到offset的id后,再直接使用limit size来获取数据。效果会好很多。

select * From customers Where customer_id >=(
select customer_id From customers Order By customer_id limit 10000,1
) limit 10;

16.Oracle分页查询语句基本上可以按照本文给出的格式来进行套用。
Oracle分分页查询格式:

SELECT * FROM  
(  
SELECT A.*, ROWNUM RN  
FROM (SELECT * FROM TABLE_NAME) A  
WHERE ROWNUM <= 40  
)  
WHERE RN >= 21  
17.使用临时表    
   在必要的情况下,为减少读取次数,可以使用经过索引的临时表加快速度。
   如:
   select e.id from employee e ,dept d where e.dept_id=d.id and e.empno>1000 order by e.id   (错)
   
   select id,empno from employee into temp_empl where empno>1000 order by id
   select m.id from temp_emp1 m,dept d where m.empno=d.id      (对)
   
原文地址:https://www.cnblogs.com/fengxiaoyuan/p/10129288.html