java MySQL所有语句

——————取自姚老师笔记

一。数据定义语言DDL(create、alter、drop)

    (1)数据库
        create database 库名;
        drop database 库名;
        alter database 库名 character set gbk;
    (2)表
        create table 表名(属性名 数据类型,..) ;
        drop table 表名;
        alter table 表名
            a.修改表名 alter table 旧表名 rename [to] 新表名;
            b.新增字段 alter table 表名 add column 字段名 类型 [after 字段|frist];
            c.修改字段 alter table 表名 change 旧字段名 新字段名 新类型;
            d.删除字段 alter table 表名 drop column 字段名;
    (3)存储过程

        a.创建    create procedure 过程名([in|out|inout] 参数名称 参数类型)

                begin
                    过程体
                end;
        call调用
        b.删除:drop procedure 过程名;
        c.修改alter
    (4)存储函数
        a.创建    create function 函数名(参数名称 参数类型)
                returns 返回类型
                begin
                    函数体
                end;
        select 调用
        b.删除:drop function 过程名;
        c.修改:alter
    (5)用户
        a.创建用户:create user '用户名'@'主机名' identify by '密码'
        b.删除用户:drop user '用户名'@'主机名';
二。数据操作语言DML(insert,delete,select,update)
    1.增加:insert into 表名 values(值);
        (1)向表中所有的字段插入数据:
            insert into user values(1,'姚天雪');
            insert into user(id,name) values(1,'姚天雪');
        (2)向表中指定的字段插入数据:
            insert into user(name) values('李萌');
        (3)向表中插入多条记录(常用)
            insert into user values(2,'姜立柱'),(3,'李萌萌'),(4,'姚甜雪');
    2.删除:delete from 表名 [where 条件];
        (1)删除所有数据
            delete from user;
        (2)删除带条件数据
            delete from user where id=1;
            delete from user where name='李萌';
    3.修改:update 表名 set 字段=值 [where 条件]
        (1)修改所有的数据
            update user    set id=5;
            update user set id=6,name='姚老师';
        (2)修改带条件的数据
            update user set name='李萌' where id=3;
        (3)练习:
            将id为4的老师名字修改成姚明:update user set name='姚明' where id=4;
            将李萌老师的id改成10:update user set id=10 where name='李萌';
            将id>2的老师的id都改成90:update user set id=90 where id>2;
    4.查询(****************************):对表中的数据没有任何修改
        (1)查询所有的字段
            select * from user;
        (2)查询指定的字段
            select name from user;
        (3)查询带条件的数据 where
            a.比较=、<、<=、〉、〉=、!=、^、!>、!<
                select * from user where id!=10;
                select * from user where id>=3;
            b.关键字查询in、not in
                select * from user where id in(2,4);//只查2和4,不是2-4
                select * from user where id in(20);
                select * from user where id not in(2,3,4);
            c.范围查询between and、not between and
                select * from user where id between 1 and 3;//查1-3(包括1和3),不是1和3
                select * from user where id not between 1 and 3;//查不在1-3之间的
            d.like模糊查询:%在哪哪里随意   _在哪哪必须有   没有符号必须没有
                <1>%:可以匹配0到多个
                select * from user where name like '%雪';//雪字前面可有可无;雪字后面必须没有
                <2>_:只能匹配1个
                select * from user where name like '_雪';//雪子前面必须一个;雪字后面必须没有
            e.null、is not null空值查询
                select * from user where id is null;
                select * from user where id is not null;
            f.and和or多条件查询 and(&&) or (||)
                select * from user where id=5 and name='张雪晴';
                select * from user where id=5 or name='姚甜雪';
        (4)order by:以..排序
            select * from user order by id [asc];按照id从小到大排序
            select * from user order by id desc;按照id从大到小排序
            需求:将姓名中带雪字的老师查询出来并按照id从大到小排序
            select * from user where name like '%雪%' order  by  id desc;
        (5)group by:以..分组(一般和聚合函数count(*)、sum(字段)、max(字段)、min(字段)、avg(字段))
            a.统计各个id有多少人:select id,count(*) from user group by id;
            b.统计各个id的老师年龄的总和:select id,sum(age) from user group by id;
            c.统计各个id年龄最大的老师:select id,max(age) from user group by id;
            d.统计各个id年龄最小的老师:select id,min(age) from user group by id;
            e.统计各个id的平均年龄:select id,sum(age)/count(*) from user group by id;
                                    select id,avg(age) from user group by id;
三。数据控制语言DCL(grant,revoke)
    (1)grant sth to sb:grant 权限类型 on 数据库名.表名 to '用户名'@'主机名' [with grant option];
    (2)revoke sth from sb:revoke 权限类型 on 数据库名.表名 from '用户名'@'主机名';
四。其他语句
    数据库导出:mysqldump -u用户名 -p密码 数据库名 表名>本地>本地地址(D:/a.sql)
    数据库导入: mysql    -u用户名 -p密码 数据库名 表名 <本地地址(D:/a.sql)
五。约束
    主键: primary key
    外键: foreign key references
    唯一:unique key
    非空:not null
    自增长:auto_increment
    默认:default

发布了15 篇原创文章 · 获赞 2 · 访问量 5240

猜你喜欢

转载自blog.csdn.net/qq_41985689/article/details/80674893