mysql必背语句_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 intostudent (id,user_name,age,class_id)values (11,'aa1',4,4),(12,'aa2',4,4);

insert intostudent 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.删(删除数据操作)

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

举例:deletefromstudent whereid=12;

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

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

举例:update student setuser_name='新值',age=11whereid=11;

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

语法:select字段from表名

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

举例:selectid,user_name fromstudent whereid<100order byid limit 2;

3.条件子句(重点)

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

举例sql:select*fromstudent whereid>2groupbyclass_id having class_id!=1order byclass_id limit 10;

格式:select字段from表名

[where字句]

[groupby字句]

[having字句]

[order by字句]

[limit字句];

//1.where子句

作用:筛选数据

用法:

and:...whereid>2andid!=5;//id>2,但!=5的

or:...whereid=2orid=3;//id=2,或id=3的

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

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

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

between :...whereid between 1and10;//id在1到10内的

//2.group by子句

作用:分组

用法:selectavg(age),class_id fromstudent groupbyclass_id;//班级平均年龄

使用场景:通常用于计算组中字段的max、min、avg、sum

//3.having

作用:筛选groupby分组后的数据

用法:selectavg(age),class_id fromstudent groupbyclass_id having class_id!=1;

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

//4.order by

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

用法:select*fromstudent order byage desc;

//5.limit

作用:限制数据条数

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

连表查询(重点)

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

//1.cross join交叉连接

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

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

举例:select*fromproduct,product_type;

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

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

举例:select*fromproduct 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*fromproduct whereprice>(

selectmax(price)fromproduct wherepro_name like '%索尼%'

);

in:

用法举例:select*fromproduct whereproduct_id in(

selectproduct_id fromproduct_type whereproduct_name like '%电%'

);

any/some和any:

/*

表1: 表2:

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

| f1 | f2 | | c1 | c2 |

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

| 1 | 5 | | 1 | 5 |

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

| 3 | 8 | | 3 | 8 |

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

| 6 | 13 |

-----------

*/

any/some:

1)select*from表1wheref2>any(selectc2 from表2);//(3,8)

2)select*from表1wheref2>=any(selectc2 from表2);//(1,5),(3,8)

3)select*from表1wheref1>=any(selectc2 from表2);//空

all:

1)select*from表1wheref2

2)select*from表1wheref2<=all(selectc2 from表2);//(1,5)

3)select*from表1wheref1<=all(selectc2 from表2);//(1,5),(3,8)

//联合查询*******************************

含义:将两个'字段一致'的表的查询结果合并在一起

语法:select语句1

union[all]//是否消除重复行

select语句2;

举例:select*fromproduct wherepro_id=1unionselect*fromproduct wherepro_id=2;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值