1、插入数据:insert
语法:
insert into customer(cust_name,cust_contact,cust_email) values(‘jim’,NULL,NULL)
以上例子中,因为给出了列名,所以列的顺序在这里不重要,可以调换顺序
备注:
如果表的定义允许,可以在insert操作中省略某些列。省略的列必须满足以下条件:
1)该列定义为允许null值(无值或空值)
2)在表定义中给出默认值。表示如果不给出值,将使用默认值
插入多行:
1)多条insert语句,每条语句用分号分隔
2)单条insert语句,每一组值用一对圆括号括起来,用逗号分隔
实例:
insert into customers(cust_name,cust_age,cust_city) values(‘jim’,17,’guangzhou’),(‘jim’,18,’shenzhen’)
插入检索出的数据:
insert select
insert into customer(cust_name,cust_contact,cust_email) select name,contact,email from custnew;
备注:
1)不一定要求列名匹配,因为使用的是列的位置
2)insert select中select语句中可以包含where子句以过滤插入的数据
2、更新数据:update
update语句由3部分组成:
1)要更新的表
2)列名与它们的新值
3)确定要更新行的过滤条件
实例:
update customer set cust_email = '18826400669@163.com‘ where cust_id = 10005;
update customer set cust_name = ‘jim’,cust_email = '18826400669@163.com‘ where cust_id = 10005;
备注:
1)即使发生错误,也继续进行更新,可以使用ignore关键字
update ignore customer …
2)删除某个列的值,可以设置为null
update customer set cust_email = NULL where cust_id = 10005;
3、删除数据
1)从表中删除特定行
2)从表中删除所有行
实例:
delete from customer where cust_id = 10006;
备注:
1)delete是删除行,update null是删除列
2)更快的删除:truncate(truncate是整表删除,delete是逐行删除)
3)更新和删除的指导原则:
一定要使用where子句,否则就是整表删除或更新
3、创建表
create table customer(
cust_id int not null auto_increment,
cust_name char(50) not null,
cust_address char(50) not null,
cust_city char(50) null,
primary_key(cust_id)
)engine=innodeDB;
备注:
1)关键字not null会阻止插入没有值的列,null为默认设置,如果不指定not null,则认为指定的是null。null和空串的概念不一样,null不等同于’’(两个单引号,其间没有字符)
2)auto_increment是自动增1,default是设置默认值,主键primary key可以是单值,也可以是多个值,engine可以设置使用哪个引擎
3)如果表已经存在,创建表会报错;如果仅想在表不存在时创建它,可以在表名之后给出IF NOT EXISTS
4、mysql常见的引擎
1)innodeDB是一个可靠地事务处理存储引擎,不支持全文搜索
2)myisam是一个性能极高的引擎,支持全文搜索,但不支持事务处理
3)memory的功能等同于myisam,但由于数据存储在内存(不是磁盘),所以速度极快(特别适用于临时表)
备注:
引擎可以混用,即同一个数据库不同表可以使用不同的存储引擎,但是外键不能跨引擎(即使用一个引擎的表不能引用具有不同引擎的表的外键)
5、更新表定义
1)添加一个列:
alter table vendors add vend_phone char(20);
以上例子中,增加一个名为vend_phone的列,并声明其数据类型
2)定义外键
alter table orderitems add constraint fk_orderitems_orders foreign_key(order_num) reference orders(order_num);
以上例子中,order_num是表orders中的一个列,fk_orderitems_orders 是orderitems的新增外键
6、删除和重命名表
1)删除表
drop table table_name
2)重命名表
rename table table_name1 to table_name2
7、创建视图
create view view_name as …
正确使用视图,可以极大的简化复杂的数据处理
备注:
1)视图可以被更新,但不是所有视图都是可更新的
2)更新一个视图,将更新其基表(视图是一个虚拟的表,本身没有数据)
3)如果视图定义中有以下操作,则不能进行视图的更新:
a、分组(使用GROUP BY和HAVING);
b、联结;
c、子查询;
d、并;
e、聚集函数(min、count、sum);
f、distinct;
g、导出(计算)列
4)一般来说,视图主要用于检索(select语句),而不用于更新(insert、update和delete)
8、存储过程
简单来说,就是为以后的使用而保存的一条或者多条mysql语句的集合,类似于批文件。
备注:
mysql将编写存储过程与执行存储过程的权限分开,许多DBA允许用户使用存储过程,但不允许他们创建存储过程。
1)执行存储过程
mysql将存储过程的执行称为调用,因此执行的语句为call。call接受存储过程的名字以及需要传递给它的任意参数。
示例:
call productpricing(@pricelow,@pricehigh,@priceaverage);
以上例子中,执行名为productpricing的存储过程,它计算并返回产品的最低、最高和平均价格
2)创建存储过程
示例:
create procedure productpricing()
begin
select avg(prod_price) as priceaverage
from products;
end:
以上例子中,此存储过程名为productpricing,在括号内可以接受参数。begin和end语句用来限定存储过程体,过程体本身仅是一个简单的select语句。如果你使用的是mysql命令行,因为存储过程自身内是使用分号作为分隔符,mysql命令行也是使用分号作为语句分隔符,这会出现语法错误。
解决方法是临时更改命令行程序的语句分隔符:
delimiter //
create procedure productpricing()
begin
select avg(prod_price) as priceaverage
from products;
end //
delimiter;
以上例子中,delimiter //告诉命令行实用程序使用//作为新的语句结束分隔符,可以看到标志存储过程结束的end定义为end //而不是end;
这样,存储过程体内的;仍然保持不动,并且正确地传递给数据库引擎。最后,恢复为原来的语句分隔符可以使用delimiter。除\符号外,任何字符都可以用作语句分隔符。
3)删除存储过程
drop procedure productpricing;
备注:
存储过程名后面没有使用()
如果指定的过程不存在,则drop procedure将产生一个错误。当过程存在想删除它时(如果过程不存在也不产生错误)可使用DROP PROCEDURE IF EXISTS。
4)使用参数
示例1:
create procedure productpricing(
out pl decimal(8,2),
out ph decimal(8,2),
out pa decimal(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;
a、以上例子中,存储过程接受3个参数:pl存储产品最低价格,ph存储产品最高价格,pa存储产品平均价格。
b、关键字out指出相应的参数用来从存储过程传出一个值(返回给调用者)。mysql支持in(传递给存储过程),out(从存储过程传出)和inout(对存储过程传入和传出)类型的参数。
c、通过指定into关键字把检索值保存到对应的变量。
示例2:
create procedure ordertotal(
in onumber int,
out ototal decimal(8,2)
)
begin
select sum(item_price*quantity)
from orderitems
where order_num = onumber
into ototal;
end;
以上例子中,
a、onumber定义为in,因为订单号被传入存储过程。
b、ototal定义为out,因为要从存储过程返回合计。
c、into使用ototal存储计算出来的合计
调用:
call ordertotal(20005,@total);
备注:
必须给ototal传递两个参数,第一个参数为订单号,第二个参数为包含计算出来的合计的变量名
为了显示此合计,可如下进行:
select @total;
为了得到另一个订单的合计显示,需要再次调用存储过程,然后重新显示变量:
call ordertotal(20009,@total);
select @total;
5)检查存储过程
a、show create procedure ordertotal;显示用来创建一个存储过程的create语句
b、show procedure status;显示包括何时、由谁创建等详细信息的存储过程列表
备注:
show procedure status会列出所有存储过程。为了限制其输出,可以使用like指定一个过滤模式,例如,
show procedure status like ‘ordertotal’;
9、游标
游标只能用于存储过程和函数
1)创建游标
示例:定义名为ordernumbers的游标,使用了可以检索所有订单的select语句
create procedure processorders()
begin
delare ordernumbers cursor
for
select order_num from orders;
end;
2)打开或者关闭游标
打开:open ordernumbers;
关闭:close ordernumbers;
备注:
隐式关闭,如果你不明确关闭游标,mysql将会在到达end语句时自动关闭它
示例:
create procedure processorders()
begin
–declare the cursor
declare ordernumbers cursor
for
select order_num from orders;
–open the cursor
open ordernumbers;
–close the cursor
close ordernumbers;
end;
3)使用游标数据
示例1:从游标中检索单个行(第一行)
create procedure processorders()
begin
–declare local varibles
declare o int;
–declare the cursor
declare ordernumbres cursor
for
select order_num from orders;
–open the cursor
open ordernumbers;
–get order number
fetch ordernumbers into o;
–close the cursor
close ordernumbers;
end;
以上例子中,fetch用来检索当前行的order_num列(第一行)到一个名为o的局部声明的变量中
示例2:
循环检索数据,从第一行到最后一行
create procedure processorders()
begin
–delare local variables
delare done boolean default 0;
delare o int;
–delare the cursor
delare ordernumbers cursor;
for
select order_num from orders;
–delare continue handler
delare continue handler for sqlstate ‘02000’ set done=1;
–open the cursor
open ordernumbers;
–loop through all rows
repeat
–get order number
fetch ordernumbers into o;
–end of loop
until done end repeat;
–close the cursor
close ordernumbers;
end;