MySQL基础整理
D表示浮点型数据小数点之后的精度,四舍五入法;
M-D为小数点前面位数,超出插入会提示报错,小数点最多显示D位;
decimal在插入数据的时候是多少就是多少,float/double存在小数点后精度丢失问题;
float/double存储的是近似值——–decimal以字符串形式;
datetime默认为空 插入null则位null; timestamp默认值不为空,插入null则为当前时间;
show databases; 显示数据库;
create database dataname; 新建dataname数据库;
use dataname; 使用dataname数据库;
exit; 退出连接;
create tables name( id int(4), name varchar(5), death date ); 创建name表;
select * from name; 查询表name全部数据; describe name; 查询name表结构;
insert into name vlaues(‘123’,‘王二’,‘2000-10-1’); 插入数据 insert name values(‘123’,‘王二’,‘2000-10-1’); insert name(id) values(‘123’); delete from name where name=‘王二’; 删除数据; update name set name=’王三‘ where name=’王二‘ 更新数据; drop table name; 删除name表;
创建用户 create user “username”@“host” identified by “password”;
删除用户 drop user ‘username’@‘host’;
授权 grant privileges on databasename.tablename to ‘username’@‘host’ IDENTIFIED BY ‘password’;
删除权限 revoke privileges on databasename.tablename from ‘username’@‘hose’;
distinct关键字 返回不同的值,id,name都加的话则两个列都不同,否则返回; limit关键字 limit n 返回不多于n行,limit x,y 返回从x+1行开始 检索y行; select table.name 返回表的name列 也可限制from中条件;
指定排序方向 默认的排序方向是升序排序(A-Z) 降序排序(Z-A)使用desc关键字 select * from table order by id desc;
select * from table order by id desc, name asc; 将id降序,name升序排列;
<> / != 不等于 between 在两者之间
select * from table where id between 01 and 04; 返回id在01到04的项,包括两端;
多个条件可以使用and 或者 or 连接; in指定条件范围 select * from table where id in(100,103); not否定后面跟的条件
select * from table where name like ”李%“;
select avg(distinct fenshu) from table; 只考虑分数中的不同值
select id, count() as sum from table group by id having count()>2; 先将表对id进行分组,过滤出id频率大于2的数据;
在一条select语句中关联表
id name在table1,class在table2中 select table1.id,name,class from table1,table2 where table1.id=tables2.id
笛卡儿积:由没有联结条件的表关系返回的结果为笛卡尔积,返回的则是第一个表行数*第二表行数
表别名用法 select t1.id,name,class from table1 t1,table2 t2 where t1.id=t2.id
重命名表:rename table table1 to table
索引可分为普通索引,唯一索引,全文索引,多列索引
- 原文作者:徐同学的博客
- 原文链接:https://blogs.xwnlearn.cn/post/2020/MySQL%E5%9F%BA%E7%A1%80%E6%95%B4%E7%90%86/
- 版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. 进行许可,非商业转载请注明出处(作者,原文链接),商业转载请联系作者获得授权。