卸载系统预装的旧版本MySQL:
python@ubuntu:~$ sudo apt-get remove mysql-server
更新源服务器软件列表:
python@ubuntu:~$ sudo apt-get update
重新启动终端,安装MySQL:
python@ubuntu:~$ sudo apt-get install mysql-server Ubuntu开启、关闭、重启MySQL服务:
python@ubuntu:~$ sudo service mysql start
python@ubuntu:~$ sudo service mysql stop
python@ubuntu:~$ sudo service mysql restart
客户端:
在终端运行如下命令安装 mysql客户端
python@ubuntu:~$ sudo apt-get install mysql-client
详细连接的命令可以查看帮助文档
python@ubuntu:~$ mysql --help
常用连接命令如下,输入后回车
-u 指定登录的用户名,-p 指定登录密码,注意没有空格
python@ubuntu:~$ mysql -uroot -p
数据库:
create database python01 charset=utf8;--创建库
show databases;--查看所有库
show create database python01;--查看创建的库
select database();--查看当前的库
use python01;--用这个库
drop database python01--删除数据库
数据表:
show tables;--查看所有表
select * from students;--查看数据
show create table classes;--显示创建的表,一个库中多个表时为show create table python01.classes;
desc students;--查看当前表
--创建数据库及数据表
create database jing_dong charset=utf8;
use jing_dong;
create table goods(
id int unsigned primary key auto_increment not null,
name varchar(150) not null,
cate_name varchar(40) not null,
brand_name varchar(40) not null,
price decimal(10,3) not null default 0,
is_show bit not null default 1,
is_saleoff bit not null default 0
);
--插入数据
insert into goods values
(0,"hs8900 14.0英寸笔记本电脑","笔记本","华硕","4500",default,default),
(0,"ls4560 16.0英寸游戏本","游戏本","雷神","9500",default,default),
(0,"lx7300 13.0英寸笔记本电脑","笔记本","联想","3000",default,default),
(0,"apple1 11.0英寸超级本","超级本","苹果","10500",default,default),
(0,"sn1260 13.7英寸超级本","超级本","索尼","8800",default,default),
(0,"ipaid mini 7.9英寸平板电脑","平板电脑","苹果","1900",default,default),
(0,"ipaid pro 11.0英寸平板电脑","平板电脑","苹果","3200",default,default),
(0,"ipaid mini 配备retina显示屏","平板电脑","苹果","2600",default,default),
(0,"dr5600 25.0英寸一体电脑","台式机","戴尔","6500",default,default),
(0,"apple4 29.0英寸一体电脑","台式机","苹果","10890",default,default),
(0,"ibm2344 30.0英寸一体电脑","台式机","ibm","5600",default,default),
(0,"hs8hufvuh 服务器","服务器/工作站","惠普","4500",default,default),
(0,"hfjkbf 工作站","服务器/工作站","华为","9500",default,default),
(0,"hfjfksb 专业级台式电脑","服务器/工作站","苹果","28000",default,default),
(0,"头戴显示设备","笔记本配件","索尼","6900",default,default),
(0,"商务双肩包","笔记本配件","小米","90",default,default),
(0,"ibm4 架构式服务器","服务器/工作站","ibm","6800",default,default);
--查询类型cata_name为超极本的商品名称,价格
select name,price from goods where cate_name="超极本";
--显示商品的种类
select distinct cate_name from goods;
select cate_name from goods group by cate_name;
--求所有电脑产品的平均价格,并保留两位
select round(avg(price),2)as 平均价格 from goods;
--显示每种商品的平均价格
select cate_name,avg(price) from goods group by cate_name;
--查询每种类型的商品中最贵,最便宜,平均价,数量
select cate_name,max(price),min(price),avg(price),count(*) from goods group by cate_name;
select cate_name,count(*) from goods group by cate_name;
--查询所有价格大于平均价的商品,并且按照价格将序排序
select id,name,price from goods
where price>(select round(avg(price),2)as 平均价格 from goods)
order by price desc;
--查询每种类型中最贵的电脑信息
select * from
(select cate_name,max(price)as cate_price from goods group by cate_name)as g_new
left join goods as g on g_new.cate_name=g.cate_name and g_new.cate_price=g.price;
--创建商品分类表
create table if not exists goods_cates(
id int unsigned primary key auto_increment not null,
name varchar(40) not null
);
--查看goods中的种类
select cate_name from goods group by cate_name;
--将分组结果写入创建的分类表中
insert into goods_cates (name) select cate_name from goods group by cate_name;
--同步表数据
--通过goods_cates表来更新goods表
update (goods as g inner join goods_cates as c on g.cate_name=c.name) set g.cate_name=c.id;
--创建商品品牌表
create table if not exists goods_barnds(
id int unsigned primary key auto_increment not null,
name varchar(20)
);
insert into goods_barnds (name) select brand_name from goods group by brand_name;
update (goods as g inner join goods_barnds as b on g.brand_name=b.name) set g.brand_name=b.id;
--修改表结构
--goods和新建的分表中关联的数据类型不同
desc goods;
--通过alter table 语句修改表结构
alter table goods
change cate_name cate_id int unsigned not null,
change brand_name brand_id int unsigned not null;
--外键
--分别在goods_cates和goods_brands中插入记录
insert into goods_cates (name) values("路由器"),("交换机"),("网卡");
insert into goods_barnds (name) values("海尔"),("清华同方"),("神州");
--在goods中混入任意记录
insert into goods (name,cate_id,brand_id,price) values("hufjk黑白激光打印机",12,4,"1344");
--通过内关联查询所有商品的详细信息
--如何防止无效信息的插入,就是可以在插入前判断品牌类型及类型是否存在呢?可以使用外键解决
--外键约束,对数据有效性进行判断
--关键字 foreign key,只有innodb数据引擎支持外键约束
--对于已经存在的数据表如何更新外键约束
--给goods_cates及goods_brands添加外键
alter table goods add foreign key (cate_id) references goods_cates(id);
alter table goods add foreign key (brand_id) references goods_barnds(id);
--取消外键约束
--获取外键名称
show create table goods;
--根据外键名称进行删除
alter table goods drop foreign key 外键名称;
from pymysql import connect
class JD(object):
def __init__(self):
# 创建Connection连接
self.conn=connect(host='localhost',port=3306,user='root',password='mysql',database='jing_dong',charset='utf8')
# 获取Cursor对象
self.cursor=self.conn.cursor()
def __delete__(self):
# 关闭对象
self.cursor.close()
self.conn.close()
def execute_sql(self,sql):
self.cursor.execute(sql)
for temp in self.cursor.fetchall():
print(temp)
def show_all_goods(self):
sql='select * from goods'
self.execute_sql(sql)
def show_all_cates(self):
sql='select name from goods_cates'
self.execute_sql(sql)
def show_all_barnds(self):
sql='select name from goods_barnds'
self.execute_sql(sql)
# 增删改
# count=cursor.execute("insert into goods_barnds(name) values('外星人')")
# count=cursor.execute("delete from goods_barnds where id=n")
# count=cursor.execute("update goods_cates set name='机械硬盘' where name='硬盘'")
def add_barnds(self):
item_name=input("请输入要添加的名称:")
sql="""insert into goods_barnds(name) values(%s)""" %item_name
self.cursor.execute(sql)
self.conn.commit()
# 防止sql注入
def get_info_by_name(self):
find_name=input("请输入要查询的名称:")
sql="select * from goods where name=%s"
self.cursor.execute(sql,[find_name])
print(self.cursor.fetchall())
@staticmethod
def print_menue(self):
while True:
print("----京东商城----")
print("1:查询所有商品")
print("2:查询所有商品分类")
print("3:查询所有商品品牌")
print("4:插入商品品牌")
print("5:根据商品名称查看商品信息")
return input("请输入正确的序号:")
def run(self):
if num ==1:
self.show_all_goods()
elif num==2:
self.show_all_cates()
elif num==3:
self.show_all_barnds()
elif num==4:
self.add_barnds()
elif num==5:
self.get_info_by_name()
else:
print("输入有误!")
if __name__=="__main__":
# 创建一个京东对象
jing_dong=JD()
# 调用这个类的方法,让其运行
jng_dong.run()