MySQL之petstore

  • 创建petstore 数据库与表-
  • 建立数据完整性约束---
  • 数据插入-1-用户表-
  • 数据插入--2--商品分类表-
  • 数据插入----4----订单表-
  • 数据修改与删除-1-数据修改-
  • 数据查询
    • 每日一言:
  • 上一章链接:[MySQL实例](https://blog.csdn.net/m0_66318554/article/details/124949034)
    • 持续更新中...

话不多说,一切都在mysql代码中,直接在这里插入代码片上代码:

创建petstore 数据库与表-

-----1--------

create database petstore;

----------2---------

use petstore;
create table account (userid char(6) not null,fullname varchar(10) not null,password varchar(20) not null,sex char(2) not null,address varchar(40) null,email varchar(20) null,phone varchar(11) not null,primary key (userid)
);
create table category(catid char(10) not null,catname varchar(20) null,primary key (catid)
);
create table product(productid char(10) not null,catid char(10) not null,name varchar(30) null,descn text null,listprice decimal(10,2) null,unitcost decimal(10,2) null,qty int(11) not null,primary key (productid)
);
create table orders (orderid int(11) not null auto_increment,userid char(6) not null,orderdate datetime not null,totalprice decimal(10,2) default null,status tinyint(1) default null,primary key (orderid)
);
create table lineitem (orderid int(11) not null,itemid char(10) not null,quantity int(11) not null,unitprice decimal(10,2) not null,primary key (orderid,itemid)
);

建立数据完整性约束—

-------3------

alter table productadd foreign key (catid)references category(catid)on delete restrict;
alter table ordersadd foreign key (userid)references account(userid)on delete restricton update restrict;
alter table lineitemadd foreign key (itemid)references product(productid)on delete cascadeon update cascade;
alter table lineitemadd foreign key (orderid)references orders(orderid)on delete cascade;
alter table account
add check(sex in ('男','女'));

数据插入-1-用户表-

--------4---------

insert into accountvalues('u0001','刘晓和','123456','男','广东深圳市','liuxh@163.com','17885091066');
insert into accountvalues('u0002','张嘉庆','147369','男','广东深圳市','zhangjq@163.com','17885061235');
insert into accountvalues('u0003','李天威','131452','男','贵州毕节市','lixw@163.com','17885091065');
insert into accountvalues('u0004','陈浪','584145','女','广东珠海市','clang@163.com','16658461684');
insert into accountvalues('u0005','吴晶','852416','女','广东广州市','wjing@163.com','15599851352');

数据插入–2–商品分类表-

--------4---------

insert into category values('01','鸟类',' ');
insert into category values('02','猫',' ');
insert into category values('03','狗',' ');
insert into category values('04','鱼',' ');
insert into category values('05','爬行类',' ');

ERROR 1136 (21S01): Column count doesn’t match value count at row 1

出现这种报错是字段名与值不匹配,少写了值。

可能 出现的情况为:

1.插入字段的时候字段名或者值少写了;

2.进行蠕虫复制的时候,想把部分字段名复制到另一张表当中,字段名或者值少写了。

缺少的值添加上问题就解决了!

在Mysql8.0版本中可以插入空值,在后面写一个null就可以,在5.5版本中,不能插入空值

–正确–

insert into category values('01','鸟类');
insert into category values('02','猫');
insert into category values('03','狗');
insert into category values('04','鱼');
insert into category values('05','爬行类');

数据插入—3—商品表-
--------4---------

insert into productvalues('AV-CB-01','05','亚马逊鹦鹉','75岁以上高龄的好伙伴',50.00,60.00,100);
insert into productvalues('AV-SB-02','05','燕雀','非常好的减压宠物',45.00,50.00,98);
insert into productvalues('FI-FW-01','05','景丽','来自日本的淡水鱼',38.00,45.50,300);
insert into productvalues('FI-FW-02','01','金鱼','来自中国的淡水鱼',6.80,6.60,100);
insert into productvalues('FI-SW-01','01','天使鱼','来自澳大利亚的海水鱼',10.00,21.05,100);
insert into productvalues('FI-SW-02','01','海鲨','来自澳大利亚的海水鱼',20.05,32.00,200);

注意:可再加:–


数据插入----4----订单表-

--------4---------

insert into orders values(20191010,'u0001','2022-01-20 20:10:08',52.00,0);
insert into orders values(20191011,'u0002','2022-01-21 10:30:20',66.50,0);
insert into orders values(20191012,'u0003','2022-01-22 12:20:06',88.88,0);
insert into orders values(20191013,'u0004','2022-01-23 16:16:20',99.00,1);
insert into orders values(20191014,'u0005','2022-01-26 20:20:20',100.00,0);

数据修改与删除-1-数据修改-

-----5-------

update product
set unitcost=(qty*unitcost+50*15)/(qty+50)
where name='天使鱼';
update product
set listprice=unitcost*1.2,qty=qty+50
where name='天使鱼';

–合并–

update product
set unitcost=(qty*unitcost+50*15)/(qty+50)
listprice = unitcost*1.2,qty=qty+50
where name='天使鱼';

–2–修改订单的状态–

update ordersset status=1where orderid='20191011';

–修改商品表的库存-*-

update lineitem,productset product.qty=product.qty-lineitem.quantitywhere lineitem.itemid=product.productidand lineitem.orderid='20191011';

–合并–

update orders,lineitem.productset orders.status=1,product.qty=product.qty-lineitem.quantitywhere orders.orderid=lineitem.orderidand lineitem.itemid=product.productidand orders.orderid='20191011';

ERROR 1049 (42000): Unknown database ‘lineitem’
--暂未解决--

–删除其所有订购信息,包括订单表和订单明细表的信息,涉及多表删除–

-*-delete orders,lineitemfrom orders,lineitemwhere orders.orderid=lineitem.idand orders.userid='u0004';-*-

—删除用户表中的用户记录—

-*-delete from account where userid='u0004';-*-

-----一次删除所有数据------

-*-delete account,orders,lineitemfrom account,orders,lineitemwhere account.userid=orders.useridand orders.orderid=lineitem.orderidand account.userid='u0004';-*-

数据查询

------6------------:
-----列查询----

select fullname as 姓名,address as 地址,phone as 电话 from account;
select distinct itemid,unitprice from lineitem;
select orderid,itemid,quantity*unitprice as 金额 from lineitem;
select fullname,case when sex='男' then '1'when sex='女' then '0'end as sex
from account;

(暂未):

select name,casewhen unitcost < 500 then '抵挡商品'when unitcost >=500 and unitcost<1000 then '中档商品'else ‘高档商品'end as 档次
from product;

-----条件查询----

select userid,totalprice,status from orders where totalprice >=50;
select * from orderswhere orderdate >='2022-01-01' and orderdate <= '2022-02-20';
select fullname as 姓名,address as 地址,phone as 电话from account where sex='女';
select * from account where fullname like '吴%';
select * from orders where totalprice>=52 and totalprice<=99;
select * from product where productid like '%F____';

-----多表查询--------

select orderid,name,quantity from lineitemjoin product on(itemid=productid);
select fullname,totalprice from ordersjoin account on (orders.userid=account.userid)where totalprice>=88;
select * from orders join accounton (orders.userid=account.userid)where fullname='李天威';
select fullname,totalprice from ordersjoin account on (orders.userid=account.userid)where orderdate<='2022-02-02' and sex='女';
select orderid,userid,orderdate from orders
where orderid in(select orderid from lineitem where itemid='FI-FW-02');
select * from product where unitcost >=any(select unitcost from product where name='景丽');

-----分类汇总与排序------
1)统计客户总数:

