最近在学习《深入浅出:MySQL数据库开发、优化与管理维护》一书,特记录笔记如下,相互学习进步吧!
mysql启动与关闭
在MYSQL命令行中使用\G参数,将输出按列显示mysql -h(IP地址) -uroot -p — 连接到MYSQL服务器 -h 后面跟服务器IP地址 -u 后面为数据库用户,-p 后跟密码,
#mysqladmin -uroot shutdown 关闭mysql服务
#systemctl shutdown mariaDB #centos7
>exit –退出mysql
mysql查看帮助命令
>? contents; –查看contents帮助
>? data types; –查看支持的数据类型
>? show;
>? create table;
常用命令
>create database test1;
>show databases;
>use dbname; –选择数据库
>show tables;
>drop database dbname;
>create table tablename(colname coltype constraint,…)
>desc tablename; –查看表结构
>show create table tablename;
>drop table tablename;
>alter table tablename modify column column_definition;
>alter table tablename add column column_definition;
>alter table tablename drop column col_name;
>alter table tablename change column old_col_namecolumn_definition;
>alter table emp add birthdate after ename;
>alter table emp modify age int(3) first; –将age列指定
>alter table tablename rename to new_tablename;
>alter table emp rename to emp1;
>insert into tablename() values();
>update tablenem set col=value,…where…
>update t1,t2,t3 set t1.col=?,t2.col=?,t3.col=? where … 多表更新
>delete from tablename where …;
>delete t1,t2,t3 from t1,t2,t3where…;
>delete a,b from emp a,dept b where a.deptno=b.deptno and a.deptno=’5′;#若from后面表名用别名,则delete后面也要用相应别名
>select * from tablename where …;
>select distinct colname from tablename where … group by (with rollup) having … order by colname asc|desclimit1,3;
(注意with rollup 不能与order by 同时使用,limit在rollup后使用)
>select count(1) from emp;
>select a.col1,a.col2 from a left join b on a.col3=b.col3;
>select a.col1,a.col2 from a right join b on a.col3=b.col3;
>select * from emp where depno in (select depno from dept);
>select emp.* from emp dept where emp.depno=dept.depno; –mysql 4.1前不支持子查询
>select * from t1 union |union all select * from t2; –union 去重
>select deptno,count(1) from emp group by deptno with rollup having deptno=2;
> select deptno,count(1) from emp group by deptno having count(1)>1;
>select sum(sal),max(sal),min(sal) from emp;
>grant select,insert on sakila.* to ‘z1’@’localhost’ identified by ‘123’; 创建一个数据库用户Z1 ,给sakila数据库中所有表赋予select,insert权限
>mysql -uz1 -p123
>use sakila
>revoke insert on sakila.* from’z1’@’localhost’; 回收z1的insert权限
>alter table t1 modify id2 int(5) zerofill; –zerofill 数值前用0 填充剩余宽度,若一个列指定zerofill,则mysql自动为该列添加UNSIGNED属性
>show variables like ‘table_type’;查看当前的存储引擎 (InnoDB(支持事务,支持外键),MyISAM(默认,不支持事务,不支持外键,但访问速度快..), MEMORY
show 命令查看参数
>show engines \G 查看当前数据库支持的存储引擎
>show variables like ‘have%’
>show table status like ‘tablename’ \G
>optimize table tablename ;对表进行碎片整理,节省磁盘空间 #du -sh tablename.* 查看该表在系统占用的空间
>show character set; >desc information_schema.character_sets; 查看可用字符集
>show collation like ‘GBK%’; >desc information_schema.collations;查看字符集的校对规则
>show variables like ‘character_set_server’; 查看服务器字符集(默认为latin1)
>show variables like ‘collation_server’; 查看服务器校对规则
索引相关
BTREE索引(unique,index,primary key ,fulltext等索引时默认选择BTREE,可使用like,>,<,between等关键字),HASH索引(只能使用=,<=>操作,全表扫描,存储引擎为MEMORY时,注意可能默认为Hash索引),空间列类型索引使用RTREE索引。
>create index cityname on city(city(10));
>drop index cityname on city;
create index i_owner_wechatid on i_owner(wechatid);
drop index i_owner_wechatid on i_owner;
show index from i_owner
视图相关
一种虚拟表,已过滤好的复合条件结果集
>create view viewname as select_statement with cascaded|local check option; (cascaded满足所有争对该视图的所有视图条件才能更新,local满足本视图的条件即可更新)
>alter view viewname as select_statement with cascaded|local check option;
>drop view viewname;
>show tables; >show table status like ‘tablename/viewname’ \G 查看视图
>show create view viewname \G 查看某视图的定义
>select * from information_schema.views where table_name=’viewname’ \G
存储过程和函数
>存储过程和函数是事先经过编译后存储在数据库中的一段SQL语句集合,存储过程参数可使用IN,OUT,INOUT类型,而函数只能使用IN类型,且必须有返回值
创建存储过程和函数需要CREATE ROUTING权限,修改或删除需要ALTER ROUTING权限,执行需要EXECUTE权限
>create procedure/function sp_name …
>alterprocedure/function sp_name …
>dropprocedure/function sp_name …
>callsp_name(parameter…) 调用存储过程或函数
>show procedure|function status like ‘sp_name’ \G 查看存储过程的定义信息
>show create procedure|function sp_name \G
>select * from information_schema.routines where routine_name=’name’ \G 查看定义
>declare var_name type ; eg:>declare last_month_start DATE; 定义变量,变量只能在BEGIN。。。AND块中
>set var_name=expr… ; eg>set last_month_start= DATE_SUB(CURRENT_DATE(),INTERVAL 1 MONTH); 变量赋值
>select col_name … into var_name … table expr变量赋值
触发器
是与表有关的数据库对象,在满足定义条件时触发,并执行触发器定义的语句集合( mysql 5.0.2之后支持)
>create trigger trigger_name trigger_time trigger_event on tb1_name for each row trigger _stmt (触发器只能创建在永久表上,不能创建在临时表上)
>drop trigger schema_name.trigger_name;
>show triggers \G >desc triggers;
>select * from triggers where trigger_name =’trigger_name’ \G
事务控制和锁定语句
(5.0.3 后支持分布事务,当前分布式事务只支持innoDB引擎)
>lock tables table_name read/write; 锁定当前线程的表
>unlock tables;
>start transaction; >select statement ;update statement; >commit/rollback;
其他
#SQL Injection SQL注入 (利用某些数据库的外部接口将用户数据插入到实际的数据库操作语言(SQL)中,从而达到入侵数据库乃至操作系统的目的)
#SQL MODE 模式(定义了数据校验,SQL语法,保证数据完整性,有利于数据迁移到其它库)
>set session sql_mode=’mode_name’;
>select @@sql_mode; 查看数据库有哪些SQL模式
>set session sql_mode=’NO_TABLE_OPTION’; 数据迁移时设置该模式,将去掉show create table 中的engine关键字,获得通用的建表脚本