MySQL第三天
发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,MySQL第二天关键词:分组聚合自关联物理上一张表,逻辑上是两张表create table areas(id int primary key,atitle varchar(20),pid int,fo
千家信息网最后更新 2026年11月02日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 shenginner join areas as shi on sheng.id=shi.pidwhere 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 MySQLdbtry: 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
语句
事务
结果
对象
索引
数据
方法
查询
乖巧
常用
数据库
时间
类型
影响
监测
一致
不可分割
一致性
一行
关键
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
什么是科技互联网
互联网巨头科技公司
金融科技创新监管互联网金融
齐家互联网科技
互联网金融科技创新
奔汉互联网科技
互联网 新科技
丰台科技园 互联网公司
互联网科技股午盘股票涨跌
华脉科技工业互联网产品
中国的互联网科技企业的崛起
互联网校园科技培训
和信科技工业互联网
互联网5g黑科技有哪些
互联网保险科技创业
乌鲁木齐新华智原互联网科技学校
启辰互联网科技
西安有哪些互联网高科技企业
互联网领先科技活动
求路客互联网科技电话
互联网科技公司简介
互联网金融 科技
互联网络科技有限公司
科技服务与互联网
美国互联网黑科技
米聚互联网科技南京有限公司排面
浙南科技城互联网大厦建设工程
区块链结合互联网科技
互联网硬科技公司
安徽九吞吴互联网科技有限公司