目录:mysql
数据库的基本操做正则表达式
建立、删除用户及受权sql
数据库字符校对集数据库
DML操做服务器
DDL操做session
索引并发
事务ide
1、数据库的基本操做函数
-- 选择要操做的数据库 -- world:数据库名 use world; -- 显示已存在的数据库列表 show databases; -- 显示指定数据库下的表的信息 show tables; -- 显示指定表的列的信息 -- world.country:数据库名.表名 show columns from world.country; -- 显示指定表的索引信息 -- world.country:数据库名.表名 show index from world.country; -- 显示指定数据库下的表的详细信息 -- world:数据库名 show table status from world; -- 显示指定数据库下的表名称以字母'c'开头的表的详细信息 -- world:数据库名 show table status from world like 'c%'; -- 显示数据库表的结构,如:字段名,字段类型等 -- world.country:数据库名.表名 describe world.country; -- 查看建立表的SQL语句 -- demo.test:数据库名.表名 show create table demo.test; -- 查看建立存储过程的SQL语句 -- demo.test_proc:数据库名.存储过程名 show create procedure demo.test_proc; -- 查看建立视图的SQL语句 -- demo.test_view:数据库名.视图名 show create view demo.test_view; -- 查看建立函数的SQL语句 -- demo.test_fun:数据库名.函数名 show create function demo.test_fun; -- 查看当前用户的数据库权限 show grants; -- 查看指定用户的数据库权限 -- admin@localhost:用户名@访问主机 show grants for 'admin'@'localhost'; -- 查询数据库用户信息 select * from mysql.user; -- 获取服务器版本信息 SELECT VERSION(); -- 获取当前数据库名 (或者返回空) SELECT DATABASE(); -- 获取当前用户名 SELECT USER(); -- 获取服务器状态 SHOW STATUS; -- 获取服务器配置变量 SHOW VARIABLES; 例如: -- 查询自增加值的步长,即每次增长多少,默认为1。 show variables like '%auto_increment%'; -- 设置自增加值每次增长的数值,会影响全部数据表。 set auto_increment_increment=3; -- 设置自增加值的起始值,会影响全部数据表。 set auto_increment_offset=100; -- mysql运行在安全模式下时,非主键条件下是没法执行update或者delete命令的 -- 查看安全模式状态 show variables like '%sql_safe_updates%'; -- 设置安全模式为关闭 set sql_safe_updates=off; -- 获取最近一次向具备identity属性(即自增列)的表插入数据时对应的自增列的值,@@identity是系统定义的全局变量。 select @@identity; -- LAST_INSERT_ID函数将返回当前链接自增列最新的 insert or update 操做生成的第一个记录的ID。由于其基于Connection的,因此也是线程安全的。 select LAST_INSERT_ID();
2、建立、删除用户及受权
-- 建立一个新的用户,并设置登陆密码 -- test:用户名;localhost:本地主机访问(若是须要其余任意主机访问,请使用通配符'%');123456:用户密码; create user 'test'@'localhost' identified by '123456'; -- 建立一个新的用户,不指定登陆密码,即不须要登陆密码 create user 'test01'@'localhost'; -- 删除指定的用户 drop user 'test01'@'localhost'; -- 修改用户名 -- test@localhost:要修改的用户名和访问主机 -- test@%:修改成的用户名和访问主机 rename user 'test'@'localhost' to 'test'@'%'; -- 修改用户密码 -- test@localhost:要修改的用户名和访问主机 -- 123456:新的用户密码 set password for 'test'@'localhost' = Password('123456');-- 授予指定用户'test'对于'world'数据库下'country'表的查询权限 -- select:查询权限;world.country:数据库名.表名;'test'@'localhost':用户名@访问主机; grant select on world.country to 'test'@'localhost'; -- 当即启用修改(默认再次登陆才会生效) flush privileges; -- 撤销指定用户'test'对于'world'数据库下'country'表的查询权限 -- select:查询权限;world.country:数据库名.表名;'test'@'localhost':用户名@访问主机; revoke select on world.country from 'test'@'localhost'; -- 当即启用修改(默认再次登陆才会生效) flush privileges; -- 授予指定用户'test'对于'world'数据库下全部表的查询、新增、修改、删除权限 grant select,insert,update,delete on world.* to 'test'@'localhost'; -- 撤销指定用户'test'对于'world'数据库下全部表的查询、新增、修改、删除权限 revoke select,insert,update,delete on world.* from 'test'@'localhost'; -- 授予指定用户'test'对于'world'数据库下全部表的表结构进行建立、修改、删除权限 grant create,alter,drop on world.* to 'test'@'localhost'; -- 撤销指定用户'test'对于'world'数据库下全部表的表结构进行建立、修改、删除权限 revoke create,alter,drop on world.* from 'test'@'localhost'; -- 授予指定用户'test'对于'world'数据库下全部存储过程的执行权限,而且该用户有权限转授予其余用户 grant execute on world.* to 'test'@'localhost' with grant option; -- 撤销指定用户'test'对于'world'数据库下全部存储过程的执行权限,转授予权限一并撤销 revoke execute on world.* from 'test'@'localhost';
3、数据库字符校对集
字符校对集,即排序规则,在某个字符集的状况下,字符集的排列顺序应该是什么,称之为校对集。
-- 查看全部的字符校对集 -- 后缀为_bin:表示基于二进制编码的直接比较 -- 后缀为_ci:表示对大小写不敏感的比较 -- 后缀为_cs:表示对大小写敏感的比较 show collation;
4、建立、删除数据库和表
-- 建立一个名为'test'的数据库 create database test; -- 建立一个名为'test'的数据库,若是该数据库已存在则不建立,不然再建立 -- 并指定默认字符集为'utf8',字符校对集为'utf8_general_ci' create database if not exists test default charset utf8 collate utf8_general_ci; -- 删除名为'test'的数据库 drop database test; -- 建立一个名为'Student'的数据表,若是该数据表已存在则不建立,不然再建立 -- engine:指定数据库引擎为'InnoDB' -- auto_increment:指定自增列的起始值为1 create table if not exists Student ( ID int not null auto_increment, #自动增加列 StuNo varchar(32) not null, StuName varchar(8) not null, StuSex varchar(8) null, StuBirthday tinyint null, CreateTime datetime null, primary key (ID) #指定主键列 ) engine=InnoDB auto_increment=1 default charset=utf8 collate=utf8_general_ci; -- 删除数据表 student,该操做会删除全部数据包括表结构、视图、索引、约束等。 drop table test.student; -- 删除数据表中的全部数据,该操做会删除表中全部的数据,可是会保留表结构、视图、索引、约束等。 truncate table test.student; -- 建立一个临时表,临时表的建立与数据表的建立相似,只不过须要添加关键字 temporary。 -- 临时表的做用域为当前会话,即当前链接可见,当断开当前链接时会自动销毁,固然也能够手动删除,删除方式与数据表同样。 create temporary table Product ( ProName varchar(32) not null, Price decimal(10,3) not null default 0.000 ); -- 复制指定数据表的表结构到建立的新表。 create table test.StudentBak like test.student; -- 复制指定数据表的表结构及全部数据到建立的新表。 create table test.StudentBak select * from test.student;
5、DML操做
-- 向数据表中插入数据 insert into student(StuNo,StuName,StuSex,Stubirthday,CreateTime) select 'A001','小张','男',str_to_date('1988-06-09','%Y-%m-%d'),current_timestamp() union all select 'A002','小红','女',str_to_date('1990-08-10','%Y-%m-%d'),current_timestamp() -- 在插入重复的数据时,会直接跳太重复数据的插入。在有自增列或主键的数据表中不起做用,由于自增列和主键都具备惟一性。 insert ignore into test.student(stuno,stuname,stusex,stubirthday,createtime) values ('A003','小鱼','女','1991-07-07',current_timestamp()); -- MySQL的WHERE子句默认是不区分大小写的,若是须要区分大小写,就要在字段前加上关键字 binary select * from student where stuno='a001'; #'1', 'A001', '小张', '男', '1988-06-09', '2018-01-12 12:17:00' select * from student where binary stuno='a001'; #null -- limit:用于设置返回的记录数。 -- offset:用于设置select语句开始查询的数据偏移量,默认为零。 -- 表示只取前10条数据 select * from world.city limit 10; -- 表示跃过5条,从第6条数据开始取10条数据。 select * from world.city limit 10 offset 5; -- 表示从第10条开始取5条数据。 select * from world.city limit 10,5; -- regexp:用于设置正则表达式匹配项,相似于模糊匹配like。 -- 表示查询名称以字符 'A'(不区分大小写)开头的记录。 select * from world.city where Name regexp '^A'; -- 表示查询名称中包含字符串 'mer' 的记录。 select * from world.city where Name regexp 'mer'; -- 表示查询名称以字符 'a' 或字符 'b' 开头的记录或者以字符 'r' 结尾的记录。 select * from world.city where Name regexp '^[ab]|r$';
6、DDL操做
-- 向指定数据表添加一列,默认添加到数据表字段的末尾。 alter table test.student add column1 varchar(10) null; -- 向指定数据表添加一列,并设置默认值为:0 alter table demo.chinesecharinfo add column IsUseful tinyint unsigned not null default 0; -- first关键字用于把添加的列设置为第一列。 alter table test.student add column1 varchar(10) null first; -- after关键字用于把添加的列设置在指定列的后面,StuSex为指定列的列名。 alter table test.student add column1 varchar(10) null after StuSex; -- 删除指定列名的列,当数据表仅剩一个字段时,没法进行删除。 alter table test.student drop column1; -- 修改指定列的数据类型,并设置该列位于指定列名的列以后。 alter table test.student modify column1 char(10) null after CreateTime; -- 关键字column可省略 alter table test.student modify column column1 char(10) null after CreateTime; -- 修改指定列的列名和数据类型,并设置该列位于指定列名的列以后。 -- column1:为原列名 -- column2:为新的列名 alter table test.student change column1 column2 varchar(10) null after CreateTime; -- 修改指定列的默认值。 alter table test.student alter column2 set default '123'; -- 删除指定列的默认值。 alter table test.student alter column2 drop default; -- 修改数据表的存储引擎。 alter table test.student engine = myisam; alter table test.student engine = InnoDB; -- 修改数据表的自增加值的起始值。 alter table test.student auto_increment=10; -- 重建自增加列,当删除数据过多,自增加列的值比较混乱时可使用,可是重建时若是有新的数据插入,有可能会出现混乱。 alter table test.student drop ID; alter table test.student add ID int not null auto_increment first; alter table test.student add primary key(ID); -- 修改数据表的表名称。 alter table test.student rename to test.StudentBak;
7、索引
-- 查看指定数据表的索引。 show index from test.student; -- 删除指定的索引。 drop index index_name on test.student; -- 修改表结构的方式删除索引。 alter table test.student drop index index_name; -- 建立普通索引。 create index index_name on test.student(StuNo); -- 修改表结构的方式添加索引,这种方式能够不指定索引名称,不指定系统会自动默认一个索引名称。 alter table test.student add index index_name(StuNo); -- 建立惟一索引,指定建立惟一索引的列的值必须是惟一的,不能重复,可是能够为null。 create unique index index_name on test.student(StuNo); -- 修改表结构的方式添加惟一索引。 alter table test.student add unique index index_name(StuNo); -- 修改表结构的方式添加主键,必须保证添加主键的列的值不能为null,而且是惟一的,不可重复。 alter table test.student add primary key PrimaryKey_Name(ID); -- 删除指定数据表的主键,删除主键时只需指定 primary key,删除索引时必须指定索引名。 -- 注意:当主键列同时是自增加列时,不能直接删除主键,须要先删除自增加约束。 alter table test.student drop primary key; -- 添加全文索引。 alter table test.student add fulltext index_name(StuNo); -- 加上关键字ignore建立的惟一索引和主键,在插入重复数据时,会直接过滤掉重复数据,而且不会报错,不然就会抛出错误。 alter ignore table test.student add primary key(ID); alter ignore table test.student add unique index index_name(StuNo);
8、事务
-- 关闭自动提交事务 set autocommit=0; -- 开启自动提交事务,默认为开启。 set autocommit=1; -- 显式地开启一个事务,有如下两种方法。 start transaction; begin; -- commit用于提交事务,只有当自动提交事务被关闭时须要使用。 commit; -- rollback用于回滚事务,撤销对于数据库所作的未提交的操做。 rollback; -- 用于设置一个保存点,identifier是指保存点的名称。 savepoint identifier; -- 用于删除一个保存点,若是指定的保存点不存在,将会抛出一个异常。 release savepoint identifier; -- 把事务回滚到指定的保存点。 rollback to identifier; -- 设置事务隔离级别,只对下一个事务有效。 set transaction isolation level {事务隔离级别}; -- 设置事务隔离级别,对当前会话的事务有效。 set session transaction isolation level {事务隔离级别}; -- 设置事务隔离级别,对后面创建MySQL链接的事务有效。 set global transaction isolation level {事务隔离级别}; -- 事务的隔离级别 read uncommitted(读取未提交): -- 该级别引起的问题是脏读,会读取到其余事务未提交的数据。 read committed(读取已提交): -- 该级别引起的问题是不可重复读,即设置为该级别的事务只能读取到其余事务已经提交的数据,未提交的数据不能读取,会形成屡次查询的结果不一致。 repeatable read(可重复读): -- 该级别引起的问题是幻读,即当用户修改某一范围内的数据行时,另外一个事务又在该范围内插入了新的行,当用户再次读取该范围内的数据时,会发现有新的数据行没有被修改。 -- 该级别是MySQL数据库默认的事务隔离级别。注意:该级别不会对事务查询到的行加行锁,也就是该事务查询到的行,其余事务依然能进行修改,可是能保证数据的一致性。 serializable(可串行化): -- 该级别是MySQL中事务隔离级别最高的,该级别会锁住事务中操做的整张表,所以不会出现以上三个级别的问题。可是这种隔离级别并发性极地,开发中不多会用到。