码迷,mamicode.com
首页 > 数据库 > 详细

MySQL插入外键

时间:2018-08-04 11:36:58      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:join   warnings   set   tab   prim   多表   直接   显示   外键   

创建外键 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>


MySQL插入外键

标签:join   warnings   set   tab   prim   多表   直接   显示   外键   

原文地址:http://blog.51cto.com/zhanx/2154318

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!