Mysql常用操作

1. 查看数据库:show databases;

2. 创建数据库:create database kuname;

3. 删除数据库:drop database kuname;

4. 选择数据库:use kuname;

5. 创建表:

create table biaoname(

age int,

name varchar(100),

sex char(10),

income int not null,

);

6. 查看某库中的表:show tables;

7. 查看表结构:desc biaoname;

查看表类型: show table status;

8. 删除表:drop table biaoname;

9. 表中插入记录:

insert into biaoname

(ziduan1,ziduan2,ziduan3)

values

("value1","value2",number,now());

# now()是一个返回日期和时间的mysql里的函数

10. 插入多条数据:

insert into biaoname

(ziduan1,ziduan2,ziduan3)#可以省略该行,省略时,插入的值需要按顺序

values

("value1","value2",number,now()),

("value1","value2",number,now()),

("value1","value2",number,now());

11. 查询全部数据:select * from biaoname;

12. 从多个表中查询数据:select * from biao1,biao2;

13. where子句(可以用binary 关键字设置 WHERE 子句的字符串是否区分大小写):

select * from biao1

where binary biaojian1="content";

14. where子句用到的操作符:>,<,=,>=,<=,!=,多个条件可以用 and ,or连接:

select * from biao1

where tjian1="X" and tiaojian2>=4;

15. 修改或更新表中数据:where 指定条件可不写

update biaoname set field1="X",field2="M"

where tjian="content" ;

16. 删除数据没有where子句将删除表中所有的数据:

delete from biaoname where tiaojian="x" ;

17. 查询含有指定内容的数据时用like(%),不用%时like和=相同的意思:

select * from biaoname

where ziduan like "%x";

意思:查找字段含有 "x"的记录

18. 连接多个select语句用union:

select * from biaoname1

where ziduan="x"

union [ALL | DISTINCT]

select * from biaoname2

where ziduan="m";

DISTINCT: 可选,删除结果集中重复的数据。默认是删除重复项

ALL: 可选,返回所有结果集,包含重复数据。

19. 查询数据排序order by:

select field1,field2 from biaoname

where ziduan="x"

order by field1 asc, field2 desc

asc升序排序,desc降序排序,默认升序

20. 结果集分组 group by:

select classname,avg(grade) from biaoname

group by classname;

#每个班级的平均成绩

在分组统计的基础上继续统计 with rollup:

如:统计该年级的平均成绩:

select classname,avg(grade) from biaoname

group by classname with rollup ;

#每班的平均成绩之和/班级个数

由于继续统计没有指定列名所以默认为null,可用coalesce指定列明

select coalesce(classname,"指定的列明"),avg(grade) from biaoname

group by classname with rollup ;

#每班的平均成绩之和/班级个数

21. 多个数据表中读取数据inner join(inner可以不要),left join,right join:

join:会读取在多个表中都存在的数据

SELECT a.runoob_id, a.runoob_author, b.runoob_count

FROM runoob_tbl a  #runoob 临时作为a表

JOIN tcount_tbl b

ON a.runoob_author = b.runoob_author; #表示a,b匹配的字段

等价于:

SELECT a.runoob_id, a.runoob_author, b.runoob_count

FROM runoob_tbl a,tcount_tbl b

WHERE a.runoob_author = b.runoob_author; #表示a,b匹配的字段

left join:会读取左边数据表的全部数据,即便右边表无对应数据

right join:会读取右边数据表的全部数据,即便左边表无对应数据

SELECT a.runoob_id, a.runoob_author, b.runoob_count

FROM runoob_tbl a  #runoob 临时作为a表

LEFT JOIN tcount_tbl b

ON a.runoob_author = b.runoob_author; #表示a,b匹配的字段

22. mysql查找数据表中某列是否为 NULL,用 IS NULL , IS NOT NULL:

select * from biaoname

where ziduan is (not) null;

23. 修改表名或字段时使用 alter命令:

# 修改表名称

alter table biaoname rename to newname;

# 添加字段,默认为null,可以设置默认值

alter table biaoname add ziduanx int/char;

# 删除字段

alter table biaoname drop ziduanx;

# 指定字段位置,默认放在最后,after后需要指出放在哪个字段后

alter table biaoname add ziduanx int first;

alter table biaoname add ziduanx char after ziduany;

# modify修改字段类型,把字段x修改为 char类型

alter table biaoname modify ziduanx char(10);

# change 可以修改字段名称 类型,字段名,类型可以和原来的相同

alter table biaoname change ziduanx ziduany int;

# 修改字段时,可以为该字段的设置默认值(该字段原来没有默认值)

alter table biaoname modify ziduanx int not null default "ellie001";

# 修改字段的默认值用alter

alter table biaoname alter ziduanx set default 1000;

# 删除字段的默认值用drop

alter table biaoname alter ziduanx drop default;

# 修改表类型

alter table biaoname engine=myisam;

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 什么是数据库? 数据库是存储数据的集合的单独的应用程序。每个数据库具有一个或多个不同的API,用于创建,访问,管理...
    chen_000阅读 4,057评论 0 19
  • 什么是SQL数据库: SQL是Structured Query Language(结构化查询语言)的缩写。SQL是...
    西贝巴巴阅读 1,862评论 0 10
  • 此篇是为数据库小白入门而写的,介绍使用数据库的最基本也是最常用的一些操作。我也是在日常工作中东一点西一点学到的,难...
    松鼠的读书笔记阅读 2,221评论 0 4
  • 7月1日 7月2日 7月3日 7月4日 7月5日 7月6日 7月7日 7月8日 7月9日 7月10日 7月11日 ...
    在装翅膀的猪阅读 159评论 0 0
  • 真的是不舒服就下结论了,昨天晚上和舅舅不在状态就会纠结,然后就按照方法,想方设法地去破这个东西,一直很不舒服,虽然...
    阮博杰阅读 178评论 0 0