创建一个SQL测试数据库 - 消费者数据库

创建TABLES

  • create_tables-bpsimple.sql
CREATE TABLE customer ( 
    customer_id serial , 
    title char(4) ,
    fname varchar(32) , 
    lname varchar(32) NOT NULL, 
    addressline varchar(64) , 
    town varchar(32) , 
    zipcode char(10) NOT NULL, 
    phone varchar(16) , 
    CONSTRAINT customer_pk PRIMARY KEY(customer_id) 
);

CREATE TABLE item ( 
    item_id serial , 
    description varchar(64) NOT NULL, 
    cost_price numeric(7,2) , 
    sell_price numeric(7,2) , 
    CONSTRAINT item_pk PRIMARY KEY(item_id) 
);

CREATE TABLE orderinfo ( 
    orderinfo_id serial , 
    customer_id integer NOT NULL, 
    date_placed date NOT NULL, 
    date_shipped date , 
    shipping numeric(7,2) , 
    CONSTRAINT orderinfo_pk PRIMARY KEY(orderinfo_id) 
); 

CREATE TABLE stock ( 
    item_id integer NOT NULL, 
    quantity integer NOT NULL, 
    CONSTRAINT stock_pk PRIMARY KEY(item_id) 
);

CREATE TABLE orderline ( 
    orderinfo_id integer NOT NULL,
    item_id integer NOT NULL, 
    quantity integer NOT NULL, 
    CONSTRAINT orderline_pk PRIMARY KEY(orderinfo_id, item_id) 
); 

CREATE TABLE barcode ( 
    barcode_ean char(13) NOT NULL, 
    item_id integer NOT NULL, 
    CONSTRAINT barcode_pk PRIMARY KEY(barcode_ean) 
);

填充TABLES

  • customer.sql
INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Miss','Jenny','Stones','27 Rowan Avenue','Hightown','NT2 1AQ','023 9876'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Andrew','Stones','52 The Willows','Lowtown','LT5 7RA','876 3527'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Miss','Alex','Matthew','4 The Street','Nicetown','NT2 2TX','010 4567'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Adrian','Matthew','The Barn','Yuleville','YV67 2WR','487 3871'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Simon','Cozens','7 Shady Lane','Oakenham','OA3 6QW','514 5926'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Neil','Matthew','5 Pasture Lane','Nicetown','NT3 7RT','267 1232'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Richard','Stones','34 Holly Way','Bingham','BG4 2WE','342 5982'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mrs','Ann','Stones','34 Holly Way','Bingham','BG4 2WE','342 5982'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mrs','Christine','Hickman','36 Queen Street','Histon','HT3 5EM','342 5432'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Mike','Howard','86 Dysart Street','Tibsville','TB3 7FG','505 5482'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Dave','Jones','54 Vale Rise','Bingham','BG3 8GD','342 8264'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Richard','Neill','42 Thatched Way','Winersby','WB3 6GQ','505 6482'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mrs','Laura','Hardy','73 Margarita Way','Oxbridge','OX2 3HX','821 2335'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','Bill','O\'Neill','2 Beamer Street','Welltown','WT3 8GM','435 1234'); 

INSERT INTO customer(title, fname, lname, addressline, town, zipcode, phone) 
VALUES('Mr','David','Hudson','4 The Square','Milltown','MT2 6RT','961 4526');
  • item.sql
INSERT INTO item(description, cost_price, sell_price) VALUES('Wood Puzzle', 15.23, 21.95); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Rubik Cube', 7.45, 11.49); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Linux CD', 1.99, 2.49); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Tissues', 2.11, 3.99); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Picture Frame', 7.54, 9.95); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Fan Small', 9.23, 15.75); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Fan Large', 13.36, 19.95); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Toothbrush', 0.75, 1.45); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Roman Coin', 2.34, 2.45); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Carrier Bag', 0.01, 0.0); 

INSERT INTO item(description, cost_price, sell_price) VALUES('Speakers', 19.73, 25.32);
  • orderinfo.sql
