mysql常用语句

创建数据库

       Create database db1;   默认字符编码为utf8,

修改数据库

       alter table db1 add id  int not null default '' comment '编号 ' after email_active;

删除数据库

       Drop database db1;

其他数据库操作

       Show databases;   //显示系统中所有数据库

       Use db1;    //选择db1库

数据类型(常用)

整形

类型       tinyint     -128-127(范围)

              Int          -21①--+21亿

注:1.unsigned :声明有无符号,就是不能为负,如tinyint声明无符号范围为0~256

    2.zerofill:填充0,数字1在tinyint类型填充后为001

浮点型

类型:                         decimal(m,d) 

大小(字节):            依赖于m和d的值   

范围(有无符号):     -8388608~8388607

字符类型

Char       0-255            固定长度

Varchar  0-65535         变化长度

Text        0-65535         长文本数据

Enum     <=65535        单选类型

Set          <=64             多选类型

时间类型

Date       范围:1000-01-01/9999-12-31 

Time                -838:59:59/838:59:59

Year                1901/2155

Datetime          1000-01-01 00:00:00/9999-12-31 23:59:59

Timestamp       1970-01-01 00:00:00/2037 年某时

数据增删改查(重点)

//数据的增/删/改/查*******************************【重点】

//1.增(添加数据操作)

语法1 : insert into 表名 [(字段1,字段2...)] values (值1,值2...);

举例 : insert into student (id,user_name,age,class_id) values (11,'aa1',4,4),(12,'aa2',4,4);

insert into student values (11,'aa1',4,4),(12,'aa2',4,4);


//以下3种写法不推荐(语法2有歧义,语法3用的少,语法4不可批量插入)

语法2:replace into 表名 [(字段1,字段2...)] values (值1,值2...);

语法3:insert into 表名 [(字段1,字段2...)] select * from.....;

语法4:insert into 表名 字段1=值1,字段2=值2,......;

//2.删(删除数据操作)

语法: delete from 表名 where 条件 [order by排序] [limit限定];

举例: delete from student where id=12;

//3.改(修改数据操作)

语法: update 表名 set 字段1=值1,字段2=值2 [where条件] [order by排序] [limit限定];

举例: update student set user_name='新值',age=11 where id=11;

//4.查(查询数据操作)

语法: select 字段 from 表名

[where字句] [group by字句] [having字句] [order by字句] [limit字句];

举例: select id,user_name from student where id<100 order by id limit 2;

3.条件子句(重点)

//条件子句*******************************【重点】

举例sqlselect * from student where id>2 group by class_id having class_id!=1 order by class_id limit 10;

格式: select 字段 from 表名

[where字句]

[group by字句]

[having字句]

[order by字句]

[limit字句];

//1.where子句

作用: 筛选数据

用法:

and ...where id>2 and id!=5; //id>2,但!=5的

or ...where id=2 or id=3; //id=2,或id=3的

not ...where not (id>2); //除去id>2的

like ...where user_name like '%王%'; //user_name中有'王'字的

in ...where id in (101,102); //id为101和102的

between ...where id between 1 and 10; //id在1到10内的

//2.group by子句

作用: 分组

用法: select avg(age),class_id from student group by class_id; //班级平均年龄

使用场景: 通常用于计算组中字段的maxminavgsum

//3.having

作用: 筛选group by分组后的数据

用法: select avg(age),class_id from student group by class_id having class_id!=1;

使用场景: 配合group by使用,筛选分组数据

//4.order by

作用: 对数据排序 asc默认正序/desc倒序

用法: select * from student order by age desc;

//5.limit

作用: 限制数据条数

用法: select * from student limit 4,3;#第4行开始,3条数据

 

 

连表查询(重点)

//各种连接方式join**********************************

//1.cross join交叉连接

描述:笛卡尔乘积,获得数据条数为'表1条数*表2条数'

语法:select * from 1 cross join 2; #或者select * from 表1,表2;

举例:select * from product,product_type;

//2.inner/left/right join 内/左/右连接 【重点】

语法:select * from 1 inner/left/right join 2 on 1.字段=表2.字段;

举例:select * from product inner join product_type on product.product_id=product_type.product_id;

区别:

inner join : 两表交集。只查询两个表间符合条件的数据

left join : 以左表为主导。显示左表全部数据,右表显示符合条件记录,记录不足的均为null

right join : left join相反。

子查询

//子查询******************************

概念:在一个select语句内部,还有select语句

缺点:子查询能够实现很多复杂功能,但性能差,查询速度慢

分类:

按返回结果分:

多行多列(表):当做'表'使用,用法举例'select * from (子查询) as tb1'

一行多列(行):当做'行'使用,用法举例'select * from 表1 where (id,name)=(子查询)'

多行一列(列):当做'多个值'使用,用法举例'select * from 表1 where id in (子查询)'

一行一列(标量):当做'一个值'使用,用法举例'select * from 表1 where id=(子查询)'

按使用位置分:select/from/where子句后面都能使用子查询。总之,操作数据的地方就能使用子查询

使用场景:

where比较运算符:

用法举例:select * from product where price>(

select max(price) from product where pro_name like '%索尼%'

);

in:

用法举例:select * from product where product_id in(

select product_id from product_type where product_name like '%电%'

);

any/someany:

/*

表1: 表2:

----------- -----------

| f1 | f2 | | c1 | c2 |

----------- -----------

| 1 | 5 | | 1 | 5 |

----------- -----------

| 3 | 8 | | 3 | 8 |

----------- -----------

| 6 | 13 |

-----------

*/

any/some:

1) select * from 1 where f2>any(select c2 from 2); //(3,8)

2) select * from 1 where f2>=any(select c2 from 2); //(1,5),(3,8)

3) select * from 1 where f1>=any(select c2 from 2);//空

all:

1) select * from 1 where f2<all(select c2 from 2); //空

2) select * from 1 where f2<=all(select c2 from 2); //(1,5)

3) select * from 1 where f1<=all(select c2 from 2); //(1,5),(3,8)

  1. //联合查询*******************************
  2. 含义: 将两个'字段一致'的表的查询结果合并在一起
  3. 语法: select 语句1
  4. union [all] //是否消除重复行
  5. select 语句2;
  6. 举例: select * from product where pro_id=1 union select * from product where pro_id=2;
原文地址:https://www.cnblogs.com/zwtqf/p/6740762.html