mysql commandline DDL DML DCL

  • 登录
    • mysql -p -u root -h localhost
  • DDL:表的定义以及结构的修改。
    • 库:

      • create database name;
      • drop database db1
      • show databases;
      • use db1;
    • 表:

      • 创建表
        • create table name(
          age int(8),
          name char(9),
          realtime datetime
          );
        • desc tablename;
        • show create table table1 \G;
      • 删除表
        • drop table table1;
      • 修改表
        • alter table table1 modify age int(20) first/after name ;
        • alter table table1 add age int(9);
        • alter table table1 drop age ;
        • altler table table1 change age age1 int(4);
        • alter table table1 rename table2;
        • =rename table table1 to table2;
  • DML:表的内部数据的操作。
    • 插入
      • insert into table1(age,name,realtime) values(7,‘wang’,now());
      • insert into table1values(8,‘wang1’,now()),(9,‘wang2’,now());
    • 更新
      • update table1 set age=10 where name=‘wang’;
    • 删除
      • delete from table1 where name=‘wang’;
    • 查询
      • 简单
        • select * from table1;
      • 不重复
        • select distinct depno from emp;
      • 条件查询
        • select * from emp where depno=1 and sal>3000;
      • 排序和限制(默认升序)
        • select * from emp order by deptno asc,sal desc;
        • select * from emp order by sal limit 3;(前三条记录)
        • select * from emp order by sal limit 1,3;(第2-4条记录)
      • 聚合
        • select count(1) from emp;
        • select deptno,count(1) from emp group by deptno with rollup;
        • select deptno,count(1) from emp group by deptno having count(1)>1;
      • 表连接
        • 内连接
          • select ename,deptname from emp,dept where emp.deptno=dept.deptno;
        • 外连接
          • 左连接
            • select ename,deptname from emp left join dept on emp.deptno=dept.deptno;(所有左表中的记录以及右边表中不匹配的记录)
          • 右连接
            • select ename,deptname from emp right join dept on emp.deptno=dept.deptno;(所有右表中的记录以及左边表中不匹配的记录)
      • 子查询:查询需要的条件是另外一个select语句的结果。
        • select * from emp where deptno in(select deptno from dept);
      • 记录联合
        • union all:多张表直接联合
          • select deptno from emp
            union all
            select deptno from dept;
        • union:多张表distinct去重后联合
          • select deptno from emp
            union
            select deptno from dept;
  • DCL:DBA用来管理用户权限的语句
    • 授权语句:grant to
      • grant select ,insert on mydb.* to ‘wangchaunxin’@‘localhost’ identified by ‘123’;
    • 收权语句:revoke from
      • revoke insert on mydb.* from ‘wangchuanxin’@‘localhost’;
  • ? create table;

你可能感兴趣的:(mysql)