我可以写一个表,级联删除引用这个表的记录。就是用on delete cascade,
也可以写一个级联更新,就是用on update cascade,
但我如何让这两个同时都设置呢?
我在建立一个表的时候,怎么同时写这两个呢?要在同一个字段啊。
也可以写一个级联更新,就是用on update cascade,
但我如何让这两个同时都设置呢?
我在建立一个表的时候,怎么同时写这两个呢?要在同一个字段啊。
解决方案 »
- 如何将MYSQL字段设置成默认当前时间戳?
- mysql中属性为varchar类型的列的值求和?(在线等!)
- Mysql各版本区别
- Postgresql 的问题?
- 有什么可视化前端支持MySQL5.0的新特性的
- 关于表只可读的问题
- select 居然出错
- MySQL物理备份
- mysql5.7.9做主从复制时问题很多,求帮助
- yii框架里面我在新建活动的时候保存了活动的开始时间和结束时间在数据库中,现在要根据保存的开始、结束时间和当前时间来判断活动的状态。
- 关于mysql 的btree索引(ACMAIN_CHM请进)
- mysql 总是提示 shutdown failed; error: 'Access denied 错误
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0-- ON DELETE CASCADE ON UPDATE CASCADE // ACMAIN 按照这个写法就行了。
mysql> create table b (
-> bid int primary key,
-> aid int,
-> FOREIGN KEY (aid) REFERENCES a (aid)
-> ON DELETE CASCADE ON UPDATE CASCADE
-> ) engine=innodb;
Query OK, 0 rows affected (0.06 sec)mysql> insert into b values
-> (1,1),
-> (2,1),
-> (3,2);
Query OK, 3 rows affected (0.05 sec)
Records: 3 Duplicates: 0 Warnings: 0mysql> select * from a;
+-----+
| aid |
+-----+
| 1 |
| 2 |
| 3 |
+-----+
3 rows in set (0.00 sec)mysql> select * from b;
+-----+------+
| bid | aid |
+-----+------+
| 1 | 1 |
| 2 | 1 |
| 3 | 2 |
+-----+------+
3 rows in set (0.00 sec) -- 测试 update
mysql> update a set aid=10 where aid=1;
Query OK, 1 row affected (0.08 sec)
Rows matched: 1 Changed: 1 Warnings: 0mysql> select * from a;
+-----+
| aid |
+-----+
| 2 |
| 3 |
| 10 |
+-----+
3 rows in set (0.00 sec)mysql> select * from b;
+-----+------+
| bid | aid |
+-----+------+
| 3 | 2 |
| 1 | 10 |
| 2 | 10 |
+-----+------+
3 rows in set (0.00 sec)-- 测试 delete
mysql> delete from a where aid=10;
Query OK, 1 row affected (0.05 sec)mysql> select * from a;
+-----+
| aid |
+-----+
| 2 |
| 3 |
+-----+
2 rows in set (0.00 sec)mysql> select * from b;
+-----+------+
| bid | aid |
+-----+------+
| 3 | 2 |
+-----+------+
1 row in set (0.00 sec)mysql>
create table t_b (id int(5) not null,ta_id int(5),t_n varchar(20)
foreign key (ta_id) references t_a(id) on delete cascade
);现在想把级联更新也加上。
create table t_b (id int(5) not null,ta_id int(5),t_n varchar(20)
foreign key (ta_id) references t_a(id) on delete cascade ON UPDATE CASCADE
);
原来我是这样写的on delete update cascade.所以错了。多谢。