MySQL的视图和存储

http://www.cnblogs.com/wupeiqi/articles/5713323.html
http://www.cnblogs.com/wupeiqi/articles/5716963.html


1、视图
定义一个名称:
a. 定义对应关系,# 视图--不是真实的表?????

temp1 <---> select * from score where course_id in (1,2)
10道题:

b. 调用temp1

select * from
select * from score where course_id in (1,2)
where xxx
select * from (

select * from score where course_id in (1,2)
) group by
select * from
( select * from score where course_id in (1,2)
) order by

# pymysql
execute('select * from temp1 left join .....')

3、存储过程
c1 =>
select * from temp1 left join .....

# mysql客户端
call c1()

# pymysql
import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='123', db='t1')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
#
# cursor.execute('select * from clasasdfasdfasdfasdfasdfasdfasdfasdfs;')
cursor.callproc('p2')

result = cursor.fetchall()

cursor.close()
conn.close()

2、触发器 

a. 为某一个表:
inser 前后
update 前后
delete 前后
b. OLD,NEW

c. 删除多行,插入,更新
for 多行:
OLD,NEW

# 删除之前,讲删除的之添加到另外一张表
delete from tb1 where nid = 1
-- 张二狗 =>tb2
delete from tb1: OLD
-- 张二狗 =>tb2
-- 张三狗 =>tb2
-- 张四狗 =>tb2
-- 张六狗 =>tb2


5、索引

1、视图
a. 临时表
b. 只能查
c. 执行时才获取数据

2、存储过程
a. 可写复杂逻辑
b. 参数:in out inout
c. 结果集:select ...
3、动态SQL
a. sql是字符串
b. 字符串格式化 ? execute proc using @p1

猜你喜欢

转载自www.cnblogs.com/xied/p/12506209.html
今日推荐