没有合适的资源?快使用搜索试试~ 我知道了~
(完整word版)计算机二级《MySQL数据库程序设计》知识点总结.doc
0 下载量 64 浏览量
2022-11-17
23:52:23
上传
评论
收藏 50KB DOC 举报
温馨提示
试读
12页
(完整word版)计算机二级《MySQL数据库程序设计》知识点总结.doc
资源推荐
资源详情
资源评论
MySQL 知识点总结
.数据操作:检索、排序、过滤、分组、汇总、计算、联接、子查询与组合查询
.表操作:表的创建、修改、删除和重命名、表数据的插入、更新和删除
.索引(含主、外键)、视图
.难度编程:存储过程与存储函数、触发器与事件、PHP
.数据库管理:事务处理、用户与权限、备份与还原、数据库维护
1. 检索数据:select…from…
Select [distinct] prod_id,prod_name from products [limit 4,5];
2. 检索排序:order by…
Select * from products order by prod_id [asc|desc],prod_name [asc|desc];
3. 过滤数据:where 字句
= <> != > >= < <= between
(1)普通 where 字句
Select prod_id,prod_name from products where prod_name=’liyang’;
Select prod_id,prod_name from products where prod_id between 10 and 50;
Select prod_id,prod_name from products where prod_name is [not] null;
(2)组合 where 字句:使用 AND 和 OR 连接多个条件表达式,且 AND 次序优于 OR;
(3)IN 与 NOT 操作符
Select prod_id,prod_name from products where prod_id [not] in(1,2,3) |prod_name
in(’zhangsan’,’lisi’,’wangwu’);
(4)LIKE 操作符与通配符:“%”与“_”
Select prod_id,prod_name from products where prod_name like ’%liu%’;
Select prod_id,prod_name from products where prod_name like ’_u%’; 找出 u 位于
名字的第二个位置的 prod_id 和 prod_name。
(5)正则表达式
4. 计算字段
(1)拼接字段:concat(…,…)
Select concat(姓氏,名字) as 姓名 from orders;
Select concat(vend_name,’(’,vend_country,’)’) from vendors;
(2)算术运算
Select prod_name,prod_price,prod_num,prod_price*prod_num as prod_money from
products;
4. 使用数据处理函数:文本处理函数、日期和时间处理函数、数值处理函数。
5. 汇总数据:聚集函数 SUM() AVG() COUNT() MAX() MIN()
Select avg(distinct prod_price) from products;
Select avg(prod_price) 均价,max(prod_price) 最高价 from products;
6. 分组数据:group by… 创建分组、过滤分组、分组排序
Select count(prod_id),prod_id from products where prod_id>1000 group by prod_id
having count(prod_id)>2 order by prod_id;求出 prod_id 大于 1000 且产品数量大于
2 的产品数量,并按 prod_id 排序,注意分组语句中对象要前后一致,如下划线部分。
7. 使用子查询:进行过滤 select…where…in(select…where…in(select…))、作为计算
字段使用子查询。
8. 联接:join…on…
(1)普通联接
Select oi.order_num,oi.prod_id,p.prod_name,p.vend_id,v.vend_name from
orderitems oi join products p on oi.prod_id=p.prod_id join vendors v on
p.vend_id=v.vend_id where vend_name=’liyang’;可同时联接多个表且可同时用于数
据过滤,这种类型的联接一般为内部联接。
(2)自联接:一个表自己与自己联接,注意判断好各字段与前后两个表的关系。
(3)自然联接:基本上简历的内部联接都是自然联接。
(4)外部联接:在关系表中没有关联的信息的行也能显示出来的联接,根据表在 join 字句
的左边还是右边可分为左联接与右联接。
(5)带聚集函数的联接
Select c.cust_id,count(o.order_num) num_ord from customers c join orders o on
c.cust_id=o.cust_id order by c.cust_id;找出客户对应的订单数。
9. 组合查询:连接多个(至少两个)查询语句,满足其中一个查询语句条件的结果都会显
示出来 union(不重复显示)/union all (可重复显示即全部显示)
Select vend_id,prod_id,prod_price from products where prod_price<=5
Union [all]
Select vend_id,prod_id,prod_price from products where vend_id in(1001,1002)
order by prod_id;
注意每个查询必须包含相同的列、表达式或者聚集函数,列的数据类型必须兼容,排序
语句只能放在最后面,该排序语句对组合查询语句中的所有 select 语句都适用。
10. 全文本搜索:只支持引擎为 MyISAM 的表,不支持引擎为 InnoDB 的表,可对搜索结果进
行智能排序后输出,具有较高等级的行先返回。
Match(全文本搜索字段) against(’全文本搜索内容’ [with query expansion])其
中下划线部分为拓展语句,使用该语句,除了可以返回符合所设置的“全文本搜索内容”
的数据结果,还可返回与“全文本搜索内容”有较高相似度的数据结果。
(1)启用全文本搜索支持
Create table fs(id int not null primary key,c text,c1 text,fulltext(c,c1))
engine=MyISAM;
(2)进行全文本搜索
Select note_text from productnotes where match(note_text) against(’liyang’
with query expansion);
11. 插入数据:insert into…{values|select}…
Insert into products(prod_id,prod_name,prod_price) values(1,’豆浆’,2),(3,’
鸡蛋’,1);可同时插入多行数据。
Insert into products(prod_id,prod_name,prod_price) select vend_id,vend_name,
vend_price from vendors where vend_id<=10;
12. 更新数据:update [ignore]…set…,一般情况下,若更新的数据中有部分数据出错,则
全部数据返回到原来的数据,而 ignore 的作用在于即使更新的数据中出现错误,只对
出现错误的数据返回到原来数据,而未出现错误的数据返回更新后的结果实现更新。
update products set prod_name='馒头',prod_price=1 where prod_id=1;
update customers set cust_city=concat(cust_city,’市’)| cust_city
=replace(cust_city,’市’,’city’) where cust_id>1000;
13. 删除数据:delete from…
Delete from products where prod_id between 10 an 50;
14. 表的相关操作
(1)创建表:对表结构进行设置 create table…
Create table products(prod_id int null auto_increment primary key,prod_name
varchar(50),prod_price int,prod_city varchar(50) default ’广州’) engine=
InnoDB;每个字段名后需要设置数据类型,default 为指定默认值,只支持常量不支持函
数,且只在插入数据时起作用而在更新数据时不起作用,InnoDB 是一个可靠的事务处理
引擎,但不支持全文本搜索。
(2)更新表:对表结构进行修改 alter table {add|drop}…
Alter table products add prod_city varchar(50);
Alter table products drop prod_price;
(3)删除表:一旦删除,无法撤销 drop table…
Drop table products;
(4)重命名表:rename table…to…
Rename table products to new_products;
15. 索引的相关操作
(1)创建索引:常用于数据的快速检索,MySQL 中,常用索引在物理可分为:BTREE、HASH
索引两类;在具体用途上可分为:INDEX、UNIQUE、PRIMARY KEY、FOREIGN KEY、FULL
TEXT、SPATIAL 等。
○
1
使用 create index 语句创建索引,对已存在的表创建索引
Create [unique|fulltext|spatial] index index_name [using BTREE|HASH] on
tbl_name(index_col_name[,index_col_name…]);
Create unique index index_products on products(prod_name(2) desc,prod_price);
○
2
使用 create table 语句创建索引,创建表的同时创建索引
Create table seller(seller_id int not null auto_increment,seller_name
char(50),seller_adress char(50),seller_contact char(50),product_type
int,sales int,primary key(seller_id,product_type),[unique|fulltext|spatial]
index index_seller(sales));
○
3
使用 alter table 语句创建索引,修改表的同时添加索引
Alter table tbl_name add {[unique|fulltext|spatial] index index_tbl_name(字段
名)|primary key(字段名)|foreign key(字段名)references elsetbl_name(相同
字段名)};
(2)查看索引:Show index from tbl_name [where expr];
(3)删除索引:drop index index_name on tbl_name 语句或 alter table 语句
Drop index index_name on tbl_name;
Alter table tbl_name drop {[unique|fulltext|spatial] index index_tbl_name(字段
名)|primary key(字段名)|foreign key(字段名)references elsetbl_name(相同
字段名};(下划线部分不确定)
16. 视图的相关操作
视图:虚拟的表,视图本身不包含表中的列和数据,它包含只是一个 SQL 查询,常用于
检索数据。*视图的作用与规则。
(1)创建视图:Create view view_name as select…[where…];
Create view view_products as select prod_id,prod_name,prod_price,prod_num,
prod_price*prod_num as prod_money from products where prod_id<=10 [with check
剩余11页未读,继续阅读
资源评论
zzzzl333
- 粉丝: 701
- 资源: 7万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功