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', '[email protected]'), ('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green',' '), ('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', '[email protected]'), ('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', '[email protected]'), ('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)

你可能感兴趣的:(数据库)