INSERT INTO orderinfo(customer_id, date_placed, date_shipped, shipping) 
VALUES(3,'03-13-2000','03-17-2000', 2.99); 

INSERT INTO orderinfo(customer_id, date_placed, date_shipped, shipping) 
VALUES(8,'06-23-2000','06-24-2000', 0.00); 

INSERT INTO orderinfo(customer_id, date_placed, date_shipped, shipping) 
VALUES(15,'09-02-2000','09-12-2000', 3.99); 

INSERT INTO orderinfo(customer_id, date_placed, date_shipped, shipping) 
VALUES(13,'09-03-2000','09-10-2000', 2.99); 

INSERT INTO orderinfo(customer_id, date_placed, date_shipped, shipping) 
VALUES(8,'07-21-2000','07-24-2000', 0.00);
  • stock.sql
INSERT INTO stock(item_id, quantity) VALUES(1,12); 

INSERT INTO stock(item_id, quantity) VALUES(2,2);

INSERT INTO stock(item_id, quantity) VALUES(4,8); 

INSERT INTO stock(item_id, quantity) VALUES(5,3); 

INSERT INTO stock(item_id, quantity) VALUES(7,8); 

INSERT INTO stock(item_id, quantity) VALUES(8,18); 

INSERT INTO stock(item_id, quantity) VALUES(10,1);
  • orderline.sql
INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(1, 4, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(1, 7, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(1, 9, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(2, 1, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(2, 10, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(2, 7, 2); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(2, 4, 2); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(3, 2, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(3, 1, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(4, 5, 2); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(5, 1, 1); 

INSERT INTO orderline(orderinfo_id, item_id, quantity) VALUES(5, 3, 1);
  • barcode.sql
INSERT INTO barcode(barcode_ean, item_id) VALUES('6241527836173', 1); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('6241574635234', 2); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('6264537836173', 3); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('6241527746363', 3); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('7465743843764', 4); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('3453458677628', 5); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('6434564564544', 6); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('8476736836876', 7); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('6241234586487', 8); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('9473625532534', 8);

INSERT INTO barcode(barcode_ean, item_id) VALUES('9473627464543', 8); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('4587263646878', 9); 

INSERT INTO barcode(barcode_ean, item_id) VALUES('9879879837489', 11);

INSERT INTO barcode(barcode_ean, item_id) VALUES('2239872376872', 11);

移除表

  • drop_tables.sql
DROP TABLE barcode; 
DROP TABLE orderline; 
DROP TABLE stock; 
DROP TABLE orderinfo; 
DROP TABLE item; 
DROP TABLE customer; 
DROP SEQUENCE customer_customer_id_seq; 
DROP SEQUENCE item_item_id_seq; 
DROP SEQUENCE orderinfo_orderinfo_id_seq;

- 摘自《PostgreSQL从入门到精通》

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQL是高级的非过程化编程语言,是沟通数据库服务器和客户端的重要工具,允许用户在高层数据结构上工作。它不要求用户指定对数据的存放方法,也不需要用户了解具体的数据存放方式,所以,具有完全不同底层结构的不同数据库系统,可以使用相同的SQL语言作为数据输入与管理的SQL接口。 它以记录集合作为操作对象,所有SQL语句接受集合作为输入,返回集合作为输出,这种集合特性允许一条SQL语句的输出作为另一条SQL语句的输入,所以SQL语句可以嵌套,这使它具有极大的灵活性和强大的功能,在多数情况下,在其他语言中需要一大段程序实现的功能只需要一个SQL语句就可以达到目的,这也意味着用SQL语言可以写出非常复杂的语句。    结构化查询语言(Structured Query Language)最早是IBM的圣约瑟研究实验室为其关系数据库管理系统SYSTEM R开发的一种查询语言,它的前身是SQUARE语言。SQL语言结构简洁,功能强大,简单易学,所以自从IBM公司1981年推出以来,SQL语言得到了广泛的应用。如今无论是像Oracle、Sybase、DB2、Informix、SQL Server这些大型的数据库管理系统,还是像Visual Foxpro、PowerBuilder这些PC上常用的数据库开发系统,都支持SQL语言作为查询语言。    美国国家标准局(ANSI)与国际标准化组织(ISO)已经制定了SQL标准。ANSI是一个美国工业和商业集团组织,负责开发美国的商务和通讯标准。ANSI同时也是ISO和International Electrotechnical Commission(IEC)的成员之一。ANSI 发布与国际标准组织相应的美国标准。1992年,ISO和IEC发布了SQL国际标准,称为SQL-92。ANSI随之发布的相应标准是ANSI SQL-92。ANSI SQL-92有时被称为ANSI SQL。尽管不同的关系数据库使用的SQL版本有一些差异,但大多数都遵循 ANSI SQL 标准。SQL Server使用ANSI SQL-92的扩展集,称为T-SQL,其遵循ANSI制定的 SQL-92标准。    SQL语言包含4个部分:    数据定义语言(DDL),例如:CREATE、DROP、ALTER等语句。    数据操作语言(DML),例如:INSERT(插入)、UPDATE(修改)、DELETE(删除)语句。    数据查询语言(DQL),例如:SELECT语句。    数据控制语言(DCL),例如:GRANT、REVOKE、COMMIT、ROLLBACK等语句。    SQL语言包括三种主要程序设计语言类别的语句:数据定义语言(DDL),数据操作语言(DML)及数据控制语言(DCL)。
package com.bjsxt.servlet; import com.bjsxt.entity.User; import com.bjsxt.service.UserService; import com.bjsxt.service.impl.UserServiceImpl; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.net.URLEncoder; import java.sql.Date; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class UserServlet extends BaseServlet { // @Override // protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // //解决POST表单的中文乱码问题 // request.setCharacterEncoding("utf-8"); // //接收method属性的值 // String methodName = request.getParameter("method"); // // //根据method属性的值调用相应的方法 // if("login".equals(methodName)){ // this.login(request,response); // }else if("register".equals(methodName)){ // this.register(request,response); // }else if("logout".equals(methodName)){ // this.logout(request,response); // } // // } public void show(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取表单的数据 String userId = request.getParameter("userId"); if(userId == null){ userId = ""; } String strAge = request.getParameter("minAge"); int minAge = 0; try{ minAge = Integer.parseInt(strAge); //"12" "abc" }catch(NumberFormatException e){ e.printStackTrace(); } //调用业务层完成查询操作 UserService userService = new UserServiceImpl(); //List<User> userList = userService.findAll(); List<User> userList = userService.find(userId,minAge); //List<User> userList = null; //List<User> userList = new ArrayList<User>(); //跳转到show.jsp显示数据 request.setAttribute("userId",userId); request.setAttribute("minAge",strAge); request.setAttribute("ulist",userList); request.getRequestDispatcher("/admin/show.jsp").forward(request,response); } public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //结束当前的session request.getSession().invalidate(); //跳转回登录页面 response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //request.setCharacterEncoding("utf-8"); //1.接收来自视图层的表单数据 String userId = request.getParameter("userId"); String realName = request.getParameter("realName"); String pwd = request.getParameter("pwd"); String rePwd = request.getParameter("repwd"); int age = Integer.parseInt(request.getParameter("age"));// "23" String [] hobbyArr = request.getParameterValues("hobby"); String strDate = request.getParameter("enterDate");//"1999-12-23" Date enterDate = Date.valueOf(strDate); //util.Date SimpleDateFormat //判断两次密码是否相同 if(pwd == null || !pwd.equals(rePwd)){ request.setAttribute("error","两次密码必须相同"); request.getRequestDispatcher("/admin/register.jsp").forward(request,response); return; } //2.调用业务层完成注册操作并返回结果 User user = new User(userId,realName,pwd,age, Arrays.toString(hobbyArr),enterDate); UserService userService = new UserServiceImpl(); int n = userService.register(user); //3.根据结果进行页面跳转 if(n>0){ response.sendRedirect(request.getContextPath()+"/admin/login.jsp"); }else{ request.setAttribute("error","注册失败"); request.getRequestDispatcher("/admin/register.jsp").forward(request,response); } } public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //解决POST表单的中文乱码问题 //request.setCharacterEncoding("utf-8"); //获取用户名和密码 request 内建对象 请求 String username = request.getParameter("username"); String password = request.getParameter("password"); String rememberme = request.getParameter("rememberme"); //调用下一层判断登录是否成功,并返回结果 //进行服务器端的表单验证 if(username ==null || "".equals(username)){ request.setAttribute("error","用户名不能为空JSP"); request.getRequestDispatcher("/admin/login.jsp").forward(request,response); return; } if (username.length()<=6){ request.setAttribute("error","用户名长度必须大于6JSP"); request.getRequestDispatcher("/admin/login.jsp").forward(request,response);//后面语句还会执行 return; //后面的语句不再执行 } // boolean flag = false;//默认失败 // if(username.indexOf("sxt")>=0 || username.contains("尚学堂")){ // flag = true; // } User user = null;//默认登录失败 // UserDao userDao = new UserDaoImpl(); // user = userDao.find(username,password); UserService userService = new UserServiceImpl(); user = userService.login(username,password); //userService.addOrder("shoppingCart"); //输出结果 if(user != null){ //登录成功才记住我 //1.办理会员卡 String username2 = URLEncoder.encode(username,"utf-8"); Cookie cookie1 = new Cookie("uname",username2); Cookie cookie2 = new Cookie("password",password); //2.指定会员卡的作用范围,默认范围是当前目录 /servlet/LoginServlet /admin/login.jsp //cookie1.setPath("/"); //当前服务器 cookie1.setPath("/myservlet2/"); //当前项目 cookie2.setPath("/myservlet2"); //3.指定会员卡的作用时间 if("yes".equals(rememberme)){ cookie1.setMaxAge(60*60*24*10); //默认的时间浏览器不关闭的时间;-1 表示一直有效 cookie2.setMaxAge(60*60*24*10); }else{ cookie1.setMaxAge(0); cookie2.setMaxAge(0); } //4.将会员卡带回家 response.addCookie(cookie1); response.addCookie(cookie2); //成功跳转到成功页面 //out.println("登录成功"); // /servlet/LoginServlet // /servlet/success.jsp // request.getRequestDispatcher("/admin/success.jsp").forward(request,response); HttpSession session = request.getSession(); // session.setAttribute("username",username); session.setAttribute("user",user); //response.sendRedirect("/myservlet2/admin/success.jsp"); //response.sendRedirect("https://www.bjsxt.com:443/news/11377.html"); //response.sendRedirect("http://localhost:8080/myservlet2/admin/success.jsp"); //response.sendRedirect("/myservlet2/admin/success.jsp"); //response.sendRedirect("/myservlet2/admin/success.jsp"); //response.sendRedirect(request.getContextPath()+"/admin/success.jsp"); //http://192.168.58.250:8080/myservlet2/servlet/LoginServlet //http://192.168.58.250:8080/myservlet2/admin/success.jsp //登录成功后,网站的访问人数+1 //1.获取当前的访问人数 ServletContext context = this.getServletContext(); Integer count2 = (Integer) context.getAttribute("count"); //2.人数+1 if(count2 == null){ //第一个用户 count2 = 1; }else{ count2++; } //3.再存放到application作用域中 context.setAttribute("count",count2); //http://192.168.58.250:8080/myservlet2/servlet/admin/success.jsp response.sendRedirect("../admin/success.jsp"); }else{ //失败跳转回登录页面 //out.println("登录失败"); request.setAttribute("error","用户名或者密码错误"); // RequestDispatcher rd = request.getRequestDispatcher("/admin/login.jsp"); // rd.forward(request,response); //RequestDispatcher rd = request.getRequestDispatcher("http://localhost:8080/myservlet2/admin/login.jsp"); //RequestDispatcher rd = request.getRequestDispatcher("/admin/login.jsp"); //http://192.168.58.250:8080/myservlet2/servlet/admin/login.jsp RequestDispatcher rd = request.getRequestDispatcher("../admin/login.jsp"); rd.forward(request,response); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值