首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Mysql命令

创建数据库:

create database database-name;(可规定使用的字符:create database database-name charset utf8)

create database '中文名字' charset utf8;

create database database-name

default character set utf8;(指定一个默认的字符集创建数据库)

创建表:

create table table-name(field-name);(在当前数据库下面建立)

create table if not exists database-name.table-name(field-name);(在某个数据库下面建立)

create table table-name(

field1 type,

field2 type,

field3 type

); (指明字段和数据类型)

进入数据库:

usedatabase-name;

查看当前使用的数据库:

select database();

查看所有数据库:

show databases;

查看当前数据库所有的表:

show tables;

查看以s开头的数据库:

show databases like 's%';

查看以s结尾的表:

show tables like '%s';

重命名表:

rename table oldtable-name to newtable-name;

查看创建数据库的语句:

show create database database-name;(关键字需要使用反引号)

查看所有的字符集:

show character set;

查看数据库默认字符集:

show create database database-name;

查看服务器默认的对外处理得字符集:

show variables like 'charater_set%';

查看表结构:

1)desc 表名;

2)describe 表名;

3)show columns from 表名;

查看看表里面的数据:

select * from 表名;(先后顺序按插入顺序排列)

查看指定字段:

select field3,field1,field2 from table-name where field3 = 1;-- 查看满足字段3为1的学生信息

查询所有列:

单表查询:

1.查询指定列:

select field2-name,field2-namefromtable-name;

2.查询时指定别名:

select field1-nameas'别名’ ,field2-name as ‘别名’ from table-name as ‘别名’;(看情况写别名,多表查询使用表的别名较多)

3.查询时添加一个常量列:

select field1-name,field2-name ‘添加的常量列’ as ‘添加的列的别名’ from student;(别名可以不加)

4.查询时合并列:

select field1-name,field2-name,(field3-name+field4-name)as ‘合并后的别名’ from table;(只能合并数值类型的字段)

select * from table-name where (field1-name+field2-name) > 值;(求合并后大于某个值的信息)

5.查询时除去重复记录:

selectdistinctfield1-name from table-name;

select distinct(field1-name)from table-name;

6.条件查询:(where)

1)逻辑条件(and,or)

select *from table-name where field1-name = ‘内容’ and field2-name = ‘内容’; (与逻辑,交集)

select*from table-name where field1-name = ‘内容’ or field2-name= ‘内容’;(或逻辑,并集)

2)比较条件( ,>= , (不等于))

select * from table-name where filed-name >值;

select * from table-name where field-name >= 值 and filed-name

select *from table-name where filed-name between 值1 and 值2;(和上面的语法作用一样)

(between and 等价于 >= 且

select * from table-name where field-name ' 不等于的内容 ';

3)判空条件(null(is null、is not null), 空字符串(=‘ ’ ,, ' ')

null :表示没有值

空字符串:有值!

select*from table-name where field1-name is null;(只显示表里为null的行)

select*from table-name where field1-name = ’ ‘ ;(显示为空的行)

select*from table-name where field1-name is null or field1-name= ’ ‘;(只要没有值都显示)

select * from table-name where field-name is not null and field-name ’ ‘;( 显示不为空和null的)

4)模糊条件(like)

select*from table-name where filed-name like ’内容%‘;

select * from table-name where field-name like ’内容_‘;(表示匹配到内容后面的一个字符,有几个_就匹配到几个字符)

7.聚合查询:使用聚合函数的查询

常用的聚合函数:sum(),avg(), max(), min(),count() (count函数统计的不包括null的数据)

select sum(field-name) as '别名‘ from table-name; 求某一列的和

select avg(field-name) from table-name ;求某一列的平均分

select count(*) from table-name; 统计一张表里面所有字段数量,取最大值(*表示所有字段)

select count(field-name) from table-name; 统计某个字段的数量

8.分页查询:(limit 起始行 ,查询几行)

起始行从0开始

1)从第一行开始查询两行:(相当于第一页)

select*from table-name limit 0,2;

公式:select*from table-name limit (当前页-1)*每页显示多少条,每页显示多少条;

9.查询排序:

语法:order by 字段 、

asc:顺序,正序。数值:递增,字母:自然顺序

desc:倒叙,反旭。数值:递减,字母:自然反旭

select *from table-nameorder byfield-name asc;(如果不写asc ,默认正序排序)

select *from table-name order by field-name desc; 降序排列

多个排序条件:(先field1-name按正序排,再按field2-name倒序)

select*from table-name order by field1-name asc,field2-name desc;(字段名再多也只写一个order by)

10.分组查询:(group by)

先分组,再统计数量

1)select field1-name ,count(*) from table-name group by field-name;

分组查询后筛选:(group by 后面不要用where,用having,分组之前用)

select field-name,count(*)from table-name group by table-name having count(*) >(=,

alter:

1)修改字段类型或长度

alter table 表名modify column字段名 数据类型;(数据类型直接写改变后的长度和类型)

2)修改字段名

alter table 表名change column现有字段名 修改后的字段名 数据类型(原来的数据类型)

3)在表中增加字段并放到第一个位置:

alter table 表名,

add column 字段名 数据类型,

first; (若不加则默认放到最后的位置)

增加多个字段:

alter table table-name add field1-name type,add field2-name type;

4)修改数据库的字符集:

alter database database-name default character set GBK;

5)把name1 变为固定长度且放到name2 的后面:

alter table table-name modify name1 char(10) after name2;

6)修改字段名:

alter table table-name change oldfield-name newfield-name+数据类型;

7)修改表名:

alter table oldtable-namerenametonewtable-name;(to 可省略)

修改

update table-name set field-name = ‘修改内容’;(修改某个字段所有的数据)

update table-name set field-name= ‘修改内容’ where field2-name = ‘满足条件’;

修改多个字段的内容时:

update table-name set field1-name=‘ ’,field2-name=‘ ’where field-name= ‘满足条件';

删除表中的字段:

alter table table-name drop column field-name;(column表示字段,可省略)

删除表中的数据:

delete from table-name where field-name = 数据;

删除数据库:

drop database database-name;

全部删除数据表:

drop table table-name;

delete from table-name;(可以带条件删除;只能删除表的数据,但不能删除表的约束;删除的数据可以回滚(事务))

truncate table table-name;(不能带条件删除;既可以删除表的数据,也可以删除表的约束;删除的数据不能回滚)

带条件的删除:

delete from table-name where field-name='满足条件';

插入数据:

1)insert into table-name values(a,b,c,d...),...,(e,f,g,h...)(插入所有字段 ,依次插入)

2)指定字段

insert into table-name(field1,field2,...)values (1,2,...);

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20180607G13CSG00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券