样例表

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;

mysql crash cource 书中实例的更多相关文章

  1. MySQL Crash Errcode: 28 - No space left on device

    一台MySQL服务器突然Crash了,检查进程 ps -ef | grep -i mysql 发现mysqld进程已经没有了, 检查错误日志时发现MySQL确实Crash了.具体如下所示: 注意日志中 ...

  2. mysql 在大型应用中的架构演变

    文正整理自:http://www.csdn.net/article/2014-06-10/2820160 可扩展性 架构的可扩展性往往和并发是息息相关,没有并发的增长,也就没有必要做高可扩展性的架构, ...

  3. MySql的like语句中的通配符:百分号、下划线和escape 的使用

    MySql的like语句中的通配符:百分号.下划线和escape %代表任意多个字符 select * from user where username like '%huxiao'; select ...

  4. Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例

    Spring 4 MVC+Hibernate 4+MySQL+Maven使用注解集成实例 转自:通过注解的方式集成Spring 4 MVC+Hibernate 4+MySQL+Maven,开发项目样例 ...

  5. MySql在生产环境中是用mysqldump还是xtrabackup备份和恢复数据

    如题,究竟该使用mysqldump还是xtrabackup,要说用,两个都能备份,都支持热备,但是生产环境我们要考虑的是效率,就是不管备份还是恢复,都要快,要稳定. 之前我在维护mysql数据库的时候 ...

  6. 运用《深入理解Java虚拟机》书中知识解决实际问题

    前言 以前看别人博客说看完<深入理解Java虚拟机>这本书并没有让自己的编程水平提高多少,不过却大大提高了自己的装逼水平.其实,我倒不这么认为,至少在我看完一遍这本书后,有一种醍醐灌顶的感 ...

  7. Spring Bean的生命周期,《Spring 实战》书中的官方说法

    连着两天的面试 ,都问到了 Spring 的Bean的生命周期,其中还包括 昨晚一波阿里的电话面试.这里找到了Spring 实战中的官方说法.希望各位要面试的小伙伴记住,以后有可能,或者是有时间 去看 ...

  8. Latch导致MySQL Crash

    作者:沃趣科技数据库专家 董红禹 问题概述 最近我们遇到一个MySQL的问题,分析后很有代表意义,特地写出来供大家参考.出现问题是,数据库先是被置为只读,然后过了一段时间,MySQL直接Crash掉了 ...

  9. mysql数据库分区功能及实例详解

    分区听起来怎么感觉是硬盘呀,对没错除了硬盘可以分区数据库现在也支持分区了,分区可以解决大数据量的处理问题,下面一起来看一个mysql数据库分区功能及实例详解   一,什么是数据库分区 前段时间写过一篇 ...

随机推荐

  1. 关于unity中BindChannels的理解

    http://blog.csdn.net/wpapa/article/details/51794277 官方文档:http://docs.unity3d.com/Manual/SL-BindChann ...

  2. 2017 Fzu校赛 [Cloned]

    A - Salty Fish 海边躺着一排咸鱼,一些有梦想的咸鱼成功翻身(然而没有什么卵用),一些则是继续当咸鱼.一个善良的渔夫想要帮这些咸鱼翻身,但是渔夫比较懒,所以只会从某只咸鱼开始,往一个方向, ...

  3. uoj#386. 【UNR #3】鸽子固定器(乱搞)

    传送门 题解 //minamoto #include<bits/stdc++.h> #define R register #define ll long long #define fp(i ...

  4. Ajax遇到的那些坑

    提前说明:这里我用的是Windows系统,所以解决问题的方法也是仅限Windows系统,浏览器使用Chrome 第一个坑:Access to XMLHttpRequest at 'file:///C: ...

  5. Linux基本系统优化

    Linux基本系统优化  Linux Linux的网络功能相当强悍,一时之间我们无法了解所有的网络命令, 在配置服务器基础环境时,先了解下网络参数设定命令. ifconfig 查询.设置网卡和ip等参 ...

  6. Joda-Time 的 DateTimeFormat 问题

    在开发过程中遇到的问题是,当我使用,如下的代码时,会报异常 :"Exception in thread "main" org.joda.time.IllegalField ...

  7. 持续集成~Jenkins构建dotnetCore的项目

    上周一个大件就是dotnet core2.0发布了,伴随着.NET Standard2.0也发布了,整个微软的生态环境大好,当然也有一个BUG出来了,比如EFCore对Mysql的支持比起1.1来说, ...

  8. C#关键字:yield

    yield是C#为了简化遍历操作实现的语法糖.在语句中使用 yield 关键字,表示在该关键字所在的方法.运算符或 get 访问器是迭代器.有两种形式: yield return <expres ...

  9. AJPFX关于Java中运用数组的四种排序方法

    JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法.快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现.冒泡法是运用遍历数组进行比 ...

  10. 记录:swift学习笔记1-2

    swift还在不断的更新做细微的调整,都说早起的鸟儿有虫吃,那么我们早点出发吧,趁着国内绝大多数的coder们还没有开始大范围普遍应用. 网上有些大神说:swift很简单!我不同意这个观点,假如你用h ...