zoukankan      html  css  js  c++  java
  • Mysql的操作应用

    1.创建数据库
    	格式:
    		create database if not exists 数据库名 default charset utf8;
    
            注意:
                  1、数据库是唯一的
                  2、if not exists先判断是否存在这个数据库,存在不创建,不存在就创建。
    	      3、创建数据库,并设定编码集为utf8
                
    
    2.显示当前数据库服务器下所有的数据库名
    	show databases;
    
            use 数据库名    选择数据库
            注意:
                windows下面数据库名是不区分大小写的,在Linux下面严格区分。
    
    3.删除数据库 
    	drop database 数据库名
    	
    	查看已经选择的数据库
            select database(); 
    
    	查看当前的数据库的版本号
        	select version();
    
    4.数据表的操作
    	show tables 查看数据库中的数据表。
    
    	创建数据表
    		格式:
    			create table 表名(列的一些信息);
    
    		例子:
    			create table t1(id int(12), name varchar(100));
    
    			create table `test t1`(id int(10), name varchar(50));
    		
    	   desc 表名 查看表的结构.
    
           show create table 表名 查看建表的语句
    
          		 G 格式化输出(文本式,竖立显示)
    
           drop table 数据表名
    
           drop table [if exists] 数据表名  尝试性的删除表。
    
    5.记录操作  增、删、改、查
    
    	(1)插入数据
    		格式: 
    			insert into 表名(字段1,字段2,字段3) values(值1,值2,值3);
    	
    	insert into 表名(字段1,字段2,字段3) values(a值1,a值2,a值3);
    	
    	(2)查询表数据
    		格式: 
    			select * from 表名;
    	
    	select 字段1,字段2,字段3 from 表名;
    
    	select * from 表名 where 字段=某个值;
    
        (3)修改表数据 
    		格式:
    			update 表名 set 字段=某个值 where 条件;
    	
    	update 表名 set 字段1=值1,字段2=值2 where 条件;
    	
    	update 表名 set 字段=字段+值 where 条件;
    	
    	(4)删除数据
    		格式: 
    			delete from 表名 where 字段=某个值;
    
    	delete from 表名; (慎重使用)
    
    	delete from 表名 where 字段 = 值;
    
    		
    	清空表数据
    			truncate table 表名;
    
    
    6.修改用户密码
    		在退出mysql服务的情况下 输入
        	1、mysqladmin -u 用户名 -p password 新密码
    		输入旧密码:
    	
        	2、登录到mysql的时候使用set password for '用户名'@'登录主机'=password('新密码');
    
    
    
    7.修改表的字段信息
    
    	修改表的字符集
    	    alter table 表名 charset utf8
    	修改字段的类型
    	    alter table 表名 modify 字段名 字段类型
    	修改字段的名字并同时修改字段类型
    	    alter table 表名 change 旧字段名 新字段名 字段类型
    	修改字段的字符集
    	    alter table 表名 modify 字段名 类型 charset utf8
    	添加新字段
    	    alter table 表名 add 字段名 类型
    	删除字段
    	    alter table 表名 drop 字段名
    	修改表名 
    	    alter table 旧表名 rename as 新表名
    
    8.删除默认匿名的账户
    	为什么我直接输入mysql也能进入数据库中。是匿名用户,MySQL默认创建的。他有test和information_schema这两个库的权限。我们可以把他删掉
    
            删除匿名用户:drop user ''@'localhost';
    
    9.MySQL数据库的数据类型:
    
    	MySQL的数据类型分为四大类:数值类型、字串类型、日期类型、NULL。
    	
    	1 数值类型:
    		*tinyint(1字节) 0~255  -128~127
    		smallint(2字节)
    		mediumint(3字节)
    		*int(4字节)
    		bigint(8字节)
    		*float(4字节)   float(6,2)  float(m,d)
    		*double(8字节)    double (m,d)
    		decimal(自定义)字串形数值  decimal(m,d)  
    								m精度    d标度
    		
    	 2 字串类型
    		普通字串
    		*char  定长字串   	 char(8):长度为8位  
    		*varchar 可变字串 varchar(8):最长为8位
    		
    		二进制类型
    		tinyblob
    		blob
    		mediumblob
    		longblob
    		
    		文本类型
    		tinytext
    		*text      常用于<textarea></textarea>
    		mediumtext
    		longtext
    		
    		*enum枚举
    		set集合
    		
    	3 时间和日期类型:
    		date  年月日
    		time  时分秒
    		datetime 年月日时分秒
    		timestamp 时间戳
    		year 年
    	
    	4 NULL值
    		NULL意味着“没有值”或“未知值”
    		可以测试某个值是否为NULL
    		不能对NULL值进行算术计算
    		对NULL值进行算术运算,其结果还是NULL
    		0或NULL都意味着假,其余值都意味着真
    
    	MySQL的运算符:
    		算术运算符:+ - * / % 
    		比较运算符:= > < >= <= <> != 
    		数据库特有的比较:in,not in, is null,is not null,like, between and 
    		逻辑运算符:and or not
    	
    		like: 支持特殊符号%和_ ; 其中 %表示任意数量的任意字符,_表示任意一位字符。
    	
    	表的字段约束:
    		unsigned    无符号(正数)
    		zerofill    前导零填充
    		default	   默认值
    		not null   非空
    		auto_increment  自增 在产生一个唯一的标识或顺序值的时候,可以利用这个约束条件。这个约束条件只能用于整数类型,值一般从1开始。每行加1,插入一个null到一个auto_increment列时,MySQL将插入一个比出现过的最大值+1的值。一个表中只能有一个auto_increment列,并且必须定义为primary key或unique才能够使用
    
    		primary key  主键索引 (非null并不重复)
    		unique   唯一索引   (可以为null但不重复)
    		index    常规索引
    
    9.建表语句格式:
    	 create table 表名(
    	   字段名 类型 [字段约束],
    	   字段名 类型 [字段约束],
    	   字段名 类型 [字段约束]
    	   ...
    	  );
    
    	实例:
    		 create table stu(
    		 id int unsigned not null auto_increment primary key,
    		 name varchar(8) not null unique,
    		 age tinyint unsigned,
    		 sex enum('m','w') not null default 'm',
    		 classid char(6)
    		 );
    
    10.插入数据
    	1 插入指定字段
    		insert into t1(字段1,字段2,字段3...) values ('值1','值2','值3',....);
    	2 插入所有的字段
    		insert into t1 values ('值1','值2','值3',....);
    	3 插入多条数据
    		insert into t1(字段1,字段2,字段3...) values ('值1','值2'),('值1','值2')....;
    	4 插入结果
    		insert into t1(字段1,字段2,字段3...) select 字段1,字段2,字段3... from t1;
    		insert into t1() select * from t1;
    	5 插入单条数据
    		insert into t1 set 字段1=值1,字段2=值2....;
    
    11.查询数据
    
    	格式:
            select [字段列表]|* from 表名
            [where 搜索条件]
            [group by 分组字段 [having 子条件]]
            [order by 排序 asc|desc]
            [limit 分页参数]
        
     	查看stu表中的信息:  select * from stu;
       
    	where条件查询
        1. 查询班级为lamp138期的学生信息
         select * from stu where classid='lamp138';
        
        2. 查询lamp138期的男生信息(sex为m)
         select * from stu where classid='lamp138' and sex='m';
        
        3. 查询id号值在10以上的学生信息
         select * from  stu where id>10;
        
        4. 查询年龄在20至25岁的学生信息
         select * from stu where age>=20 and age<=25;
         select * from stu where age between 20 and 25;
        
        5. 查询年龄不在20至25岁的学生信息
         select * from stu where age not between 20 and 25;
         select * from stu where age<20 or age>25;
        
        6. 查询id值为1,8,4,10,14的学生信息
         select * from stu where id in(1,8,4,10,14);
         select * from stu where id=1 or id=8 or id=4 or id=10 or id=14;
        
        7. 查询lamp138和lamp94期的女生信息
         select * from stu where classid in('lamp138','lamp94') and sex='w';
         select * from stu where (classid='lamp138' or classid='lamp94') and sex='w
    
    LIKE 子句
    
    	这时我们就需要在 WHERE 子句中使用 SQL LIKE 子句。
    
    	LIKE 子句中使用百分号%字符来表示任意字符,类似于UNIX或正则表达式中的星号*。
    	
    	如果没有使用百分号%, LIKE 子句与等号=的效果是一样的。
    	
    	LIKE 通常与 % 一同使用,类似于一个元字符的搜索。
    	你可以使用 AND 或者 OR 指定一个或多个条件。
    	你可以在 DELETE 或 UPDATE 命令中使用 WHERE...LIKE 子句来指定条件。
    	可以使用regexp正则来代替 like
    
    	1.查询name字段值是以zh开头的所有信息
    	 select * from stu where name like "zh%";
    	 select * from stu where name regexp  "^zh"; --正则写法
    	
    	2.查询姓名name中含有ang子串的所有信息
    	 select * from stu where name like "%ang%";
    	 select * from stu where name regexp  "ang";
    
    	3.查询姓名是任意四位字符构成的信息。
    	 select * from stu where name like "____";
    	 select * from stu where name regexp "^[a-z0-9]{4}$";
    	
    	创建一个表结构和另一个表一样的结构:create table t3 like t1;
            那么现在t3表的结构和t1表的结构是一样的,但是要注意数据是没有的
    
    MySQL的统计函数(聚合函数):max() min() count() sum() avg()
    
    	1.获取学生表中最大、最小以及平均年龄是多少?
    	 select max(age),min(age),avg(age) from stu;
    
    	2.获取学生表中男生m的数量
    	 select count(*) from stu where sex='m';
    
    GROUP BY 语句 分组
    
    	GROUP BY 语句根据一个或多个列对结果集进行分组。
    
    	统计班级信息,按性别分组,并统计每组人数;
    	 select sex,count(*) from stu  group by sex;
    	
    	1.统计每个班级的人数
    	 select classid,count(*) from stu  group by classid;
    	
    	2.统计每个班级的,男生和女生各多少人数。
    	 select classid,sex,count(*) from stu  group by classid,sex;
    
    ORDER BY 排序 -- asc 默认升序 desc 降序
    
    	我们知道从 MySQL 表中使用 SQL SELECT 语句来读取数据。
    
            如果我们需要对读取的数据进行排序,我们就可以使用 MySQL 的ORDER BY子句来        设定你想按哪个字段哪种方式来进行排序,再返回搜索结果。
    
        你可以使用任何字段来作为排序的条件,从而返回排序后的查询结果。
        你可以设定多个字段来排序。
        你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升序排列。
        你可以添加 WHERE...LIKE 子句来设置条件。
    
    	1.按年龄升序排序查询学生信息
    	 select * from stu order by age;
    	 select * from stu order by age asc;  --默认asc升序 可省略
    
    	2.年龄降序排序
    	 select * from stu order by age desc;
    
    	3.查询学生信息,按班级做升序排序,相同班级按年龄降序排序
    	 select * from stu order by classid asc,age desc;
    
    LIMIT 关键字 查询部分数据
    
        -- 例如: .... limit m; 查询数据只显示前m条
    
        -- 例如: .... limit m,n; 排除前m条,然后再查询出前n条
    	
    	1.查询前5条信息
    	 select * from stu limit 5;
    
    	2.排除前2条后再获取4条信息
    	 select * from stu limit 2,4;
    
    12.修改数据
    	 格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n... where 条件 
            
            -- 将id为11的age改为35,sex改为m值
             update stu set age=35,sex='m' where id=11;
           
            -- 将id值为12和14的数据值sex改为m,classid改为lamp92
             update stu set sex='m',classid='lamp92' where id=12 or id=14 --等价于下面
             update stu set sex='m',classid='lamp92' where id in(12,14);
          
            
    13.删除操作
    	格式:delete from 表名 [where 条件]
    
             -- 删除stu表中id值为100的数据
             delete from stu where id=100;
           
    
            -- 删除stu表中id值为20到30的数据
             delete from stu where id>=20 and id<=30;
           
    
            -- 删除stu表中id值为20到30的数据(等级于上面写法)
             delete from stu where id between 20 and 30;
           
    
            -- 删除stu表中id值大于200的数据
             delete from stu where id>200;
    
    14.导入和导出
    	注意: 在操作导入和导出数据的时候 不要登录到mysql服务中,要退出mysql服务进行操作
    
    	-- 将lamp138数据库里面的所有的表导出
    	C:>mysqldump -u root -p lamp138 > C:lamp138.sql
    	Enter password:
    
    	---- 将lamp138数据库中的stu表导出
    	C:>mysqldump -u root -p lamp138 stu > C:lamp138_stu.sql
    	Enter password:
    
    	-- 将lamp138库中的所有表导入
           
    	C:>mysql -u root -p lamp138 < C:lamp138.sql
    	Enter password:
    
    	-- 将lamp138库中stu表导入
    	C:>mysql -u root -p lamp138 < C:lamp138_stu.sql
    	Enter password:
    
    	*********************
    	-- 将所有的数据库进行导出
    	mysqldump -uroot -p密码  --all-databases  --events > /tmp/bak.sql       所有数据库备份
    
    
    
    15.MySQL的权限管理
    	格式:
        	grant 权限 on 数据库.数据表 to '用户名'@'登录主机'  identified  by '密码'
    		
    		刷新权限
    		flush privileges;
    
    
    	举例:
        	grant select,insert,update,delete on *.* to  'wjs'@'%' identified by '12345';       
    
    
    	    安全的做法
    	   	grant select,insert,update,delete on pass.* to 'root'@'localhost'  identified by '12345';   
    	    只针对localhost主机里面的  pass数据库里面的数据表进行增删该查 
    		   
    
    	删除用户
    	drop user 'xxoo'@'%'  
    
    16.索引的作用:
    	索引是数据库中用来提高搜索性能的。我们通常在做数据库优化的时候通常先做索引的优化,数据量少的时候没效果,数据越多效果越明显。
    
    	查看表中都有哪些索引
    
        show index from 表名G
    
    索引的分类:
    
    常规索引(index)
    	最基本的索引,没有任何限制
    
    	
    	添加表字段的常规索引
    	    create index 索引名 on 表名(字段名)
    	    alter table 表名 add index 索引名(字段名)    
    
    	在创建表的时候也可以进行添加
    	    create table t2(
    	            id int(10) unsigned auto_increment primary key,
    	            name varchar(255), index  索引名(字段名)
    	        );
    
    	删除表字段常规索引**
    	    drop index 索引名 on 表名
    	    alter table 表名 drop index 索引名
    
    唯一索引(unique)
    	唯一索引是可以给每个字段进行添加的,添加完了之后字段里面的值就不可以重复了,主键索引和唯一索引类似,
    	但是数据表里的主键索引只能加在一个字段里(一般都加在id上),id是自增的,索引不会有重复的时候出现
    
    	
    
    	添加表字段的唯一索引
    	    create unique index 索引名 on 表名(字段名)
    	    alter table 表名 add unique 索引名(字段名)
    
    	在创建表的时候也可以进行添加
    	    create table t2(
    	            id int(10) unsigned auto_increment primary key,
    	            name varchar(255), unique  索引名(字段名)
    	        );
    
    	删除唯一索引
    	    drop index 索引名 on 表名
    
    
    
    主键索引(primary key)
    	主键索引是关系数据库中最常见的索引类型,主要作用是确定数据表里一条特定的数据记录的位置。
    	我们可以在字段后添加PRIMARY KEY来对字段设置为主键索引。
    
    	注意:
    		1.最好为每张表指定一个主键,但不是必须指定。
    		2.一个表只能指定一个主键,而且主键的值不能为空
    		3.主键可以有多个候选索引(例如NOT NULL,AUTO_INCREMENT)
    
    	添加表字段的主键索引
    	    alter table 表名 add  primary key (字段名)
    	添加自增
    	    alter table 表名 modify id int(4) auto_increment
    	删除主键索引
    	    如果字段有了auto_increment 和 primary key的时候,需要删除主键的话,
    	    先删除自增,然后再删除主键    
    	    删除自增
    	    alter table 表名 change 字段名  字段名 类名
    	    删除主键
    	    alter table 表名 drop primary key 
    
    全文索引(fulltext)
    	
    	全文索引在MySQL中是一个FULLTEXT类型索引,但FULLTEXT索引只能用于MyISAM表,
    	并且只可以在CHAR、VARCHAR或TEXT类型的列上创建,也允许创建在一个或多个数据列上。
    
    	添加表字段的全文索引
    	    alter table 表名 add fulltext 索引名(字段名)
    	删除全文索引
    	    drop index (索引名) on 表名
    	    alter table 表名 drop index 索引名
    
    	
    
    注意:如果在创建索引的时候,不添加索引名的话 默认会把字段名当做索引名
    
    
    
    17.表的存储引擎
    	存储引擎: 是MySQL的一个特性之一,用户可以根据应用的需要选择如何存储
    
    	MySQL支持MyISAM、InnoDB、HEAP、BOB、ARCHIVE、CSV、MEMORY等多种数据表引擎,在创建一个新MySQL数据表时,可以为它设置一个引擎类型。
    
    		MyISAM和InnoDB两种表类型最为重要:
    	
    		1.MyISAM数据表类型的特点是成熟、稳定和易于管理。
    		2.MyISAM表类型会产生碎片空间,要经常使用OPTIMIZE TABLE命令去清理表空间
    		3.MyISAM不支持事务处理,InnoDB支持
    		4.MyISAM不支持外键,InnoDB支持
    		5.MyISAM表类型的数据表效率更高
    		6.MyISAM表类型的数据表会产生三个文件
    				---.frm (表结构)
    				---.myd (表数据)
    				---.myi (表索引)
    		  InnoDB表类型表默认只会产生两个文件
    				---.frm (表结构)
    				---.ibd (表数据+表索引)
    
    		7.MyISAM表,其优势就是访问的速度快。对事务没有完整性的要求或以select、insert为主的通常使用MyISAM表
    
            8.InnoDB表:会占用更多的磁盘空间
    
            锁机制:MyISAM  表锁:开销小,加锁快,发生冲突的概率高。
            	   InnoDB  行锁:开销大,加锁慢,发生冲突的概率低。
    	
    		9.默认是InnoDB引擎 
    			数据存储在 innodb_data_home_dir选项所选的文件名的目录里
    	
    	查看默认的引擎
    	
    	    show variables like 'default_storage_engine';
    
    	查看支持的引擎
    	
    	    show engines
    
    	查看当前库里面所有的表引擎
    	
    		show table status from 库名
    	
    	修改表引擎
    		
    		alter table 表名 engine = 新的表引擎
    
    
    	ubuntu下
    	//mysql配置文件
    	sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
    
    	//重启
    	sudo /etc/init.d/mysqld restart
    
    
    18.表的结构要满足三范式
    	1NF 原子性  字段不可再分,否则就不是关系数据库; 
    		即表的列的具有原子性,不可再分解,即列的信息,不能分解, 只要数据库是关系型数据库(mysql/oracle/db2/informix/sysbase/sql server),就自动的满足1NF。数据库表的每一列都是不可分割的原子数据项,而不能是集合,数组,记录等非原子数据项。如果实体中的某个属性有多个值时,必须拆分为不同的属性 。通俗理解即一个字段只存储一项信息。
    	2NF 唯一性 
    		要求数据库表中的每个实例或行必须可以被惟一地区分。为实现区分通常需要我们设计一个主键来实现(这里的主键不包含业务逻辑)。
    	3NF 无冗余性	
    		要求一个数据库表中不包含已在其它表中已包含的非主键字段。就是说,表的信息,如果能够被推导出来,就不应该单独的设计一个字段来存放(能尽量外键join就用外键join)。很多时候,我们为了满足第三范式往往会把一张表分成多张表。
    
    29.left join、right join、inner join的区别
    
    	left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录
    	right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录
    	inner join(等值连接) 只返回两个表中联结字段相等的行
    
    	举例:
    	left join
    		select * from p1 left join p2 on p1.id = p2.id;
    
    	right join
    		select * from p1 right join p2 on p1.id = p2.id;
    
    	inner join
    		select * from p1 inner join p2 on p1.id = p2.id;
    	
    
    20.子查询
    	1、单行子查询
    
            select name,age,score
            from user
            where age=(select age from stu where id = 1);
    
    	2、多行子查询
         
    		select name,age,class 
    		from user 
    		where age in (select age from stu where id < 10);
    
    		select name,age.class 
    		from user 
    		where age in (select age from stu where name like '王%');
    
    
    21.分组 group by
    	
    	1 查询各个班级中年龄大于20,性别为男的人数、姓名和班级 (数据表: stu) 
    	 为了防止班级里面有重名的现象发生(例如 两个男生都是20岁 以上的)  所以会有count(*)
    
    	  select count(*),classid,name  from t1 where sex='男' group by classid,age having age > 20; 
    
    
    	2 学校评选先进学生,要求平均成绩大于等于90分的学生,并且语文课必须在95分以上,请列出有资格的学生  (数据表: score)
    
    	  select classid,name from sc where (yw+sx+en)/3 >= 90 and yw>=95 group by classid,name;
             
           select classid,group_concat(name) from sc where (yw+sx+en)/3>=90 and yw>=95 group by classid;
    
    	3 用一条sql语句查询出每门课都大于80分的学生姓名 (数据表: courseinfo)
    	   select name from courseinfo group by name having min(score) > 80;
    
    	  select DISTINCT  name from courseinfo where name not in (select distinct name from courseinfo where score < 80);
    	
    22.创建视图
    
        视图是一个虚拟表,其内容由查询定义。同真实的表一样,视图包含一系列带有名称的列和行数据。但是,视图并不在数据库中以存储的数据值集形式存在。行和列数据来自由定义视图的查询所引用的表,并且在引用视图时动态生成。
    
        如果你想创建视图的话 首先你的数据库要有创建视图的权限,不然的话是创建不成功的
    
    		create view v1 as select * from t1 where id > 1;
    
        创建v1的视图  把查询t1表里面的符合条件的数据放到v1里面
    
    	注意:如果主表不存在 那么视图表失效
    
    	删除视图表
    		drop view v1;
    
    
    23.mysql内置函数
    	字符串链接   concat('name','id');
    	最大值 max('price')
    	最小值 min('price')
    	平均值  avg('price')
    	定义变量  set @a = 10
    	获取变量的值  select @a
    	计算  select @a + @b;          select 10 + 20
    	字符串转换成大写和小写    select ucase('name')     select lcase('NAME')
    	计算字符串的长度  select length('namesss') as len;
    			select * from user where length(name) > 5;  名字大于5的长度找出来
    	去除两侧的空白  select trim('   pass   ');
    			select length(trim('  over '));
    	随机数 从0-1之间的随机数  select rand();
    	获取0-10之间的随机整数向上取整   select ceil(rand()*10)
    	获取时间戳 select unix_timestamp();
    
    24.mysql预处理
    	**设置一个占位符
    	prepare 名字 from "select * from t2 where id > ?";
    	设置变量
    	set @id = 2;
    	执行
    	execute 名字 using @id;
    	**设置多个占位符
    	prepare 名字 from "select * from t2 where id > ? and id < ?";
    	设置变量
    	set @id = 2;
    	set @id1 = 5;
    	执行
    	execute 名字 using @id,$id1;
    
    25.mysql存储过程
    	简单的说,就是一组SQL语句集,功能强大,可以实现一些比较复杂的逻辑功能
    
    	MySQL存储过程的创建
    		
    	创建的格式:CREATE PROCEDURE 过程名 ([过程参数[,...]])
    	[特性 ...]过程体
    	
    	举个例子:
    		**修改结束符
    	    mysql-> delimiter //
            **创建
    		mysql-> create procedure proc()  
    	     -> begin
    	     -> select * from user;  
    	     ->end// 
    
    		**使用
    		mysql-> call proc()//
    
    		**修改结束符 
    	     mysql-> delimiter ;
    
    注意:
    
    (1)这里需要注意的是DELIMITER //和DELIMITER ;两句,DELIMITER是分割符的意思,因为MySQL默认以";"为分隔符,如果我们没有声明分割符,那么编译器会把存储过程当成SQL语句进行处理,则存储过程的编译过程会报错,所以要事先用DELIMITER关键字申明当前段分隔符,这样MySQL才会将";"当做存储过程中的代码,不会执行这些代码,用完了之后要把分隔符还原。
    
    	格式:
    	d //  把结束符的分号改成 //结束本条sql语句
    	
        d  和 delimiter 是一样的 都可以修改mysql的结束符
    
    
    (2)过程体的开始与结束使用BEGIN与END进行标识
    
    (3)存储过程根据需要可能会有输入输出参数,输入参数x 类型int型 ,输出参数num 类型是int型,如果有多个参数用","分割开。
    
    	create procedure lamp172(in x int,out num int)
    	  -> begin
    	  -> set num = x + 20;
    	  -> end//
    	调用 
    	 call lamp172(10,@s)//
    
    	select @s as num//
    
    
    	使用循环插入数据
    	create procedure total(num int(10))
    	 ->begin
    	 ->set @i = 0;
    	 ->while @i<num do
    	 ->insesrt into t1 values (null,'www',20);
    	 ->set @i = @i+1;
    	 ->end while;
    	 ->end//
    
    
    
    注意 : 写存储过程名字的时候 不要使用关键字 和 数字
    	
    	例如:
    	    create procedure 111()   数字错误
    	    create procedure  add()   关键字错误
    
    	删除存储名
    
    	drop procedure if exists 存储名;
    
    26.mysql触发器 trigger
    在t2表插入数据的同时使用trigger 修改其他表的信息
    
    
    	格式: 	create trigger 名字 after/before  insert/update/delete on 表名 for each row 
    
    			begin
    			语句
    			end
    
    	例如:
    	create trigger tg_1 before insert on t2 for each row
    	begin
    	insert into t3(name) values (new.name);
    	end//
    	插入数据
    	insert into t3(name) values ('wwww')//
    
    
    	create trigger tg_2 after update on t1 for each row
    	begin
    	update t2 set t2.name= new.name where t2.id = old.id;
    	end//
    	
    	update t1 set name = 'xxoo' where id = 2//
    
    	create trigger tg_3 after delete on t1 for each row
    	begin
    	delete from t2 where id = old.gid;          // old.gid 这里面的gid是t1表与t2表相关的id号
    	end//
    	delete from t1 where gid = 2//    gid=2   ====> old.gid
    
    
    	//查看触发器
    	show triggers;
    	//删除触发器
    	drop trigger 名字;
    
    
    27.mysql 读写锁
    读锁: 所有的终端都可以读取数据  但是不能操作数据(增 删 改)
        
    	lock table t2 read
     解锁
        
    	unlock tables;
    
    写锁: 当前的终端对表进行写锁之后,代表本终端可以进行读写操作,但别的终端不可以进行读写操作
        
    	lock table t2 write;
    解锁
        
    	unlock tables;
    
    
    
    28.mysql表数据的备份与恢复操作
    使用SQL语句备份和恢复
    
    你可以使用SELECT INTO OUTFILE语句备份数据,并用LOAD DATA INFILE语句恢复数据。
    这种方法只能导出数据的内容,不包括表的结构,如果表的结构文件损坏,你必须要先恢复原来的表的结构。
    
    格式:
       
    	备份操作
        SELECT * INTO {OUTFILE | DUMPFILE} 'file_name' FROM tbl_name
        恢复操作
        LOAD DATA [LOW_PRIORITY] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE tbl_name
    
    	举例:
    	导出
    	select * from t2 into outfile '../lamp185.txt';  //相对于mysql.exe文件的路径  
    	清空
    	truncate table t2;
    
    	导入
    	load data infile '../lamp185.txt' into table t2; 
    
    29.增量备份
    	
    	一种mysql操作记录的备份
    
    	(1)操作
    		修改配置文件my.ini
    		[mysqld]
    		log-bin=c:/mysql/mysql-log    #(这里是备份文件存放的位置) 文件夹要存在才行
    		expire_logs_days=7     # binlog过期清理时间   单位为天
    		binlog_format = MIXED    #binlog日志格式
    
    	(2)查看
    		可以使用mysql的命令进行查看增量备份中的内容
    		mysqlbinlog c:/mysql/mysql-log.000001
    
    	(3)数据恢复
    		通过时间: mysqlbinlog --stop-datetime="2016-10-10 10:20:30"  c:/mysql/mysql-log.000001 | mysql -uroot  -p
    		通过位置: mysqlbinlog --stop-position="150" c:/mysql/mysql-log.000001 | mysql -uroot  -p
    
    		#171208 15:50:06 server id 1  end_log_pos 2227 CRC32 0x7ff79ffa
    		这里面的171208 15:50:06 时间是上面的datetime的时间
    		这里面的end_log_pos 2227 是上面 position="150"
    			
    
    	(4)重新记录增量备份
    		 reset master      在mysql命令行中进行输入
    
    30.查询处理
    
    	语句分析 explain指令
    	格式:
    		explain  select  *  from  user
    
    	type 类型
    
    	快慢顺序
    		system> const >eq_ref > ref > fulltext >ref_or_null >index_merge > unique_subquery > index_subquery > range>index > all
    
    	const:表中满足条件的记录最多一条,通常会出现在主键和unique索引中
    		explain  select  *  from  test  where id=1
    		explain  select  *  from test_2 where name = 'zhangsan'
    
    	eq_ref:对于每个来自于前面的表的行组合,从该表中读取一行。这可能是最好的联接类型,除了const类型。它用在一个索引的所有部分被联接使用并且索引是unique或primary key
    		explain  select type.name,typechild.childname  from  type,typechild where type.id = typechild.pid;
    
    	ref:使用普通索引进行查询
    		explain select *  from test where  name = 'abc';
    
    	ref_or_null:通过普通索引检索,并且会检索null值
    		explain  select  *  from  test_2  where  nickname  = 'zhangsan' or nickname = null;
    
    	range :当查询范围的时候
    		explain  select *  from test  where id < 3;
    		 或
    		 explain  select *  from test  where id in (1,2,3);
    		或
    		explain  select *  from test  where id between 1 and 3;
    		
    	index:跟all一样,不过只会扫描索引
    		explain  select  id  from test;
    
    	all:全表扫描
    		explain  select  *  from test
    
    	注意:查询一定或者尽量不要出现all类型(全表扫描,速度是最慢的)
    
    	
    	key(代表的是搜索时使用到的索引名)
    		
    31.开启慢查询记录
    
    	需要进行配置  在mysql中的my.ini中进行设置
        slow-query-log = 1    # 开启慢查询操作
        slow-query-log-file = slow.log      # 文件默认存放在mysql的data目录下 (文件名自定义)
        long-query-time = 1     # 超时时间定为1秒  如果执行的sql语句超过1秒钟就把语句存放到slow.log文件中 (不能使用小数)
        
    # 菜鸟教程.链接python的一个网站:  www.runoob.com
    

      

  • 相关阅读:
    软件质量保证体系是什么&#160;国家标准中与质量保证管理相关的几个标准是什么?他们的编号和全称是什么?
    软件测试分为几个阶段&#160;各阶段的测试策略和要求是什么?
    BUG管理工具的跟踪过程(用BugZilla为例子)
    多线程
    设计模式
    一个简单的layui登陆界面
    反射技术
    Win10繁体——简体
    Vue学习—组件的学习
    IDEA 相关问题
  • 原文地址:https://www.cnblogs.com/w-zc/p/8029115.html
Copyright © 2011-2022 走看看