select count(*) as 总人数 from account;

2)计算orders表中每单的平均价:

select avg(totalprice) as 每单平均价 from orders;

3)计算orders表中的成交总额:

select sum(totalprice) as 成交总额 from orders;

4)显示orders表中的单笔最高成交额和最低成交额。

select max(totalprice) as 最高成交额,min(totalprice) as 最低成交额from orders;

5)按性别统计客户人数:

select sex,count(*) from account group by sex;

6)按商品类别统计各类商品总数、平均单价:

select catid,sum(qty),avg(unitcost) from product group by catid;

7)将客户信息按电话号码从大到小排序。

select * from account order by phone desc;

8)将orders表按用户号从小到大排序,用户号相同的按订单日期从大到小排序:

select * from orders order by userid,orderdate desc;

9)显示lineitem表中商品的购买总数量超过2件的商品号和购买总数量,并按购买数量从小大大排序:(暂未):

select itemid,sum( quantity ) from lineitemgroup by itemidhaving sum ( quantity ) >=2order by sum( quantity );

-----------7--------

-字段名用中文表示-

create view account_v1
as(select userid as 用户号,fullname as 姓名,password as 密码,sex as 性别,phone as 电话from account where sex='男')with check option;
select * from account_v1;

–查询客户信息–

select * from account_v1 where 姓名 like '李%';

