MySQL8.0-外键约束

1、创建订单表(ordes)

订单表(orders)
orders(id,customerId,saleId,orderDate,notes)
orders表数据

订单ID客户ID销售员ID订单日期备注
10001132015-05-15
10002222015-05-16
10003322015-05-16
10004242015-05-19

说明:设置id为主键;为customerId字段设置外键约束,参照客户表(customer)的id字段(不执行任何操作);为saleId字段设置外键约束。

-- 订单表(orders)
CREATE TABLE orders(
	id INT UNSIGNED NOT NULL COMMENT '订单ID',
	customerId INT UNSIGNED NOT NULL COMMENT'客户ID',
	saleId INT UNSIGNED NOT NULL COMMENT'单价',
	orderDate date COMMENT'订单日期',
	notes VARCHAR(50) COMMENT'备注',
	PRIMARY KEY(id),
	FOREIGN KEY(customerId) REFERENCES customer(id),
	FOREIGN KEY(saleId) REFERENCES seller(id)
)ENGINE=INNODB DEFAULT charset=utf8mb4;
INSERT orders(id,customerId,saleId,orderDate,notes)
VALUE('10001','1','3','2015-05-15',''),
('10002','2','2','2015-05-16',''),
('10003','3','2','2015-05-16',''),
('10004','2','4','2015-05-19','');

2、创建订单明细表(orderDetail)

orderDetail(id,orderId,productId,quantity,totalMoney)
orderDetail表数据

ID订单ID商品ID订货数量订货总额
11000132276492.2
2100016335670
31000110248520.8
41000211723096
51000232206292
61000311152070
71000372803864
81000421136949.5
91000473394678.2
101000410325682.5

说明:设置id为主键;为productId字段设置外键约束。

-- 订单明细表 (orderdetail)
CREATE TABLE orderDetail(
	id INT UNSIGNED NOT NULL auto_increment COMMENT 'ID',
	orderId CHAR(10) COMMENT'订单ID',
	productId INT UNSIGNED COMMENT'商品ID',
	quantity CHAR(5) COMMENT'订货数量',
	totalMoney VARCHAR(9) COMMENT'单价',
	PRIMARY KEY(id),
	FOREIGN KEY(productId) REFERENCES product(id)
)ENGINE=INNODB DEFAULT charset=utf8mb4;
INSERT orderDetail(orderId,productId,quantity,totalMoney)
VALUE ('10001','3','227','6492.2'),
('10001','6','335','670'),
('10001','10','248','520.8'),
('10002','1','172','3096'),
('10002','3','220','6292'),
('10003','1','115','2070'),
('10003','7','280','3864'),
('10004','2','113','6949.5'),
('10004','7','339','4678.2'),
('10004','10','325','682.5');
  • 1
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页

打赏作者

面带笑容

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值