1-连接数据库:
mysql -h localhost -u jiangbiao -p
xxxxx@xxx:~$ mysql -h localhost -u jiangbiao -p Enter password: Welcome to the MySQL monitor. Commands end with ; or g. Your MySQL connection id is 2444832 Server version: 5.5.40-log Source distribution
2-显示表的信息,包括所用引擎,对于mysql
,只有InnoDB
引擎支持触发器操作
SHOW CREATE TABLE tbl_buniness;
3-显示数据库 :
show databases;
4-使用数据库:
use databasename;
5-显示数据表:
show tables;
6-显示具体的表结构 :
describe tablename;
7-插入操作:
INSERT INTO `root_trigger` (`id`,`data`) VALUES ('1', 'test root line 1'), ('2', 'test root line 2'), ('3', 'test root line 3');
8 - 通过DELETE CASCADE
外键约束的处理机制来处理级联删除操作:
-- 创建测试主表. ID 是主键. CREATE TABLE test_main ( id INT NOT NULL, value VARCHAR(10), PRIMARY KEY(id) ); -- 创建测试子表. CREATE TABLE test_sub ( id INT NOT NULL, main_id INT , value VARCHAR(10), PRIMARY KEY(id) ); -- 插入测试主表数据. INSERT INTO test_main(id, value) VALUES (1, 'ONE'); INSERT INTO test_main(id, value) VALUES (2, 'TWO'); -- 插入测试子表数据. INSERT INTO test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE'); INSERT INTO test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO'); --- --通过 DELETE CASCADE 外键约束的处理机制来处理 mysql> ALTER TABLE test_sub -> ADD CONSTRAINT main_id_cons -> FOREIGN KEY (main_id) -> REFERENCES test_main(id) -> ON DELETE CASCADE// Query OK, 2 rows affected (0.16 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> DELETE FROM -> test_main -> WHERE -> id = 1; -> // Query OK, 1 row affected (0.02 sec) mysql> SELECT -> * -> FROM -> test_sub; -> // +----+---------+--------+ | id | main_id | value | +----+---------+--------+ | 2 | 2 | TWOTWO | +----+---------+--------+ 1 row in set (0.00 sec)
9 - 如果你非要使用触发器来处理, 那么下面是一个例子:
-- 创建测试主表. ID 是主键. CREATE TABLE t_test_main ( id INT NOT NULL, value VARCHAR(10), PRIMARY KEY(id) ); -- 创建测试子表. CREATE TABLE t_test_sub ( id INT NOT NULL, main_id INT , value VARCHAR(10), PRIMARY KEY(id) ); -- 插入测试主表数据. INSERT INTO t_test_main(id, value) VALUES (1, 'ONE'); INSERT INTO t_test_main(id, value) VALUES (2, 'TWO'); -- 插入测试子表数据. INSERT INTO t_test_sub(id, main_id, value) VALUES (1, 1, 'ONEONE'); INSERT INTO t_test_sub(id, main_id, value) VALUES (2, 2, 'TWOTWO'); DELIMITER // CREATE TRIGGER tr_t_test_main_Del BEFORE DELETE ON t_test_main FOR EACH ROW BEGIN DELETE FROM t_test_sub WHERE main_id = OLD.id; END; // DELIMITER ; mysql> delete from t_test_main where id = 1; Query OK, 1 row affected (0.01 sec) mysql> select * from t_test_sub; +----+---------+--------+ | id | main_id | value | +----+---------+--------+ | 2 | 2 | TWOTWO | +----+---------+--------+ 1 row in set (0.00 sec)
关于8、9两点的
原文链接:https://zhidao.baidu.com/question/562372469.html
10 - 其他:
这里有个已经整理的博客链接