MYSQL操作表 |
|
查询数据库 /表 |
show databases /tables |
创建数据库 |
create database 数据库名 |
显示创建的数据库(数据库之前已被创建) |
show create database 数据库名; |
修改数据库(主要修改数据库的字符集和校验规则) |
alter database 数据库名 charset=gbk; |
删除数据库/表 |
drop database 数据库名/drop tables 表名 |
备份数据库 |
mysqldump -P端口号 -u root -p密码 -B 数据库名 >存储路径 |
还原数据库 |
source 存储路径 |
source F:\my.sql; |
mysqldump -uroot -p -B helloworld>F:\my.sql (无分号) |
备份多个数据库 |
mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径 |
创建表 |
create table 表名(数据名称 数据类型,数据名称 数据类型) |
复制表 |
create table 新表名 like 表名 |
删除表(中的所有数据,表存在) |
delete from 表名 |
向表中添加字段 |
alter table 表名 add 字段名 字段类型 after 已存在的字段 alter table 表名 add 字段名 字段类型 first |
|
|
修改表中字段的类型或长度 |
alter table 表名 modify 字段名 修改后的字段类型或者长度 |
重命名表名 |
alter table 表名 rename [to] 新表名 |
重命名字段名 |
alter table 表名change 字段名 新字段(字段名 类型) |
插入数据 |
Insert into 表名(数据类型) values(数据) |
Insert into 新表名 select distinct *from 表名
| |
删除数据(某条) |
Delete from 表名 [条件] |
在表中更新数据 |
update 表名 set 字段名=改完后的数值 [where条件] |
截断表(会重置自增项) |
turncate 表名 |
查看表结构 |
desc 表名 |
查询表(所有数据) |
select* from 表名 |
查询表(有条件的查询) |
select 所查询数据的名称 from 表名 (条件) |
查询表(去重结果) |
select 字段名 distinct from 表名 |
|
|
查询索引 |
show keys from 表名\G
|
show index from 表名 | |
删除索引 |
alter table 表名 drop primary key;
|
alter table 表名 drop index 索引名; 索引名就是show keys from 表名 中的 Key_name 字段 | |
drop index 索引名 on 表名
| |
删除主键 |
alter table 表名 drop primary key |
|
|
查看字符集/校验规则 |
show variables like ‘character_set_database’/’collation_database’ |
查看字符集 |
show charset |
查看校验规则 |
show collation |
校验规则不区分大小写 |
create database test1 collate utf8_general_ci; |
校验规则区分大小写 |
create database test2 collate utf8_bin; |
查看连接情况 |
show processlist |
enum 单选 |
enum (‘事件1’,’事件2’,’事件3’) |
set 多选 |
set (‘事件1’,’事件2’,’事件3’) select*from 表名 where find_in_set(‘事件’,字段名) |
zerofill |
填充长度 |
添加主键(当表中无主键时,才可以追加)同样的方法追加外键 |
Alter table 表名 add primary key(字段列表) |
复合主键 |
primary key(字段名1,字段名二) |
外键 |
Foreign key(字段名) references 主表(列) |
更新重复键的数值 |
insert into 表名 values() on duplicate key update 字段名=更新后的数值,字段名=更新后的数值; |
替换(插入)重复键中的数值 |
replace into 表名(字段名,字段名) values([新]字段名,字段名) |
降序 |
字段名[where条件] order by 字段名 desc |
升序 |
字段名 order by 字段名 [asc] |
模糊匹配 |
Like ‘模糊字%’ ’ |
严格匹配 |
Like ’模糊字-’ |
limit 显示语句条数 offset 从第几条语句开始计数 |
limit 3 offset 0 从第一条语句开始,显示三条语句 |
分组查询 |
select 字段名 from 表名 group by 字段名 [having 条件] |
查询次数 |
select count(*) from 表名 [条件] *可以替换为要查询的字段 |
查询总和 |
select sum(*) from 表名 [条件] *可以替换为要查询的字段 |
查询平均 |
select avg(*) as 新字段名 from 表名 [条件] *可以替换为要查询的字段 |
查询最大分 |
select max(*) from 表名 [条件] *可以替换为要查询的字段 |
查询最小分 |
select min(*) from 表名 [条件] *可以替换为要查询的字段 |
查询当前用户 |
select user() |
查询正在使用的数据库 |
select database(); |
如果val1为null,返回val2,否则返回val1的值 |
ifnull(val1, val2) |
内连接 |
select 字段 from 表1 inner join 表2 on 连接条件 and 其他条 |
左连接(以左表为准) |
select 字段 from 表1 left join 表2 on 连接条件 and 其他条 |
右连接(以右表为准) |
select 字段 from 表1right join 表2 on 连接条件 and 其他条 |
创建视图 |
create view 视图名 as select语句 |
删除视图 |
drop view 视图名 |
创建用户 |
create user '用户名'@'登陆主机/ip' identified by '密码'; |
删除用户 |
drop user '用户名'@'主机名' |
SQL查询各个关键字的执行先后顺序:from>on>join>where>group by>with>having>select>distinct>order by>limit
使用now()可以调用当前时间
Bit(M) M表示位数 bit(1) 插入的值的二进制表达式的位数小于等于M
使用精度高的类型就使用decimal
索引创建原则:
视图规则和限制