mysql 外键 insert_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: 0  Duplicates: 0  Warnings: 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)

full  join 求并集

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值