1:插入语句的一些其他形式:
mysql中常用的三种插入数据的语句:
-
insert into 表示插入数据,数据库会检查主键,如果出现重复会报错; -
replace into 表示插入替换数据,需求表中有PrimaryKey,或者unique索引,如果数据库已经存在数据,则用新数据替换,如果没有数据效果则和insert into一样; -
insert ignore 表示,如果中已经存在相同的记录,则忽略当前新数据;
答案:
insert ignore into actor values(“3”,“ED”,“CHASE”,“2006-02-15 12:34:33”);
36:表格数据导入操作:
41:关于条件构造器的相关使用:
触发器概念:触发器是一种特殊的存储过程,它在试图更改触发器所保护的数据时自动执行。
触发器与存储过程的异同
-
相同点:1. 触发器是一种特殊的存储过程,触发器和存储过程一样是一个能够完成特定功能、存储在数据库服务器上的SQL片段。 -
不同点:2. 存储器调用时需要调用SQL片段,而触发器不需要调用,当对数据库表中的数据执行DML操作时自动触发这个SQL片段的执行,无需手动调用。
create database if not exists mydb01_trigger;
use mydb01_trigger;
create table if not exists user(
uid int primary key auto_increment,
username varchar(50) not null,
password varchar(50) not null
)default charset=utf8;
create table if not exists user_logs(
id int primary key auto_increment,
time timestamp,
log_text varchar(100)
)default charset=utf8;
create trigger trigger_test1 after insert on user for each row
insert into user_logs values(NULL,now(),'new');
insert into user values(2,'zbb','123456');
2:使用NEW 和 OLD
create database if not exists mydb01_trigger;
use mydb01_trigger;
create table if not exists user(
uid int primary key auto_increment,
username varchar(50) not null,
password varchar(50) not null
)default charset=utf8;
create table if not exists user_logs(
id int primary key auto_increment,
time timestamp,
log_text varchar(255)
)default charset=utf8;
create trigger trigger_test1 after insert on user for each row
insert into user_logs values(NULL,now(),'new');
insert into user values(3,'zbb','123456');
drop trigger trigger_test1
create trigger trigger_test2 after insert on user for each row
insert into user_logs values(NULL,now(),concat('有新用户添加,信息为:',NEW.username,NEW.password));
insert into user values(4,'abb','123456');
drop trigger trigger_test2
create trigger trigger_test3 after update on user for each row
insert into user_logs values(NULL,now(),concat('有用户信息修改,旧数据是:',OLD.uid,OLD.username,OLD.password));
update user set password = '00000' where uid=3;
drop trigger trigger_test3
create trigger trigger_test4 after update on user for each row
insert into user_logs values(NULL,now(),concat('有用户信息修改:新数据是',NEW.uid,NEW.username,NEW.password));
update user set password = '666666' where uid=3;
create trigger trigger_test5 after delete on user for each row
insert into user_logs values(NULL,now(),concat('有用户被删除,删除信息为:',OLD.uid,OLD.username,OLD.password));
delete from user where uid=3;
|