SQL的学习

Posted 0岁的数据分析师

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL的学习相关的知识,希望对你有一定的参考价值。

SQL的学习之路,主要参考资料是:

  1. SQL必知必会
  2. W3school的SQL简介

操作使用的数据库为Access及mysql

数据采用的是SQL必知必会中的数据(最下方可以下载数据)。其有5个表,表的关系如下:

 

  1 # 这里补充下,MySQL创建该数据库(2017.5.17),复制到MySQL创建执行创建即可(数据来源于《SQL必知必会》);
  2 
  3 -- ----------------------
  4 -- Create Customers table
  5 -- ----------------------
  6 CREATE TABLE Customers
  7 (
  8   cust_id      char(10)  NOT NULL ,
  9   cust_name    char(50)  NOT NULL ,
 10   cust_address char(50)  NULL ,
 11   cust_city    char(50)  NULL ,
 12   cust_state   char(5)   NULL ,
 13   cust_zip     char(10)  NULL ,
 14   cust_country char(50)  NULL ,
 15   cust_contact char(50)  NULL ,
 16   cust_email   char(255) NULL 
 17 );
 18 
 19 -- -----------------------
 20 -- Create OrderItems table
 21 -- -----------------------
 22 CREATE TABLE OrderItems
 23 (
 24   order_num  int          NOT NULL ,
 25   order_item int          NOT NULL ,
 26   prod_id    char(10)     NOT NULL ,
 27   quantity   int          NOT NULL ,
 28   item_price decimal(8,2) NOT NULL 
 29 );
 30 
 31 
 32 -- -------------------
 33 -- Create Orders table
 34 -- -------------------
 35 CREATE TABLE Orders
 36 (
 37   order_num  int      NOT NULL ,
 38   order_date datetime NOT NULL ,
 39   cust_id    char(10) NOT NULL 
 40 );
 41 
 42 -- ---------------------
 43 -- Create Products table
 44 -- ---------------------
 45 CREATE TABLE Products
 46 (
 47   prod_id    char(10)      NOT NULL ,
 48   vend_id    char(10)      NOT NULL ,
 49   prod_name  char(255)     NOT NULL ,
 50   prod_price decimal(8,2)  NOT NULL ,
 51   prod_desc  text          NULL 
 52 );
 53 
 54 -- --------------------
 55 -- Create Vendors table
 56 -- --------------------
 57 CREATE TABLE Vendors
 58 (
 59   vend_id      char(10) NOT NULL ,
 60   vend_name    char(50) NOT NULL ,
 61   vend_address char(50) NULL ,
 62   vend_city    char(50) NULL ,
 63   vend_state   char(5)  NULL ,
 64   vend_zip     char(10) NULL ,
 65   vend_country char(50) NULL 
 66 );
 67 
 68 
 69 -- -------------------
 70 -- Define primary keys
 71 -- -------------------
 72 ALTER TABLE Customers ADD PRIMARY KEY (cust_id);
 73 ALTER TABLE OrderItems ADD PRIMARY KEY (order_num, order_item);
 74 ALTER TABLE Orders ADD PRIMARY KEY (order_num);
 75 ALTER TABLE Products ADD PRIMARY KEY (prod_id);
 76 ALTER TABLE Vendors ADD PRIMARY KEY (vend_id);
 77 
 78 
 79 -- -------------------
 80 -- Define foreign keys
 81 -- -------------------
 82 ALTER TABLE OrderItems ADD CONSTRAINT FK_OrderItems_Orders FOREIGN KEY (order_num) REFERENCES Orders (order_num);
 83 ALTER TABLE OrderItems ADD CONSTRAINT FK_OrderItems_Products FOREIGN KEY (prod_id) REFERENCES Products (prod_id);
 84 ALTER TABLE Orders ADD CONSTRAINT FK_Orders_Customers FOREIGN KEY (cust_id) REFERENCES Customers (cust_id);
 85 ALTER TABLE Products ADD CONSTRAINT FK_Products_Vendors FOREIGN KEY (vend_id) REFERENCES Vendors (vend_id);
 86 
 87 # 插入数据
 88 -- ------------------------
 89 -- Populate Customers table
 90 -- ------------------------
 91 INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
 92 VALUES(\'1000000001\', \'Village Toys\', \'200 Maple Lane\', \'Detroit\', \'MI\', \'44444\', \'USA\', \'John Smith\', \'sales@villagetoys.com\');
 93 INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
 94 VALUES(\'1000000002\', \'Kids Place\', \'333 South Lake Drive\', \'Columbus\', \'OH\', \'43333\', \'USA\', \'Michelle Green\');
 95 INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
 96 VALUES(\'1000000003\', \'Fun4All\', \'1 Sunny Place\', \'Muncie\', \'IN\', \'42222\', \'USA\', \'Jim Jones\', \'jjones@fun4all.com\');
 97 INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
 98 VALUES(\'1000000004\', \'Fun4All\', \'829 Riverside Drive\', \'Phoenix\', \'AZ\', \'88888\', \'USA\', \'Denise L. Stephens\', \'dstephens@fun4all.com\');
 99 INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact)
100 VALUES(\'1000000005\', \'The Toy Store\', \'4545 53rd Street\', \'Chicago\', \'IL\', \'54545\', \'USA\', \'Kim Howard\');
101 
102 -- ----------------------
103 -- Populate Vendors table
104 -- ----------------------
105 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
106 VALUES(\'BRS01\',\'Bears R Us\',\'123 Main Street\',\'Bear Town\',\'MI\',\'44444\', \'USA\');
107 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
108 VALUES(\'BRE02\',\'Bear Emporium\',\'500 Park Street\',\'Anytown\',\'OH\',\'44333\', \'USA\');
109 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
110 VALUES(\'DLL01\',\'Doll House Inc.\',\'555 High Street\',\'Dollsville\',\'CA\',\'99999\', \'USA\');
111 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
112 VALUES(\'FRB01\',\'Furball Inc.\',\'1000 5th Avenue\',\'New York\',\'NY\',\'11111\', \'USA\');
113 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
114 VALUES(\'FNG01\',\'Fun and Games\',\'42 Galaxy Road\',\'London\', NULL,\'N16 6PS\', \'England\');
115 INSERT INTO Vendors(vend_id, vend_name, vend_address, vend_city, vend_state, vend_zip, vend_country)
116 VALUES(\'JTS01\',\'Jouets et ours\',\'1 Rue Amusement\',\'Paris\', NULL,\'45678\', \'France\');
117 
118 -- -----------------------
119 -- Populate Products table
120 -- -----------------------
121 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
122 VALUES(\'BR01\', \'BRS01\', \'8 inch teddy bear\', 5.99, \'8 inch teddy bear, comes with cap and jacket\');
123 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
124 VALUES(\'BR02\', \'BRS01\', \'12 inch teddy bear\', 8.99, \'12 inch teddy bear, comes with cap and jacket\');
125 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
126 VALUES(\'BR03\', \'BRS01\', \'18 inch teddy bear\', 11.99, \'18 inch teddy bear, comes with cap and jacket\');
127 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
128 VALUES(\'BNBG01\', \'DLL01\', \'Fish bean bag toy\', 3.49, \'Fish bean bag toy, complete with bean bag worms with which to feed it\');
129 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
130 VALUES(\'BNBG02\', \'DLL01\', \'Bird bean bag toy\', 3.49, \'Bird bean bag toy, eggs are not included\');
131 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
132 VALUES(\'BNBG03\', \'DLL01\', \'Rabbit bean bag toy\', 3.49, \'Rabbit bean bag toy, comes with bean bag carrots\');
133 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
134 VALUES(\'RGAN01\', \'DLL01\', \'Raggedy Ann\', 4.99, \'18 inch Raggedy Ann doll\');
135 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
136 VALUES(\'RYL01\', \'FNG01\', \'King doll\', 9.49, \'12 inch king doll with royal garments and crown\');
137 INSERT INTO Products(prod_id, vend_id, prod_name, prod_price, prod_desc)
138 VALUES(\'RYL02\', \'FNG01\', \'Queen doll\', 9.49, \'12 inch queen doll with royal garments and crown\');
139 
140 -- ---------------------
141 -- Populate Orders table
142 -- ---------------------
143 INSERT INTO Orders(order_num, order_date, cust_id)
144 VALUES(20005, \'2012-05-01\', \'1000000001\');
145 INSERT INTO Orders(order_num, order_date, cust_id)
146 VALUES(20006, \'2012-01-12\', \'1000000003\');
147 INSERT INTO Orders(order_num, order_date, cust_id)
148 VALUES(20007, \'2012-01-30\', \'1000000004\');
149 INSERT INTO Orders(order_num, order_date, cust_id)
150 VALUES(20008, \'2012-02-03\', \'1000000005\');
151 INSERT INTO Orders(order_num, order_date, cust_id)
152 VALUES(20009, \'2012-02-08\', \'1000000001\');
153 
154 -- -------------------------
155 -- Populate OrderItems table
156 -- -------------------------
157 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
158 VALUES(20005, 1, \'BR01\', 100, 5.49);
159 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
160 VALUES(20005, 2, \'BR03\', 100, 10.99);
161 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
162 VALUES(20006, 1, \'BR01\', 20, 5.99);
163 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
164 VALUES(20006, 2, \'BR02\', 10, 8.99);
165 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
166 VALUES(20006, 3, \'BR03\', 10, 11.99);
167 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
168 VALUES(20007, 1, \'BR03\', 50, 11.49);
169 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
170 VALUES(20007, 2, \'BNBG01\', 100, 2.99);
171 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
172 VALUES(20007, 3, \'BNBG02\', 100, 2.99);
173 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
174 VALUES(20007, 4, \'BNBG03\', 100, 2.99);
175 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
176 VALUES(20007, 5, \'RGAN01\', 50, 4.49);
177 INSERT INTO OrderItems(order_num, order_item, prod_id, quantity, item_price)
178 VALUES(20008, 1, \'RGAN01\', 5, 4.99);
179 INSERT INTO以上是关于SQL的学习的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft SQL Server 代码片段收集

缺少 SQL SERVER 2014 代码片段

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?

sql Oracle代码片段

sql 日期转换代码片段 - Dato,120,konvertere

Discuz代码片段