jydg 发表于 2018-10-4 07:58:10

MySQL插入外键

创建外键  
mysql> CREATE TABLE `study_record` (
  
    ->   `id` int(11) NOT NULL,
  
    ->   `day` int NOT NULL,
  
    ->   `status` char(32) NOT NULL,
  
    ->   `stu_id` int(11) NOT NULL,
  
    ->   PRIMARY KEY (`id`),
  
    ->   KEY `fk_student_key` (`stu_id`),
  
    ->   CONSTRAINT `fk_student_key` FOREIGN KEY (`stu_id`) REFERENCES `student` (`id`)
  
    -> );
  
Query OK, 0 rows affected (0.05 sec)
  

  
mysql> alter table study_record modify id int auto_increment;
  
Query OK, 0 rows affected (0.07 sec)
  
Records: 0Duplicates: 0Warnings: 0
  

  
mysql> insert into study_record (day,status,stu_id) values(1,'Yes',4);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into study_record (day,status,stu_id) values(1,'Yes',1);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql>
  
mysql> select * from study_record;
  
+----+-----+--------+--------+
  
| id | day | status | stu_id |
  
+----+-----+--------+--------+
  
|1 |   1 | Yes    |      4 |
  
|2 |   1 | Yes    |      1 |
  
+----+-----+--------+--------+
  
2 rows in set (0.00 sec)
  

  
mysql> insert into study_record (day,status,stu_id) values(2,'No',1);
  
Query OK, 1 row affected (0.01 sec)
  

  
mysql> select * from study_record;
  
+----+-----+--------+--------+
  
| id | day | status | stu_id |
  
+----+-----+--------+--------+
  
|1 |   1 | Yes    |      4 |
  
|2 |   1 | Yes    |      1 |
  
|3 |   2 | No   |      1 |
  
+----+-----+--------+--------+
  
3 rows in set (0.00 sec)
  

  

  
mysql> select * from study_record;
  
+----+-----+--------+--------+
  
| id | day | status | stu_id |
  
+----+-----+--------+--------+
  
|1 |   1 | Yes    |      4 |
  
|2 |   1 | Yes    |      1 |
  
|3 |   2 | No   |      1 |
  
+----+-----+--------+--------+
  
3 rows in set (0.00 sec)
  

  
mysql> delete from study_record where id=3;
  
Query OK, 1 row affected (0.02 sec)
  

  
mysql> select * from study_record;
  
+----+-----+--------+--------+
  
| id | day | status | stu_id |
  
+----+-----+--------+--------+
  
|1 |   1 | Yes    |      4 |
  
|2 |   1 | Yes    |      1 |
  
+----+-----+--------+--------+
  
2 rows in set (0.00 sec)
  

  
mysql>
  

  

  
多表查询:
  

  
mysql> create table A(
  
    -> a int not null);
  
Query OK, 0 rows affected (0.04 sec)
  

  
mysql> create table B( b int not null);
  
Query OK, 0 rows affected (0.02 sec)
  

  
mysql> insert into A (a) values(1);
  
Query OK, 1 row affected (0.01 sec)
  

  
mysql> insert into A (a) values(2);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into A (a) values(3);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into A (a) values(4);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into B (b) values(3);
  
Query OK, 1 row affected (0.02 sec)
  

  
mysql> insert into B (b) values(4);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into B (b) values(5);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into B (b) values(6);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> insert into B (b) values(7);
  
Query OK, 1 row affected (0.00 sec)
  

  
mysql> select * from A;
  
+---+
  
| a |
  
+---+
  
| 1 |
  
| 2 |
  
| 3 |
  
| 4 |
  
+---+
  
4 rows in set (0.00 sec)
  

  
mysql> select * from B;
  
+---+
  
| b |
  
+---+
  
| 3 |
  
| 4 |
  
| 5 |
  
| 6 |
  
| 7 |
  
+---+
  
5 rows in set (0.00 sec)
  

  
Inner join语法 其实就是只显示2个表的交集
  
mysql> select * from A inner join B on A.a = B.b;
  
+---+---+
  
| a | b |
  
+---+---+
  
| 3 | 3 |
  
| 4 | 4 |
  
+---+---+
  
2 rows in set (0.00 sec)
  
第二种语法
  
mysql> select A.*,B.* from A,B where A.a=B.b;
  
+---+---+
  
| a | b |
  
+---+---+
  
| 3 | 3 |
  
| 4 | 4 |
  
+---+---+
  
2 rows in set (0.00 sec)
  

  
left join 语法求差级
  
mysql> select * from A left join B on A.a = B.b;
  
+---+------+
  
| a | b    |
  
+---+------+
  
| 3 |    3 |
  
| 4 |    4 |
  
| 1 | NULL |
  
| 2 | NULL |
  
+---+------+
  
4 rows in set (0.00 sec)
  

  
mysql> select * from B left join A on A.a = B.b;
  
+---+------+
  
| b | a    |
  
+---+------+
  
| 3 |    3 |
  
| 4 |    4 |
  
| 5 | NULL |
  
| 6 | NULL |
  
| 7 | NULL |
  
+---+------+
  
5 rows in set (0.00 sec)
  

  
right join
  
mysql> select * from A right join B on A.a = B.b;
  
+------+---+
  
| a    | b |
  
+------+---+
  
|    3 | 3 |
  
|    4 | 4 |
  
| NULL | 5 |
  
| NULL | 6 |
  
| NULL | 7 |
  
+------+---+
  
5 rows in set (0.01 sec)
  

  

  
fulljoin 求并集
  
mysql 并不直接支持full join,but 总是难不到我们
  
mysql> select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;
  
+------+------+
  
| a    | b    |
  
+------+------+
  
|    3 |    3 |
  
|    4 |    4 |
  
|    1 | NULL |
  
|    2 | NULL |
  
| NULL |    5 |
  
| NULL |    6 |
  
| NULL |    7 |
  
+------+------+
  
7 rows in set (0.01 sec)
  

  
mysql>


页: [1]
查看完整版本: MySQL插入外键