用户表(user)
create table `user` (
`id` int auto_increment primary key,
`username` varchar(50) -- 用户姓名
);
-- 订单表(orders)
create table `orders` (
`id` int auto_increment primary key,
`price` double,
`user_id` int
);
-- 给订单表添加外键约束
alter table orders add constraint user_fk foreign key (user_id) references user(id);
-- 向user表中添加数据
insert into user values(3,'张三');
insert into user values(4,'李四');
insert into user values(5,'王五');
insert into user values(6,'赵六');
-- 向orders 表中插入数据
insert into orders values(1,1314,3);
insert into orders values(2,1314,3);
insert into orders values(3,15,4);
insert into orders values(4,315,5);
insert into orders values(5,1014,null);
---------------------
作者:如晴天-似雨天
来源:CSDN
原文:https://blog.csdn.net/puyu9714/article/details/82872984
版权声明:本文为博主原创文章,转载请附上博文链接!