时间:2021-07-01 10:21:17 帮助过:7人阅读
------------------------------------------------------------- -- Sams Teach Yourself SQL in 10 Minutes -- http://forta.com/books/0672336073/ -- Example table population scripts for Microsoft SQL Server. ------------------------------------------------------------- --------------------------- -- 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, ‘2012-05-01‘, ‘1000000001‘); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20006, ‘2012-01-12‘, ‘1000000003‘); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20007, ‘2012-01-30‘, ‘1000000004‘); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20008, ‘2012-02-03‘, ‘1000000005‘); INSERT INTO Orders(order_num, order_date, cust_id) VALUES(20009, ‘2012-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);插入数据
SELECT 要返回的列或表达式 是
FROM 从中检索数据的表 仅在从表选择数据时使用
WHERE 行级过滤 否
GROUP BY 分组说明 仅在按组计算聚集时使用
HAVING 组级过滤 否
ORDER BY 输出排序顺序 否
SELECT DISTINCT vend_id, prod_price --会列出二列数据不相同每行不同的数据 FROM Products;
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price, prod_name; --排序 先排prod_price顺序,然后在其基础上排prod_name
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY 3, 2; --先排第3列,在其基础上排第2列
SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_name DESC,prod_price DESC; --DESC是反转,先排前面的,再排后面的
排序ORDER BY应该在where之后
where的字段不是一定要在select里出现的
‘‘单引号用来限定字符串,与数值
<> | != | 不等于 注:Access支持<>不支持!= |
! | 不(非) 不可单用,用于组合 | |
>= | 大于等于 | |
BETWEEN | 在指定的两个值之间 | |
IS NULL | 为NULL值 (NULL表示无值的意思) |
SELECT prod_name, prod_price FROM Products WHERE prod_price BETWEEN 5 AND 10; --在二者之间
SELECT cust_name FROM CUSTOMERS WHERE cust_email IS NULL; --显示其列cust_email为空的 cust_name
SELECT prod_name, prod_price FROM Products WHERE vend_id = ‘DLL01‘ OR vend_id = ‘BRS01‘ AND prod_price >= 10;--SQL(像多数语言一样)在处理OR操作符前,优先处理AND操作符 --等价于vend_id = ‘DLL01‘ OR (vend_id = ‘BRS01‘ AND prod_price >= 10)
所以如果想先OR 应该在or二个条件加括号
SELECT prod_name, prod_price FROM Products WHERE vend_id IN ( ‘DLL01‘, ‘BRS01‘ ) ORDER BY prod_name;--显示包含了‘DLL01‘, ‘BRS01‘的
SELECT prod_name FROM Products WHERE NOT vend_id = ‘DLL01‘ ORDER BY prod_name;--除去‘DLL01‘ 的所有都显示出来
多个通配符% 任何字符出现任意次数 (注 Access需要使用*) 不匹配null
SELECT prod_id, prod_name FROM Products WHERE prod_name LIKE ‘Fish%‘; --开头为Fish的
单个通配符_ 匹配单个字符 (注 Access需要使用?) 不匹配null 语法和%一样
[]通配符 匹配一个字符,内容为方括号内的
SELECT cust_contact FROM Customers WHERE cust_contact LIKE ‘[JM]%‘ --匹配J或M开头的cust_contact
拼接字段
SELECT vend_name + ‘ (‘ + vend_country + ‘)‘ FROM Vendors ORDER BY vend_name;--拼接vend_name与end_country字段
RTRIM()删除右边的空格 LTRIM()删除左边的空格 trim()删除左右二边的空格
SELECT RTRIM(vend_name) + ‘ (‘ + RTRIM(vend_country) + ‘)‘ FROM Vendors ORDER BY vend_name;--删除右边的空格再合并
AS 显示别名
SELECT vend_name AS vend_title FROM Vendors --用vend_title别名显示结果
执行算术运算
SELECT prod_id, quantity, item_price, quantity*item_price AS expanded_price --把查询到的quantity与item_price字段的结果相乘用expanded_price显示 FROM OrderItems WHERE order_num = 20008;
函数 | 说明 | 函数 | 说明 | |
LEFT(字段名,2) | 返回字符串左边的2个字符 | 相反 | RIGHT(字段名,2) | 返回字符串右边的2个字符 |
LOWER() Access使用LCASE() |
将字符串转换为小写 | UPPER() Access使用UCASE() |
将字符串转换为大写 | |
LTRIM() | 去掉字符串左边的空格 | RTRIM() | 去掉字符串右边的空格 | |
Soundex() | 返回字符串的SOUNDEX值 主要用于英语读法相似的(略过) |
length() | 返回字符串的长度 |
日期处理函数
SELECT order_num,order_date FROM Orders WHERE DATEPART(yy, order_date) = 2012; --表示字段order_date的年份为2012年的 --DATEPART(M, order_date)如果是M则表示月份是5月,d为日,H为时,mm为分,ss为秒
数值处理函数