Chinaunix首页 | 论坛 | 博客
  • 博客访问: 231223
  • 博文数量: 57
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 557
  • 用 户 组: 普通用户
  • 注册时间: 2015-10-01 18:05
文章分类

全部博文(57)

文章存档

2017年(57)

我的朋友

分类: Python/Ruby

2017-11-23 17:11:41

1.     修改数据

首先,我们把分数低于5分的成绩所有都加60分。



结果:

383 

解释:

1.刚开始,我们可以查到分数小于5分的总个数有383

2.select *, (grade+60) as newGrade from Score where Grade <5;这个sql是把所有的成绩小于5的都列出来,然后最后加一列分数加60分的结果。

3.update Score set grade = grade + 60 where grade < 5;是把分数小于5的所有成绩都加60

最后在检查分数小于5的个数为0,说明所有低于5分的分数都发生了改变


程序如下:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-23 16:58
  5. # @file :modifydata.py

  6. import MySQLdb
  7. def connect_mysql():
  8.     db_config = {
  9.         'host': '10.89.1.10',
  10.         'port': 3306,
  11.         'user': 'demo',
  12.         'passwd': '123qaz',
  13.         'db': 'python_test',
  14.         'charset': 'utf8'
  15.     }
  16.     cnx = MySQLdb.connect(**db_config)
  17.     return cnx


  18. if __name__ == '__main__':
  19.     cnx = connect_mysql()

  20.     sql = '''select *, (grade+60) as newGrade from Score where Grade <5;'''
  21.     update = '''update Score set grade = grade + 60 where grade < 5; '''
  22. try:
  23.     cus_start = cnx.cursor()
  24.     cus_start.execute(sql)
  25.     result1 = cus_start.fetchall()
  26.     print(len(result1))
  27.     cus_start.close()

  28.     cus_update = cnx.cursor()
  29.     cus_update.execute(update)
  30.     cus_update.close()

  31.     cus_end = cnx.cursor()
  32.     cus_end.execute(sql)
  33.     result2 = cus_end.fetchall()
  34.     print(len(result2))
  35.     cus_end.close()

  36.     cnx.commit()
  37. except Exception as e:
  38.     cnx.rollback()
  39.     print('error')
  40.     raise e
  41. finally:
  42.     cnx.close()

2.     索引

MySQL索引的概念

索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针。更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度。

索引类别

2.1.普通索引
2.2.  唯一索引
2.3.  主索引
2.4.  复合索引
2.5 . 外键索引

mysql主键和索引的区别:

1. 主键一定是唯一性索引,唯一性索引并不一定就是主键。

所谓主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引。因为主键可以唯一标识某一行记录,所以可以确保执行数据更新、删除的时候不会出现张冠李戴的错误。主键除了上述作用外,常常与外键构成参照完整性约束,防止出现数据不一致。数据库在设计时,主键起到了很重要的作用。

主键可以保证记录的唯一和主键域非空,数据库管理系统对于主键自动生成唯一索引,所以主键也是一个特殊的索引。

2. 一个表中可以有多个唯一性索引,但只能有一个主键。

3. 主键列不允许空值,而唯一性索引列允许空值。

4. 索引可以提高查询的速度。

创建Course的CouID的字段为主键   Score的SID字段为主键    Student的StdID字段为主键  Teacher的TID字段为主键
以下是没做索引时的查询:

做索引时的查询:

程序代码:

点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. # Author :Alvin.xie
  4. # @Time :2017-11-23 17:46
  5. # @file :demo.py


  6. import MySQLdb
  7. def connect_mysql():
  8.     db_config = {
  9.         'host': '10.89.1.10',
  10.         'port': 3306,
  11.         'user': 'demo',
  12.         'passwd': '123qaz',
  13.         'db': 'python_test',
  14.         'charset': 'utf8'
  15.     }
  16.     cnx = MySQLdb.connect(**db_config)
  17.     return cnx


  18. if __name__ == '__main__':
  19.     cnx = connect_mysql()
  20.     sql1 = '''alter table Score add index idx_StdID_CID(StdID, CID);'''
  21.     sql2 = '''alter table Score add index idx_CID_StdID(CID,StdID);'''
  22.     sql3 = '''explain select * from Score where StdID = 16213;'''
  23. try:
  24.     cus = cnx.cursor()
  25.     cus.execute(sql1)
  26.     cus.close()

  27.     cus = cnx.cursor()
  28.     cus.execute(sql2)
  29.     cus.close()

  30.     cus = cnx.cursor()
  31.     cus.execute(sql3)
  32.     result = cus.fetchall()
  33.     print(result)
  34.     cus.close()

  35.     cnx.commit()
  36. except Exception as e:
  37.     cnx.rollback()
  38.     print('error')
  39.     raise e
  40. finally:
  41.     cnx.close()




















阅读(20676) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~