【笔记】SQL语言的设计与编写

    数据定义语言(DDL):    CREATE DROP ALTER
        用于定义和管理数据对象(库,表,索引,视图),数据库和数据表
        
    数据操作语言(DML):    insert  update  delete
        用于操作数据库对象中所包含的数据。

    数据查询语言(DQL):    select
        用于查询数据库对象所包含的数据,能够进行单表查询、连接查询、嵌套查询,以及各种不同的数据查询。
        
    数据控制语言(DCL):    grant revoke commit rollback
        用于管理数据库的语言,包含管理权限及数据更改。
    
对于程序员就是:
    创建//EG1
    增、删、改、查
    
    插入 insert
        insert into 表名([字段列表]) values (值列表)
            1.表名后面列表没有给那么就要按循序values的添
            2.字段名不要双引号或者单引号 值的东西都要以单引号,,mysql可以自动转换
            3.最好带上字段名,,一一对应就好;
    更新 update
        update 表名 set 字段='' [条件]
            条件是确定的更改,可以通过条件指定一个或者多条
        
    删除 delect
        delect from 表明 [条件]
    
    
        
    where条件语句//用各种运算符号(可以把字段当做一个变量)
    
    
    
    
    DQL:SELECT
        查询 select//EG2
        SELECT [ALL | DISTINCT/*不重复*/ |]
                {* | table.* | [table.]xx1[as oo1][,[table.]xx2[as oo2].....]}
        FROM    表名 [] 表名
        
        [WHERE...]
        
        [GROUP BY...]
        
        [HAVING...]        having 是筛选组 
        
        [ORDER BY...]    
        
        [LIMIT/*数量*/...]
        
        用select目的就是可以按照你的想法将数据查询出来,将结果返回给我
    //EG_2  全部都有
        1.字段要列出查询字段
        
        2.别名 as 查询(关键字冲突,多表查询)表也可以取别名
        
        3.使用 distinct 的时候{整个}查询列表  只返回一个  不是单独的一列 在select后面用
        
        4.SQL中使用表达式列(算数,逻辑)
    
        5.where可以再select update delete
        
            逻辑运算符号(多个组合)
                &&   ||   !
                AND  OR   NOT
            比较运算符号
                =<=>(和=好一样 但是可以用于null比较)、<<=>>=
                is null
                is not null
                between and     一定范围内
                not between and
                like     '_'  '%'两个通配符
                not like 
                in        在一定里面
                regexp rlike     正则表达式
    
    
    6.多表查询(连接查询)
        //EG_3
        笛卡尔乘积...
        
    7.嵌套查询    (子查询)
        where 里面包含另一个语句
        
        
    8.order by        排序
        order by 字段[asc|desc]  //asc正序  desc 倒序
        select * from products order by cid desc;
        
    9.limit        数量  限制个数
        select * from products limit 10;
        
        
        
【重要】如果  看文章的时候  id号被删除了  就不能用+1或者-1来寻找文章...怎么办?
            //delete from products where  id in(5,7);
        6的上面是4  6的下面是8
    select * from products where id<6 order by id desc limit 0,1;//上一条
    select * from products where id>6 order by id asc limit 0,1;//下一条
        
    10.group by 
                count()
                sum()
                avg()
                max()
                min()
            select count(price),min(price),sum(price) from products group by cid;
                按xxx组分组
        可以各种的统计,,,
            select count(price),min(price),sum(price) from products group by cid having sum(price)>200;//having是按照组来区分   就好比 一个班里面男生的最好分..女生的最低分 之类的
        
    
