zoukankan      html  css  js  c++  java
  • mySQL相关知识

    创建表
    #品牌表
    create table pinpai(
    ids int auto_increment primary key, #主键自增长列
    name varchar(50) #品牌名称
    );
    #商品表
    create table shangpin(
    code int primary key, #商品代号
    name varchar(50) not null, #商品名称
    price float, #商品价格
    pinpai int, #所属品牌
    foreign key(pinpai) references pinpai(ids)
    )


    主键:primary key
    非空:not null
    自增长列:auto_increment
    外键:foreign key

    CRUD操作
    添加数据:
    insert into 表名 values('p007','李四',0,'n001','1988-2-3 14:20:30');

    insert into info values('p007','李四',0,'n001','1988-2-3 14:20:30');

    insert into info(Code,Name,Sex,Nation) values('p008','张三',0,'n001');

    insert into work values(0,'p001','1989-2-3','1990-3-4','aa','aa',1);

    1.如果该列类型为字符串,数据外层加单引号
    2.如果该列类型为bool型,添加0或1
    3.如果该列为日期时间类型,添加的格式'年-月-日 时:分:秒'
    4.如果该列为整数或小数类型,数据外层不要加任何东西
    5.表里面有几列,添加的数据就有几个。
    6.SQL语句里面不区分大小写
    7.自增长列给0

    修改数据:
    update 表名 set Name='王五' where 条件
    update info set name='王五' where code='p005'

    update info set name='张三',sex=1 where code='p005'

    删除数据:
    delete from 表名 where 条件
    delete from info where code='p008'

    查询数据:
    查询所有数据
    select * from 表名
    select * from info
    查询指定列
    select code,name from info
    查询某些行的数据(条件查询)
    select * from info where nation='n001'


    数据库设计的三大范式
    第一范式:列的原子性
    每一列是不可再拆分的
    第二范式:表里面的每一列都应该与主键有关系
    第三范式:表里面的每一列都应该与主键有直接关系
    查询
    简单查询:
    1.查询所有数据
    select * from info
    2.查询指定列
    select code,name from info
    3.给列指定名称
    select code as '代号',name as '姓名' from info
    4.条件查询
    select * from info where code='p001'
    select * from info where code='p001' and nation='n001'
    5.模糊查询
    select * from car where name like '%奥迪%'
    6.排序查询
    select * from car order by price asc,oil desc
    7.去重查询
    select distinct brand from car
    8.分页查询
    select * from car limit 5,5
    9.统计查询(聚合函数)
    数据条数
    select count(code) from car
    取最大值
    select max(price) from car
    取最小值
    select min(price) from car
    取平均值
    select avg(price) from car
    10.分组查询
    select brand,count(*) from car group by brand
    select brand from car group by brand having count(*)>=3
    11.范围查询
    select * from car where price>=40 and price<=60
    select * from car where price between 40 and 60
    12.离散查询
    select * from car where price in(10,20,30,40,50,60)
    select * from car where price not in(10,20,30,40,50,60)

    高级查询
    1.联合查询

    select code,name from info
    union
    select code,name from nation

    2.连接查询
    形成笛卡尔积
    select * from info,nation where info.nation=nation.code
    select info.code,info.name,sex,nation.name,birthday from info,nation where info.nation=nation.code

    select info.code,info.name,sex,nation.name,birthday from info join nation on info.nation=nation.code

    3.子查询=
    子查询的结果作为父查询的条件使用

    无关子查询
    子查询和父查询没有关系,子查询单独拿出来可以执行
    1.查找民族为“汉族”的所有人员信息
    select * from info where nation =(select code from nation where name='汉族')
    2.查询所有厂商是“一汽大众”的汽车信息
    select * from car where brand in(select brand_code from brand where prod_code in(select prod_code from Productor where prod_name='一汽大众'))

    相关子查询
    查询油耗低于该系列平均油耗的汽车信息
    select * from car where oil<(该系列平均油耗)
    select avg(oil) from car where brand='该系列'

    select * from car a where oil<(select avg(oil) from car b where b.brand=a.brand)

    any 只要其中一个
    all 所有
    now 可以取到当前年份

  • 相关阅读:
    .Net常用的命名空间
    Jquery测试纠错笔记
    第一章 学习总结
    Java和C++引用的区别
    gin的墙内开发艺术
    golang几个环境变量的问题
    Leetcode240_搜索二维矩阵II
    Leetcode1358_包含所有三种字符的子字符串数目
    Leetcode1354_多次求和构造目标数组
    Leetcode1353_最多可以参加的会议数目
  • 原文地址:https://www.cnblogs.com/palpitate/p/8183529.html
Copyright © 2011-2022 走看看