sql学习

作者: 寒微123 | 来源:发表于2018-03-04 17:51 被阅读0次

    一、对数据库的操作

    创建库
    1、CREATE DATABASE db;--创建数据库db
    2、SHOW DATABASES;--显示数据库
    3、SHOW CREATE DATABASE db; --查看数据库db的编码
    删除一个库
    1、drop database db
    使用库
    use db
    查看当前正在操作的库
    select database()
    

    二、对数据库表的操作

    1、创建一张表
    create table user(
    uid int(32) primary key auto_increment,
    uname varchar(32),
    upassword varchar(32)
    );
    2、查看数据库表
    show tables;
    查看表的结构(设计表)
    desc user;
    3、删除一张表
    drop table user;
    4、修改表
    alter table 表名 add 字段名 类型(长度) [约束]
    alter table user add uinfo varchar(32) not null;--添加一列
    alter table 表名 modify 要修改的字段名 类型(长度) [约束]
    alter table user modify uinfo varchar(100) null;--修改列的类型(长度、约束)
    alter table 表名 change 旧列名 新列名 类型(长度) [约束]
    alter table user change uinfo info varchar(32) not null;--修改列的列名
    alter table 表名 drop 列名
    alter table user drop info;--删除表的列
    rename table 表名 to 新表名
    rename table user to tbl_user;--修改表名
    alter table 表名 character set 编码
    

    三、对数据库表记录进行操作(修改)

    1、insert into 表名(列名1,列名2,列名3……) values(值1,值2,值3……)
    insert into tbl_user(uid,uname,upassword) values(null,'zhang','123');--插入记录
    insert into 表名 values(值1,值2,值3……)
    insert into tbl_user values(null,'liu','456');
    2、修改表记录
    2.1不带条件的
    update 表名 set 字段名=值, 字段名=值, 字段名=值……
    2.2 带条件的
    update 表名 set字段名=值, 字段名=值, 字段名=值…… where 条件
    update tbl_user set upassword='999' where uid=1;
    3.删除表记录
    3.1 带条件的
    delete from 表名 where 条件
    delete from tbl_user where uid=1;            --删除后,uid不会重置!
    3.2.不带条件的
    先准备数据
    insert into tbl_user values(null,’老王’,’666’);
    删除操作
    delete from 表名;
    4.查询操作
    语法:
        select [distinct] *| 列名,列名 from 表名 [where条件]
    4.1 简单查询
    1.查询所有商品
    select * from product;
    2. 查询商品名和商品价格
    select pname,price from product;
    3.查询所有商品信息使用表别名
    select * from product as  p;
    4.查询商品名,使用列别名
    select pname as  p from product
    5.去掉重复值(按照价格)
    select distinct(price) from product;
    6.将所有的商品的价格+10进行显示
    select pname,price+10 from product;
    4.2 条件查询
    1.查询商品名称为"左慈"的商品信息
    select * from product where pname='左慈';
    2.查询价格>60元的所有商品信息
    select * from product where price>60;
    3.查询商品名称含有"士"字的商品信息
    select * from product where pname like '%士%';
    4.查询商品id在(3,6,9)范围内的所有商品信息
    select * from product where pid in (3,6,9);
    5.查询商品名称含有"士"字并且id为6的商品信息
    select * from product where pname like '%士%' and pid=6;
    6.查询id为2或者6的商品信息
    select * from product where pid=2 or pid=6;
    4.3 排序
    1.查询所有的商品,按价格进行排序(升序、降序)
    select * from product order by price asc/desc;
    2.查询名称有"士"的商品信息并且按照价格降序排序
    select * from product where pname like '%士% order by price desc;
    4.4 聚合函数
    1.获得所有商品的价格的总和
    select sum(price) from product;
    2.获得所有商品的平均价格
    select avg(price) from product;
    3.获得所有商品的个数
    select count(*) from product;
    4.5 分组操作
    1.添加分类id (alter table product add cid varchar(32);)
    2.初始化数据
    update product set cid='1';
    update product set cid='2' where  pid in (5,6,7);
    1.根据cid字段分组,分组后统计商品的个数
    select cid,count(*) from product group by cid;
    2.根据cid分组,分组统计每组商品的平均价格,并且平均价格大于20000元。
    select cid,avg(price) from product group by cid having avg(price)>20000;
    4.6 查询总结
    select  一般在的后面的内容都是要查询的字段
    from  要查询到表
    where
    group by 
    having  分组后带有条件只能使用having
    order by 它必须放到最后面
    

    多表查询

    SELECT * FROM category,product WHERE cid = category_id;
    SELECT * FROM category c ,product p WHERE c.cid = p.category_id;
    SELECT cname FROM category c ,product p WHERE c.cid = p.category_id;
    SELECT DISTINCT cname from category c,product p WHERE c.cid = p.category_id;
    
    --内连接
    SELECT * from category c INNER JOIN product p WHERE c.cid = p.category_id;
    --左外连接
    SELECT * from category c LEFT JOIN product p on c.cid = p.category_id;
    
    SELECT cname,count(category_id) from category c LEFT OUTER JOIN product p on c.cid = p.category_id GROUP BY cid;
    
    
    
    
    子查询
    select cid from category where cname = '化妆品';
    select * from product where category_id = 'c003';
    
    select * from product where category_id = (select cid from category where cname = '化妆品');
    
    select p.* from product p ,category c where p.category_id = c.cid and cname = '化妆品';
    

    相关文章

      网友评论

          本文标题:sql学习

          本文链接:https://www.haomeiwen.com/subject/qdlqfftx.html