MySQL第三天
admin
2023-04-30 03:20:20
0

MySQL第二天

关键词:分组聚合

自关联

物理上一张表,逻辑上是两张表
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

相关内容

热门资讯

烈焰熊熊,堪比“原子弹”,欧洲... 据美国有线电视新闻网(CNN)7月29日报道,席卷法国西南部的大片野火燃烧得如此猛烈,以至于形成了一...
外交部:对日本熊本县地震遇难者... 据凤凰卫视报道,针对日本熊本县发生强震造成人员伤亡,中国外交部发言人毛宁7月29日在例行记者会上表示...
高铁喧哗被劝阻后叫嚣全家体制内... “女子高铁喧哗被劝阻后叫嚣全家体制内”一事迎来官方通报。据新京报报道,7月29日,辽宁北票市公安局发...
广东专业蓝牙耳机定制厂家推荐,... 在珠三角腹地,广东早已成为全球蓝牙耳机产业链的“心脏地带”。从深圳华强北的声学元件批发,到东莞、惠州...
格雷厄姆葬礼变地缘竞价场!两大... 有些葬礼,送别的是逝者;有些葬礼,演的却是活人最现实的政治博弈。当地时间7月28日,美国老牌鹰派参议...
一道小学数学题难住3名医学博士... 3名不同学校的博士同做一道数学题,谁最厉害?近日,一陕西网友发布的视频引发热议。29日,视频发布者“...
华为全新“小艺任务”亮相:整合... 7月29日,在华为鸿蒙电脑新品技术沟通会上,终端云服务小艺业务部总裁贾永利宣布,小艺将迎来“从对话到...
广西日报特稿《王的猜想》火了,... 澎湃新闻记者 蒋子文 实习生 汤煜炜为应对外界购买纸质版特稿《王的猜想》的热情,《广西日报》宣布紧急...
月租20元内!2026年平价云... 不少手游玩家、账号运营从业者挑选云手机时,首要诉求就是低价够用,想要控制每月支出在20元以内,同时兼...
诺奖得主如何看待王虹境遇?“我... 中村修二(2014年诺贝尔物理学奖得主)【导读】近日王虹与邓煜双双获得2026年菲尔兹奖,成为了首批...