—创建视图—

create view orders_v2
as(select orderid,fullname,address,orderdate,totalpricefrom orders join accounton (orders.userid=account.userid) );
select * from orders_v2;

----查询订单----

select * from orders_v2 where year(orderdate)=2022;

-----创建并查询视图------

create view lineitem_v3
as(select name,orderdate,quantity,unitpricefrom lineitemjoin orders on (lineitem.orderid=orders.orderid)join product on (lineitem.itemid=product.productid) );

—多余—

select * from lineitem_v3;
insert into account_v1values('u0007','陈薇','131452','男','15984247598');
update orders_v2 set totalprice=totalprice+200where orderid=20191012;

删除视图-1

delete from account_v1 where 用户号='u0009';
drop view order_v2,linitem_v3;

--------7----------

-1-

create index I_em_ind on account(email desc);
create index C_fa_ind on account(fullname,address);
create unique index U_na_ind on product(name(4));

2

alter table categoryadd primary key(catid),add unique U_ca_ind(catname);

ERROR 1068 (42000): Multiple primary key defined

错误原因:定义了两次主键;

解决办法:去掉主键:primary key;

alter table lineitemadd primary key(orderid,itemid),add index C_qu_ind(quantity,unitprice);

*–上同--

alter table accountadd primary key(userid),add unique U_fu_ind(fullname);

–*–同上–

3

create table shopcat(shopcatid int(11) not null primary key,userid char(10) not null,itemid char(10) not null,quantity int(11) not null,unitprice decimal(10,2) not null,index C_up_ind( userid,itemid )
);
show index from shopcat;

–*–

drop index C_up_ind on shopcat;

---

alter table orders partition by Key() partitions 3;

ERROR 1506 (HY000): Foreign keys are not yet supported in conjunction with partitioning

错误 1506年:尚未支持将外键与分区 SQL 语句: 更改表 。按哈希(id)分区

每日一言:

无论这个世界对你怎样,都请你一如既往的努力、勇敢、充满希望。

上一章链接:MySQL实例

持续更新中…

[^1]本人不才,若有错误,欢迎在评论区或私信指正。

