数据库SQL语句学习笔记-检索数据库

Posted gaojiaxing

tags:

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

首先设置好此连载笔记的演示所用的表

CREATE database sells;  #创建名叫sells的数据库

USE sells; #告诉mysql,我要开始使用这个数据库了

CREATE TABLE Customers #创建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 TABLE OrderItems #创建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 TABLE Orders #创建Orders表
(
  order_num  int      NOT NULL ,
  order_date datetime NOT NULL ,
  cust_id    char(10) NOT NULL 
);

CREATE TABLE Products #创建表Product
(
  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 TABLE Vendors #创建表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 
);

#设置主键
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);

#设置外键
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);


#插入记录
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\');

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\');

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\');

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\');

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);
查看代码

1. SELECT语句

关键字(keyword)作为SQL组成部分的保留字,不可以作为表或者列的名字。

使用SELECT语句,要清楚需要选择什么?从哪里选择的问题。

2. 检索单个列

SELECT prod_name FROM Products;

结果:        

分析:从Product表中检索一个名为prod_name的列。列名写在关键字SELECT之后,FROM关键字表明从哪个表中检索。这样返回的数据没有特定的顺序。

提示:(1)多条SQL语句必须以;分隔

(2)SQL语句不区分大小写,约定俗成的是关键字大写,表名和列名小写

(3)以分号结尾,看到分号就知道这一条语句结束了

3.检索多个列

SELECT关键字后给出多个列名,列名之间以逗号分隔,最后一个列名后不可以加逗号 

SELECT prod_id,prod_name,prod_price FROM Products;

结果: 

4.检索所有列

通配符星号(*)可以检索所有列

SELECT * FROM Products;

 5. 检索不同的值

 SELESTC语句返回所有匹配的行,但是有很多值重复出现,此时就要用到检索不同的值

SELECT DISTINCT vend_id FROM Products; 

注意:DISTINCT关键字作用于所有选择的列

6.限制结果

SELECT prod_name FROM Products LIMIT 5;

结果:

LIMIT 5指示MYSQL返回不超过5行数据

LIMIT 5 OFFSET 5从第5行起,检索5个数,注意第一行是0

简写为LIMIT 5,5 第一个数是OFFSET,第二个是LIMIT

7.使用注释

--嵌在行内

# 行内注释,在一行开始处使用#,这一整行都将作为注释

/*        */开始结束中间都作为注释

以上是关于数据库SQL语句学习笔记-检索数据库的主要内容,如果未能解决你的问题,请参考以下文章

SQL学习笔记

数据库SQL语句学习笔记-汇总数据

数据库SQL语句学习笔记-创建计算字段

Oracle学习笔记—— 游标

Mysql DBA 高级运维学习笔记-sql语言介绍及分类

SQL学习02——检索数据