数据库(database,简称DB): 是按照数据结构来组织,存储和管理数据的仓库
数据库管理系统是指数据库中对数据管理的软件系统
mysql 的启动 net start [mysql]-这里写你服务器的名字
mysql的停止 net start [mysql]-这里写你服务器的名字
mysql 的登陆 -h127.0.0.1 -uroot -p
mysql 的退出 exit, quit,\q 这三个命令
修改命令 mysqladmin -uroot -p[这里写密码] password
进入mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';
showdatabases; 显示所有数据库
使用数据库的命令 use [写数据库的名称]
select 命令
1, 显示当前的服务器版本 select version();
2、显示当前连接的数据库 select database();
3、 显示当前日期时间 select now();
4、显示当前用户 select user();
表级操作。
create table 表名(nid int(4),name varchar(20));
create table 表名(列名 类型 是否为空,
列名 类型 是否为空,
)engine=innodb default charset = utf8;
引擎 为 innodb 默认 编码 utf8
自增 auto_increment
自增列(数字,必须是索引 - 主键)
主键 (称为索引)
一张表只能有一个主键
唯一不能重复,不能为空,
一般情况下,自增列设为主键 primary key
唯一和主键的区别,,唯一可以为null 主键不可以为null, 唯一 一张表可以有多个
好处 查找速度非常快,他们都可以作为索引
create table XXX( nid ... primary key...)
craete table student(
name varchar(10) not null auto_increment ,
num int not null,
age int,
gender int,
primary key(name,num))engine=innodb default charser = utf8;
primary key (name,num) 这是联合起来作个约束条件
主键:
不能为空 不能重复 一张表只有一个主键(可以多列组成主键)
一般用法 nid int not null auto_increment primary key
外键:
添加列 alter table 表名 add 列名 类型
删除列 alter table 表名 drop column 列名
修改列:
alter table 表名 modfy column 列名 类型;-- 类型
alter table 表名 change 原列名 新列名类型;----列名,类型
添加主键:
alter table 表名 add primary key(列名);
删除主键:
alter table 表名 drop primary key;
alter table 表名 modify 列名 int,drop primary key;
添加外键 alter table 从表 add constraint 外键名称(形如:fk_从表_主表) foreign key 从表(外键字段)references 表名(这里写那个字段)
alter table user add constraint fk_u_p foreign key user(department) references part(nid);
删除外键 alter table 表名 drop foreign 外键名称;
修改默认值 alter table testalter_tb1 alter i set default 1000;
alter table user alter column age set default 19;
删除默认值 alter table testalter_tb1 alter i drop default;
直接表删除 drop table 表名;
清空表里面的内容 delete from 表名; 自增记住上次的位置
清空表内容 truncate table 表名; 速度快 自增回到原点
数据行级别
select * from tb1;
增 、
insert into tb1(name,age) values(1,2),(2,4),(...);
insert into 表(列名,列名。。。) select 列名,列名... from 表;
insert into XXX(name,age) select caption,gender from XXX1; 在这张表中插入另一张表里面的caption,gender 字段;
删除
delete from xxx where nid=3;
delete from xxx where nid=3 and name='xxx'; 删除这个表中的指定数据
delete from xxx where nid=3 or name='xxx'; 删除这个表中的指定数据
delete from xxx where nid>3; 删除这个表中的指定数据
改
update tb set age = 1;
update tb set age = 1 wher nid=10;
update tb set age = 2,name='xxx' where nid > 12 ;
update tb set age = 2,name='xxx' where nid > 12 and XXX=XXX..... ;
查
select * from tb;
select * form where nid >=12 and name='xxx';
select name,age form where nid >=12 and name='xxx'; where 是条件
其他
a 条件
select * from 表 where id> 1 and name !='XXX' and number = 12;
select * from 表 where id between 5 and 16;
select * from 表 where id in (11,,12,13...);
select * from 表 where id in (select nid from 表);
b 条件 通配符
select * from 表 where name like 'al%' 以 al 开头的所有(多字符)
select * from 表 where name like 'al_' 以 al 开头的所有(一个字符)
限制
select * from 表 limit 4: 表示取前4 条
select * from 表 limit 1, 4: 表示从第一行取 取4条数据
select * from 表 limit 6, 4: 表示从第6行取 取4条数据
select * from 表 limit 4 offset 6: 表示从第6行取 取4条数据 ;
排序
select * from tb order by nid asc; 正序
select * from tb order by nid desc; 倒序
select * from order by 列1 desc ,列2 asc 先根据倒序排序,如果有二个一样的在根据列2 正序排序
分组
select num from 表 group by num
select num,nid from 表 group by num,nid
select num nid from 表 where nid > 10 group by num,nid order nid desc
select num,nid count(*),sum(score),max(score),min(score) from 表 group by num,nid
select num from 表 group by num having max(id)>10;
特别注意 group by 必须在where 之后 ,order 之前
连表操作
pychram 获取数据据自增ID ,增,删,改,查
import pymysql
创建联接
conn = pymysql.connect(host='127.0.0.1',port=3306,user='root',passwd='666',db='mysql',charset='utf8')
创建游标
# cur = conn.cursor()
cur = conn.cursor(cursor=pymysql.cursor.DictCursor) 里面有这个参数时下面查出来的 结果会以字典的形式出现
cur.execute(‘insert into 表(字段)values(值,。。。%s, %s),(值1,值2))
cur.executemany(‘insert into 表(字段)values(值,。。。%s, %s),[(值1,值2),(值2,值3]) 插入多条
cur.execute('update 表 set 字段=''xxx" where 条件') 更新
cur.execute('delete from 表 where 条件=%s', (要删除的,)
conn.commit() 提交
new_id = cursor.lastrowid() # 获取最后一行新增的自增ID
查
cur.execute('select * from 表 where ......')
取用 result = cur.fetchone() 拿一个
result = cur。fetchall() 拿所有
result = cur. fetchmany(3) 表示拿3条
cur.close()
conn.close()