Skip to content

Instantly share code, notes, and snippets.

@Goryudyuma
Created August 4, 2018 05:51
Show Gist options
  • Save Goryudyuma/1fc2608abad4b0006e37e9186db33cfd to your computer and use it in GitHub Desktop.
Save Goryudyuma/1fc2608abad4b0006e37e9186db33cfd to your computer and use it in GitHub Desktop.
権限がないテーブルでも、外部キーが貼られていれば中身が見えるかもしれないメモ ref: https://qiita.com/Goryudyuma/items/4fe3ce5eeab02aa530f8
$ mysql -uroot
mysql> create database testDB;
Query OK, 1 row affected (0.00 sec)
mysql> use testDB
Database changed
mysql> create table A(num int not null unique);
Query OK, 0 rows affected (0.03 sec)
mysql> create table B(num int not null);
Query OK, 0 rows affected (0.02 sec)
mysql> alter table B add constraint num_key foreign key (num) references A(num);
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> insert into A(num) values (3), (5), (8);
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from A;
+-----+
| num |
+-----+
| 3 |
| 5 |
| 8 |
+-----+
3 rows in set (0.00 sec)
mysql> insert into B(num) values (3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into B(num) values (4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> delete from B;
Query OK, 1 row affected (0.00 sec)
mysql> create user cracker;
Query OK, 0 rows affected (0.04 sec)
mysql> grant all on testDB.B to cracker;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
$ mysql -ucracker;
mysql> use testDB;
Database changed
mysql> select * from A;
ERROR 1142 (42000): SELECT command denied to user 'cracker'@'localhost' for table 'a'
mysql> select * from B;
Empty set (0.00 sec)
mysql> insert into B(num) values (0);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (1);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (2);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (3);
Query OK, 1 row affected (0.00 sec)
mysql> insert into B(num) values (4);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into B(num) values (6);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (7);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (8);
Query OK, 1 row affected (0.00 sec)
mysql> insert into B(num) values (9);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
mysql> insert into B(num) values (10);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`testdb`.`b`, CONSTRAINT `num_key` FOREIGN KEY (`num`) REFERENCES `A` (`num`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment