mysql常用语句入门整理

这篇属于小白入门级别,如果你已经高手可以直接跳过

1.运行数据库mysqld.exe,客户端直接mysql -uroot(root是默认用户名) -p

2 showdatabases,showtables;drop databases name ,drop table name 展示删除库表

3 desc table 观察表结构

4

create table test(

id int unsigned auto_increasement primary key,
name varchar(30) not null default 'lyz'
);

  创建数据表,主键无符号自增,名字非nul默认 ,注意能用varchar最好用varchar,因为从大小角度上看varchar优于char的

5 增

insert into test (name) value('qll');

如果id设置主键自增的话,可以不写,当然name如果没有值得话则默认lyz

6删

delete from test where name='qll'

这里如果不加where则整个表清空,where尽量用id,减少索引,用了where最好索引,因为如果有索引是一次,没有是循环

7改

update test set name='ql' where name='qll';

如果不加where则这一列全部更新;

8查

select * from test;

这里可以是各个字段具体名字;

9.函数(随机,限制,计数)

select * from test limit 1,2这是从第几个开始输出几个
select * from test order by id desc 倒叙
select * from test order by rand() 随机排序
select *count(*) as total from test 输入并统计共有多少人

10 where可以结合in,and  大于,小于一起使用

11 更改表字段

alert table test  modify name varchar(10);更改字段属性用modify
alert table test  change name  username varchar(10);更改字段名用change
alert table test add index name(name);给某个字段添加索引;
alert table test drop index name;删除索引

12 类查询和正则查询

select * from test where name like%q%’;注意like查询要加%%
select * from test where name reqexp  ‘ ^q’;正则查询,不过不建议使用,速度不好

13分组和聚合

select *count(*)  as total from test group by name;根据name字段分组,有分组必有count聚合,不然所有的数据会压缩为一条

 14左连接

select user.name, post.title  from users left join post on user.id =post.id
//注意左连接时on不是where也不是having

15数据库权限

grant select on *.* to user1@192.168.1 idemntified by '123' //授权查
grant all on *.* to user1@192.168.1 idemntified by '123'//授权所有权限

16本想把日志放在本文,但后来想日志已经属于偏高级内容,暂时自己也不是太熟练,准备等熟之后,和主从复制,一起放置

原文地址:https://www.cnblogs.com/lyz1991/p/5709482.html