标签:between 生效 connect NPU engine res xpl enc 防止
# 增 create database review; # 查 show databases; show create database review; # 查看数据库的创建,默认为 CHARACTER SET latin1 编码 # 改 # 没有专门的改操作,删了重建 alter database review charset utf8; # 将数据库的字符编码改为utf8编码 # 删 drop database review;
use review; # 增(创建) create table test ( id int auto_increment primary key, name varchar(32) not null default ‘‘ )engine=Innodb charset=utf8; create table test_join ( id int auto_increment primary key, course varchar(32) not null default ‘‘, name_join int not null default 0, constraint fk_name_id foreign key (name_join) references test(id) )engine=Innodb charset=utf8; # 删 drop table test; alter table test drop name; # 删除列/字段名 # 改 alter table test rename new_test; # 更改表名 alter table test change/modify name title varchar(32) not null default ‘‘; # 修改字段名 alter table test add title varchar(32) not null default ‘‘; # 增加新列 # 查 show tables;
# 增 insert into test (name) values (‘wangyong‘), (‘liguo‘), (‘jiyuzhi‘); # 删 delete from test where id=3; delete from test; # 删除之后,增加数据id会直接增加 truncate test; # 删除之后,增加数据id会清空 # 改 update test set name=‘title‘; # 将name属性的值全部改为title update test set name=‘title‘ where id=2; # 将id=2d的name属性的值改为title # 查 select * from test; select name from test;
# where select * from test where id>1 and id<=3; # between and 闭区间 select * from test where id between 1 and 3; # in / not in select * from test where id in (1,2,3); # 通配符 select * from test where name like ‘%wang%‘; select * from test where name like ‘%wang_‘; # limit select * from 表名 limit 索引偏移量, 取出多少条数据; select * from test where id limit 2,3; # 从第2号位(第3个数)开始查询3个 # group by 分组 # select age, 聚合函数(count(num)/sum(num)/max(num)/min(num)/avg(num)) from 表名 group by 列名; select name, count(name) from test group by name; select name, count(name) from test group by name having name=‘wangyong‘; # 二次筛选 # order by 排序 select * from test order by name desc; # desc:降序 select * from test order by name asc; # asc:升序 # 连表(left join、 right join、 inner join) # left join 左边的表全部显示, 右边没有用到不显示 select test.name, test_join.course from test left join test_join on test.id=test_join.name_join;
自己手动去判断转义用户输入的数据
不要拼接SQL语句, 使用PyMySQL中的execute方法, 防止SQL的注入
import pymysql gender = input(‘gender>>>:‘) sname = input("sname>>>:") # 链接服务器 conn = pymysql.connect(host = ‘localhost‘, user = ‘root‘, password = ‘‘, database = ‘practiceday43‘, charset = ‘utf8‘ ) cursor = conn.cursor(cursor = pymysql.cursors.DictCursor) # 写SQL语句,不要去拼接SQL语句 sql = "select * from student where gender = %s and sname = %s" # 传值的时候,以元组形式给SQL传参 cursor.execute(sql,(gender, sname)) res = cursor.fetchone() print(res) if res: print(‘获得值成功‘) else: print(‘获得值失败‘) cursor.close() conn.close()
原子性: 一组操作, 要么全部成功, 要么全部失败 一致性: 操作之前和操作之后, 总的金额是一致的 隔离性: 本次事物的操作对其他事务的操作是没有任何影响的 持久性: 当我们commit/rollback之后, 影响就已经生效了, 补偿性事务来解决
开启: start transaction 一组SQL语句的操作 完成(commit/rollback)
create table user( id int auto_increment primary key, name varchar(32) not null default ‘‘, money int not null default 1000 )engine=innodb charset=utf8; insert into user (name,money) values (‘wangyong‘,1000),(‘liguo‘,1000); # 正常操作 start transaction; update user set money=1100 where name=‘wangyong‘; updata user set money=900 where name=‘liguo‘; # 出现异常 rollback; # 最终结果, 数据未发生变化 mysql> select * from user; +----+----------+-------+ | id | name | money | +----+----------+-------+ | 1 | wangyong | 1000 | | 2 | liguo | 1000 | +----+----------+-------+
# 第一种: create table test( id int auto_increment primary key, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; # 第二种: create table test( id int not null default 0, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; alter table test change id id int auto_increment primary key;
# 第一种 create table test( id int auto_increment primary key, name varchar(32) not null default ‘‘, unique ix_name (name) )engine=innodb charset=utf8; # 第二种 create unique index 索引名称 on 表名(name); create table test( id int auto_increment primary key, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; create unique index ix_name on test (name); # 创建联合唯一索引 create unique index 索引名称 on 表名 (name,age); create table test( id int auto_increment primary key, age int not null default 0, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; create unique index ix_name_age on test (age, name);
# 第一种 create table test( id int auto_increment primary key, name varchar(32) not null default ‘‘, index ix_name (name) )engine=innodb charset=utf8; # 第二种 create table test( id int auto_increment primary key, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; create index ix_name on test (name); # 联合索引 create table test( id int auto_increment primary key, age int not null default 0, name varchar(32) not null default ‘‘ )engine=innodb charset=utf8; create index ix_name_age on test (age, name);
explain selext * from test; +----+-------------+-------+-------+---------------+---------+---------+------+------+------------- | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra +----+-------------+-------+-------+---------------+---------+---------+------+------+------------- | 1 | SIMPLE | test | index | NULL | ix_name | 98 | NULL | 1 | Using index +----+-------------+-------+-------+---------------+---------+---------+------+------+-------------
- 不建议使用 like 进行搜索 - 组合索引最左前缀 如果组合索引为:(name,email) where name and email -- 使用索引 where name -- 使用索引 where email -- 不使用索引
# drop 索引名称 on 表名 drop index ix_name_age on test; mysql> explain select * from test; +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-------+------+---------------+------+---------+------+------+-------+ | 1 | SIMPLE | test | ALL | NULL | NULL | NULL | NULL | 1 | NULL | +----+-------------+-------+------+---------------+------+---------+------+------+-------+
Innodb(支持高并发) 1.(默认版本包含5.5) 2.支持事务 3.不支持全文索引 4.索引和数据都是在同一个文件中, .ibd 表的结构实在.frm文件中 MyIsam(支持高并发不是特别理想) 1.(默认版本5.5以下 5.3) 2.不支持事务 3.支持全文索引 4..frm: 表结构 .MYD: 表数据 .MYI: 表索引 全文索引:(中文很好) sphinx
https://www.cnblogs.com/wangyong123/p/11040895.html
https://www.cnblogs.com/wangyong123/p/11045633.html
标签:between 生效 connect NPU engine res xpl enc 防止
原文地址:https://www.cnblogs.com/wangyong123/p/11051866.html