【创建petstore数据库与表】


话不多说,一切都在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 product
	add foreign key (catid)
		references category(catid)
			on delete restrict;
alter table orders
	add foreign key (userid)
		references account(userid)
			on delete restrict
			on update restrict;
alter table lineitem
	add foreign key (itemid)
		references product(productid)
			on delete cascade
			on update cascade;
alter table lineitem
	add foreign key (orderid)
		references orders(orderid)
			on delete cascade;
alter table account
add check(sex in ('男','女'));

数据插入-1-用户表-

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

insert into account
	values('u0001','刘晓和','123456','男','广东深圳市','[email protected]','17885091066');
insert into account
	values('u0002','张嘉庆','147369','男','广东深圳市','[email protected]','17885061235');
insert into account
	values('u0003','李天威','131452','男','贵州毕节市','[email protected]','17885091065');
insert into account
	values('u0004','陈浪','584145','女','广东珠海市','[email protected]','16658461684');
insert into account
	values('u0005','吴晶','852416','女','广东广州市','[email protected]','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 product
	values('AV-CB-01','05','亚马逊鹦鹉','75岁以上高龄的好伙伴',50.00,60.00,100);
insert into product
	values('AV-SB-02','05','燕雀','非常好的减压宠物',45.00,50.00,98);
insert into product
	values('FI-FW-01','05','景丽','来自日本的淡水鱼',38.00,45.50,300);
insert into product
	values('FI-FW-02','01','金鱼','来自中国的淡水鱼',6.80,6.60,100);
insert into product
	values('FI-SW-01','01','天使鱼','来自澳大利亚的海水鱼',10.00,21.05,100);
insert into product
	values('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 orders
	set status=1
	where orderid='20191011';

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

update lineitem,product
	set product.qty=product.qty-lineitem.quantity
	where lineitem.itemid=product.productid
		and lineitem.orderid='20191011';

–合并–

update orders,lineitem.product
	set orders.status=1,
		product.qty=product.qty-lineitem.quantity
	where orders.orderid=lineitem.orderid
		and lineitem.itemid=product.productid
		and orders.orderid='20191011';

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

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

-*-delete orders,lineitem
	from orders,lineitem
	where orders.orderid=lineitem.id
		and orders.userid='u0004';-*-

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

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

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

-*-delete account,orders,lineitem
	from account,orders,lineitem
	where account.userid=orders.userid
		and orders.orderid=lineitem.orderid
		and 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,
	case
		when 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 orders
	where 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 lineitem
	join product on(itemid=productid);
select fullname,totalprice from orders
	join account on (orders.userid=account.userid)
		where totalprice>=88;
select * from orders join account
	on (orders.userid=account.userid)
		where fullname='李天威';
select fullname,totalprice from orders
	join 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 lineitem
	group by itemid
		having sum ( quantity ) >=2
			order 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,totalprice
	from orders join account
		on (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,unitprice
	from lineitem
		join orders on (lineitem.orderid=orders.orderid)
		join product on (lineitem.itemid=product.productid) );

—多余—

select * from lineitem_v3;
insert into account_v1
	values('u0007','陈薇','131452','男','15984247598');
update orders_v2 set totalprice=totalprice+200
	where 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 category
	add primary key(catid),
	add unique U_ca_ind(catname);

ERROR 1068 (42000): Multiple primary key defined

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

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

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

*–上同--

alter table account
	add 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]本人不才,若有错误,欢迎在评论区或私信指正。

猜你喜欢

转载自blog.csdn.net/m0_66318554/article/details/124960260
今日推荐