Foreign key constraints and their applications in mysql

Creating an associated table in MySQL can be understood as a foreign key relationship between two tables, but these two tables must meet three conditions:
1. The two tables must be the InnoDB data engine
2. The domain used in the foreign key relationship must be an index Type (Index)
3. The field used in the foreign key relationship must be similar to the data type

create table s_user(
       u_id int auto_increment primary key,
       u_name varchar(15),
       u_pwd varchar(15),
       u_truename varchar(20),
        u_role varchar(6),
       u_email varchar(30)
) ENGINE=INNODB DEFAULT CHARSET=utf8 ;
insert into s_user values
       (1,"wangc","aaaaaa","wangchao","buyer","wang@163.com"),
      (2,"huangfp","bbbbbb","huangfp","seller","huang@126.com"),
      (3,"zhang3","cccccc","zhangsan","buyer","zhang@163.com"),
      (4,"li4","dddddd","lisi","seller","li@1256.com");
create table s_orderform(

          o_id int auto_increment primary key,
         o_buyer_id int,
         o_seller_id int,
         o_totalprices double,
         o_state varchar(50),
         o_information varchar ( 200 ),
          foreign  key (o_buyer_id) references s_user(u_id), #external link to the u_id field of the s_user table
          foreign  key (o_seller_id) references s_user(u_id) #external link to the u_id field of the s_user table
) ENGINE=INNODB DEFAULT CHARSET=utf8 ;

Attempt to join with foreign key not in main table:

adding data

insert into s_orderform values (4,1,1,12.3,"pending","1234567");
insert into s_orderform values (2,3,2,12.3,"pending","1234567");
insert into s_orderform values (3,4,1,12.3,"pending","1234567");

Now try to delete the data in the main table s_user

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325672976&siteId=291194637