zoukankan      html  css  js  c++  java
  • mysql 视图

    http://www.cnblogs.com/wupeiqi/articles/5748496.html   练习题

    21 题目着重 看看。。

    http://www.cnblogs.com/wupeiqi/articles/5713323.html   视图

    select  其实相当于 Python 里的print  

    mysql> select (select count(1 ) from student where gender = '男') as '男', (select count(1 ) from student where gender = '女') as '女';

    +------+------+
    | 男 | 女 |
    +------+------+
    | 10 | 6 |
    +------+------+

     、


    CREATE VIEW team1 as select * from score where student_id = 3            视图命令 其实动态生成表  保存再视图里

    CREATE PROCEDURE  f1() SELECT student.class_id from  student      函数 相当于存储     call   f1()  调用 它

    -- 创建存储过程
     
    delimiter //       ###mysql默认是 分号结尾的 这边定义成//结尾
    create procedure p1()   
    BEGIN      ##好比python里的 :
        select * from t1;    ##好比 缩进
    END//                    ##终止
    delimiter ;              ## 修改会默认 分号终止
    
    
    
    -- 执行存储过程

    
    call p1()
    
    无参数存储过程   

    BEGIN
    SELECT * FROM class;
    SELECT * from student;
    end

     #可以批量执行多条 sql语句
    #!/bin/python
    #-*-coding:utf-8-*-
    import  pymysql                              
    
    conn = pymysql.connect(host = '192.168.132.127',user = 'tenghu',passwd = '111111'
                           ,port = 3306 ,charset = 'utf8',db = 'class_nu')
    cursor  = conn.cursor(cursor=pymysql.cursors.DictCursor)  #运行函数格式
    
    #a = ('狗狗',2)
    #cursor.execute('insert into users (name,age) values (%s,%s)',a )
                  # 'insert into users (name,age) values (%s,%s) ',a   元组形式传递参数
    # reque_1 = cursor.fetchall()
    # print(reque_1)
    
    cursor.callproc('f1')   ##执行函数  f1 
    reslut = cursor.fetchall()
    print(reslut)



    -- 创建存储过程
    delimiter \
    create procedure p1(
        in i1 int,
        in i2 int,
        inout i3 int,   ##形参,
        out r1 int      ## 实参,后期声明的变量不予理睬
    )
    BEGIN
        DECLARE temp1 int;
        DECLARE temp2 int default 0;
        
        set temp1 = 1;
    
        set r1 = i1 + i2 + temp1 + temp2;
        
        set i3 = i3 + 100;
    
    end\
    delimiter ;
    
    -- 执行存储过程
    set @t1 =4;
    set @t2 = 0;
    CALL p1 (1, 2 ,@t1, @t2);
    SELECT @t1,@t2;
    
    有参数的存储过程 
    delimiter \
    DROP PROCEDURE IF EXISTS proc_sql \
    CREATE PROCEDURE proc_sql ()
    BEGIN
        declare p1 int;
        set p1 = 11;
        set @p1 = p1;
    
        PREPARE prod FROM 'select * from tb2 where nid > ?';    ##prod  为 自定义命名,mysql里已 ? 作为占位符。
        EXECUTE prod USING @p1;       ##格式化   传入的值 变量 必须 @开头的  
        DEALLOCATE prepare prod; 
    
    END\
    delimiter ;
    
    动态执行SQL

    无题

    
    
    #!/bin/python
    #-*-coding:utf-8-*-
    import pymysql

    conn = pymysql.connect(host = '192.168.132.127',user = 'tenghu',passwd = '111111'
    ,port = 3306 ,charset = 'utf8',db = 'class_nu')
    # cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) #以字典的形式创建游标

    cursor = conn.cursor()
    cursor.execute('select * from score')
    a = cursor.fetchall()
    print(a)


    #a = ('狗狗',2)
    a = (('狗狗1',2),('狗狗2',2),('狗狗3',2))
    #cursor.execute('insert into users (name,age) values (%s,%s)',a )
    #cursor.executemany('insert into users (name,age) values (%s,%s)',a ) ##many针对多个循环
    # 'insert into users (name,age) values (%s,%s) ',a 元组形式传递参数
    # reque_1 = cursor.fetchall()
    # print(reque_1)

    #cursor.callproc('f1')
    #cursor.execute('select * from teacher')
    #reslut = cursor.fetchall()
    # cursor.callproc('p1',args=(100,200,33,22)) ##执行存储格式的 命令调用
    # r1 = cursor.fetchall()
    # print(r1)
    #
    # cursor.execute('select @_p1_0,@_p1_1,@_p1_2,@_p1_3') ##固定格式 p1为 函数名
    # r2 = cursor.fetchall()
    # print(r2)
    #


    conn.commit()
    cursor.close()
    conn.close()

     

     触发器 

    insert  用 NEW    ,,,delete 用  OLD       update  new用 新值  old用 老值  一起用。

    如果删除某个表的话  那么 表有多少行 就执行多少次。。


  • 相关阅读:
    java提高篇(四)-----抽象类与接口
    hdu1004----用java链表实现
    jkfladsdjfkldsa
    Handler_1
    Handler实现线程间的通信2
    Handler实现线程间的通信1
    Handler基本运行机制
    Android线程
    Activity声明周期2
    Activity声明周期1
  • 原文地址:https://www.cnblogs.com/th-lyc/p/8997835.html
Copyright © 2011-2022 走看看