MySql命令总结

一、数据库操作:

显示数据库:show databases;

创建数据库:create database 数据库名;

删除数据库:drop 数据库名;

进入数据库:use 数据库名;

二、表操作:

显示所有表:

show tables;

创建表:

create table  表名(

字段名1 类型[(宽度) 约束条件],

字段名2 类型[(宽度) 约束条件],

字段名3 类型[(宽度) 约束条件] );

#注意: 1. 在同一张表中,字段名是不能相同 2. 宽度和约束条件可选 3. 字段名和类型是必须的

约束条件:

自增:auto_incr ement

主键:primary key

   或primary key(字段1,字段2…… ) (主键不能为空)

默认值:default 值(不输入时设置默认值)

外键;foreign key(文件内部字段名) references 表名(字段名);

   constraint 外键明  foreign key(文件内部字段名) references 表名(字段名);

   

唯一索引:unique 索引名 (字段1,字段2)(可以为空)


create table class(
cid int not null auto_increment primary key,
name char(4)
);

create table student(
sid int not null auto_increment primary key,
sname char(2),
gender char(1),
class_id int,
foreign key(class_id) references class(cid));
如果为某列设置自增列,插入数据时无需设置此列,默认将自增(表中只能有一个自增列)
            create table tb1(
                nid int not null auto_increment primary key,
                num int null
            )
            或
            create table tb1(
                nid int not null auto_increment,
                num int null,
                index(nid)
            )
            注意:1、对于自增列,必须是索引(含主键)。
                 2、对于自增可以设置步长和起始值
                     show session variables like 'auto_inc%';
                     set session auto_increment_increment=2;
                     set session auto_increment_offset=10;

                     shwo global  variables like 'auto_inc%';
                     set global auto_increment_increment=2;
                     set global auto_increment_offset=10;
自增

删除表:

drop table 表名;

重命名表:

alter table 旧表名 rename 新表名;

rename table 旧表名 to 新表名;

查看表结构:

show create table 表名 G; 显示表内详细信息,包含存储引擎和字符集等(G便于观看)

desc 表名;describe 表名

更改自增列的初始值:alter table 表名 set AUTO_INCREMENT = 值

更改表结构:

ALTER table 表名 add 字段名 数据类型(长度) default 0;  增加字段
alter table 表名 drop 字段名;                                             删除字段名
alter table 表名 alter 字段名 set default '默认值' ; 设置默认值
alter table 表名 modify 字段名 数据类型(长度) not null; 更改数据为非空

复制表

create table 新表名 select * from原表;复制表数据,不带外键,主键,自增,触发器

create table 新表名 like 原表;只复制表结构,不带数据

三、数据操作:

新增数据:insert into 表名;(字段名1,字段名2)values(值11,值12),(值21,值22);

导入数据:insert into 表名1 (字段名11,字段名12)select 字段21,字段22 from 表名2;

清除数据:delete from 表名;(此方法清除之后加入的数据,自增列延续未清除标号)

     truncate table 表名;(此方法清除之后加入的数据,自增列从1开始,大量数据此方法更快)

条件删除:delete from 表名 where 条件;

修改数据:update 表名 set 字段名=值;(将所有的字段名统一值)

     update 表名 set 字段名=值 where 条件(将满足条件的字段名统一)

查看数据:select * from 库名.表名;全部数据 (库名可以省略)

       select 字段名1,字段名2 as 字段名3 ,xx from 表名;别名查看指定列后跟随xx列

     select 字段名 from 表名 where 条件名 in (条件值1,条件值2……);多条件筛选

     select 字段名 from 表名 where 条件名 between 条件值1 and 条件值2;区间筛选 闭区间

     select 字段名 from 表名 where 值 like ‘’X_‘’ 或‘’X%‘’;迷糊匹配,_一个字符,%任意多字符

     select 字段名 from 表名 limit n;返回前n条数据

     select 字段名 from 表名 limit m,n;返回从m开始的n条数据

     select 字段名 from 表名 limit n offset m;返回从m开始的n条数据

     select 字段名1 from 表名 order by 字段名2 asc或desc;asc为从小到大,desc为从大到小 

分组查看:

     select 字段名1 from 表名 group by 字段名2;根据字段名2进行分组,返回每组的第一个值

     select group_concat(字段名1)  from 表名 group by 字段名2;根据字段名2进行分组,返回每组的所有值,用逗号分隔

     select count(字段名1)  as 别名,字段名2 from 表名 group by 字段名2;分组统计并显示别名

     select max/min(字段名1),字段名2 from 表名 group by 字段名2;显示组内最大/最小成员

     select sum/avg(字段名1),字段名2 from 表名 group by 字段名2;分组求和/平均值

     **count,max,min,sum,avg成为聚合函数,进行二次筛选使用having 而不是where

 连表:将几张表的关联数据显示出来

  select 字段名 from 表名1,表名2 where 表名1.字段名1=表名2.字段名2;

  select 字段名 from 表名1 left join 表名2 on 表名1.字段名1=表名2.字段名2;#左边全显示

  select 字段名 from 表名1 right join 表名2 on 表名1.字段名1=表名2.字段名2;#右边全显示

  select 字段名 from 表名1 inner join 表名2 on 表名1.字段名1=表名2.字段名2;#包含null的值隐藏

----上下连表:列数相同时 表1 union 表2:将两张表的数据合成一张表显示(自动去重)

      列数相同时 表1 union  all 表2:将两张表的数据合成一张表显示(不去重)

 

视图:

create view 视图名 as select……(筛选表名),动态获取符合条件的表,作为视图,不能进行增删改

触发器:

create trigger 触发器名 before/after insert/delete/update on 表名 for each row

     begin (代码块)end

    -----使用触发器前后要使用 delimiter 特殊符号 来修改结束符号

 

原文地址:https://www.cnblogs.com/modengdai/p/9858102.html