《MySQL必知必会》学习笔记(2)—增删改、视图、存储过程、游标

目录

数据的增删改

创建和操纵表

视图

存储过程

游标


数据的增删改

  • 插入数据

插入数据时VALUES必须以其指定的次序匹配指定的列名,而不一定要按各个列出现在实际表中的次序。

insert into customers(cust_name, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) 
values('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', null, null),
      ('Pep E. LaPew', '100 Main Street', 'Los Angeles', 'CA', '90046', 'USA', null, null);

insert还可以将一条select语句的结果插入表中 :

--使用INSERT SELECT从custnew中将所有数据导入customers
insert into customers(cust_contact, cust_email, cust_name, cust_adress, cust_city, cust_state, cust_zip, cust_country) 
select cust_id, cust_email, cust_name, cust_adress, cust_city, cust_state, cust_zip, cust_country from custnew;
  • 更新数据

在更新多个列时,只需要使用单个SET命令,每个“列=值”对之间用逗号分隔。另外,如果要删除某一列的值,可设置它成为null。

update customers set cust_emailn = '[email protected]', cust_name = 'The Fudds' where cust_id = 10005;
  • 删除数据
delete from customers where cust_id = 10006;

创建和操纵表

  • 创建表
create table customers
(
    cust_id  int  not null auto_increment,
    cust_name char(50) not null, 
    cust_adress char(50) not null, 
    cust_city char(50) not null,
    cust_state char(5) not null,
    cust_zip char(10) not null, 
    cust_country char(50) not null,
    cust_contact char(50) not null,
    cust_email char(255) not null,
    primary key(cust_id)
)Enging = InnoDB;
  1. 表的主键由PRIMARY KEY关键字指定;
  2. AUTO_INCREMENT会告诉MySQL,本列每当增加一行时自动增量;每个表只能有一个AUTO_INCREMENT列,并且必须被索引;
  3. DEFAULT关键字可以指定某列的默认值;
  4. ENGINE来指定使用的存储引擎,常用的有:InnoDB是一个可靠的事务处理引擎,它不支持全文本搜索;MEMORY在功能等同于MyISAM,但由于数据存储在内存(不是磁盘)中,速度很快(特别适合于临时表);MyISAM是一个性能极高的引擎,它支持全文本搜索,但不支持事务处理
  • 更新表
--增加一个列
alter table vendors add vend_phone char(20);
--删除一个列
alter table vendors drop column vend_phone;

ALTER TABLE的一种常见用途是用来定义外键:

alter table orders add constraint fk_orderitems_products foreign key (cust_id) references customers (cust_id);
  • 删除表
drop table customers;
  • 重命名表
rename table customers2 to customers;

视图

  • 为什么要使用视图
  1. 重用SQL语句。
  2. 简化复杂的SQL操作。在编写查询后,可以方便地重用它而不必知道它的基本查询细节。
  3. 使用表的组成部分而不是整个表。
  4. 保护数据。可以给用户授予表的特定部分的访问权限而不是整个表的访问权限。
  5. 更改数据格式和表示。视图可返回与底层表的表示和格式不同的数据。
  • 利用视图简化复杂的联结
create view productcustomers as
select cust_name, cust_contact, prod_id
from customers, orders, orderitems
where customers.cust_id = orders.cust_id and orderitems.order_num = orders.order_num;
--利用视图,可一次性编写基础的SQL,然后根据需要多次使用。
select cust_name, cust_contact from productcustomers where prod_id = 'TNT2';
  • 用视图重新格式化检索出的数据
--创建一个视图来存储某种格式化的结果,而不必在每次需要时执行联结
create view vendorlocations as 
select contact(RTrim(vend_name), '(', RTrim(vend_country), ')') as vend_title from vendors order by vend_name;
  • 用视图过滤不想要的数据
--过滤没有电子邮件地址的客户
create view customeremaillist as
select cust_id, cust_name, cust_email from customers where cust_email is not null;
  • 使用视图与计算字段

create view orderitemsexpanded as 
select order_num prod_id, quantity, item_price, quantity*item_price as expanded_price from orderitems;
一般来说,应该将视图用于检索(SELECT语句) 而不用于更新(INSERTUPDATEDELETE)。

存储过程

  • 为什么要使用存储过程
存储过程是为了以后的使用而保存的一条或多条MySQL语句的集合,使用存储过程主要有3个好处:简单、安全、高性能
  1. 通过把处理封装在容易使用的单元中,简化复杂的操作;
  2. 防止错误,由于不要求反复建立一系列处理步骤,这保证了数据的完整性;
  3. 简化对变动的管理。如果表名、列名或业务逻辑等有变化,只需要更改存储过程的代码,使用它的人员甚至不需知道这些变化;
  4. 提高性能,使用存储过程比使用单独的SQL语句要快。
使用存储过程也存在一些缺陷,首先,一般来说,存储过程的编写比基本SQL语句复杂,编写存储过程需要更高的技能,更丰富的经验。另外,需要限制存储过程的创建权限,允许用户使用存储过程,但不允许他们创建存储过程。
 
  • 使用存储过程

(1)执行存储过程

--执行名为productpricing的存储过程,它计算并返回产品的最低、最高和平均价格
CALL productpricing(@pricelow, @pricehigh, @priceaverage);

(2)创建存储过程

CREATE PROCEDURE productpricing()
begin
         select avg(prod_price) as priceaverage
         from products;
end;

(3)删除存储过程

--注意没有使用后面的()
drop procedure productpricing;

(4)使用参数

create procedure productpricing(
        out pl declimal(8,2),
        out ph declimal(8,2),
        out pa declimal(8,2)
)
begin
         select min(prod_price)  into pl  from products;
         select max(prod_price)  into ph  from products; 
         select avg(prod_price)  into pa  from products;
end;

--调用此存储过程,必须指定3个变量名,用于保存该存储过程的结果
CALL productpricing(@pricelow, @pricehigh, @priceaverage);

(5)检查存储过程

show create procedure ordertotal;

游标

游标是一个存储在MySQL服务器上的数据库查询,它不是一条SELECT语句,而是被该语句检索出来的结果集。在存储了游标之后,应用程序可以根据需要滚动或浏览其中的数据。它主要用于交互式应用,其中用户需要滚动屏幕上的数据,并对数据进行浏览或做出更改。

  • 使用游标

(1)在能够使用游标前,必须声明(定义)它,这个过程实际上没有检索数据,只是定义要使用的SELECT语句。游标用DECLARE语句创建,如:

create procedure processorders()
begin
         DECLARE ordernumbers cursor
         for
         select order_num from orders;
end;

(2)一旦声明后,必须打开游标以供使用,这个过程用前面定义的 SELECT语句把数据实际检索出来;

--在处理OPEN语句时执行查询,存储检索出的数据以供浏览和滚动。
open ordernumbers;

(3)对于填有数据的游标,根据需要取出(检索)各行;

--用fetch检索当前行的order_num列到一个名为o的局部声明的变量中
create procedure processorders()
begin 
         declare o int;
         declare ordernumbers cursor
         for
         select order_num from orders;
         open ordernumbers;
         FETCH ordernumbers into o;
         close ordernumbers;
end;

(4)在结束游标使用时,必须关闭游标。

close ordernumbers;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章