一、删除数据
数据如下:
[root@localhost ~]# mysql -uroot -ppzk123 -e "use test; select * from t1;"
+----+------+-----+| id | name | age |+----+------+-----+| 1 | Tom | 17 || 2 | John | 25 || 3 | Jeny | 25 |+----+------+-----+使用 Python 删除第二条记录:
1 #!/usr/bin/env python 2 import MySQLdb 3 4 def connect_mysql(): 5 db_config = { 6 'host': '127.0.0.1', 7 'port': 3306, 8 'user': 'root', 9 'passwd': 'pzk123',10 'db': 'test'11 }12 c = MySQLdb.connect(**db_config)13 return c14 15 if __name__ == '__main__':16 c = connect_mysql()17 cus = c.cursor()18 sql = 'delete from t1 where id=2;'19 try:20 cus.execute(sql)21 c.commit()22 except Exception as e:23 c.rollback()24 raise e25 finally:26 c.close()
结果如下:
[root@localhost ~]# mysql -uroot -ppzk123 -e "use test; select * from t1;"
+----+------+-----+| id | name | age |+----+------+-----+| 1 | Tom | 17 || 3 | Jeny | 25 |+----+------+-----+二、更改数据
数据如下:
[root@localhost ~]# mysql -uroot -ppzk123 -e "use test; select * from t1;"
+----+------+-----+| id | name | age |+----+------+-----+| 1 | Tom | 17 || 2 | John | 25 || 3 | Jeny | 25 |+----+------+-----+
使用 Python 修改数据(把 John 的年龄修改为20):
1 #!/usr/bin/env python 2 import MySQLdb 3 4 def connect_mysql(): 5 db_config = { 6 'host': '127.0.0.1', 7 'port': 3306, 8 'user': 'root', 9 'passwd': 'pzk123',10 'db': 'test'11 }12 c = MySQLdb.connect(**db_config)13 return c14 15 if __name__ == '__main__':16 c = connect_mysql()17 cus = c.cursor()18 sql = 'update t1 set age=20 where id=2;'19 try:20 cus.execute(sql)21 c.commit()22 except Exception as e:23 c.rollback()24 raise e25 finally:26 c.close()
修改结果:
[root@localhost ~]# mysql -uroot -ppzk123 -e "use test; select * from t1;"
+----+------+-----+| id | name | age |+----+------+-----+| 1 | Tom | 17 || 2 | John | 20 || 3 | Jeny | 25 |+----+------+-----+三、索引
1、索引简介
索引就像书的目录一样,也可以理解是一个标签,创建索引的目录是为了加快我们对数据的查询,举个例子,数据库中有两万条记录,现在要查询 id 为 10086 的记录,如果没有索引,必须遍历整个表,直到 id 等于 10086 这一行被找到为止,如果在 id 字段上创建索引,MySQL 不需要任何扫描,直接在索引里面找 10086 就可以得知这一行的位置,可见,创建索引可以提高数据库的查询速度
2、索引的分类
(1) 普通索引:允许在定义索引的列中插入重复值和空值
(2) 唯一索引:索引列的值必须唯一,但允许有空值(3) 单列索引:即一个索引只包含单个列,一个表可以有多个单列索引(4) 组合索引:即在表的多个字段组合上创建的索引,只有在查询条件中使用了这些字段的左边字段时,索引才会被使用(5) 全文索引:在定义索引的列上支持值的全文查找,允许在这些索引列中插入重复值和空值(6) 空间索引:是对空间数据类型的字段建立的索引,MySQL 有四中空间数据类型,分别是 GEOMETRY 、POINT 、LINESTRING 、POLYGON3、索引的设计原则
(1) 索引不宜过多,否则不仅占用磁盘空间,而且会影响插入、删除、更新语句的性能
(2) 避免对经常更新的表进行过多的索引,并且索引中的列尽可能少(3) 数据量小的表最好不要使用索引,因为查询花费的时间可能比遍历索引的时间还要短,索引不会产生太大效果(4) 在条件表达式中经常用到的不同值较多的列上建立索引,在不同值很少的列上不要建立索引(5) 当唯一性是某种数据本身的特征时,指定唯一索引以确保数据完整性,提高查询速度(6) 在频繁进行排序或分组的列上建立索引,如果待排序的列有多个,可以在这些列上建立组合索引4、在创建表的同时创建索引
1. 创建普通索引
mysql> create table book
-> ( -> bookid int not null, -> bookname varchar(255) not null, -> authors varchar(255) not null, -> info varchar(255) null, -> comment varchar(255) null, -> index(bookid) -> );mysql> explain select * from book where bookid=1\G //explain可以用来查看是否使用了索引
*************************** 1. row *************************** id: 1 select_type: SIMPLE //表示查询类型,SIMPLE表示只是简单的查询,不使用UNION或子查询 table: book //对哪个表进行查询 type: system //指明该表与其他表之间的关联关系possible_keys: bookid //查询时可能选用的索引 key: NULL //查询时实际选用的索引 key_len: NULL //索引长度,值越小表示查询越快 ref: NULL //指明了关联关系中另一个表里的数据列的名字 rows: 1 //查询时预计会从这个数据表里读出的数据行的个数 Extra: //提供了与关联操作有关的信息2. 创建唯一索引
mysql> create table book
-> ( -> bookid int not null, -> bookname varchar(255) not null, -> authors varchar(255) not null, -> info varchar(255) null, -> comment varchar(255) null, -> unique index unique_index(bookid) //索引列的值必须唯一,unique_index是索引名 -> );3. 创建组合索引
mysql> create table book
-> ( -> bookid int not null, -> bookname varchar(255) not null, -> authors varchar(255) not null, -> info varchar(255) null, -> comment varchar(255) null, -> index multi_index(bookid, bookname) //multi_index是索引名 -> );4. 创建全文索引
mysql> create table book
-> ( -> bookid int not null, -> bookname varchar(255) not null, -> authors varchar(255) not null, -> info varchar(255) null, -> comment varchar(255) null, -> fulltext index fulltext_index(authors) //fulltext_index是索引名 -> );5、在已经存在的表上创建索引
alter table book add index bookid_index(bookid); //为 bookid 字段创建普通索引,其中 bookid_index 是索引名alter table book add index bookid_index(bookid,bookname); //为 bookid 字段创建组合索引,其中 bookid_index 是索引名alter table book add unique index bookid_index(bookid); //为 bookid 字段创建唯一索引,其中 bookid_index 是索引名alter table book add fulltext index info_index(info); //为 info 字段创建全文索引,其中 info_index 是索引名
6、删除索引
alter table table_name drop index index_name;