主要是看《SQL必知必会》第四版的书,而写的一些SQL笔记,红色的是方便以后查询的sql语句,工作中主要是使用mysql数据库,所以笔记也是围绕mysql而写的。

下文调试的数据表sql语句,如果要尝试的调试的话可以复制过去运行即可

  1 # Host: localhost  (Version: 5.5.40)
  2 # Date: 2017-05-05 13:45:46
  3 # Generator: MySQL-Front 5.3  (Build 4.120)
  4 
  5 /*!40101 SET NAMES utf8 */;
  6 
  7 #
  8 # Structure for table "customers"
  9 #
 10 
 11 DROP TABLE IF EXISTS `customers`;
 12 CREATE TABLE `customers` (
 13   `cust_id` char(10) NOT NULL,
 14   `cust_name` char(50) NOT NULL,
 15   `cust_address` char(50) DEFAULT NULL,
 16   `cust_city` char(50) DEFAULT NULL,
 17   `cust_state` char(5) DEFAULT NULL,
 18   `cust_zip` char(10) DEFAULT NULL,
 19   `cust_country` char(50) DEFAULT NULL,
 20   `cust_contact` char(50) DEFAULT NULL,
 21   `cust_email` char(255) DEFAULT NULL,
 22   PRIMARY KEY (`cust_id`)
 23 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 24 
 25 #
 26 # Data for table "customers"
 27 #
 28 
 29 /*!40000 ALTER TABLE `customers` DISABLE KEYS */;
 30 INSERT INTO `customers` 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',NULL),('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',NULL);
 31 /*!40000 ALTER TABLE `customers` ENABLE KEYS */;
 32 
 33 #
 34 # Structure for table "orderitems"
 35 #
 36 
 37 DROP TABLE IF EXISTS `orderitems`;
 38 CREATE TABLE `orderitems` (
 39   `order_num` int(11) NOT NULL,
 40   `order_item` int(11) NOT NULL,
 41   `prod_id` char(10) NOT NULL,
 42   `quantity` int(11) NOT NULL,
 43   `item_price` decimal(8,2) NOT NULL,
 44   PRIMARY KEY (`order_num`,`order_item`),
 45   KEY `FK_OrderItems_Products` (`prod_id`)
 46 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 47 
 48 #
 49 # Data for table "orderitems"
 50 #
 51 
 52 /*!40000 ALTER TABLE `orderitems` DISABLE KEYS */;
 53 INSERT INTO `orderitems` 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);
 54 /*!40000 ALTER TABLE `orderitems` ENABLE KEYS */;
 55 
 56 #
 57 # Structure for table "orders"
 58 #
 59 
 60 DROP TABLE IF EXISTS `orders`;
 61 CREATE TABLE `orders` (
 62   `order_num` int(11) NOT NULL,
 63   `order_date` datetime NOT NULL,
 64   `cust_id` char(10) NOT NULL,
 65   PRIMARY KEY (`order_num`),
 66   KEY `FK_Orders_Customers` (`cust_id`)
 67 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 68 
 69 #
 70 # Data for table "orders"
 71 #
 72 
 73 /*!40000 ALTER TABLE `orders` DISABLE KEYS */;
 74 INSERT INTO `orders` VALUES (20005,'2004-05-01 00:00:00','1000000001'),(20006,'2004-01-12 00:00:00','1000000003'),(20007,'2004-01-30 00:00:00','1000000004'),(20008,'2004-02-03 00:00:00','1000000005'),(20009,'2004-02-08 00:00:00','1000000001');
 75 /*!40000 ALTER TABLE `orders` ENABLE KEYS */;
 76 
 77 #
 78 # Structure for table "products"
 79 #
 80 
 81 DROP TABLE IF EXISTS `products`;
 82 CREATE TABLE `products` (
 83   `prod_id` char(10) NOT NULL,
 84   `vend_id` char(10) NOT NULL,
 85   `prod_name` char(255) NOT NULL,
 86   `prod_price` decimal(8,2) NOT NULL,
 87   `prod_desc` text,
 88   PRIMARY KEY (`prod_id`),
 89   KEY `FK_Products_Vendors` (`vend_id`)
 90 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
 91 
 92 #
 93 # Data for table "products"
 94 #
 95 
 96 /*!40000 ALTER TABLE `products` DISABLE KEYS */;
 97 INSERT INTO `products` VALUES ('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'),('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'),('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');
 98 /*!40000 ALTER TABLE `products` ENABLE KEYS */;
 99 
100 #
101 # Structure for table "vendors"
102 #
103 
104 DROP TABLE IF EXISTS `vendors`;
105 CREATE TABLE `vendors` (
106   `vend_id` char(10) NOT NULL,
107   `vend_name` char(50) NOT NULL,
108   `vend_address` char(50) DEFAULT NULL,
109   `vend_city` char(50) DEFAULT NULL,
110   `vend_state` char(5) DEFAULT NULL,
111   `vend_zip` char(10) DEFAULT NULL,
112   `vend_country` char(50) DEFAULT NULL,
113   PRIMARY KEY (`vend_id`)
114 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
115 
116 #
117 # Data for table "vendors"
118 #
119 
120 /*!40000 ALTER TABLE `vendors` DISABLE KEYS */;
121 INSERT INTO `vendors` VALUES ('BRE02','Bear Emporium','500 Park Street','Anytown','OH','44333','USA'),('BRS01','Bears R Us','123 Main Street','Bear Town','MI','44444','USA'),('DLL01','Doll House Inc.','555 High Street','Dollsville','CA','99999','USA'),('FNG01','Fun and Games','42 Galaxy Road','London',NULL,'N16 6PS','England'),('FRB01','Furball Inc.','1000 5th Avenue','New York','NY','11111','USA'),('JTS01','Jouets et ours','1 Rue Amusement','Paris',NULL,'45678','France');
122 /*!40000 ALTER TABLE `vendors` ENABLE KEYS */;
MySQL语句

相关文章: