以案例来学习知识,也是一种不错的学习方法。可以更好的给人以兴趣,毕竟,实践才是唯一的真理。
例3-1
创建一个名为“Bookstore”的数据库:
create database Bookstore;
例3-2
创建一个名为“Bookstore” 的数据库,采用字符集 gb2312 和校对规则gb2312_chinese_ci【注意:mysql校对规则的命名约定:它们以其相关的字符集名开始,通常包括一个语言名,并且以_ci(对大小写不敏感)、_cs(对大小写敏感)、_bin(二元)结束】:
create database lxw
default character set gb2312
collate gb2312_chinese_ci;
例3-3
修改数据库 “Bookstore”的默认字符集为 “utf8mb4”、校对规则为“utf8mb4_09900_ai_ci”:
alter database lxw
default character set utf8mb4
default collate utf8mb4_09900_ai_ci;
Unknown collation: ‘utf8mb4_09900_ai_ci’
报错原因:
生成转储文件的数据库版本为8.0,要导入sql文件的数据库版本为5.6,因为是高版本导入到低版本,引起1273错误
解决方法:
打开sql文件,将文件中的所有
utf8mb4_0900_ai_ci替换为utf8_general_ci
utf8mb4替换为utf8
保存后再次运行sql文件,运行成功
alter database lxw
default character set utf8
default collate utf8_general_ci;
use Bookstore;
create table book (
图书编号 char(10) not null primary key,
图书类别 varchar(20) not null default '计算机',
书名 varchar(40) not null,
作者 char(10) not null,
出版社 varchar(20) not null,
出版时间 date not null,
单价 float(5,2) not null,
数量 int(5),
折扣 float(3,2),
封面图片 blob
) engine=innodb;
use Bookstore;
alter table book
add 浏览次数 tinyint null,
drop column 书名;
use Bookstore;
alter table book
rename to mybook;
use Bookstore;
rename table mybook to booklist,members to memberlist;
create table book_copy1 like book;
create table book_copy2
as
(select * from book);
drop table if exists test;
use Bookstore;
show tables;
describe book;
desc book 图书编号;
疑惑:Empty set (0.00 sec)
解决:提示你实体表为空,说的是你的数据库或表里数据为空,添加上数据就可以了。
create table book_copy (
图书编号 varchar(6) null,
书名 varchar(20) not null primary key,
出版日期 date
);
create table course
(
学号 varchar(6) not null,
姓名 varchar(8) not null,
毕业日期 date not null,
课程号 varchar(3),
学分 tinyint,
primary key (学号,课程号,毕业日期)
);
create table coursel
(
学号 varchar(6) not null,
姓名 varchar(18) not null,
毕业日期 datetime not null,
课程号 varchar(3),
学分 tinyint,
primary key index_course1(学号,课程号,毕业日期)
);
create table book_copy3
(
图书编号 varchar(20) not null,
书名 varchar(20) not null unique,
出版日期 date null,
primary key(图书编号)
);
create table book_copy4
(
图书编号 varchar(20) not null,
书名 varchar(20) not null,
出版日期 date null,
primary key(图书编号),
unique(书名)
);
alter table book
add primary key(图书编号),
add unique u_idx(书名);
错误点
:ERROR 1068 (42000): Multiple primary key defined
原因:有两个主键,删除其中一个即可用。
alter table booklist
drop primary key,
drop index u_idx;
create table book_ref
(
图书编号 varchar(20) null,
书名 varchar(20) not null,
出版日期 date null,
primary key (书名),
foreign key (图书编号)
references booklist (图书编号)
on delete restrict
on update restrict
);
—如下select语句不会返回任何行—
select * from book_ref
where 图书编号 not in
(select 图书编号 from booklist);
create table book_ref1
(
图书编号 varchar(20) null,
书名 varchar(20) not null,
出版日期 date null,
primary key (书名),
foreign key (图书编号)
references booklist (图书编号)
on update cascade
);
alter table booksell
add foreign key (用户号)
references members (用户号)
on delete cascade
on update cascade;
create table student
(
学号 char(6) not null,
性别 char(2) not null check(性别 in (`男`,`女`))
);
出错点
:ERROR 3813 (HY000): Column check constraint ‘student_chk_1’ references other column
MySQL错误代码3813:“列检查约束”,无法创建表
create table student1
(
学号 char(6) not null,
出生日期 date not null,
学分 int null,
check (出生日期>`2001-01-01`)
);
错误点
:ERROR 3820 (HY000): Check constraint ‘student1_chk_1’ refers to non-existing column ‘2001-01-01’
create table student2
(
学号 char(6) not null,
最好成绩 int(1) not null,
平均成绩 int(1) not null,
check(最好成绩>平均成绩)
);
出错点
: ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘骞冲潎鎴愮哗`))’ at line 1
create table student3
(
学号 char(6) not null,
最好成绩 int(1) not null,
平均成绩 int(1) not null,
check(最好成绩<=100),
check(最好成绩>平均成绩)
);
错误点
:ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘鏈?ソ鎴愮哗
<= 100))’ at line 1
alter table book drop primary key;
alter table book_ref drop foreign key book_ref_ibfk_1;
alter table student drop check student_chk_1;
alter table student1 alter check student1_chk_1 not enforced;
—查看表的所有信息—
show create table;
use bookstore;
insert into booklist values (
'TP.9501','计算机','宝丽嘉','高等教育出版社','2022-01-20',33.25,52,0.8,'Dreamwearer',2);
错误点
:ERROR 1406 (22001): Data too long for column ‘作者’ at row 1
解决方案(暂定):alter table booklist modify column 作者 char(20);
解决:与表一一对应
insert into booklist (
图书编号,作者,出版社,出版时间,单价,数量,折扣)
values ('TP.9502','宝丽嘉','高等教育出版社','2022-01- 20',33.25,52,0.8);
insert into booklist
set 图书编号='TP.9503',作者='宝丽嘉',图书类别='DEFAULT',
出版社='高等教育出版社',出版时间='2021-02-06',单价=32,数量=20,折 扣=0.6;
replace into booklist values('TP.9504','计算机','林小红','高等教育出版社','2020-06-08',23.5,30,0.6);
错误点
:ERROR 1136 (21S01): Column count doesn’t match value count at row 1
出现问题:新插入一行数据时候,返回错误
解决方法:检查表格:select * from 表名; desc 表名;
然后与表一一对应加入或改正即可:
replace into booklist values('TP.9504','计算机','林小红','高等教育出版社','2020-06-08',23.5,30,0.6,62015,3);
insert into booklist
values('TP.2467','计算机','林华中','高等教育出版社','2020-01-23',42.5,45,0.8,'D:\pic\ic.jpg',null);
insert into booklist values
('C0138','李华','女','147369','17885091065','2022-01-20'),
('C0139','张明','男','258456','17684271352','2020-09-23');
----数据修改----
update booklist
set 数量=数量+10;
update members
set 联系电话='18798372060',密码='147369'
where 姓名='张三';
update sell,booklist
set sell.订购册数=订购册数-2,booklist.数量=数量+2
where sell.图书编号=book.图书编号 and sell.订单号='6';
细分:
update booklist set booklist.数量=数量+2;
-------数据删除-------
use bookstore;
delete from members
where 姓名='张三';
use bookstore;
delete from booklist
where 数量<5;
—1—
delete sell,members
from sell,members
where sell.用户号=members.用户号
and members.用户号='D1973';
—2----
delete
from sell,members
using sell,members
where sell.用户号=members.用户号
and members.用户号='D1973';
-–消除表数据语句–-
truncate table 表名;
--------数据查询--------
use bookstore;
select 姓名,联系电话,注册时间
from members;
[^1]显示members中的所有列:select * from members;
select 书名 as name,作者 as auther,出版社 as publisher
from booklist
where 图书类别='计算机';
select 作者 as auther,出版社 as publisher
from booklist
where 图书类别='计算机';
当自定义的列标题中含有空格
时,必须使用引号将标题括起来
:
select 作者 as 'name of Auther',出版社 as Publish
from booklist where 图书类别='计算机';
注意
:不允许在where子句中使用列别名(这是因为执行where代码时,可能尚未确定列值)。
例如,下述查询时非法的
select 性别 as sex from members where sex='男';
select 图书编号,书名
case
when 数量 is null then '尚未进货'
when 数量<5 then '需进货'
when 数量>=5 and 数量<=50 then '库存正常'
else '库存积压'
end as 库存
from booklilst;
select 图书编号,作者
case
when 数量 is null then '尚未进货'
when 数量<5 then '需进货'
when 数量>=5 and 数量<=50 then '库存正常'
else '库存积压'
end as 库存
from booklilst;
select 图书编号,round(订购册数*订购单价,2) as 订购金额
from sell
where 是否发货='已发货';
–消除结果集中的重复行–
select distinct 列名1,列名2;
当对book表中只选择图书类别和出版社两列时,结果集中有很多重复行:
select distinct 图书类别,出版社 from book;
select *
from book
where 书名='网页程序设计';
select *
from book
where 单价>30;
select 订单号,订购时间,是否收货
from sell
where 是否收货<=>null;
--1--
select * from book
where (出版社='清华大学出版社' or 出版社='高等教育出版社')
and 单价>=35;
--2--
select * from book
where(出版社='高等教育出版社' and 单价>=30)
or (出版社='清华大学出版社' and 单价>=30);
select *
from sell
where 是否收货='已收货' and 是否结清='已结清';
select 用户名,姓名,注册时间
from members
where 姓名 like '李__';
select 图书编号,书名 from book
where 图书编号 like '%6_';
select 图书编号,书名
from book
where 书名 like '%#_A%'eacape '#';
--1--
select * from book
where 出版时间 between '2020-1-1' and '2020-12-31';
--2--
select * from book where 出版时间>='2020-1-1' and 出版时间<='2020-12-31';
select *
from book
where 出版时间 not between '2020-1-1' and '2020-12-31';
select * from book where 出版时间<='2020-1-1' and 出版时间>='2020-12-31';
--1--
select * from book
where 出版社 in ('高等教育出版社','北京大学出版社','人民邮电出版社');
--2--
select * from book
where 出版社='高等教育出版社' or 出版社='北京大学出版社' or '人民邮电出版社';
select * from sell
where 是否发货 is null;
------多表查询------
select *
from members as users;
select book.书名,sell.订购册数,sell.订购时间
from book,sell
where book.图书编号=sell.图书编号;
select book.书名,sell.订购册数,sell.订购时间
from book inner join sell
on (book.图书编号=sell.图书编号);
select 书名,订购册数
from book join sell
on book.图书编号=sell.图书编号
where 书名='mysql数据库' and 订购册数>5;
select book.图书编号,姓名,书名,订购册数
from sell join book on book.图书编号=sell.图书编号
join members on sell.用户号=members.用户号
where 书名='mysql数据库' and 订购册数>5;
select distinct a.订单号,a.图书编号,a.订购册数
from sell as a join sell as b
on a.图书编号=b.图书编号 and a.订单号=b.订单号;
--1--
select distinct 姓名 from members
join sell using(用户号);
--2--
select distinct 姓名
from members join sell
on members.用户号=sell.用户号;
select book.图书编号,book.单价,用户号
from book left outer join sell
on book.图书编号=sell.图书编号 and 图书类别='计算机';
select 订单号,图书编号,订购册数,members.姓名,members.联系电话
from sell right join members
on members.用户号=sell.用户号 and 性别='男';
select *
from sell
where 用户号 in
( select 用户号 from members where 姓名='张三');
select * from members where 用户号 in
(select 用户号 from sell where 图书编号 not in
(select 图书编号 from book where 书名='mysql数据库'));
select * from members where 用户号=any
(select 用户号 from sell
where 图书编号='TP.2525');
–可先–
select 用户号 from sell where 图书编号='TP.2525';
select 图书编号,图书类别,单价 from book
where 单价>all
(select 单价 from book where 图书类别='网页设计');
select 图书编号,订购册数 from sell where 订购册数>=some
(select 订购册数 from sell where 图书编号='Ts.3035');
--先查询--
select 图书编号,订购册数 from sell where 图书编号='Ts.3035';
select 姓名 from members where exists
( select * from sell
where 用户号=members.用户号 and 订购册数>10);
select 订单号,用户号,图书编号,订购册数 from sell where 用户号='c0138';
select 订单号,用户号,图书编号,订购册数 from sell where 图书编号='TP.5252';
-*-合并如果要保留所有的记录,则使用All–
select 订单号,用户号,图书编号,订购册数 from sell where 用户号='c0138'
union [all]
select 订单号,用户号,图书编号,订购册书 from sell where 图书编号='TP.2525';
select 图书编号,订购册数,订购单价 from sell where 订购册数>30;
select 图书编号,数量,单价 from book where 数量>50;
--合并--
select 图书编号,订购册数,订购单价 from sell where 订购册数>30
union
select 图书编号,数量,单价 from book where 数量>50;
(select 图书编号,订购册数,订购单价 from sell where 订购册数>30
order by 订购单价 limit 2)
union all
(select 图书编号,数量,单价 from book where 数量>50 order by
单价 limit 2);
-—分类汇总与排序—-():–
select count (*) as '会员数' from members;
select count(是否收获) as '已收货的订单数' from sell;
select count(订购册数) as '订购册书在5本以上的订单数'
from sell where 订购册数>5;
select max(订购册数),min(订购册数)
from sell
where 图书编号='Ts.3035';
select sum(订购册数) as '订购总册数'
from sell where 图书编号='Ts.3035';
select avg(订购册数) as '每笔订单平均册数'
from sell where 图书编号='Ts.3035';
----------GROUP BY 子句----------
select 图书类别
from book
group by 图书类别;
select 图书类别,sum(数量) as '库存数'
from book
group by 图书类别;
select 图书编号,avg(订购册数) as '订购册数',
count(订单号) as '订单数'
from sell
group by 图书编号;
–-1--
select 图书类别,出版社,sum(数量) as '库存数'
from book group by 图书类别, 出版社;
--2--
select 图书类别,出版社,sum(数量) as '库存数'
from book
group by 图书类别,出版社
with rollup;
select 用户号,avg(订购册数) as '平均订购册数'
from sell
group by 用户号
having avg(订购册数) >5;
select 用户号
from sell
where 订购册数 > 5
group by 用户号
having count(*) >= 2;
–先查找每笔订购册数都在5本以上的用户:—
select 用户号,订购册数 from sell where 订购册数 >5;
select * from book
group by 出版时间;
select * from sell
group by 订购册数 desc;
select * from members
group by 注册时间
limit 5;
select * from book
order by 图书编号
limit 3,5;
create or replace view jsj_sell
as
select 订单号,sell.图书编号,书名,订购册数
from book,sell
where book.图书编号=sell.图书编号
and book.图书类别='计算机'
with check option;
create view sale_avg (name,sale_avg)
as
select 书名,avg(订购册数)
from jsj_sell
group by 书名;
select 订单号,订购册数
from jsj_sell;
create view kh_avg (userID,order_avg)
as
select 用户号,avg(订购册数)
from sell
group by 用户号;
select * from kh_avg where order_avg > 5;
create or replace view jsj_book
as
select *
from book
where 图书类别='计算机'
with check option;
insert into jsj_book
values('TP.0837','计算机','李天威','人民邮电出版社','2026-5-20',36,520,0.8,'jpg',12,'Office应用实例');
update jsj_book
set 单价=单价*(1-0.05);
改书名
update jsj_sell
set 书名='PHP网站制作'
where 图书编号='TP.2525';
改订购册数
update jsj_sell
set 订购册数=100
where 图书编号=5;
合:
update jsj_sell
set 书名='PHP网站制作',订购册数=100
where 订单号=5 and 图书编号='TP.2525';
delete from jsj_book
where 出版社='人民邮电出版社';
(修改视图定义):
alter view jsj_book
as
select 图书编号,书名,单价 from book
where 图书类别='计算机';
create index name_book
on book(书名(6) asc);
create index user_bh_sell
on sell(用户号,图书编号);
alter table book
add index(书名);
alter table book
add primary key (图书编号),
add index (出版社,出版时间);
查看表中创建的索引的情况:
show index from book;
create table sell(
用户号 char(18) not null,
图书编号 char(20) not null,
订购册数 int(5),
订购时间 datetime,
primary key(用户号,图书编号),
index (订购册数)
);
-*-别–
drop index 书名 on book;
alter table book
drop primary key,
drop index name_book;
alter table book
add index (出版社) invisible;
alter table sell
partition by range(year(订购时间))
(partition p1 values less than (2010),
partition p2 values less than (2015),
partition p3 values less than maxvalue);
查看分区情况:
select
partition_name part,
partition_expression expr,
partition_description descr,
table_rows
from information_schema.partitions
where table_schema=schema() and table_name='sell';
alter table sell
partition by list (是否结清)
(partition p1 values in (1),
partition p2 values in (0));
alter table sell
partition by hash(订单号) partitions 3;
alter talbe sell
partition by key() partitions 3;
alter table sell
add partition (partition p3 values in (2));
alter table sell
reorganize partition p2,p3 into (partition m values in (0,2));
alter talbe sell
drop partition p2;
alter table sell
remove partitioning;
–编程基础知识–
set @b_name=(select 书名 from book where 图书编号='TP.0837');
–引用用户变量的值–
select * from book where 书名=@b_name;
select substring(姓名,1,1) as 姓,
substring(姓名,2,length(姓名)-1) as 名
from members order by 姓名;
–length函数的作用
:返回一个字符串的长度------
select 姓名,year(now())-year(注册时间) as 注册年数
from members;
select 姓名,if(性别='男',1,0) as 性别
from members
where 姓名 like '__';
if n2>n2 then
set result='大于';
elseif n1=n2 then
set result='等于';
else
set result='小于';
end if;
case str
when 'U' then set direct='上升';
when 'D' then set direct='下降';
else set direct='不变';
end case;
case
when str='U' then set direct='上升';
when str='D' then set direct='下降';
else set direct='不变';
end case;
----存储过程----
declare a int default 5;
where a > 0 DO
set a=a-1;
end while;
没有不可治愈的伤痛,只有不可结束的沉沦。 所有失去的,会以另一种方式归来。
本人不才,若有错误,还请在评论区或者私信留言指点一下。
[^1]注
:本文章仅用于参考学习,如有错误,欢迎指正。