EG_1:    创建
    mysql> create database xxoo;
    mysql> show databases;
    mysql> use xxoo;
    
    create table cats(
        id int not null auto_increment,
        pid int not null default '0',
        name varchar(60) not null default '',
        desn text not null default '',
        primary key(id),
        index name(name,pid)
    );//创建一个主表
    create table products(
        id int not null auto_increment,
        cid int not null default '0',
        name varchar(60) not null default'',
        price double(7,2) not null default '0.00',
        num int not null default '0',
        desn text not null default '',
        ptime int not null default '0',
        primary key(id),
        key pname(name,price)
    );//一个商品附表
    
    
    insert into cats values(null,0,'soft','this is test');
    insert into cats values(null,0,'java','this is java');
    update cats set pid='2' where id='2';//上一条插错用update改
    insert into cats(pid,name,desn) values ('3','php','this is php');
    insert into cats(pid,name) values('2','j2se'),
    ('2','j2mm'),
    ('3','thinkphp'),
    ('1','xxoo');
    update cats set desn='this is xxoo' where name='xxoo';
        delect from cats;//删除
        truncate cats;//删除表效率更高
    insert into cats values(null,0,'soft','this is test');
    delete from cats where id>=8;
    //查询最难
        insert into 
        products(cid,name,price,num,desn,ptime)
        values(1,'1111','34.5','10','good','123321123'),
        (1,'1111','44.57','30','good','123321123'),
        (2,'2222','66.66','30','good','123321123'),
        (2,'2222','44.57','50','good','123321123'),
        (3,'3333','45.57','10','good','123321123'),
        (3,'3333','44.57','11','good','123321123'),
        (4,'4444','54.69','22','good','123321123'),
        (4,'4444','44.57','55','good','123321123'),
        (5,'5555','44.57','66','good','123321123'),
        (5,'5555','44.57','88','good','123321123'),
        (6,'6666','44.57','89','good','123321123'),
        (6,'6666','82.28','80','good','123321123'),
        (7,'7777','46.64','8','good','123321123'),
        (7,'7777','73.37','37','good','123321123'),
        (5,'555','37.37','38','good','123321123'),
        (5,'5555','73.73','39','good','123321123');
    
EG_2://查询  接上面的表继续查    
    select * from products;//查找全部    
    select price,name from products;//查找其中    
    select price as jiaqian,name as bookname from products;//别名
    
    
    select distinct price as jiaqian,name as bookname from products;//无效..查找的是整个字段的
    select distinct price as jiaqian from products;//有效..因为他的整个字段有重复的
                                            //别名,转换php中可以避免错误
                                            
    
    select 1+1;//在哪用呢?  用于更新
    update products set num=num+1 where id=1;//刷新的时候就执行一次就会积累增加一次
    select name,price,price*0.8 as hou from products;//打折..可用
    select name,price,price*0.8 as hou from products where id>4 && id<10;//可以加where条件
    
    
        //where 
    select * from products where price<80 && price>50;//where  如果有等号就用=个等号 不用==!=
                            //不能用null的'='号处理查询  之能 is nullnot null
    .......where not null;//null转成php程序 有可能有多种情况
    <=>可以找出null
    
    
        //between;
    select *from products where id between 10 and 60  <=>  select *from products where id>10 &&id<60;//等价
    
        //in
    select *from products where id in (5,12,34);
    update products set num=11 where id in(1,2,3,4,5);
    delete from products where id in(1,2,3);
    
        //like && not like             '_'是单个      '%'是多个            
    select * from products where name like '____';
    select * from products where name like '2___';
    select * from produc    ts where name like '2%';
    
        //regexp rlike
    select * from products where name regexp '^2';//用的很少
    
        //limit && order by desc && where
    select * from products where id<=10 order by id desc limit 3;//where最前 其次是 order by 然后是limit
    
    
EG_3://多表查询
    select c.name,c.desn,p.name  from cats c,products p;//指定表名
    select c.name cname,c.desn cdesn,p.name pname,p.price pprice,p.num pnum from cats c,products p;
    
    select c.name cname,c.desn cdesn,p.name pname,p.price pprice,p.num pnum from cats c,products p where c.d=p.cid;//笛卡尔乘积...
    
    select c.name cname,c.desn cdesn,p.name pname,p.price pprice,p.num pnum from cats c,products p where c.d=p.cid and c.id=2;//可以加上条件  查询类别
    
        //扯淡的多表查询   子查询  可以里面再有子查询....无聊
    select * from products where cid in(select pid from cats where pid like '%');
                //多查询就是in在另一个表里的数来匹配现在的表的条件
        
原文地址:https://www.cnblogs.com/xiguayizu/p/3356789.html