数据库
作者:互联网
drop database db1;
create database db1 default charset utf8;
create table t2(id int null,name char(10) not null) engine=innodb default charset=utf8;事务 原子性操作 回滚
create table t3(id int auto_increment primary key,name char(10) not null) engine=innodb default charset=utf8;
primary key 约束 不能空,不能重复 加速查找
一个表里只能有一个自增列和主键
delete from t1;清空表 再插入继续自增
truncate table t1;再插入从1自增 效率快
drop table t1;删除表
数字
tinyint
int
bigint
小数用decimal 精确
num decimal(10,5)总位数和小数点后位数
字符串
char(10)未占满填充 速度快 限制均是255字符
varchar(10)节省空间
text多字符
sql优化:定长放在前,变长放在后
文件,图片,视频存硬盘,数据库存路径
时间类型
date
time
datetime
枚举
create table shirts(
name char(20),
size ENUM(‘small’,‘midium’,‘large’)); 选其一插入
insert into shirts(name,size)values(‘a’,‘large’) ;
集合
create table myset(
name char(20),
col SET(‘a’,‘b’,‘c’));选任意组合插入
insert into myset(name,col)values(‘a’,‘a,b’) ;
增
insert into tb11(name,age) values('alex',12);
insert into tb11(name,age) values('alex',12),('root',18);
insert into tb12(name,age) select name,age from tb11;
删
delete from tb12;
delete from tb12 where id !=2;
delete from tb12 where id =2 ;
delete from tb12 where id > 2 ;
delete from tb12 where id >=2;
delete from tb12 where id >=2 or name='alex';
改
update tb12 set name='alex' where id>12 and name='xx';
update tb12 set name='alex',age=19 where id>12 and name='xx';
查
select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
select id,name as cname from tb12 where id > 10 or name ='xxx';
select name,age,11 from tb12;
其他:
select * from tb12 where id != 1;
select * from tb12 where id in (1,5,12);
select * from tb12 where id not in (1,5,12);
select * from tb12 where id in (select id from tb11);
select * from tb12 where id between 5 and 12;
通配符:
select * from tb12 where name like "a%";以a开头
select * from tb12 where name like "a_";
分页:
select * from tb12 limit 10;
select * from tb12 limit 0,10;
select * from tb12 limit 10,10; 从10开始看10条
select * from tb12 limit 20,10;
select * from tb12 limit 10 offset 20;
从第20行开始读取,读取10行;
结合Python分页:
# page = input('请输入要查看的页码')
# page = int(page)
# (page-1) * 10
# select * from tb12 limit 0,10; 1
# select * from tb12 limit 10,10;2
排序:
select * from tb12 order by id desc; 大到小
select * from tb12 order by id asc; 小到大
select * from tb12 order by age desc,id desc; 先按age排序,如果重复,再按照id排
取后10条数据
select * from tb12 order by id desc limit 10;
外键 节省空间 约束(保证表与表之间数据一致性) CREATE TABLE t5 ( nid int(11) NOT NULL AUTO_INCREMENT, pid int(11) not NULL, num int(11), primary key(nid,pid) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; create table t6( id int auto_increment primary key, name char(10), id1 int, id2 int,CONSTRAINT fk_t5_t6 foreign key (id1,id2) REFERENCES t5(nid,pid) )engine=innodb default charset=utf8; desc t5; show create table t5;看表是怎么创建的(sql语句) show create table t5 \G;竖着看 alter table t5 set AUTO_INCREMENT=1;设置自增起始值 MySQL 步长,基于会话 sqlserver 基于表 show session variables like 'auto_inc%';查看步长 set session auto_increment_increment=2;设置步长 show global variables like 'auto_inc%';查看全局步长 set global auto_increment_increment=2;不推荐 唯一: 约束不能重复(可以为空) PS: 主键不能重复(不能为空) 加速查找 create table t1( id int ...., num int, xx int, unique 唯一索引名称 (列名,列名), 联合唯一,两列间不能重复 constraint .... ) 一对一 create table userinfo1( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table admin( id int not null auto_increment primary key, username varchar(64) not null, password VARCHAR(64) not null, user_id int not null, unique uq_u1 (user_id), CONSTRAINT fk_admin_u1 FOREIGN key (user_id) REFERENCES userinfo1(id) )engine=innodb default charset=utf8; 多对多 示例1: 用户表 相亲表 示例2: 用户表 主机表 用户主机关系表 create table userinfo2( id int auto_increment primary key, name char(10), gender char(10), email varchar(64) )engine=innodb default charset=utf8; create table host( id int auto_increment primary key, hostname char(64) )engine=innodb default charset=utf8; create table user2host( id int auto_increment primary key, userid int not null, hostid int not null, unique uq_user_host (userid,hostid), CONSTRAINT fk_u2h_user FOREIGN key (userid) REFERENCES userinfo2(id), CONSTRAINT fk_u2h_host FOREIGN key (hostid) REFERENCES host(id) )engine=innodb default charset=utf8;
分组:
select count(id),max(id),part_id from userinfo5 group by part_id;通过part_id进行分组,返回部门id一样的人数和部门id一样的人中id最大的那一个
count
max
min
sum
avg
**** 如果对于聚合函数结果进行二次筛选时?必须使用having ****
select count(id) as count,part_id from userinfo5 group by part_id having count(id) > 1;
连表操作:
select * from userinfo5,department5 where userinfo5.part_id = department5.id;
select * from userinfo5 left join department5 on userinfo5.part_id = department5.id; 推荐使用
# userinfo5左边全部显示
select * from department5 left join userinfo5 on userinfo5.part_id = department5.id;
select * from userinfo5 right join department5 on userinfo5.part_id = department5.id;
# department5右边全部显示
#备份数据表结构+数据:mysqldump -u root db1 > db1.sql -p;转储数据库,备份,防黑客
#备份数据表结构:mysqldump -u root -d db1 > db1.sql -p;
import pymysql cid=input('>>>') user=input('>>>') conn=pymysql.connect(host='localhost',user='root',password='',database='day61') cursor=conn.cursor() #游标,拿数据 sql="select * from app01_userinfo where id='%s'and name='%s'"%(cid,user) #不要自己写,会有sql注入 1" o r 1=1 -- cursor.execute(sql) result=cursor.fetchone() #只显示一条 print(result) cursor.close() conn.close() if result: print('登陆成功') else: print('登陆失败')
import pymysql cid=input('>>>') user=input('>>>') conn=pymysql.connect(host='localhost',user='root',password='',database='day61') cursor=conn.cursor() #游标,拿数据 sql="select * from app01_userinfo where id=%(c)s and name=%(u)s" #cursor.execute(sql,[cid,user]) cursor.execute(sql,{'c':cid,'u':user}) result=cursor.fetchone() #只显示一条 print(result) cursor.close() conn.close() if result: print('登陆成功') else: print('登陆失败')
正确的写法,防注入
import pymysql cid=input('>>>') user=input('>>>') conn=pymysql.connect(host='localhost',user='root',password='',database='day61') cursor=conn.cursor() #游标,拿数据 sql="insert into app01_userinfo (id,name) values (%s,%s)" cursor.execute(sql,(cid,user)) conn.commit() cursor.close() conn.close() #增删改都要commit
r=cursor.executemany(sql,[('',''),('',''),('','')]) #提交多个
r是一个返回值,受影响的行数
import pymysql conn=pymysql.connect(host='localhost',user='root',password='',database='day61') cursor=conn.cursor(cursor=pymysql.cursors.DictCursor) #游标,拿数据 sql="select * from app01_userinfo limit 3" cursor.execute(sql) ''' result=cursor.fetchone() print(result) result=cursor.fetchone() print(result) result=cursor.fetchone() print(result) #cursor.scroll(1,mode='relative') cursor.scroll(2,mode='absolute') result=cursor.fetchone() print(result) result=cursor.fetchone() print(result) ''' result=cursor.fetchall() print(result) cursor.close() conn.close()
标签:10,name,数据库,cursor,tb12,id,select 来源: https://www.cnblogs.com/qwer-123/p/12001732.html