我在下面创建了两个表users
和login_flag
:
CREATE TABLE IF NOT EXISTS users (
user_id int NOT NULL AUTO_INCREMENT,
email varchar(200) NOT NULL,
first_name varchar(100) NOT NULL,
last_name varchar(100) NOT NULL,
password varchar(200) NOT NULL,
priv varchar(20) NOT NULL,
status varchar(20) NOT NULL DEFAULT 'unlocked',
ctime TIMESTAMP NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (user_id)
);
CREATE TABLE IF NOT EXISTS login_flag (
flag_id int NOT NULL AUTO_INCREMENT,
user_id int NOT NULL,
flag int NOT NULL DEFAULT 0,
PRIMARY KEY (flag_id),
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
);
mysql> select * from users;
| user_id | email | first_name | last_name | password | priv | status | ctime |
| 1 | admin@test.com | admin | admin | $2b$12$oMMYOM45LxzsibodyPPBx.JUocceiBoU0Ui2TrZaKEmXnhO0jkCTW | admin | unlocked | 2018-08-23 11:02:40 |
mysql> select * from login_flag;
+---------+---------+------+
| flag_id | user_id | flag |
+---------+---------+------+
| 1 | 1 | 0 |
+---------+---------+------+
1 row in set (0.00 sec)
我从users
表中删除了记录:
mysql> delete from users where user_id='1';
Query OK, 1 row affected (0.16 sec)
这也应该从表login_flag
中删除条目,但不是那样的。
mysql> select * from login_flag;
+---------+---------+------+
| flag_id | user_id | flag |
+---------+---------+------+
| 1 | 1 | 0 |
+---------+---------+------+
1 row in set (0.00 sec)
任何人都可以将我指向正确的方向,或者让我知道我做错了什么地方。
我还检查了foreign_key_checks
,其值为ON
mysql> SHOW VARIABLES LIKE 'foreign_key_checks';
+--------------------+-------+
| Variable_name | Value |
+--------------------+-------+
| foreign_key_checks | ON |
+--------------------+-------+
1 row in set (0.00 sec)
我可以看到Engine是InnoDB
mysql> SHOW TABLE STATUS where Name = 'users'
-> ;
+-------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |
+-------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
| users | InnoDB | 10 | Compact | 0 | 0 | 16384 | 0 | 0 | 10485760 | 2 | 2018-08-23 11:58:47 | NULL | NULL | latin1_swedish_ci | NULL | | |
+-------+--------+---------+------------+------+----------------+-------------+-----------------+--------------+-----------+----------------+---------------------+-------------+------------+-------------------+----------+----------------+---------+
1 row in set (0.02 sec)
show create table login_flag;
mysql> show create table login_flag;
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| login_flag | CREATE TABLE `login_flag` (
`flag_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`flag` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`flag_id`),
KEY `user_id` (`user_id`),
CONSTRAINT `login_flag_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)