`
BabyDuncan
  • 浏览: 584350 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

做数据库练习的时候可以用到的脚本(例子数据库)

阅读更多
首先是建立数据库的脚本
--------------------------------------------
-- Sams Teach Yourself SQL in 10 Minutes
-- http://www.forta.com/books/0672325675/
-- Example table creation scripts for MySQL.
--------------------------------------------


-------------------------
-- Create customers table
-------------------------
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 
);

--------------------------
-- Create orderItems table
--------------------------
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 
);


----------------------
-- Create orders table
----------------------
CREATE TABLE orders
(
  order_num  int      NOT NULL ,
  order_date datetime NOT NULL ,
  cust_id    char(10) NOT NULL 
);

------------------------
-- Create products table
------------------------
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 
);

-----------------------
-- Create vendors table
-----------------------
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 ,
  vend_country char(50) NULL 
);


----------------------
-- Define primary keys
----------------------
ALTER TABLE customers ADD PRIMARY KEY (cust_id);
ALTER TABLE orderItems ADD PRIMARY KEY (order_num, order_item);
ALTER TABLE orders ADD PRIMARY KEY (order_num);
ALTER TABLE products ADD PRIMARY KEY (prod_id);
ALTER TABLE vendors ADD PRIMARY KEY (vend_id);


----------------------
-- Define foreign keys
----------------------
ALTER TABLE orderItems ADD CONSTRAINT FK_orderItems_orders FOREIGN KEY (order_num) REFERENCES orders (order_num);
ALTER TABLE orderItems ADD CONSTRAINT FK_orderItems_products FOREIGN KEY (prod_id) REFERENCES products (prod_id);
ALTER TABLE orders ADD CONSTRAINT FK_orders_customers FOREIGN KEY (cust_id) REFERENCES customers (cust_id);
ALTER TABLE products ADD CONSTRAINT FK_products_vendors FOREIGN KEY (vend_id) REFERENCES vendors (vend_id);


下面是往里面添加数据的脚本
----------------------------------------------
-- Sams Teach Yourself SQL in 10 Minutes
-- http://www.forta.com/books/0672325675/
-- Example table population scripts for MySQL.
----------------------------------------------


---------------------------
-- Populate customers table
---------------------------
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');
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES('1000000002', 'Kids Place', '333 South Lake Drive', 'Columbus', 'OH', '43333', 'USA', 'Michelle Green');
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES('1000000003', 'Fun4All', '1 Sunny Place', 'Muncie', 'IN', '42222', 'USA', 'Jim Jones', 'jjones@fun4all.com');
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
VALUES('1000000004', 'Fun4All', '829 Riverside Drive', 'Phoenix', 'AZ', '88888', 'USA', 'Denise L. Stephens', 'dstephens@fun4all.com');
INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
VALUES('1000000005', 'The Toy Store', '4545 53rd Street', 'Chicago', 'IL', '54545', 'USA', 'Kim Howard');

-------------------------
-- Populate vendors table
-------------------------
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');
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES('BRE02','Bear Emporium','500 Park Street','Anytown','OH','44333', 'USA');
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES('DLL01','Doll House Inc.','555 High Street','Dollsville','CA','99999', 'USA');
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES('FRB01','Furball Inc.','1000 5th Avenue','New York','NY','11111', 'USA');
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES('FNG01','Fun and Games','42 Galaxy Road','London', NULL,'N16 6PS', 'England');
INSERT INTO vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
VALUES('JTS01','Jouets et ours','1 Rue Amusement','Paris', NULL,'45678', 'France');

--------------------------
-- Populate products table
--------------------------
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');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('BR02', 'BRS01', '12 inch teddy bear', 8.99, '12 inch teddy bear, comes with cap and jacket');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('BR03', 'BRS01', '18 inch teddy bear', 11.99, '18 inch teddy bear, comes with cap and jacket');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('BNBG01', 'DLL01', 'Fish bean bag toy', 3.49, 'Fish bean bag toy, complete with bean bag worms with which to feed it');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('BNBG02', 'DLL01', 'Bird bean bag toy', 3.49, 'Bird bean bag toy, eggs are not included');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('BNBG03', 'DLL01', 'Rabbit bean bag toy', 3.49, 'Rabbit bean bag toy, comes with bean bag carrots');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('RGAN01', 'DLL01', 'Raggedy Ann', 4.99, '18 inch Raggedy Ann doll');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('RYL01', 'FNG01', 'King doll', 9.49, '12 inch king doll with royal garments and crown');
INSERT INTO products(prod_id, vend_id, prod_name, prod_price, prod_desc)
VALUES('RYL02', 'FNG01', 'Queen doll', 9.49, '12 inch queen doll with royal garments and crown');

------------------------
-- Populate orders table
------------------------
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20005, '2004-05-01', '1000000001');
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20006, '2004-01-12', '1000000003');
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20007, '2004-01-30', '1000000004');
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20008, '2004-02-03', '1000000005');
INSERT INTO orders(order_num, order_date, cust_id)
VALUES(20009, '2004-02-08', '1000000001');

----------------------------
-- Populate orderItems table
----------------------------
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 1, 'BR01', 100, 5.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20005, 2, 'BR03', 100, 10.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 1, 'BR01', 20, 5.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 2, 'BR02', 10, 8.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20006, 3, 'BR03', 10, 11.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 1, 'BR03', 50, 11.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 2, 'BNBG01', 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 3, 'BNBG02', 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 4, 'BNBG03', 100, 2.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20007, 5, 'RGAN01', 50, 4.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 1, 'RGAN01', 5, 4.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 2, 'BR03', 5, 11.99);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 3, 'BNBG01', 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 4, 'BNBG02', 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20008, 5, 'BNBG03', 10, 3.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 1, 'BNBG01', 250, 2.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 2, 'BNBG02', 250, 2.49);
INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
VALUES(20009, 3, 'BNBG03', 250, 2.49);


注释的地方可能会引起报错,有人用的话自己去掉吧
分享到:
评论

相关推荐

    Mastering.Oracle.SQL英文第二版及所用到的示例数据表创建脚本

    在这些表中填充数据后,读者可以按照书中的例子进行练习,亲手执行SQL查询,理解并掌握各种技术。修复了中文Oracle日期转换问题的脚本,确保了在处理中文日期数据时的正确性和兼容性,这对于在中国地区工作的数据库...

    asp练习题目

    通过这些练习,你可以深入理解ASP.NET的网页开发流程,包括数据库操作、页面交互和数据展示,这些都是构建动态Web应用的基础。不断练习,你的ASP技能将会逐渐熟练,从而能更高效地开发出功能丰富的Web应用。

    ASP实例开发源码-深山留言板系统 7.22.zip

    ASP可以使用ADO(ActiveX Data Objects)来连接和查询数据库,如Access或SQL Server。 4. **表单处理**:在HTML表单中,用户输入的数据会被发送到服务器。ASP脚本会解析这些数据,进行验证,然后保存到数据库中。 ...

    只对新用户计数的计数器

    在JSP中,我们可以编写脚本来获取客户端的IP地址(通过`HttpServletRequest`的`getRemoteAddr()`方法),然后传递给后台的JavaBean(如`UserCounter`)进行处理。 3. **数据库集成**: - 长期统计和持久化数据通常...

    ASP编程常用的代码

    在《asp精华集》这个压缩包中,很可能包含了关于这些主题的示例代码、教程或练习,可以帮助你进一步理解和应用ASP编程技术。通过学习这些内容,你可以提升在ASP环境下处理数据库的能力,为构建动态网站打下坚实的...

    jsp+servlet的增删改查

    - **读取(Read)**:Servlet可以获取数据库中的数据,然后传递给JSP展示。 - **更新(Update)**:当用户编辑现有记录并保存时,Servlet会更新数据库中的相应条目。 - **删除(Delete)**:用户选择删除某条记录,...

    精品资料(2021-2022年收藏)高二信息技术选择题练习二模板.doc

    9. 弹出问候语窗口的技术:这通常通过JavaScript实现,JavaScript可以编写脚本来实现各种动态效果,包括弹窗。选项B CSS样式表用于控制样式,C Flash动画用于创建动画,D GIF动画是静态图像的动画形式,不涉及交互。...

    毕业设计-通用英语辅导网站

    首先,我们可以从“通用英语辅导”这个主题推测,网站可能包括了各种英语学习模块,如词汇、语法、听力、阅读、写作和口语练习等。这些功能的实现需要数据库存储大量的教学资料,以及前端界面展示和交互设计,使得...

    简单易懂的ASP程序

    ASP(Active Server Pages)是一种由微软开发的服务器端脚本环境,用于生成动态网页或Web应用程序。这个"简单易懂的ASP程序"很可能是为了帮助初学者理解和掌握ASP的基本概念和操作,包括如何添加、删除和修改数据,...

    简易的BBS论坛系统

    这一过程涉及数据库操作,通常会用到如MySQL等关系型数据库来存储帖子内容、作者信息、发布时间等关键数据。用户界面则通过HTTP请求与服务器交互,常见的技术栈可能是HTML、CSS和JavaScript,后端可能使用PHP、...

    PHP实例开发源码—Essay php+html日志程序.zip

    3. 数据库交互:如果日志存储在数据库中,可能需要用到`PDO`或`mysqli`扩展进行SQL查询,例如插入、检索和更新日志条目。 4. HTTP请求处理:理解`$_GET`,`$_POST`等超全局变量,以及`$_SERVER`数组中的信息,用于...

    JScript.NET最简(源码)留言本_网站在线聊天留言源码.rar

    【标题】"JScript.NET最简...网站在线聊天留言源码.rar"是一个宝贵的教育资源,对于想要提升.NET Web开发技能的开发者来说,提供了实战练习的机会,同时也是了解JScript.NET语言特性和.NET Framework集成的好例子。

    ArcGIS中的地理处理

    - **练习5:找到受拟建公路影响的栖息地**:结合前几个练习的知识,用户可以评估未来道路建设对周边生态环境的影响程度,并采取相应措施进行缓解。 #### 第三章 地理处理基础 - **地理处理概述**:这部分内容涵盖...

    《Python程序设计与算法基础教程(第二版)》江红余青松,第十章课后习题答案.pdf

    最后,案例研究提到了基于模块的库存管理系统,这可能涉及到数据库操作、类和对象的使用,以及更复杂的数据结构和控制流程,是将所学知识应用于实际问题的一个例子。 总的来说,这些练习涵盖了Python的基础语法、...

    NETWORK.rar_Java编程_Java_

    1. "localhost.sql":这通常是一个数据库脚本文件,可能包含了网站所需的数据库结构和初始数据。在Java Web开发中,开发者通常会使用如MySQL这样的关系型数据库来存储和检索数据。用户可以通过导入这个SQL文件来创建...

    first_app:Ruby on Rails 教程的第一个应用程序

    在Ruby on Rails教程中,"first_app"是一个典型的起步项目,用于引导初学者熟悉Rails框架的基本概念和...在这个过程中,不断练习和理解MVC模式、路由、数据库操作以及前端技术的集成,将使你成为更熟练的Rails开发者。

    latihan-ukk

    "latihan-ukk"可能通过一个简单的例子展示了如何实现MVC。 9. **PHP框架**:虽然"latihan-ukk"没有明确提到框架,但有可能使用了像Laravel、CodeIgniter或Symfony等流行的PHP框架,这些框架提供了便利的工具和最佳...

Global site tag (gtag.js) - Google Analytics