sql创建数据库、设置主键外键、插入记录

在数据库中创建一个玩具经销商订单录入系统。这些表用来完成下面几项任务:

  • 管理供应商
  • 管理产品目录
  • 管理顾客列表
  • 录入顾客订单

一、供应商Vendors表

1.1表描述

说明
vend_id唯一但供应商id
vend_name供应商名
vend_address供应商地址
vend_city供应商所在城市
vend_state供应商所在州
vend_zip供应商地址邮政编码
vend_country供应商所在国家

在我本机的mysql中创建一个名为toyshop的数据库:

mysql> create database toyshop;

1.2创建表

创建venderos表

mysql> create table Vendors (vend_id char(10) NOT NULL,
    -> vend_name char(50) NOT NULL,vend_address char(50) NULL,
    -> vend_city char(50) NULL,vend_state char(5) NULL,
    -> vend_zip char(10) NULL,vender_country char(50) NULL);
Query OK, 0 rows affected (0.02 sec)

1.3设置主键

alter table vendors add primary key (vend_id);

1.4插入数据

insert into Vendors(vend_id,vend_name,vend_address,vend_city,vend_state,vend_zip,vend_country) VALUES('BRS01','Bears R Us','123 Main Street','Bear Town','MI','44444', 'USA'),('BRE02','Bear Emporium','500 Park Street','Anytown','OH','44333', 'USA'),('DLL01','Doll House Inc.','555 High Street','Dollsville','CA','99999', 'USA'),('FRB01','Furball Inc.','1000 5th Avenue','New York','NY','11111', 'USA'),('FNG01','Fun and Games','42 Galaxy Road','London', NULL,'N16 6PS', 'England'),('JTS01','Jouets et ours','1 Rue Amusement','Paris', NULL,'45678', 'France');

二、Products表

2.1表描述

说明
prod_id唯一的产品ID
vend_id产品供应商ID
prod_name产品名
prod_price产品价格
prod_desc产品描述

2.2创建表

mysql> create table Products(prod_id char(10) NOT NUll,
    -> vend_id char(10) NOT NULL,prod_name char(255) NOT NULL,
    -> prod_price decimal(8,2) NOT NULL,
    -> prod_desc text NULL);
Query OK, 0 rows affected (0.01 sec)

2.3设置主键

alter table products add primary key (prod_id);

2.4插入数据

mysql> insert into Products(prod_id,vend_id,prod_name,prod_price,prod_desc) values('BR01', 'BRS01', '8 inch teddy bear', 5.99, '8 inch teddy bear, comes with cap and jacket'),('BR02', 'BRS01', '12 inch teddy bear', 8.99, '12 inch teddy bear, comes with cap and jacket'),('BR03', 'BRS01', '18 inch teddy bear', 11.99, '18 inch teddy bear, comes with cap and jacket'),('BNBG01', 'DLL01', 'Fish bean bag toy', 3.49, 'Fish bean bag toy, complete with bean bag worms with which to feed it'),('BNBG02', 'DLL01', 'Bird bean bag toy', 3.49, 'Bird bean bag toy, eggs are not included'),('BNBG03', 'DLL01', 'Rabbit bean bag toy', 3.49, 'Rabbit bean bag toy, comes with bean bag carrots'),('RGAN01', 'DLL01', 'Raggedy Ann', 4.99, '18 inch Raggedy Ann doll'),('RYL01', 'FNG01', 'King doll', 9.49, '12 inch king doll with royal garments and crown'),('RYL02', 'FNG01', 'Queen doll', 9.49, '12 inch queen doll with royal garments and crown');
Query OK, 9 rows affected (0.00 sec)
Records: 9  Duplicates: 0  Warnings: 0

三、Customers表

3.1表描述

Customers表存储所有顾客的信息,每个顾客有唯一的ID.

说明
cust_id唯一的顾客ID
cust_name顾客名
cust_address顾客地址
cust_city顾客所在的城市
cust_state顾客所在州
cust_zip顾客地址邮政编码
cust_country顾客所在国家
cust_contact顾客的联系名
cust_email顾客的邮件地址

3.2创建表


mysql> create table Customers(cust_id char(10) NOT NULL,
    -> cust_name char(50) NOT NULL,
    -> cust_address char(50) NULL,
    -> cust_city char(50) NULL,
    -> cust_state char(5) NULL,cust_zip char(10) NULL,cust_country char(50) NULL,
    -> cust_contact char(50) NULL,cust_email char(255) NULL);
Query OK, 0 rows affected (0.02 sec)

3.3设置主键

alter table Customers add primary key (cust_id);

3.4插入数据


INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com'),
('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green',' '),
('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com'),
('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com'),
('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard',' ')
;

四、Orders表

4.1表描述

Orders表存储顾客订单,每个订单唯一编号。

说明
order_num唯一的订单号
order_date订单日期
cust_id订单顾客ID

4.2创建表


mysql> create table Orders(order_num int NOT NULL,order_date datetime NOT NULL,cust_id char(10) NOT NULL);
Query OK, 0 rows affected (0.02 sec)

4.3 设置主键

alter table orders add primary key(order_num);

4.4插入数据

mysql> INSERT INTO Orders(order_num, order_date, cust_id)
    -> values (20005, '2004-05-01', '1000000001'),
    -> (20006, '2004-01-12', '1000000003'),
    -> (20007, '2004-01-30', '1000000004'),
    -> (20008, '2004-02-03', '1000000005'),
    -> (20009, '2004-02-08', '1000000001');
Query OK, 5 rows affected (0.01 sec)
Records: 5  Duplicates: 0  Warnings: 0

五、OrderItems表

5.1描述表

OrderItems表存储每个订单中的实际物品。

说明
order_num订单号
order_item订单物品号
order_id产品ID
quantity物品数量
item_price物品价格

5.2创建表

mysql> create table OrderItems( order_num int NOT NULL,order_item int NOT NULL,prod_id char(10) NOT NULL,
    -> quantity int NOT NULL,item_price decimal(8.2) NOT NULL);
Query OK, 0 rows affected (0.01 sec)

5.3 设置主键

 ALTER TABLE OrderItems add primary key (order_num,order_item);

5.4插入数据

mysql> INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
    -> VALUES(20005, 1, 'BR01', 100, 5.49),
    -> (20005, 2, 'BR03', 100, 10.99),
    -> (20006, 1, 'BR01', 20, 5.99),
    -> (20006, 2, 'BR02', 10, 8.99),
    -> (20006, 3, 'BR03', 10, 11.99),
    -> (20007, 1, 'BR03', 50, 11.49),
    -> (20007, 2, 'BNBG01', 100, 2.99),
    -> (20007, 3, 'BNBG02', 100, 2.99),
    -> (20007, 4, 'BNBG03', 100, 2.99),
    -> (20007, 5, 'RGAN01', 50, 4.49),
    -> (20008, 1, 'RGAN01', 5, 4.99),
    -> (20008, 2, 'BR03', 5, 11.99),
    -> (20008, 3, 'BNBG01', 10, 3.49),
    -> (20008, 4, 'BNBG02', 10, 3.49),
    -> (20008, 5, 'BNBG03', 10, 3.49),
    -> (20009, 1, 'BNBG01', 250, 2.49),
    -> (20009, 2, 'BNBG02', 250, 2.49),
    -> (20009, 3, 'BNBG03', 250, 2.49);
Query OK, 18 rows affected, 18 warnings (0.00 sec)
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

esc_ai

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值