MySQL插入外鍵

來源:互聯網
上載者:User

標籤: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: 0mysql> 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 joinmysql> 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插入外鍵

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.