【创建petstore数据库与表】相关推荐

  1. C#使用VS 2010在程序加载时创建Access数据库和表

    最近在用C#做一个时间管理系统,需要用到数据库.但由于之前都没有接触过C#,以至于走了很多弯路,所以做完之后顺便在这里记录一下.一来可以使自己养成写文档的习惯,二来也可以帮助到后面学习C#的朋友,可谓 ...

  2. Linux下和Windows下创建Oracle数据库,表空间,Oracle用户

    通过SSH工具或是XShell工具进入远程Linux下. 其中,通过SSH工具登录的界面如下: 其中Host Name是远程服务器的ip地址.User Name是服务器的一个用户名,端口号默认22,C ...

  3. mariadb mysql表_mysql/mariadb学习记录——创建删除数据库、表的基本命令

    查看已有的数据库: mysql>show databases;+--------------------+ | Database | +--------------------+ | infor ...

  4. 创建Oracle数据库和表

    1.用'Database Configuration Assistant'向导来创建数据库 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序 所建的数据库目录就在*/p ...

  5. mysql创建的数据库都在哪里看_mysql 怎么查看创建的数据库和表

    1. //看当前使用的是哪个数据库 ,如果你还没选择任何数据库,结果是NULL.mysql>select database(); +------------+ | DATABASE() | +- ...

  6. mysql查阅建立的库_mysql 怎么查看创建的数据库和表

    展开全部 1.e68a8462616964757a686964616f31333339666635 //看当前使用的是哪个数据库 ,如果你还没选择任何数据库,结果是NULL. mysql>sel ...

  7. 通过Navicat创建MySQL数据库并倒入表数据的两种方式

    方式一:通过图形界面(Navicat)进行创建 创建数据库 1.首先建立MySQL数据库连接 2. 然后创建一个数据库 导入表结构(资料/数据模型/sql文件) 1.首先打开数据库 2.然后执行sql ...

  8. 数据库之数据库和表的创建

    数据库和表的创建 本文使用SQLserver数据库为示范 1 目的 1.掌握利用 SSMS 及 SQL 命令两种方式管理数据库: 2.掌握利用 SSMS 及 SQL 命令两种方式管理表: 3.理解数据 ...

  9. 使用PowerDesigner15创建MySQL数据库表结构设计

    1.打开PowerDesigner,依次点击:文件->建立新模型,出现以下画面,然后按照画面中的红色箭头进行操作: 2.然后点击"OK",即可创建MySQL数据库的表结构设计 ...

最新文章

  1. hardnet68尝试
  2. java的query_Java-Query
  3. 9款jQuery插件为你的网站增加亮点
  4. php安装event扩展的问题
  5. redis持久化RDB和AOF
  6. HTML5 CANVAS 弹幕插件
  7. Python 猜数字游戏
  8. FPGA学习笔记---Verilog延迟语句分析比较
  9. 【Vue2.0学习】—Vuex工作原理图(二十五)
  10. Dart基础-泛型和库
  11. Docker入门之四搭建私有仓库
  12. python爬虫案例典型:爬取大学排名(亲测有效)
  13. 想开发一个在线的合同模板管理套件
  14. Android集成华为Push及注意事项
  15. 司铭宇讲师为平安人寿保险300位管理层培训《新生代员工的管理与激励》企业内训
  16. 计算机毕业设计ssm基于ssm框架的动漫网站设计与实现q6dcx系统+程序+源码+lw+远程部署
  17. python分支结构、循环结构
  18. Go 每日一库之 roaring
  19. uniapp跳转外部链接
  20. 测试iphone硬件好坏的软件,iPhone手机如何检测硬件故障,硬件检测必备技能,建议了解一下...

热门文章

  1. C语言抽象数据类型实现复数的加减乘运算、输入实部虚部输出复数,输入复数输出实部虚部
  2. 用Matlab处理TDMS数据(降噪+频谱分析)
  3. Python栈与队列
  4. 淘宝垂直爬虫之关键字搜索(实战+源码+可视化)
  5. 工业视觉系统相关知识和选型介绍(一):相机篇
  6. 《经济学通识》五、反垄断的罪与罚
  7. FITC标记,CY5标记,CY3标记,CY5标记,CY5.5标记,CY7标记,CY7.5标记金纳米团簇
  8. 分辨mqtt在线与离线_最全视频下载方案,100%下载所有在线视频!
  9. 2020西工大c语言程序设计在线作业答案,西工大16秋《C语言程序设计》在线作业答案...
  10. 学生硬件电路设计经验教训浅谈