MySQL是一个关系型数据库管理系统,在 WEB 应用方面,MySQL是最好的关系数据库管理系统应用软件。
一、MySQL基础语法
博主用了半个多月的时间学完了MySQL数据库的一些基本操作,现在将我整理后的一些MySQL语法分享给大家,说白了,也就是学习时的笔记。
创建数据库
create database 数据库名称;
创建数据库时指定编码方式
create database 数据库名称 character set 编码方式 collate 编码方式_bin;
查看数据库
show databases;
修改数据库编码
alter database 数据库名称 default character set 编码方式 collate 编码方式_bin;
删除数据库
drop database 数据库名称;
创建数据表
create table 表名(
字段名,数据类型,
字段名,数据类型,
...
);
查看表结构
desc 表名;
查看当前数据库所有表
show tables;
查看表的建表语句
show create table 表名;
表中增加列
alter table 表名 add 列名 类型;
修改某列类型的长度
alter table 表名 modify 列名 类型;
删除某列
alter table 表名 drop 列名;
更改表名
rename table 原表名 to 新表名;
修改表的字符集为UTF8
alter table 表名 character set utf8;
修改列名
alter table 表名 change 原列名 新列名 类型;
删除表
drop table 表名;
修改字段的排列位置
alter table 表名 modify 字段名1 数据类型 first|after 字段名二;
单字段主键约束
字段名 数据类型 primary key;
唯一约束
字段名 数据类型 unique;
默认约束
字段名 数据类型 default 默认值;
设置表的字段值自动增加
字段名 数据类型 auto_increment; (设置字段值的自动增加时必须设置字段为主键)
创建表的时候创建普通索引
create table 表名(字段名1 数据类型 [条件约束],
字段名2 数据类型 [条件约束],
...
index(字段名1 长度)
);
创建表的时候创建唯一索引
create table 表名(字段名1 数据类型 [条件约束],
字段名2 数据类型 [条件约束],
...
unique index 别名(字段名1 (长度) ASC|DESC )
);
创建表的时候创建全文索引
create table 表名(字段名1 数据类型 [条件约束],
字段名2 数据类型 [条件约束],
...
fulltext index 别名(字段名1 (长度) ASC|DESC )
)engine=myisam;
创建表的时候创建多列索引
create table 表名(字段名1 数据类型 [条件约束],
字段名2 数据类型 [条件约束],
...
index 别名(字段名1 (长度),字段名2(长度) ASC|DESC )
);
创建表的时候创建空间索引
create table 表名(字段名1 数据类型 [条件约束],
字段名2 数据类型 [条件约束],
字段名3 geometry not null,
...
spatial index 别名(字段名1 (长度) ASC|DESC )
)engine=myisam;
创建普通索引
create index 别名 on 表名(字段名(长度) ASC|DESC);
创建唯一索引
create unique index 别名 on 表名(字段名(长度) ASC|DESC);
创建多列索引
create index 别名 on 表名(字段名(长度),字段名(长度));
创建全文索引
create fulltext index 别名 on 表名(字段名(长度) ASC|DESC);
创建空间索引
create spatial index 别名 on 表名(字段名(长度) ASC|DESC);
创建普通索引
alter table 表名 add index 别名(字段名(长度) ASC|DESC);
创建唯一索引
alter table 表名 add unique 别名(字段名(长度) ASC|DESC);
创建多列索引
alter table 表名 add index 别名(字段名(长度),字段名(长度));
创建全文索引
alter table 表名 add fulltext index 别名(字段名(长度) ASC|DESC);
创建空间索引
alter table 表名 add spatial index 别名(字段名(长度) ASC|DESC);
删除索引
alter table 表名 drop index 索引名;
删除索引
drop index 索引名 on 表名;
指定所有字段名添加数据
insert into 表名(字段名1,字段名2,...)
values(值1,值2,...);
不指定字段名添加数据
insert into 表名 values(值1,值2,...);
为表的指定字段添加数据
insert into 表名(字段1,字段2,...)
values(值1,值2,...);
其他添加数据方法
insert into 表名
set 字段名1=值1,字段名2=值2...;
同时添加多条记录
insert into 表名 values
(值1,值2,值3,...),
(值1,值2,值3,...),
...;
更新部分数据
update 表名
set 字段名1=值1,字段名2=值2
[where 条件表达式];
更新全部数据
update 表名
set 字段名1=值1,字段名2=值2...;
删除部分数据
delete from 表名 [where条件表达式];
删除全部数据
delete from 表名;
使用truncate删除表中所有数据
truncate table 表名;
查询指定所有字段
select 字段名1,字段名2,...from 表名;
查询所有字段
select * from 表名;
带关系运算符的查询
select 字段名1,字段名2 from 表名 where 条件表达式;
带in关键字的查询
in关键字判断某个字段值是否在指定集合中,如果有则被查询出来
select 字段名1,字段名2, from 表名 where 字段名 [not] in (元素1,元素2);
带between and关键词的查询
select 字段名1,字段名2 from 表名 where 字段名1 [not] between 值1 and 值2;
空值查询
select 字段名1,字段名2 from 表名 where 字段名1 is [not] null;
带distinct关键字查询
select distinct 字段名 from 表名;
带like关键字的查询(模糊查询)
select 字段名1,字段名2 from 表名 where 字段名1 like '匹配的字符串';
带and关键字多条件查询和带or关键字的多条件查询
select 字段名1,字段名2 from 表名 where 条件表达式 and 条件表达式;
select 字段名1,字段名2 from 表名 where 条件表达式 or 条件表达式;
对查询结果排序
select 字段名1,字段名2 from 表名 order by 字段名1[ASC|DESC];
分组查询
select 字段名1,字段名2 from 表名 group by 字段名1;
select 字段名1,字段名2 from 表名 group by 字段名1 having 条件表达式;
限制查询结果的数量
select 字段名1,字段名2 from 表名 limit [offset] 记录数;
为表取别名
select * from 表名 [AS] 别名;
例:select * from student as s where s.gender='女';
为字段取别名
select 字段名[AS] 别名 from 表名;
为表添加外键约束
alter table 表名 add constraint 别名 foreign key(外键字段名) references 外表表名(主键字段名);
删除外键约束
alter table 表名 drop foreign key 外键名;
内连接
select 查询字段 from 表1 [inner] join 表2 on 表1.关系字段=表2.关系字段;
外连接(左、右连接)
select 所查字段 from 表1 left|right join 表2 on 表1.关系字段=表2.关系字段 where 条件;
设置隔离级别
set session transaction isolation level 隔离级别;
查看当前隔离级别
select @@tx_isolation;
创建存储过程
create procedure 存储过程名称()
begin
SQL语句...
end;
执行存储过程
call 存储过程名称();
结束符设置
delimiter 结束符
单表上创建视图
create view 视图名称 as 查询语句 from 表名;
查看视图
1、desc 视图名; (查看视图字段信息)
2、show table status like '视图名'; (查看视图基本信息)
3、show create view 视图名; (查看视图的创建语句)
修改视图
alter view 视图名 as 查询语句;
更新视图
update 视图名 set 设置;
insert into 表名 values (字段);
delete from 视图名 where 表达式;
删除视图
drop view 视图名;
备份单个数据库
mysqldump -u 用户名 -p 数据库名称 >文件存放路径
备份多个数据库
mysqldump -u 用户名 -p --database 数据库名1 数据库名2 >文件存放路径
备份所有数据库
mysqldump -u 用户名 -p --all-databases >文件存放路径
数据还原
mysql -u 用户名 -p 数据库名称
创建普通用户
grant 权限[select等] on 数据库.表 to '用户名'@'localhost' identified by '密码';
例:grant select on db2.* to 'caomao'@'localhost' identified by 'abc123';
create user '用户名'@'localhost' identified by '密码';
删除普通用户
drop user '用户名'@'主机名';
delete from mysql.user where host='主机名' and '用户名'
修改root用户密码
进入C:\Documents and Settings\Administrator
mysqladmin -u 用户名 -p password 新密码
root用户修改普通用户密码
update mysql.user set authentication_string='密码' where user='用户名' and host='主机名';
普通用户修改密码
set authentication_string='密码';
授予权限
grant 权限 on 数据库.表 to '用户名'@'主机名' identified by '密码' with grant option;
查看权限
show grants for '用户名'@'主机名';
收回权限
revoke 权限 on 数据库.表 from '用户名'@'主机名';
revoke all privileges,grant option from '用户名'@'主机名';