900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > sql创建数据库 设置主键外键 插入记录

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

时间:2019-05-28 07:01:29

相关推荐

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

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

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

一、供应商Vendors表

1.1表描述

在我本机的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表描述

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.

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@'),('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@'),('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@'),('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard',' ');

四、Orders表

4.1表描述

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

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, '-05-01', '1000000001'), -> (20006, '-01-12', '1000000003'), -> (20007, '-01-30', '1000000004'), -> (20008, '-02-03', '1000000005'), -> (20009, '-02-08', '1000000001');Query OK, 5 rows affected (0.01 sec)Records: 5 Duplicates: 0 Warnings: 0

五、OrderItems表

5.1描述表

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

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)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。