SQL必知必会笔记

  • MySQL数据类型DECIMAL(N,M),N代表总长度,M代表小数部分长度

  • 创建外键
    alter table orderitems(表) add constraint fk_orderitems_orders(外键的ID) foreign key (order_num) references orders(order_num);
    在创建表时绑定外键
    cust_id CHAR(10) reffrences customers(cust_id)

  • DISTINCT选择不重复的值(不能部分使用)
    select distinct vend_id from products;

  • offset 从第几行起
    select prod_name from products limit 5 offset 5
    ;
    #从第五行起选择5行 ## MYSQL 支持 limit 5,5简写

  • or 跟 and 组合

    
    # 注意优先处理顺序 and优先 加括号
    
    mysql> select prod_name,prod_price
    -> from products
    -> where (vend_id='dll01' or vend_id='brs01')
    -> and prod_price >=10;
  • IN
    指定条件范围。范围中的每个条件都可以匹配
 select prod_name,prod_price
 from products 
 where vend_id in ('dll01''brs01') #相当于 'dll01' or 'brs01'
 order by prod_name;
  • NOT
    只有一个功能、否定其后所跟的任何条件
  select prod_name
  from products
  where not vend_id = 'dll01'
  order by prod_name;
  • 通配符 # like ‘%bean bag%’

    • % 匹配0个、1个或多个字符(不能匹配NULL)
    • 下划线 _ 匹配单个字符
  • 创建计算字段 field (有别于column)

    1. 拼接字段
# 用于大多数DBMS 
mysql> select vend_name + '(' vend_country + ')'
    -> from vendors;
# 用于MYSQL
mysql> select concat(vend_name , '(', vend_country , ')')
    -> from vendors;
  1. 算术计算
mysql> select prod_id,
    -> quantity,
    -> item_price,
    -> quantity*item_price as expanded_price
    -> from orderitems;
  • 函数
    • trim 去掉字符串两边空格
      ltrim 去掉字符串左边空格
      rtrim 去掉字符串右边空格 例子 trim(column)
#mysql 选择年份
 mysql> select order_num from orders
    -> where year(order_date) = 2012;
  • 聚集函数

    • avg() 返回某列的平均值
    mysql> select avg(prod_price) as avg_price
    -> from products
    -> where vend_id = 'dll01';
    • . count() 获得某列的行数

      mysql>  select count(*) as num_cust
      -> from customers;
      
      #获得所有行数
      
    • max()/min()
      用于文本数据时。返回该列排序后最前面/最后面的行。

    • sum()
      返回指定列的值的和
    mysql> select sum(item_price*quantity) as items_ordered
    -> from orderitems
    -> where order_num = 20005;
    • 组合聚焦函数
  mysql> select count(*) as num_items,
    -> min(prod_price) as price_min,
    -> max(prod_price) as price_max,
    -> avg(prod_price) as price_avg
    -> from products;
  • 分组 group by
 mysql> select vend_id,count(*) as num_prods
    -> from products
    -> group by vend_id;
  • 过滤分组 having
 mysql> select cust_id,count(*) as orders
    -> from orders
    -> group by cust_id
    -> having count(*) >=2;
  • having 和 where 结合
 mysql> select vend_id, count(*) as num_prods
    -> from products
    -> where prod_price >=4
    -> group by vend_id
    -> having count(*) >=2;
  • group by 和 order by 结合
 mysql> select order_num,count(*) as items
    -> from orderitems
    -> group by order_num
    -> having count(*)>=3
    -> order by items,order_num;
output+-----------+-------+
| order_num | items |
+-----------+-------+
|     20006 |     3 |
|     20009 |     3 |
|     20007 |     5 |
|     20008 |     5 |
+-----------+-------+
4 rows in set
  • select 子句顺序
    SELECTfromwheregroup byhavingorder by

  • 作为计算字段使用子查询

mysql> select cust_name,
    -> cust_state,
    -> (select count(*)
    -> from orders
    -> where orders.cust_id = customers.cust_id ) as orders
    -> from customers
    -> order by cust_name;
  • 内部联结(两种写法)
 #1 简单写法
 mysql> select vend_name,prod_name,prod_price
    -> from vendors,products
    -> where vendors.vend_id = products.vend_id;
 #2 标准写法
 mysql> select vend_name,prod_name,prod_price
    -> from vendors inner join products
    -> on vendors.vend_id = products.vend_id;

# 多个表内联结
mysql> select cust_name,cust_contact
    -> from customers,orders,orderitems
    -> where customers.cust_id=orders.cust_id
    -> and orderitems.order_num = orders.order_num
    -> and prod_id = 'rgan01';    
  • 使用带聚集函数的联结
 mysql> select customers.cust_id,
    -> count(orders.order_num) as num_ord
    -> from customers inner join orders
    -> on customers.cust_id = orders.cust_id
    -> group by customers.cust_id;
  • union 组合查询
    • 在一个查询中从不同的表返回结构数据
    • 对一个表执行多个查询、按一个查询返回数据
mysql> select cust_name,cust_contact,cust_email
    -> from customers
    -> where cust_state in ('il','in','mi')
    -> union
    -> select cust_name,cust_contact,cust_email
    -> from customers
    -> where cust_name = 'fun4all';
  • 复制数据到新表
    mysql> create table custcopy as select * from customers;
  • 插入选择的数据
 mysql> insert into customers(cust_id,cust_contact,cust_email,cust_name) 
         select cust_id,cust_contact,cust_email,cust_name 
         from custornew;
  • 更新数据
 mysql> update customers
    -> set cust_contact = 'sam roberts',
    -> cust_email = '410583828@qq.com'
    -> where cust_id = '1000000006';
  • 删除数据
    mysql> delete from orders where order_num = '20005';
  • 给表添加列
    mysql> ALTER table vendors add vend_phone char(20);
    给列添加主键
    alter table Vendors add constraint primary key (vend_id)

  • 创建视图

 # 创建标题拼接视图
 mysql> create view vendorloca as
    -> select concat(rtrim(vend_name),'(',rtrim(vend_country),')') as vend_title
    -> from vendors;
# 筛选没有邮箱的客户
mysql> create view customeremail as
    -> select cust_id,cust_name,cust_email
    -> from customers
    -> where cust_email is not null;     
  • 存储过程(类似批处理)
  • 事务
    使用事务处理,通过确保成批的SQL操作要么完全执行,要么完全不执行,来维护数据库的完整性。
 START TRANSACTION
INSERT INTO customers(cust_id,cust_name)
values('1000000011','meng meizhe');
SAVEPOINT start1;
INSERT INTO orders(order_num,order_date,cust_id)
VALUES(20100,'2001/12/1','1000000011');
IF @@ERROR <> 0 ROLLBACK start1;
INSERT INTO orderitems(order_num,order_item,prod_id,quantity,item_price)
VALUES(20100,1,'BR01',100,5.49);
IF @@ERROR <> 0 ROLLBACK start1;
INSERT INTO orderitems(order_num,order_item,prod_id,quantity,item_price)
VALUES(20100,2,'BR03',100,10.49);
IF @@ERROR <> 0 ROLLBACK start1;
COMMIT TRANSACTION
  • 游标
  • 检查约束
    CHECK (quantity > 0)
    ADD CONSTRAINT CHECK (gender LIKE '[MF]')
  • 索引
    CREATE INDEX prod_name_ind ON PRODUCTS (prod_name);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值