mysql crash cource 书中实例

时间:2021-05-05 03:44:13
样例表

CREATE TABLE customers
(
  cust_id      int       NOT NULL AUTO_INCREMENT,
  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 ,
  PRIMARY KEY (cust_id)
) ENGINE=InnoDB; CREATE TABLE 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 ,
  PRIMARY KEY (order_num, order_item)
) ENGINE=InnoDB; CREATE TABLE orders
(
  order_num  int      NOT NULL AUTO_INCREMENT,
  order_date datetime NOT NULL ,
  cust_id    int      NOT NULL ,
  PRIMARY KEY (order_num)
) ENGINE=InnoDB; CREATE TABLE products
(
  prod_id    char(10)      NOT NULL,
  vend_id    int           NOT NULL ,
  prod_name  char(255)     NOT NULL ,
  prod_price decimal(8,2)  NOT NULL ,
  prod_desc  text          NULL ,
  PRIMARY KEY(prod_id)
) ENGINE=InnoDB; CREATE TABLE vendors
(
  vend_id      int      NOT NULL AUTO_INCREMENT,
  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 ,
  PRIMARY KEY (vend_id)
) ENGINE=InnoDB; CREATE TABLE productnotes
(
  note_id    int           NOT NULL AUTO_INCREMENT,
  prod_id    char(10)      NOT NULL,
  note_date datetime       NOT NULL,
  note_text  text          NULL ,
  PRIMARY KEY(note_id),
  FULLTEXT(note_text)
) ENGINE=MyISAM; #####################
# Define foreign keys
#####################
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);

实例

第四章 检索数据

1、检索单个列

select prod_name from products;

2、检索多个列

select prod_name,prod_price from products;

3、检索所有列 (除非需要每个列,否则最好别用,降低效率)

select * from products;

4、检索不同的行

select distinct vend_id from products;

5、限定结果

select * from products limit 2,2;

6、限定表名

select products.prod_name from mysqlcrashcource.products;

第五章 检索排序数据 (order by)

1、排序数据

select prod_name from products order by prod_name;

2、按多个列排序 (仅在多个行具有相同的prod_price时才对产品prod_name进行排序)

select prod_id,prod_name,prod_price from products order by prod_price,prod_name;

3、指定排序方向 (默认是升序 asc)

select prod_id,prod_name,prod_price from products order by prod_price desc,prod_name;

4、order by 和 limit 组合 (limit 在order by之后)

select prod_price from products order by prod_price desc limit 1;

第六章 过滤数据 (where) order by 位于where之后

1、where子句操作符(=、<>、!=、<、>、<=、>=、between and)

select * from products where prod_price=2.5;

2、空值检查

select prod_name from products where prod_price is null;

第七章 数据过滤

1、组合where子句(and or)

select * from products where vend_id=1003 and prod_price<10;

select * from products where vend_id=1003 or vend_id=1002;

2、计算次序 (and优先级高)

select * from products where (vend_id=1003 or vend_id=1005) and prod_price>10;

3、IN操作符 (in比or优点:快,简洁,可以包含其它select)

select * from products where vend_id in (1002,1003) order by prod_price;

4、NOT操作符

select * from products where vend_id not in (1002,1003) order by prod_price;

第八章 通配符过滤

1、LIKE操作符 %通配符 (不区分大小写,匹配多个字符)

select prod_name,prod_price from products where prod_name like 'TNT%';

select prod_name,prod_price from products where prod_name like '%'; //匹配任何东西,除null

2、_通配符(匹配单个字符)

select prod_name,prod_price from products where prod_name like '_ ton anvil';

第九章 正则表达式搜索

1、基本字符匹配

select prod_name from products where prod_name regexp '1000'; //regexp在列内匹配,可以使用^和$达到匹配整列

select prod_name from products where prod_name like '1000';//like匹配整个列

2、进行OR匹配

select prod_name from products where prod_name regexp '1000|2000';

3、匹配几个字符之一

select prod_name from products where prod_name regexp '[123] Ton'; //[123]是[1|2|3]缩写

select prod_name from products where prod_name regexp '[^123] Ton'; //[^123]匹配除这些字符外的其它任何

4、匹配范围

select prod_name from products where prod_name regexp '[1-9] Ton';

5、匹配特殊字符 ("\\+特殊字符")

select prod_name from products where prod_name regexp '\\.5';

6、匹配多个实例 (*,+,?,{n},{n,},{n,m})

select prod_name from products where prod_name regexp '\\([0-9] sticks? \\)';

7、定位匹配^$ (^两个功能,1否定集合,2串的开始)

select prod_name from products where prod_name regexp '^[0-9\\.]';

第十章 创建计算字段

1、concat拼接字段

select concat (vend_name,'(',vend_country,')') from vendors order by vend_name;

2、使用别名 (就像实际的列一样)

select concat (vend_name,'(',vend_country,')') as vend_title from vendors order by vend_name;

3、执行算数计算

select prod_id,quantity,item_price,quantity*item_price as expanded_price from orderitems where order_num=20005;

数据处理函数:文本处理函数,日期和时间处理函数,数值处理函数  略 *用到时查

第十二章 汇总数据

1、聚集函数 avg count min max sum

select avg(prod_price) as avg_price from products where vend_id=1003;

select count(*) as num_cust from customers;

select count(cust_email) as num_cust from customers;//cust_email为null的忽略

select min(prod_price) as min_pirce from products;

select sum(quantity) as items from orderitems where order_num=20005;

2、聚集不同值

select avg(distinct prod_price) as avg_price from products where vend_id=1003;

3、组合聚集函数

select count(*) as num_items,min(prod_price) as min_price,avg(prod_price) as avg_price from products;

第十三章 分组数据 --以便能汇总表内容的子集

1、group by --select中的每个列(除聚集函数外)都必须在group by中给出

select vend_id,count(*) from products group by vend_id;

2、过滤分组  --where过滤行,having过滤分组

select cust_id,count(*) as onum from orders group by cust_id having count(*) >2;

3、列出具有2个以上、价格为10以上的产品的供应商

select vend_id,count(*) as num_vend from products where prod_price>=10 group by vend_id having num_vend>=2;

4、按总计订单价格排序

select order_num,sum(item_price*quantity) as ordertotal from orderitems group by order_num having ordertotal>=50 order by ordertotal;

**select字句顺序  select - from - where - group by - having - order by - limit

第十四章 使用子查询

1、列出订单物品TNT2的所有客户  --子查询一般与IN结合使用

select cust_id from orders where order_num in (SELECT order_num from orderitems where prod_id ='TNT2');

2、作为计算字段采用子查询 --显示customers表中的每个客户的订单数

select cust_name,cust_country,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers;

*子查询对检索出的每个客户执行一次,共执行了5次。 子查询效率一般。

第十五章 联结表 join

1、创建联结

SELECT vend_name,prod_name,prod_price from vendors,products where vendors.vend_id=products.vend_id ORDER BY vend_name,prod_name;

2、笛卡尔积 --没有连接条件,应该保证所有的联结都有where子句

SELECT vend_name,prod_name,prod_price from vendors,products ORDER BY vend_name,prod_name;

3、内部连接 --第一个sql语句的另一种语法

select vend_name,prod_name,prod_price from vendors inner join products on vendors.vend_id=products.vend_id order by vend_name,prod_name;

4、联结多张表 3张表 略

第十六章 高级联结

1、使用表别名 (两个用处 1简练 2一个select中多次使用相同表)

select vend_name,prod_name,prod_price from vendors as v inner join products  as p on v.vend_id=p.vend_id order by vend_name,prod_name;

2、自联结--查询生产ID为DTNTR的物品供应商生成的其它产品

1>select prod_id,prod_name from products where vend_id = (select vend_id from products where prod_id='DTNTR');

2>select p1.prod_id,p1.name from products p1 inner join products p2 on p1.vend_id=p2.vend_id where p2.vend_id='DTNTR';//效率高

3、外部联结 --联结包含没有关联行的那些行(left join&right join)

1>检索所有客户及其订单,含没有下单客户

select c.cust_id,o.order_num from customers c  left outer join orders o on c.cust_id=o.cust_id;(left outer join 简写 left join)

1>对每个客户下了多少订单计数,包含没有下单的客户--外连接与聚合函数

select c.cust_id,count(order_num) num_order from customers c left join orders o on c.cust_id=o.cust_id group by c.cust_id;

*****count(*)是错误的,没有下单的客户count(*)会为1*****

第十七章 组合查询

1、UNION --可用一条where代替

select vend_id,prod_id,prod_price from products where prod_price<5 union select vend_id,prod_id,prod_price from products where vend_id in (1001,1002);

*UNION自动去除了重复行,如果想返回所有匹配行UNION ALL(where 没有此功能)

*mysql不支持全外连接,可以用left join union right join 来实现。

第十八章 全文本搜索

SELECT note_text  FROM `productnotes` where MATCH (note_text) AGAINST ('rabbit');

第十九章 插入数据

1、插入完整行 --总是给出列明

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES(10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com');

2、插入部分行 --(该列允许null或该列有默认值) 略

3、插入多行

INSERT INTO customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) VALUES(10001, 'Coyote Inc.', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'Y Lee', 'ylee@coyote.com'),(10002, 'Mouse House', '333 Fromage Lane', 'Columbus', 'OH', '43333', 'USA', 'Jerry Mouse');

4、插入检索出的值 --列名可以不同,类型一致即可

INSERT INTO customers(cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) SELECT (cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email) from custnew; //可以+where

5、insert ignore .. 用法

第二十章 更新和删除数据 --切记加where

1、更新数据

update customers set cust_email = 'sdf@126.com',cust_name='ww' where cust_id=10005;

2、删除数据--删除的是行

delete from customers where cust_id=10006;