1. 程式人生 > >MySQL第三天

MySQL第三天

分組聚合 事務 python

MySQL第二天

關鍵詞:分組聚合

自關聯

物理上一張表,邏輯上是兩張表
技術分享圖片

create table areas(
id int primary key,
atitle varchar(20),
pid int,
foreign key(pid) references areas(id)
);  

導入sql文件

source areas.sql;  

示例圖

示例語句

select sheng.id as sid,sheng.title as stile,shi.id as shiid,shi.title as shititle from areas as sheng
inner join areas as shi on sheng.id=shi.pid
where sheng.pid is null and sheng.title=‘山西省‘
limit 0,100;

視圖

create view stuscore as + 查詢語句

事務

四個特性(ACID)

  • 原子性(Atomicity):事務不可分割
  • 一致性(Consistency):事務間執行順序不影響結果
  • 隔離性(Isolation):不受幹擾
  • 持久性(Durability):對數據庫的更改不丟失

引擎類型:engine=innodb/bdb 支持事務,默認innodb
查看表創建的語句: show create table students;
修改表類型 : alter table students engine=innodb;

事務語句

begin; //開啟
commit; //提交
rollback; // 回滾操作

索引

查看索引

show index from 表名;

創建索引

create index indexName on areas(title(20));  

刪除索引

drop index [indexName] on 表名;  

執行時間

  • 開啟執行時間監測: set profiling=1;
  • 執行語句
  • 顯示監測結果 : show profiles;

Python的數據庫操作

每一個python會話都是一次事務

常用類

Connec類

connection = connect(host,port,db,user,passwd,charset)

connection對象的方法

close() 關閉連接
commit() 事務提交,所以需要提交才會生效
rollback() 事務回滾,放棄之前的操作
cursor() 返回Cursor對象,用於執行sql語句並獲得結果  

Cursor

執行SQL語句

cursor1=connection.cursor()   // 調用cursor方法 返回一個cursor對象    

cursor對象的常用方法

execute(operation ,[ parameters ]) //執行語句,返回受影響的行數
fetchone() //獲取查詢結果集的第一個行數據,返回一個元組
fetchall()  //獲取查詢結果集的所有行,一行構成一個元組,返回一個大元組

增刪改查

import MySQLdb
try:
    connection=MySQLdb.connect(host=‘localhost‘,port=3306,db=‘python3‘,user=‘root‘,passwd=‘***‘,charset=‘utf8‘)
    cursor1=connection.cursor()
    sql=‘SQL語句增刪查改‘
    count=cs1.execute(sql)
    connection.commit()
    cursor1.close()
    connection.close()
except Exception,e:
    print (e.message)

參數化

防止SQL註入

from MySQLdb import *
try:
    name = raw_input(‘請輸入一個名字‘)
    connection = connect(host=‘localhost‘,port=3306,db=‘python3‘,user=‘root‘,passwd=‘***‘,charset=‘utf8‘)

    #sql = ‘insert into students(name) values("小乖巧")‘
    #sql = ‘update students set name=‘乖巧‘ where id=3‘
    #sql = ‘delete from students where id = 3‘

    sql = ‘insert into students(name) values(%s)‘
    cursor1.execute(sql,[name])  
    connection.commit()
    cursor1.close()
    connection.close()
except Exception,e:
    print (e.message)

封裝

class MYSQL:
    def __init__(self,host,port=3306,db,user,passwd,charset=‘uft8‘):
        self.host = host
        self.port = port
        self.db = db
        self.user = user
        self.passwd = passwd
        self.charset = charset
    def open(self):
        self.connection = connect(host=self.host,port=self.port,db=self.db,user=self.user,passwd=self.passwd,charset=self.charset)
        self.cursor = self.connection.cursor()
    def close(self):
        self.cursor.close()
        self.connection.close()
    def curd(self):
        try:
            self.open()
            self.cursor(sql,param)
            self.commit()

            self.close()
        except Exception,e:
            print(e.message)
    def all(self,sql,param=[]):
        try:
            self.open()
            self.cursor(sql,param)
            result = self.cursor.fetchall()
            self.commit()

            self.close()
            return result
        except Exception,e:
            print(e.message)

5/13/2018 9:57:42 PM

MySQL第三天